Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics Practicing with Immersive Examples Immersive example 1 - Ping-Pong

Armend Azemi
Armend Azemi
19,619 Points

Feedback 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
Jonas Gamburg
11,193 Points

Well, 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
Armend Azemi
19,619 Points

Thanks alot for your feedback, will keep that in mind.

Regards Armend