# Game of Nim (PROJECT HELP!)

Show 80 post(s) from this thread on one page
Page 6 of 6 First 123456
• 10-04-2012
ervinako
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? :)
• 10-04-2012
Click_here
Code:

`if(sum==1)`
• 10-04-2012
Click_here
Or better yet - Move the part where it changes the player's turn around to after the
Code:

`if(sum==0)`
• 10-05-2012
Try this version of your program: :cool:

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; }```
• 10-05-2012
jsuite
Oh is this that new game you were talking about adak? haha...
• 10-05-2012
ervinako
Quote:

Originally Posted by jsuite
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;```
• 10-05-2012
ervinako
@ Adak and Click_me! Thank you so much for your help! :) Have a blessed day! Thank you thank you thank you! :D
• 10-05-2012
Quote:

Originally Posted by ervinako
@ Adak and Click_me! Thank you so much for your help! :) Have a blessed day! Thank you thank you thank you! :D

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.
• 10-05-2012
whiteflags
I wouldn't be so sure. You can play it online: pearls before swine
• 10-05-2012
Quote:

Originally Posted by whiteflags
I wouldn't be so sure. You can play it online: pearls before swine

Appears I underestimated "The Game of Nim". ;)
• 10-05-2012
garbag
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); }```
• 10-05-2012
Click_here
Quote:

Originally Posted by ervinako
@ Adak and Click_me! Thank you so much for your help! :) Have a blessed day! Thank you thank you thank you! :D