Using C programming language, design and develop a two-player number game which can b

This is a discussion on Using C programming language, design and develop a two-player number game which can b within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> int main() { unsigned int x=10; unsigned int j; int axe; while(x<=10) { printf("\nEnter 1 ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    9

    Lightbulb Using C programming language, design and develop a two-player number game which can b

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    
    
    {
    
    
         unsigned int x=10;
         unsigned int j;
         int axe;
    
    
    while(x<=10)
    {
    
    
    
    
         printf("\nEnter 1 or 2");
         scanf("%d",&axe);
         if(axe>2)
         {
             printf(" Sorry only 1 or 2 allowed");
         }
    
    
         else{
               x=x-axe;
    
    
         for ( j = 1; j <= x; ++j )
         printf( "%s", "O" );
         if(x==0)
         {
    
    
             printf("The last player won!!");
             exit(0);
         }
    
    
         }
    
    
    }
    
    
    }
    
    // Please anyone can help me complete the bold marked points in this program as mentioned below in the description to complete this program, I'd be really very grateful!!
    Actual required program: (I'm very close here)
    Game Description:
    This is a number game based on logic and strategy.
    The game is designed for two players.
    The goal of this game is to be the player who pops the last of ten balloons from the computer screen.
    A player can pop one or two balloons during his/her turn.
    The player who pops the last balloon WINS.

    Game Specifications:

    The player who pops the last balloon is the winner of the game.

    - Display the winner of the game with an appropriate message.

    - Also display the number of balloons popped by each player.

    - The game description must be provided to the players.

    - The names for each player must be accepted from the players/user.
    - Each player must be addressed by his/her name.

    - The user must select the player who starts the game.

    - The balloons must be displayed to the players/user.

    - The first player must be given the option to pop 1 or 2 balloons.
    - The updated list of balloons must be displayed to the user.

    - Then the second player must be given the option to pop 1 or 2 balloons.
    - The updated list of balloons must be displayed to the user.

    - This must continue till all the balloons from the computer screen are popped.
    Last edited by ian212; 10-05-2012 at 07:35 PM.

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Sounds a lot like the Game of Nim....

    (Sorry for not being of any help here)

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,668
    Quote Originally Posted by ian212 View Post
    Actual required program: (I'm very close here)
    My take is that you have a bit of code to start meeting a couple of the dozen requirements, but you haven't really met any of the requirements as yet. You need to work systematically through the requirements, rather than simply posting here in the hope that someone else will complete your work for you.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    Quote Originally Posted by grumpy View Post
    My take is that you have a bit of code to start meeting a couple of the dozen requirements, but you haven't really met any of the requirements as yet. You need to work systematically through the requirements, rather than simply posting here in the hope that someone else will complete your work for you.
    I'm ready to work on it, but believe me I'm short of some right resources, if you can please help me out with appropriate/similar/helping example links, I'll get to work because Google did not help me much even after long hours of searching

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,415
    I'll get to work because Google did not help me much even after long hours of searching
    O_o

    Your internet must be broken.

    grumpy is saying, in his way, that we aren't going to do this for you. We will help, but we aren't going to do it for you.

    So, taking the advice: what does "The player who pops the last balloon is the winner of the game." mean in code?

    Soma

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        char player1[20],player2[20];
        int turn,x=10,num,j;
        printf("\t\nPlease enter name of player who will take the first turn:");
        scanf("%s",&player1);
        printf("\n %s will take the first turn",player1);
        printf("\n\nPlease enter name of player who will take the second turn:");
        scanf("%s",&player2);
         printf("\n %s will take the second turn",player2);
          printf("\n\t\nGame started");
         printf("\t\n\t\nOOOOOOOOOO");
         while(x<=10)
    {
    
    
    
    
    
    
         printf("\n\n\t%s, your turn",player1);
         printf("\n\n\tEnter 1 or 2:");
         scanf("%d",&num);
         if(num>2)
         {
             printf(" Invalid choice, please try again !!");
         }
    
    
    
    
         else{
               x=x-num;
    
    
    
    
         for ( j = 1; j <= x; ++j )
         printf( "%s", "O" );
    
    
         if(x==0)
         {
    
    
    
    
             printf("The last player won!!");
             exit(0);
         }
    
    
    
    
         }
    
    
    }
    }
    This is some revised code I could try...

    I just need some help/advice so as how do I print the number of balloons popped by each player for which this code would not be much of use

    And then how do I print the player's name who won!!

    "the player who pops the last balloon is the winner of the game." in means x comes to 0 finally and there are no more balloons left out of 10

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    Quote Originally Posted by christop View Post
    Sounds a lot like the Game of Nim....

    (Sorry for not being of any help here)
    Yeah, both look like cousins

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Ian!

    Before you start programming, it's very helpful to sit down and do the puzzle/game/assignment by hand, without a computer.

    Note the patterns you use - those are the basics of the logic for the program, since the computer will need to do the same things you did.

    In a two player game, you've correctly seen that the entire game is set inside a while() loop (but a for loop or do while loop can be used).

    And straight up, STOP USING those single meaningless variable names! For a simple counter, i is fine and n for a number, but once you start running amok with a, b, c, x, etc., you'll be obscuring the logic you need.

    So:
    Code:
    int balloons = 10
    int player = 1  //player 0 will play first - this is a subtlety
    char players[2][30]; //holds both players names
    will be some of the variables you'll want to use.
    you'll want to get the players names, before the game loop starts
    and store their names in players[0] and players[1].

    Note that there is NO players[2], since valid index are 0 to size -1.

    Code:
    //start of the game loop
    while(balloons>0)  {
        //if player is 0, 
            //make it 1 
        //else 
           //make it 0
    
        //print out how many balloons are in play
        //print out the players name on turn
        //get their number of balloons to be popped
        //and subtract them
    
    }
    
    announce the winner
    That's a start. You WILL find subtleties as you code and test the program.

    Try to make your code as CLEAR and SIMPLE as you can. If you use variable names that obscure their meaning, or write complex code, you're just making it more difficult.

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    Quote Originally Posted by Adak View Post
    Welcome to the forum, Ian!

    Before you start programming, it's very helpful to sit down and do the puzzle/game/assignment by hand, without a computer.

    Note the patterns you use - those are the basics of the logic for the program, since the computer will need to do the same things you did.

    In a two player game, you've correctly seen that the entire game is set inside a while() loop (but a for loop or do while loop can be used).

    And straight up, STOP USING those single meaningless variable names! For a simple counter, i is fine and n for a number, but once you start running amok with a, b, c, x, etc., you'll be obscuring the logic you need.

    So:
    Code:
    int balloons = 10
    int player = 1  //player 0 will play first - this is a subtlety
    char players[2][30]; //holds both players names
    will be some of the variables you'll want to use.
    you'll want to get the players names, before the game loop starts
    and store their names in players[0] and players[1].

    Note that there is NO players[2], since valid index are 0 to size -1.

    Code:
    //start of the game loop
    while(balloons>0)  {
        //if player is 0, 
            //make it 1 
        //else 
           //make it 0
    
        //print out how many balloons are in play
        //print out the players name on turn
        //get their number of balloons to be popped
        //and subtract them
    
    }
    
    announce the winner
    That's a start. You WILL find subtleties as you code and test the program.

    Try to make your code as CLEAR and SIMPLE as you can. If you use variable names that obscure their meaning, or write complex code, you're just making it more difficult.
    Thanks a ton for the idea and advice, that was really helpful,I'll keep the points in mind and try to be more organized

    Will post revised/complete game once I crack it!!

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    Sorry for posting the solution after so many days..I had cracked it the same day, but some other assignments kept me busy

    So here goes the code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        char player1[20],player2[20];
        int current_player=player1;
        int balloons_play1=0;
        int balloons_play2=0;
    	char dummy[5];
        int x=10,num,j;
        printf("\n\n\t\t\t *Welcome to the Balloon Pop Game*");
        printf("\n\n\ About: This is a fun game between two players which will challenge\n\tyour logic and strategy");
        printf("\n\n\ Instructions:\n\n\t *Each player can pop 1 or 2 balloons in his turn");
        printf("\n\n\t *The Player who pops the last balloon wins");
    
    
        printf("\n\n *Please enter name of player who will take the first turn:");
        scanf("%s",&player1);
        printf("\n *%s will take the first turn",player1);
        printf("\n\n *Please enter name of player who will take the second turn:");
        scanf("%s",&player2);
         printf("\n *%s will take the second turn",player2);
          printf("\n\n\n\t\t\t\t***Game started***");
         printf("\n\n\t\t\tOOOOOOOOOO balloons remaining");
         do{
    {
    
    
    
    
         printf("\n\n\t*%s, your turn",current_player);
         printf("\n\n\t*Enter '1' to pop one balloon or '2' to pop 2 balloons:");
         scanf("%d",&num);
    
    
         if(num>2)
         {
             printf(" \n\n\t*Invalid choice, please try again !!");
         }
    
    
    
    
         else{
               x=x-num;
    
    
    
    
         for ( j = 1; j <= x; ++j )
         printf( "\n\t\t\t%s", "O" );
         printf("\n\n\t\t\t* %d balloons remaining *",x);
    
    
         if(current_player==player1)
         {
             balloons_play1=balloons_play1+num;
             current_player=player2;
    
    
         }
         else
         {
             balloons_play2=balloons_play2+num;
             current_player=player1;
    		 getchar();
         }
         }
    }
         }
    	 
    while(x>0);
    
    
            if(current_player==player1)
            {
                printf("\n\n Congratualations %s you won by popping the last balloon !!!",player2);
    
    
            }
            else
            {
                printf("\n\n\tCongratualations %s you won by popping the last balloon !!!",player1);
            }
             printf("\n\n\tBalloons popped by %s:%d",player1,balloons_play1);
             printf("\n\n\tBalloons popped by %s:%d",player2,balloons_play2);
    		 getchar();
    		 fflush(stdin);
    		 printf("");
    		 scanf("%s",dummy);
    		
    		
    
    
    
    
              
         }

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well done, Ian. Looks like you got it. You will see a benefit as your indentation style improves.

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    Quote Originally Posted by Adak View Post
    Well done, Ian. Looks like you got it. You will see a benefit as your indentation style improves.
    Yes, I hope to improve in time..Thank you!!

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by ian212 View Post
    Sorry for posting the solution after so many days..I had cracked it the same day, but some other assignments kept me busy
    Have you already tried to compile your code with all warnings on?
    You'll get a bunch of warnings mostly because of this:
    Code:
    char player1[20],player2[20];
    int current_player=player1;
    "player1" (and "player2") is an array of char, thus "player1" is a pointer to the first element. "current_player" is an int and you try to assign a pointer to it. Additionally you treat "current_player" as a pointer to char in several other parts of your code. You should therefore declare it as a pointer to char.

    Code:
    printf("\n\n\ About: This is a fun game between two players which will challenge\n\tyour logic and strategy");
    The third backslash ('\') isn't necessary.

    Code:
    scanf("%s",&player1);
    As said above, "player1" is a pointer thus you don't need the &-operator.

    Code:
    if(num>2)
    What if the player enters a number below 1? If s/he enters a negative number the balloon count rises.

    Code:
    for ( j = 1; j <= x; ++j )
    In this particular case it's just a matter of style but a more idiomatic way to write the loop is:
    Code:
    for (j = 0; j < x; ++j)
    In some cases (especially in regard to arrays) comparisons with "<=" in a loop could introduce subtle bugs.

    Code:
    fflush(stdin);
    printf("");
    scanf("%s",dummy);
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    And what should the last two lines do?

    Bye, Andreas

  14. #14
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    Quote Originally Posted by AndiPersti View Post
    Have you already tried to compile your code with all warnings on?
    You'll get a bunch of warnings mostly because of this:
    Code:
    char player1[20],player2[20];
    int current_player=player1;
    "player1" (and "player2") is an array of char, thus "player1" is a pointer to the first element. "current_player" is an int and you try to assign a pointer to it. Additionally you treat "current_player" as a pointer to char in several other parts of your code. You should therefore declare it as a pointer to char.

    Code:
    printf("\n\n\ About: This is a fun game between two players which will challenge\n\tyour logic and strategy");
    The third backslash ('\') isn't necessary.

    Code:
    scanf("%s",&player1);
    As said above, "player1" is a pointer thus you don't need the &-operator.

    Code:
    if(num>2)
    What if the player enters a number below 1? If s/he enters a negative number the balloon count rises.

    Code:
    for ( j = 1; j <= x; ++j )
    In this particular case it's just a matter of style but a more idiomatic way to write the loop is:
    Code:
    for (j = 0; j < x; ++j)
    In some cases (especially in regard to arrays) comparisons with "<=" in a loop could introduce subtle bugs.

    Code:
    fflush(stdin);
    printf("");
    scanf("%s",dummy);
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    And what should the last two lines do?

    Bye, Andreas
    Thank you for pointing out all those junk

    You're right about the warnings, and you're right about the incorrect logic too!!

    The last two lines do nothing..they just keep the program open, maybe i should do it some other way

    Got 96/100 just because when 1 balloon remains and the player enters 2, it says "-1 balloons remaining"

    I don't know why I missed such simple logic, well, for now I've got some assignments and tests, will post a revised solution very soon!!

    Thanks once again for taking the time to read my code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to develop good logic in C language
    By rocky in forum C Programming
    Replies: 3
    Last Post: 02-19-2006, 08:52 AM
  2. assembly language...the best tool for game programming?
    By silk.odyssey in forum Game Programming
    Replies: 50
    Last Post: 06-22-2004, 02:11 PM
  3. Game Programming Language
    By ??? in forum Game Programming
    Replies: 18
    Last Post: 06-09-2004, 09:39 PM
  4. Game Programming Language?
    By drdroid in forum Game Programming
    Replies: 62
    Last Post: 09-30-2002, 09:18 AM
  5. game design from the non programming side
    By Scourfish in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-29-2002, 01:07 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21