# Thread: Game of Nim (PROJECT HELP!)

1. 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. Code:
`if(sum==1)`

3. Or better yet - Move the part where it changes the player's turn around to after the
Code:
`if(sum==0)`

4. 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);
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. Oh is this that new game you were talking about adak? haha...

6. 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;
}

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. @ Adak and Click_me! Thank you so much for your help! Have a blessed day! Thank you thank you thank you!

8. 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!
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. I wouldn't be so sure. You can play it online: pearls before swine

10. 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".

11. 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);

}```

12. 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!

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 -