Thread: "Rock, paper, scissors" game won't end

  1. #1
    Registered User
    Join Date
    Aug 2018
    Posts
    19

    Question "Rock, paper, scissors" game won't end

    Hi, I'm making a simple "Rock, paper, scissors" game in C to practice the usage of pointers in functions to modify variables defined in the `main()' function. The problem is that when one of the players reach the score needed to win the game, the program will continue the loop instead of exiting it. Here's the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    void PointsNeededToWin(int *points) {
        do {
            printf("How many points are needed to win? ");
            scanf("%i", points);
            if ((*points) <= 0) {
                printf("It must be a positive number.\n");
            }
        } while ((*points) <= 0);
        getchar();
        printf("\n");
    }
    
    int RivalTurn() {
        int element = rand() % 3;
        return element;
    }
    
    void PlayerTurn(char elements[3][9], int *element) {
        int check = 0;
        char PlayerElement[8];
        do {
            printf("Enter your choice [rock/paper/scissors]: ");
            fgets(PlayerElement, 8, stdin);
            strtok(PlayerElement, "\n");
            for (int i = 0; i < 3; i++) {
                if (strncmp(PlayerElement, elements[i], 8) == 0) {
                    *element = i;
                    check = 1;
                }
            }
            if (check == 0) {
                printf("Invalid choice.\n");
            }
        } while (check == 0);
    }
    
    void ChangeScoreboard(char elements[3][9], int RivalElement, int PlayerElement, int *RivalScore, int *PlayerScore) {
        printf("You chose %s, and the rival chose %s.\n", elements[PlayerElement], elements[RivalElement]);
        if (PlayerElement == 0) {
            if (RivalElement == 0) {
                printf("Draw.\n");
            }
            else {
                if (RivalElement == 1) {
                    printf("Paper beats rock.\n");
                    (*RivalScore)++;
                }
                else {
                    printf("Rock beats scissors.\n");
                    (*PlayerScore)++;
                }
            }
        }
        else {
            if (PlayerElement == 1) {
                if (RivalElement == 0) {
                    printf("Paper beats rock.\n");
                    (*PlayerScore)++;
                }
                else {
                    if (RivalElement == 1) {
                        printf("Draw.\n");
                    }
                    else {
                        printf("Scissors beats paper.\n");
                        (*RivalScore)++;
                    }
                }
            }
            else {
                if (RivalElement == 0) {
                    printf("Rock beats scissors.\n");
                    (*RivalScore)++;
                }
                else {
                    if (RivalElement == 1) {
                        printf("Scissors beats rock.\n");
                        (*PlayerScore)++;
                    }
                    else {
                        printf("Draw.\n");
                    }
                }
            }
        }
        printf("You've got %i point(s) y your rival has got %i point(s).\n\n", *PlayerScore, *RivalScore);
    }
    
    void winner(int NeededPoints, int PlayerScore) {
        if (PlayerScore == NeededPoints) {
            printf("ˇYou won!\n");
        }
        else {
            printf("You lost...\n");
        }
    }
    
    int main() {
        char elements[3][9] = {"rock", "paper", "scissors"};
        int NeededPoints, PlayerScore, RivalScore, PlayerElement, RivalElement;
        PlayerScore = 0;
        RivalScore = 0;
        PointsNeededToWin(&NeededPoints);
        srand(time(NULL));
        while ((PlayerScore != NeededPoints) || (RivalScore != NeededPoints)) {
            RivalElement = RivalTurn();
            PlayerTurn(elements, &PlayerElement);
            ChangeScoreboard(elements, RivalElement, PlayerElement, &RivalScore, &PlayerScore);
        }
        winner(NeededPoints, PlayerScore);
        return 0;
    }
    Like I said, the `while' loop inside the `main()' function never stops even if one of the players achieve the required score. As you can see, I increment the players' scores using a function with two pointers, the players' elements and the array of the elements' names as arguments. I can't figure out why the loop never ends. If I print the variables' values below the `ChangeScoreboard()' it turns out they are being incremented successfully, so I can't find the reason why the `while' statement never evaluates to true.

    Does anyone know why is it behaving like that?
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    778
    You need to change your || to an &&. Right now it says "if either player hasn't reached the winning score, keep going." So it won't stop until they both reach the winning score. You want it to say "keep going as long as player one AND player two haven't reached the winning score."
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  3. #3
    Registered User
    Join Date
    Aug 2018
    Posts
    19
    Quote Originally Posted by john.c View Post
    You need to change your || to an &&. Right now it says "if either player hasn't reached the winning score, keep going." So it won't stop until they both reach the winning score. You want it to say "keep going as long as player one AND player two haven't reached the winning score."
    I understand. Thank you for your help, john.c!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help With Rock Paper Scissors Game
    By anonymoususer in forum C Programming
    Replies: 12
    Last Post: 12-01-2011, 12:31 AM
  2. rock, paper, scissors game
    By mrsmoss3791 in forum C++ Programming
    Replies: 12
    Last Post: 03-20-2011, 11:53 PM
  3. Replies: 8
    Last Post: 10-24-2007, 05:46 PM
  4. Rock Paper Scissors Game
    By tbca in forum C++ Programming
    Replies: 12
    Last Post: 07-09-2007, 12:16 PM
  5. Rock, Paper, Scissors game help
    By MillaTime in forum Game Programming
    Replies: 4
    Last Post: 09-08-2002, 05:55 PM

Tags for this Thread