# Game of Nim (PROJECT HELP!)

Show 80 post(s) from this thread on one page
Page 5 of 6 First 123456 Last
• 10-04-2012
Quote:

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;           }           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);             }         }         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.
• 10-04-2012
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 :)
• 10-04-2012
ervinako
@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);             }```
• 10-04-2012
ervinako
Quote:

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?..
• 10-04-2012
Click_here
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) ```
• 10-04-2012
Click_here
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.
• 10-04-2012
ervinako
@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?..
• 10-04-2012
Click_here
Quote:

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

Quote:

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

• 10-04-2012
ervinako
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;           }                     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 lose!\n\n", player);             }         }         printf("Do you want to try again? (Y/N)");         scanf(" %c", &ans);     }while (ans == 'Y' || ans == 'y');  return 0; }```
• 10-04-2012
ervinako
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..
• 10-04-2012
Click_here
Code:

`rows[rowChosen-1]== 0`
• 10-04-2012
ervinako
Quote:

Originally Posted by Click_here
Code:

`rows[rowChosen-1]== 0`

Code:

`rows[rowChosen-1] == 0;`
This doesnt delete any of sticks in a row.
• 10-04-2012
Click_here
The input to the array of 3 elements can only be
row[0]
row[1]
row[2]

• 10-04-2012
Click_here
Hmmm... I see you've added a redundant element to your array -> Don't worry about it :)
• 10-04-2012
Click_here
Quote:

It deleted the row http://im.cprogramming.com/images/smilies/frown.png i wanted that the row is still there but its not part of the game anymore..
anyone? http://im.cprogramming.com/images/smilies/frown.png
If you look at the if statement just before you print it out, you will see
Code:

`if (rows[i])`
Show 80 post(s) from this thread on one page
Page 5 of 6 First 123456 Last