# Thread: Game of Nim (PROJECT HELP!)

1. Originally Posted by ervinako
Help.

Try that.

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++)
{
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);
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: ");
scanf("%d", &sticksChosen);
}
rows[rowChosen] -= sticksChosen;
sum -= sticksChosen;

if(sum==0)
{
printf("\n\nPlayer %d You lose!\n\n", player);
}
}

printf("Do you want to try again? (Y/N)");
scanf(" %c", &ans);

}while (ans == 'Y' || ans == 'y');

return 0;

}```
Note that I moved all the rows up by one, so rows[0] is empty and not part of the game, anymore.

2. After you check to see if the row was a valid input, check to see if the row is not equal to zero with an 'if' statement in exactly the same way -> Yes it is that easy

3. @Adak, Sir the the one who will picks the last stick will be the looser not the one who gets last.

I got the problem:

Player 2 turn:

Row1: I
Row2:
Row3:

Enter row: 1
Enter sticks to remove: 1

Player 2 is the looser.

My code actually says: Player 1 is the looser. how come? T.T the player 2 must be the looser cause he/she picks the last stick.

Code:
```  if(sum == 0)
{
printf("\n\nPlayer %d You lose!\n\n", player);
}```

4. Originally Posted by Click_here
After you check to see if the row was a valid input, check to see if the row is not equal to zero with an 'if' statement in exactly the same way -> Yes it is that easy
What are you trying to say?..

Code:
```            for(i = 0; i < 3; i++)
{
if(rows[i] ==0)
{
printf("\nRow %d:", i+1);
for(j = 0; j < rows[i] ; j++)
{
printf("\t|");
}

printf("\n");
}
}```
is this what you try to say? or what?..

5. Adak's solution to the problem does what I meant, but one step further. Instead of two 'if' statements, Adak has combined them into one and held the code's execution there until the correct input is received.

Code:
```while(rowChosen > 3 || rowChosen < 1 || rows[rowChosen]== 0)
```

6. On second thought, it might be better to split the 2 apart - Check that the rowChosen is one, two, or three before
Code:
`rows[rowChosen-1] == 0`
If you try to read out of bounds for the array, you will crash your program.

7. @Adak, oh the program works sorry for not reading it.. now i know.
But i wanted that the row1: (empty) and it not part of the game anymore,
do you have an idea?..

8. Originally Posted by ervinako
Can you please tell were to put the this

Originally Posted by click_here
After you check to see if the row was a valid input, check to see if the row is not equal to zero with an 'if' statement in exactly the same way

9. It deleted the row i wanted that the row is still there but its not part of the game anymore..
anyone?

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++)
{
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);
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 lose!\n\n", player);
}
}

printf("Do you want to try again? (Y/N)");
scanf(" %c", &ans);

}while (ans == 'Y' || ans == 'y');

return 0;

}```

10. One problem also is, when it declared the looser the looser will be the one who picks the
last stick not the one who is not picking the stick anymore..

11. Code:
`rows[rowChosen-1]== 0`

12. Originally Posted by Click_here
Code:
`rows[rowChosen-1]== 0`
Code:
`rows[rowChosen-1] == 0;`
This doesnt delete any of sticks in a row.

13. The input to the array of 3 elements can only be
row[0]
row[1]
row[2]

`if (rows[i])`