Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJake Nisenboim
3,374 PointsObjective-C variable
My code is as follows:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
//Nate vs Kyle
//first to 21 or more win by 2
//print winner
int kyleScore = 0;
int nateScore = 0;
bool gamePlaying = YES;
int pointWinner;
int winTwentyOne = 21;
int winByTwo = ((kyleScore - nateScore >= 2) || (nateScore - kyleScore >= 2));
NSString *gameWinner;
while(gamePlaying) {
pointWinner = arc4random()%2;
(pointWinner == 0) ? kyleScore++ : nateScore++;
if ((kyleScore >= winTwentyOne || nateScore >= winTwentyOne) && ((kyleScore - nateScore >= 2) || (nateScore - kyleScore >= 2))) {
gameWinner = (kyleScore > nateScore) ? @"Kyle" : @"Nate";
gamePlaying = NO;
}
}
NSLog(@"%@ is the winner!", gameWinner);
return 0;
}
This code works. I am curious, however, why if instead I use
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
//Nate vs Kyle
//first to 21 or more win by 2
//print winner
int kyleScore = 0;
int nateScore = 0;
bool gamePlaying = YES;
int pointWinner;
int winTwentyOne = 21;
int winByTwo = ((kyleScore - nateScore >= 2) || (nateScore - kyleScore >= 2));
NSString *gameWinner;
while(gamePlaying) {
pointWinner = arc4random()%2;
(pointWinner == 0) ? kyleScore++ : nateScore++;
if ((kyleScore >= winTwentyOne || nateScore >= winTwentyOne) && winByTwo) {
gameWinner = (kyleScore > nateScore) ? @"Kyle" : @"Nate";
gamePlaying = NO;
}
}
NSLog(@"%@ is the winner!", gameWinner);
return 0;
}
does not. All I changed was used winByTwo rather than typing it all into the last if statement.
Build succeeds however I get no output.
Thank you !
2 Answers
Nathan Tallack
22,160 PointsIn your version you are no longer evaluating the winByTwo with each loop through the while. You are just using the result of that one evaluation before you entered the while loop.
If you do want to keep the evaluation seperate to your if conditional inside the loop at least move the winByTwo evaluation into the while loop so that it can keep getting updated with each iteration of the loop.
Marleen Berg
22,524 PointsMaybe if people are curious, I think this is the most shortest way to complete this challenge.. not written by me, but maybe it is nice to open a topic where people can post their answer to learn from others?
it is not the most readable code, the for loop isn't used like is should be, but the output is fine...
int main(int argc, const char * argv[]) {
int pp1 = 0, pp2 = 0, ws = 21;
for(int i = 0; pp1 != ws && pp2 != ws; i++){
int j = arc4random()%2;
pp1 = j == 0 ? pp1 + 1 : pp1;
pp2 = j == 1 ? pp2 + 1 : pp2;
if(pp1 == ws || pp2 == ws)
NSLog(pp1 == ws ? @"Player 1 wins" : pp2 == ws ? @"Player 2 wins" : @"");
}
Jake Nisenboim
3,374 PointsJake Nisenboim
3,374 PointsThank you Nathan ! Figured it out last night and forgot to update. Either way I appreciate it :)