|
I would be very appreciative if some more experienced developers could take a quick look at my code for this challenge and share some advice on how to improve it. While my app is working as intended, I feel like I could be doing something things more effciently when it comes to the actual logic of the program. Not so interested in the style stuff at the moment. Thanks in advance!
|
|
I would work mainly on naming.
This could be single line, you already have an array |
|
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Howdy, I am a novice, as in I just completed the Day 25 Challenge yesterday, so we're in the same boat. Congratz! I was able to follow your code very easily & you did a few things I did not such as using a It's hard for me to say anything in particular given my lack of xp & in building my own project I specifically didn't use a few techniques, because they were ones I just used making Guess the Flag & maybe you took this concept to mind in making Rock. Paper. Scissors. The only thing I notice is the use of your nested I do all of my work/study with #100DaysOfSwiftUI in my stream, so I know it took me ~17 hours over 5ish days to complete this project! Baron Blackbird |
|
To start, I have been on the books until recently, so I can't really comment on some stuff here since I haven't done the 100 days, but I will add to the above comments with more details that I learned from the books. So for what it's worth, here goes: 1- since 2- Because
This reads much better as mentioned above. 3- Considering how many conditions you check, I agree with the above comment that a
Whether that is something you like or not is up to you. They both work almost the same. Except a 4- Finally, regardless of which you end up choosing, if-else or switch, you can use string interpolation for your print statements without writing out what each side chose:
This will automatically print the correct choices. Hope that helps! Keep it up mate. Hope you're enjoying it. Paul certainly has a fun way of explaining things. All the best. |
|
I have gone and read the Day 25 details, and gave it some thoughts. First though, you should know your app logic is sound. Also, in the text you will see he actually writes I did not wish to edit the above comment, so I will simpy add a simplification of the switch statement here, which makes the string interpolation more relevant:
This is why in his "hints" he suggests simplifying the logic of how you determine the result. Essentially you are iterating through all possible scenarios here, but there is another way to look at it as he suggests. It has to do with the relationship between the moves. |
|
Hey everyone, hey @MarcusKay:
I believe I'm trying to tackle what you and Paul are alluding to: Given an ordering of the array of possible moves that corresponds to the actual rules of the game (and to its name ๐), the "winning" countermove would be "on the right" of the move (+1), with the "losing" countermove "on its left" (-1). Now, while I like how the below code reads, it only works if the logic doesn't have to cross the boundaries of the array, because the resulting subscripts will be out of bounds and the thing crashes. I can't for the life of me figure out how to accomplish the necessary wrap-around behaviour. Any help would be greatly appreciated!
|
|
Well I'm not sure if I have the most elegant solution, but what is the relationship between rock and scissors? it's the index of scissors - 2... Also, a tip on the if statement. It basically checks this:
A boolean is either True or False. So you don't need to use
That was my approach. I didn't overthink it to be honest. There might be some better way, but for this stage, I think we're good ๐ |
|
@MarcusKay:
Thanks so much for your answer! To my eyes, it made so much sense immediately (I had definitely been overthinking things). However, my code still crashes in (I believe) the 'wrap-around' cases. Do you have any idea why? P.S. Also thanks on the pointer re: booleans. Makes for much more naturally-reading code!
|
|
Woops ๐ I was working on another version for another thread and completely did not notice the array. The thing is if the player should win, and the app chose paper (meaning 1) and the user chooses anything other than scissors (2) then we will get an out of index error. Because as soon as it hits the move - 2 part, it will become -1 which is wrong... similar issue with the others. The quick fix is to convert your player's move into an int, and compare those.
Sorry about that... I actually tested that... and it works... unless I missed something... in which case, I will need to take a break... what day is it? ๐ |
|
Yep, that does it! I feel like it shouldn't have been such a braintwister for me though, so I guess it's time to take a break and stop looking at the screen for a bit ๐ Thank you for your kind help. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.