Thread: Game of Nim (PROJECT HELP!)

  1. #16
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    You need to still track how many sticks are left with the variable sum, so:

    Code:
    #include <stdio.h>
    
    int main ()
    {
        int rows[] = { 2, 4, 6};
        int i, j,sum;
        
        sum=rows[0]+rows[1]+rows[2];
        while(sum>0) {
           printf("\n\n\tWELCOME TO GAME OF NIM\t\n\n");
    
           for(i = 0; i < 3,; i++)
           {
              printf("\nRow %d:", i+1);
    
              for(j = 0; j < rows[i] ; j++)
              {
                    printf("\t|");
              }
    
              printf("\t|");
           }
           
           //ask the user what row they want to pick sticks from
           scanf() to get that number. (an int rowChosen)
    
           //ask the user how many sticks they want to remove
           scanf() to get that number. (an int sticksChosen)
    
           rows[rowChosen+1] -= sticksChosen;
           sum -= sticksChosen;
       }
        return 0;
    }
    And so on. Add in your player code, etc.

    Code:
    printf("Please Enter a Row: ");
        scanf("%d", rowChosen);
        printf("Please Enter how many stick you will get: ");
        scanf("$d", sticksChosen);
    
        rows[rowChosen+1] -= sticksChosen;
        sum -= sticksChosen
    Is this right? or cause it has error expected ';'

  2. #17
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Code:
    #include <stdio.h>
    
    int main ()
    {
        int rows[] = { 2, 4, 6}; //Printing Game board
        int i, j; // Variable in Printing Gameboard
        int player =1; //The players
        int rowChosen, sticksChosen; //Scan purpose
        int sum;
    
    
    
        printf("\n\n\tWELCOME TO GAME OF NIM\t\n\n");
    
    
    
        for(i = 0; i < 3; i++)
            {
              printf("\nRow %d:", i+1);
    
                for(j = 0; j < rows[i] ; j++)
                {
                    printf("\t|");
                }
    
              printf("\t|");
            }
    
              printf("\n");
    
        printf("Please Enter a Row: ");
        scanf("%d", rowChosen);
        printf("Please Enter how many stick you will get: ");
        scanf("$d", sticksChosen);
    
            rows[rowChosen+1] -= sticksChosen;
            sum -= sticksChosen;
    
        return 0;
    }
    It runs now but when i enter a row number the program will be shut down..

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Typo on that dollar sign, replace it with percent sign:

    Code:
     scanf("$d", sticksChosen);
    Also, you forgot the & in front of rowChosen and sticksChosen, on the scanf() lines.

  4. #19
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    Typo on that dollar sign, replace it with percent sign:

    Code:
     scanf("$d", sticksChosen);
    Also, you forgot the & in front of rowChosen and sticksChosen, on the scanf() lines.
    Code:
    printf("Please Enter a Row: ");
        scanf("%d", &rowChosen);
        printf("Please Enter how many stick you will get: ");
        scanf("%d", &sticksChosen);
    
            rows[rowChosen+1] -= sticksChosen;
            sum -= sticksChosen;
    Thank you so much! and now i will be doing the while loop in entire program?
    or i will do the player code?

    Code:
      printf("\nPlayer %d's turn! \n\n",player);        player++;
     
     
            if(player == 3)
                player = player - 2;

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    My mistake!
    Code:
    rows[rowChosen - 1] -= sticksChosen;
    Change the +1 to -1.

    The while loop is the whole game loop. Add your code to swap the player on turn, right inside it.

  6. #21
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    My mistake!
    Code:
    rows[rowChosen - 1] -= sticksChosen;
    Change the +1 to -1.

    The while loop is the whole game loop. Add your code to swap the player on turn, right inside it.
    doing this will take the sticks in the game board or just take a stick in the program without deleting on the game board? cause
    the game will be if you take a stick then

    Row1: I I I
    Row2: I I I I I
    Row3: I I I I I I I

    for example it will delete 3 from row1 so

    Row1:
    Row2: I I I I I
    Row3: I I I I I I I

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    After making the last change, try compiling and running it. See what you get.

  8. #23
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    After making the last change, try compiling and running it. See what you get.
    after compiling it and running it
    It just ask me the row and stick to get then end of program..

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Post your latest code, so I can see what's doing.

  10. #25
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Code:
    #include <stdio.h>
    
    int main ()
    {
        int rows[] = { 2, 4, 6}; //Printing Game board
        int i, j; // Variable in Printing Gameboard
        int player =1; //The players
        int rowChosen, sticksChosen; //Scan purpose
        int sum;
    
    
    
        printf("\n\n\tWELCOME TO GAME OF NIM\t\n\n");
    
    
            for(i = 0; i < 3; i++)
                {
                    printf("\nRow %d:", i+1);
    
                    for(j = 0; j < rows[i] ; j++)
                        {
                            printf("\t|");
                        }
    
                    printf("\t|");
                }
    
              printf("\n");
    
        printf("\nPlayer %d's turn! \n\n",player);
            player++;
                if(player == 3)
                {
                 player = player - 2;
                }
    
                printf("Please Enter a Row: ");
                scanf("%d", &rowChosen);
                printf("Please Enter Number of sticks: ");
                scanf("%d", &sticksChosen);
    
        rows[rowChosen -1] -= sticksChosen;
        sum -= sticksChosen;
    
    
        return 0;
    }
    This is my code.

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You dropped this part:

    Code:
        sum=rows[0]+rows[1]+rows[2];
        while(sum>0) {
             //All your other code goes in here.
    
       }
    Did you leave anything else out? Try it with the while loop back in place.

  12. #27
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    You dropped this part:

    Code:
        sum=rows[0]+rows[1]+rows[2];
        while(sum>0) {
             //All your other code goes in here.
    
       }
    Did you leave anything else out? Try it with the while loop back in place.
    What you mean the while loop in my other thread?

  13. #28
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I mean the while loop that is in #16 of this thread.

  14. #29
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    I mean the while loop that is in #16 of this thread.
    let me code it first

  15. #30
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Hee Hee -- lazy bones!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ and SDL amateur looking for a game project.
    By traitor_651 in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 10-15-2010, 11:16 AM
  2. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  3. Little game project
    By Gen. in forum Projects and Job Recruitment
    Replies: 7
    Last Post: 08-19-2006, 10:29 PM
  4. 2D Game Project with DirectX and C++
    By VMJC in forum Projects and Job Recruitment
    Replies: 4
    Last Post: 12-23-2004, 12:28 AM
  5. new game project need help
    By L_I_Programmer in forum Game Programming
    Replies: 1
    Last Post: 03-15-2003, 09:41 PM