Thread: Game of Nim (PROJECT HELP!)

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

    Game of Nim (PROJECT HELP!)

    My Professor want as to make a game that is called game of nim and this will be pass on october 8. i want your help so that we can pass it on time. the game goes like this;

    The game will be played by 2players:

    Player 1 Turn:

    ROW1: I I I
    ROW2: I I I I I
    ROW3: I I I I I I I

    Number of Row: 3
    Number of Stick to remove: 5

    ROW1: I I I
    ROW2: I I I I I
    ROW3: I I

    Player 2 Turn:

    Number of Row: 1
    Number of Stick to remove: 3

    ROW1:
    ROW2: I I I I I
    ROW3: I I

    Until the one who pick the last stick will be the looser.

    P.S: I STARTED THE CODE BUT I DON'T THINK IT IS RIGHT
    AND I'M JUST A BEGINNER. SO PLEASE I REALLY NEED YOUR HELP.



    Code:
    #include <stdio.h>
    main()
    {
        int row1;
        int row2;
        int row3;
        int i = 1;
        int player=1;
        int sum;
        int row;
    
    
    
    
    
    
        while (i<sum)
        {
            printf("\nPlayer %d's turn! \n\n",player);
            player++;
    
    
            if(player == 3)
                player = player - 2;
    
        printf("\nRow 1: ");
            for(row1 = 0; row1 < 3; row1++)
                {
                    printf("\t|");
                }
        printf("\nRow 2: ");
            for(row2 = 0; row2 <5; row2++)
                {
                    printf("\t|");
                }
        printf("\nRow 3: ");
            for(row3 = 0; row3 <7; row3++)
                {
                    printf("\t|");
                }
    
            printf("\nEnter row number: ");
            scanf("%d", &row);
    
    
            sum= i* 15;
    
        }
    
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Consider something like

    Code:
    int rows[] = { 3, 5, 7 };
    for ( i = 0 ; i < 3 ; i++ ) {
      printf("Row %d:", i+1 );
      for ( j = 0 ; j < rows[i] ; j++ ) {
        printf("\t|");
      }
      printf("\n");
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Does the game always start with 3, 5, and 7 "sticks"?

    Is the program playing, or is it just to show a game two humans are playing? This "smells" like a forced win by the first player, imo. If winning is very important, it could quickly be way over your head to program it.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I get the sense from your code that you're just trying to write code all willy-nilly. That is not how you're going to write a successful program

    Consider, for instance, your first "while()" loop. "i" is initialized to 1, but "sum' is not initialized, so there's no guarantee (while(i<sum)) that this loop will even execute.

    Also consider that, at the end of your loop (the way the code stands now), "sum" will always be equal to 15.

    Start with a pen and paper, writing out by hand each step you would take to produce this game. You need to understand how you're going to program it before you can program it.

    They way you should approach this (and any program) is to break it down into little steps. First, work on printing out the "game board." Compile and run, and make sure it prints how you want it to. Next, receive the user input, and print that to the screen. Compile and run. Then start working on how the user input will change the game board, implement that, compile and run. Step by step.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Adak View Post
    This "smells" like a forced win by the first player, imo.
    Yes, it is.

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Aha!

    Reminds me of the old Young German Soldier joke:
    Teen aged German soldier radio's that he's captured a Russian soldier.

    His officer tells him to bring him in for questioning.

    Young soldier radios back: "He won't let me."

    Last edited by Adak; 10-04-2012 at 08:47 AM.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    Does the game always start with 3, 5, and 7 "sticks"?

    Is the program playing, or is it just to show a game two humans are playing? This "smells" like a forced win by the first player, imo. If winning is very important, it could quickly be way over your head to program it.
    Yaah, it always start with 3, 5 , and 7. It Yah the game will be played with two human, and Yah our professor said that a player can take all or any number in a row.

    Like

    ROW1: I I I
    ROW2: I I I I I
    ROW3: I I I I I I

    Player can take 7 in row 3 for 1st round or 5 in row 2 or 3 in row 1. or just 2 in row 3.. that is the instruction of our teacher..

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Matticus View Post
    I get the sense from your code that you're just trying to write code all willy-nilly. That is not how you're going to write a successful program

    Consider, for instance, your first "while()" loop. "i" is initialized to 1, but "sum' is not initialized, so there's no guarantee (while(i<sum)) that this loop will even execute.

    Also consider that, at the end of your loop (the way the code stands now), "sum" will always be equal to 15.

    Start with a pen and paper, writing out by hand each step you would take to produce this game. You need to understand how you're going to program it before you can program it.

    They way you should approach this (and any program) is to break it down into little steps. First, work on printing out the "game board." Compile and run, and make sure it prints how you want it to. Next, receive the user input, and print that to the screen. Compile and run. Then start working on how the user input will change the game board, implement that, compile and run. Step by step.
    Thank you for your suggestion. Let me try printing the game board first..

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Salem View Post
    Consider something like

    Code:
    int rows[] = { 3, 5, 7 };
    for ( i = 0 ; i < 3 ; i++ ) {
      printf("Row %d:", i+1 );
      for ( j = 0 ; j < rows[i] ; j++ ) {
        printf("\t|");
      }
      printf("\n");
    }
    Okie let me try this one..

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So that's easy enough. You keep track of how many sticks are in each row, and print them out with for loops.

  11. #11
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Here is the game board. And now what is next do i need to do?


    @Salem Thank you for the suggestion.



    Code:
    #include <stdio.h>
    
    int main ()
    {
        int rows[] = { 2, 4, 6};
        int i, j;
    
        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|");
            }
    
        return 0;
    }

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    So that's easy enough. You keep track of how many sticks are in each row, and print them out with for loops.
    I'm just a bigeneer

  13. #13
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    This is now my code.. Anyone will continue to help me? PLEASE!!!



    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
    
    
    
        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("\n\nPlayer %d's turn! \n\n",player);
                    player++;
                    if(player == 3)
                    {
                        player = player - 2;
                    }
    
        return 0;
    }

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  15. #15
    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.
    Okie dude let me try it.

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