Thread: Can someone help me with this beginner-level program?

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Can someone help me with this beginner-level program?

    I have been working on this simple program for about an hour and I really don't understand where my logic went wrong. The 'game' is suppose to end when either player 1 or player 2 takes the last marble in the bag. I added comments to my code explaining what I am trying to do. Can someone please help?


    Instructions:
    Write a program to play a game of marbles. The game starts with 32 marbles and two players. Each player must take 1, 2 or 3 marbles on their turn. Turns go back and forth between the two players. The winner is the person who takes the last marble. Your program should prompt each player with a message that states the current number of marbles and asks them how many they'd like to take. Continue until there is a winner. Then your program should print out the winner (either player #1 or player #2.)


    My Code:
    Code:
    #include <stdio.h>
    
    int main() {
    
    int num_marbles1, num_marbles2, all_marbles=32;
    
    
       printf("There are 32 marbles remaining.\n\n");
    
    
       while(all_marbles!=0 && all_marbles>0){  //while loop that runs as long as the number of marbles is NOT equal to 0
          printf("How many marbles would you like to take? ...Player 1\n");
             scanf("%d", &num_marbles1);
                printf("There are %d marbles remaining\n\n", all_marbles=all_marbles-num_marbles1); //resets 'all_marbles' value after player 1 takes
     out selected number of marbles
          printf("How many marbles would you like to take? ...Player 2\n");
             scanf("%d", &num_marbles2);
                printf("There are %d marbles remaining.\n", all_marbles=all_marbles-num_marbles2);//resets 'all_marbles' value after player 2 takes out
     selected number of marbles
    
        
       if (all_marbles-num_marbles1<0 || all_marbles-num_marbles2<0) //in the case that the user tries to take out more marbles then what is in the bag
          printf("\n Sorry, you both lose.\n");
       else if (all_marbles-num_marbles1==0) //in the case that player one takes the last marble in the bag
          printf("\nPlayer 1 Wins.\n");
       else if (all_marbles-num_marbles2==0) //in the case that player two takes the last marble in the bag
          printf("\nPlayer 2 Wins.\n");
    
    
    
    }
    
    system ("PAUSE");
    return 0;
    
    }
    Last edited by matthayzon89; 05-08-2010 at 01:28 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    74
    the condition you have on while doesn't seem right. if you want the loop to run until the number of marbles is 1, then you should do a condition accordingly(if not this, then the comment is wrong, one of those )

    also, if each player can only take 1,2 or 3, you could make a cicle until the player choose one of the valid possibilities.

    you could also have a variable that marks which of the players played last, to tell which one is the winner.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    lol the comment was wrong, i fixed it.

    thanks for the reply.

    What do you mean by "you could also have a variable that marks which of the players played last, to tell which one is the winner."

    ...I believe that, that is what I was hoping to accomplish by using num_marbles1 to represent num_marbles that player1 took out of the bag and num_marbles2 to represent the number of marbles player2 took out the bag.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by matthayzon89 View Post

    ...I believe that, that is what I was hoping to accomplish by using num_marbles1 to represent num_marbles that player1 took out of the bag and num_marbles2 to represent the number of marbles player2 took out the bag.
    Player 1 took 43 marbles, while Player 2 took 43 marbles. Which one took the last marble?

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Neither. You cannot take more marbles out of the bag then actually exists.
    This program is suppose to recreate a 'real game' with a 'real' sack of marbles. When you reach into a sack of marbles, you have an option to take either 1, 2, or 3 marbles on each turn.

    btw I have an if statement saying that if one of the players take out more marbles then what actually exists then they both lose which is suppose to suggest that you are not suppose to do it, I can change it up a bit to make it say something like "Impossible, please re-enter marble amount"

    Any other suggestions?

    My main problem is when I get to 3 or 2 marbles left in the 'bag' it says player 1 or player 2 wins BEFORE one of the players actually takes the last marble, you know? it doesn't really make sense....

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    74
    it tells you that the player won due to the way you programmed, you have the if condition that tells which one won on the loop.
    so lets say all_marbles are equal to 6, one player goes and takes 3, then the if goes, if 3-3 == 0 , which end up as true and you get that message, even though there are still 3 marbles left.

    My sugestion is that you take the ifs that tell which one won out of the loop, and you use a variable to tell which one played last, and then run the cicle until all_marbles is equal to 0.
    The last one that played is the winner.

    you also have to make sure the player only chooses 1,2 or 3.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM