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 trialArmend Azemi
27,921 PointsFeedback on my code.
Just wanted some feedback on my code, some constructive criticism, would appreciate it. Cheers! Code below.
//
// main.m
// pingPongOwnVersion
//
// Created by Armend Azemi on 2017-03-20.
// Copyright © 2017 Armend Azemi. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
/*
PINGPONG GAME
PLAYER 1 VS PLAYER 2
FIRST TOO 21, MUST WIN BY 2
*/
bool gamePlaying = true;
int playerOneScore = 0;
int playerTwoScore = 0;
int maxScore = 21;
int pointWinner;
while (gamePlaying){
//RANDOM NUMBER, 0 GIVES PLAYER 1 POINT, 1 GIVES PLAYER 2 POINT
pointWinner = arc4random()%2;
//GIVING POINTS TO EITHER PLAYER 1 OR PLAYER 2.
(pointWinner == 0) ? playerOneScore++ : playerTwoScore++;
//--------------PLAYER 1---------------
//First if statement we see if PLAYER 1 has reached max points and IS leading with 2 or more points.
if ((playerOneScore >= maxScore) && (playerOneScore >= (playerTwoScore+2))){
NSLog(@"Player One WON!!");
gamePlaying = false;
}
//Here we see if PLAYER 1 has reached max points but is NOT leading with 2 or more points
else if ((playerOneScore >= maxScore) && (playerOneScore == (playerTwoScore+2))){
NSLog(@"Player One reached the max point: %i, but is not leading with 2"
"Extending the max points by 2", maxScore);
maxScore +=2;
}
//--------------PLAYER 2---------------
//PLAYER 2 reached max points and IS leading with 2 or more points
else if ((playerTwoScore >= maxScore) && (playerTwoScore >= (playerOneScore+2))){
NSLog(@"Player Two WON!!");
gamePlaying = false;
}
//PLAYER 2 reached max points but is NOT leading with 2 or more points.
else if ((playerTwoScore >= maxScore) && (playerTwoScore == (playerOneScore+2))){
NSLog(@"Player Two reached the max point: %i, but is not leading with 2"
"Extending the max points by 2", maxScore);
maxScore +=2;
}
}
NSLog(@"SCORE OF PLAYER 1: %i \n SCORE OF PLAYER 2: %i", playerOneScore,playerTwoScore);
return 0;
}
2 Answers
Jonas Gamburg
11,193 PointsWell, if it an opinion you're looking for then you shall have one indeed! I like how you make it clean and organized, everything is explained with the comments that you've added, it's nice. Although I must say, that when you create a larger structure for this game ( I assume each player would have to have a name, score.. etc. some specific data relevant to him/she) you'd probably want to use classes for this. Overall, I like :).
Armend Azemi
27,921 PointsThanks alot for your feedback, will keep that in mind.
Regards Armend