Thread: Game of Nim (PROJECT HELP!)

  1. #76
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Last question?

    Declaring the in declaring the winner the one who picks the last stick will be the looser not the one who have no more to pick..
    Any suggestion?

  2. #77
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    if(sum==1)
    Fact - Beethoven wrote his first symphony in C

  3. #78
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Or better yet - Move the part where it changes the player's turn around to after the
    Code:
    if(sum==0)
    Fact - Beethoven wrote his first symphony in C

  4. #79
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Try this version of your program:

    Code:
    #include <stdio.h>
    
    int main ()
    {
        char ans;
        int rows[4];
        int i, j, player,rowChosen, sticksChosen, sum;
        
        do
        {
            rows[0] = 0;
            rows[1] = 3;
            rows[2] = 5;
            rows[3] = 7;
            
            player =1;
            
    
            printf("\n\n");
            printf("\t\t******************************\t");
            printf("\n\t\t****WELCOME TO GAME OF NIM****\t\t\n");
            printf("\t\tErvin Ardiente & Orven Dumasig\t\n");
            printf("\t\t******************************\t\n\n");
    
            sum = rows[1] + rows[2] + rows[3];
    
            while(sum > 0)
            {
                for(i = 1; i <= 3; i++)
                {
                    if(rows[i])
                    {
                        printf("\nRow %d:", i);
                        for(j = 0; j < rows[i] ; j++)
                        {
                            printf("\t|");
                        }
                        printf("\n");
                    }
                }
                printf("\n");
    
                printf("\nPlayer %d's turn! \n\n",player);
                printf("Please Enter a Row: ");
                scanf("%d", &rowChosen);
    
                while(rowChosen > 3 || rowChosen < 1 || rows[rowChosen] == 0)
                {
                    printf("Error! Please enter a row with sticks: ");
                    scanf("%d", &rowChosen);
                }
                printf("Please Enter Number of sticks to remove: ");
                scanf("%d", &sticksChosen);
    
                while(sticksChosen > rows[rowChosen] || sticksChosen < 1)
                { 
                    printf("Error! Please Enter Number of sticks to remove: ");
                    scanf("%d", &sticksChosen); 
                }    
                rows[rowChosen] -= sticksChosen;
                sum -= sticksChosen;
    
                if(sum == 0)
                {
                    printf("\n\nPlayer %d You lose!\n\n", player);
                }
                player++;
                if(player == 3)
                {
                    player = player - 2;
                }
            }
            printf("Do you want to try again? (Y/N) ");
            scanf(" %c", &ans);
    
        }while (ans == 'Y' || ans == 'y');
    
        return 0;
    }

  5. #80
    Registered User
    Join Date
    Sep 2012
    Posts
    48
    Oh is this that new game you were talking about adak? haha...

  6. #81
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by jsuite View Post
    Oh is this that new game you were talking about adak? haha...
    I have this final code anyone check it?

    Code:
    #include <stdio.h>
    
    
    int main ()
    {
    
        char ans;
    
        do{
    
            int rows[] = {0, 3, 5, 7};
            int i, j;
            int player =1;
            int rowChosen, sticksChosen;
            int sum;
    
            printf("\n\n");
            printf("\t\t******************************\t");
            printf("\n\t\t****WELCOME TO GAME OF NIM****\t\t\n");
            printf("\t\tErvin Ardiente & Orven Dumasig\t\n");
            printf("\t\t******************************\t\n\n");
    
            sum=rows[1]+rows[2]+rows[3];
    
            while(sum>0)
            {
    
               for(i = 1; i <=3; i++)
               {
                  {
                     printf("\nRow %d:", i);
                     for(j = 0; j < rows[i] ; j++)
                     {
                        printf("\t|");
                     }
                     printf("\n");
                  }
               }
               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);
    
            while(rowChosen > 3 || rowChosen < 1 || rows[rowChosen]== 0)
            {
                printf("Error! Please enter a row with sticks: ");
                scanf("%d", &rowChosen);
            }
    
                printf("Please Enter Number of sticks to remove: ");
                scanf("%d", &sticksChosen);
    
            while(sticksChosen > rows[rowChosen] || sticksChosen < 1)
            {
                printf("Error! Please Enter Number of sticks to remove again: ");
                scanf("%d", &sticksChosen);
            }
    
            rows[rowChosen] -= sticksChosen;
            sum -= sticksChosen;
    
            if(sum==0)
                {
                    printf("\n\nPlayer %d You Win!!\n\n", player);
                }
            }
    
            printf("Do you want to try again? (Y/N)");
            scanf(" %c", &ans);
    
        }while (ans == 'Y' || ans == 'y');
    
     return 0;

  7. #82
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    @ Adak and Click_me! Thank you so much for your help! Have a blessed day! Thank you thank you thank you!

  8. #83
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by ervinako View Post
    @ Adak and Click_me! Thank you so much for your help! Have a blessed day! Thank you thank you thank you!
    You're welcome and do test that final program you posted. I don't believe anyone here actually plays "The Game of Nim", so it's up to you and your partner on the program.

  9. #84
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I wouldn't be so sure. You can play it online: pearls before swine

  10. #85
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by whiteflags View Post
    I wouldn't be so sure. You can play it online: pearls before swine
    Appears I underestimated "The Game of Nim".

  11. #86
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    Code:
    #include <stdio.h>
    
    
    #define ARRAY_SIZE 3
    
    
    int MyArray[ARRAY_SIZE] = { 2, 4, 6};
    int print()
    {
        int result=0;
        int i,j;
        for(i=0;i<ARRAY_SIZE;i++) {
            printf("Row %d:", i+1);
            for(j=0;j<MyArray[i];j++) {
                printf(" |");
            }
            printf("\n"); fflush(stdout);
        }
        return 0;
    }
    int sum()
    {
        int result=0;
        int i;
        for(i=0;i<ARRAY_SIZE;i++) {
            printf("MyArray[ %d ] = %d\n", i, MyArray[i]);
            result += MyArray[i];
        }
        return result;
    }
    
    
    main()
    {
        int row, sticks;
        int player=2;
        while(sum() != 0) {
            player = (player == 1) ? 2: 1;
            print();
    
    
            printf("\nPlayer %d plays\n", player);
    
    
            printf("row:"); fflush(stdout);
            scanf("%d", &row);
            printf("sticks:"); fflush(stdout);
            scanf("%d", &sticks);
    
    
            if( (row <= 0) || (row > ARRAY_SIZE) ) {
                printf("ERROR: row %d is invalid\n", row);
                player = (player == 1) ? 2: 1;
                continue;
            }
    
    
            row -= 1;
    
    
            if ( (sticks==0) || (MyArray[row] < sticks) ) {
                printf("ERROR: sticks %d is invalid\n", sticks);
                player = (player == 1) ? 2: 1;
                continue;
            }
    
    
            MyArray[row] -= sticks;
        }
        printf("Player %d Lost\n", player);
    
    
    }
    Last edited by garbag; 10-05-2012 at 01:56 PM.

  12. #87
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by ervinako View Post
    @ Adak and Click_me! Thank you so much for your help! Have a blessed day! Thank you thank you thank you!
    Your welcome.

    I hope I do - today is my first cricket match of the season!

    I noticed that in your code that you didn't move where the play's turns change to after the code checks to see if the player has lost -
    Fact - Beethoven wrote his first symphony in C

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