# Game of Nim (PROJECT HELP!)

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

Originally Posted by Adak
You need to still track how many sticks are left with the variable sum, so:

Code:

```#include <stdio.h> int main () {     int rows[] = { 2, 4, 6};     int i, j,sum;         sum=rows[0]+rows[1]+rows[2];     while(sum>0) {       printf("\n\n\tWELCOME TO GAME OF NIM\t\n\n");       for(i = 0; i < 3,; i++)       {           printf("\nRow %d:", i+1);           for(j = 0; j < rows[i] ; j++)           {                 printf("\t|");           }           printf("\t|");       }             //ask the user what row they want to pick sticks from       scanf() to get that number. (an int rowChosen)       //ask the user how many sticks they want to remove       scanf() to get that number. (an int sticksChosen)       rows[rowChosen+1] -= sticksChosen;       sum -= sticksChosen;   }     return 0; }```
And so on. Add in your player code, etc.

Code:

```printf("Please Enter a Row: ");     scanf("%d", rowChosen);     printf("Please Enter how many stick you will get: ");     scanf("\$d", sticksChosen);     rows[rowChosen+1] -= sticksChosen;     sum -= sticksChosen```
Is this right? or cause it has error expected ';'
• 10-04-2012
ervinako
Code:

```#include <stdio.h> int main () {     int rows[] = { 2, 4, 6}; //Printing Game board     int i, j; // Variable in Printing Gameboard     int player =1; //The players     int rowChosen, sticksChosen; //Scan purpose     int sum;     printf("\n\n\tWELCOME TO GAME OF NIM\t\n\n");     for(i = 0; i < 3; i++)         {           printf("\nRow %d:", i+1);             for(j = 0; j < rows[i] ; j++)             {                 printf("\t|");             }           printf("\t|");         }           printf("\n");     printf("Please Enter a Row: ");     scanf("%d", rowChosen);     printf("Please Enter how many stick you will get: ");     scanf("\$d", sticksChosen);         rows[rowChosen+1] -= sticksChosen;         sum -= sticksChosen;     return 0; }```
It runs now but when i enter a row number the program will be shut down..
• 10-04-2012
Typo on that dollar sign, replace it with percent sign:

Code:

` scanf("\$d", sticksChosen);`
Also, you forgot the & in front of rowChosen and sticksChosen, on the scanf() lines.
• 10-04-2012
ervinako
Quote:

Originally Posted by Adak
Typo on that dollar sign, replace it with percent sign:

Code:

` scanf("\$d", sticksChosen);`
Also, you forgot the & in front of rowChosen and sticksChosen, on the scanf() lines.

Code:

```printf("Please Enter a Row: ");     scanf("%d", &rowChosen);     printf("Please Enter how many stick you will get: ");     scanf("%d", &sticksChosen);         rows[rowChosen+1] -= sticksChosen;         sum -= sticksChosen;```
Thank you so much! and now i will be doing the while loop in entire program?
or i will do the player code?

Code:

```  printf("\nPlayer %d's turn! \n\n",player);        player++;             if(player == 3)             player = player - 2;```
• 10-04-2012
My mistake!
Code:

`rows[rowChosen - 1] -= sticksChosen;`
Change the +1 to -1.

The while loop is the whole game loop. Add your code to swap the player on turn, right inside it.
• 10-04-2012
ervinako
Quote:

Originally Posted by Adak
My mistake!
Code:

`rows[rowChosen - 1] -= sticksChosen;`
Change the +1 to -1.

The while loop is the whole game loop. Add your code to swap the player on turn, right inside it.

doing this will take the sticks in the game board or just take a stick in the program without deleting on the game board? cause
the game will be if you take a stick then

Row1: I I I
Row2: I I I I I
Row3: I I I I I I I

for example it will delete 3 from row1 so

Row1:
Row2: I I I I I
Row3: I I I I I I I
• 10-04-2012
After making the last change, try compiling and running it. See what you get.
• 10-04-2012
ervinako
Quote:

Originally Posted by Adak
After making the last change, try compiling and running it. See what you get.

after compiling it and running it
It just ask me the row and stick to get then end of program..
• 10-04-2012
Post your latest code, so I can see what's doing.
• 10-04-2012
ervinako
Code:

```#include <stdio.h> int main () {     int rows[] = { 2, 4, 6}; //Printing Game board     int i, j; // Variable in Printing Gameboard     int player =1; //The players     int rowChosen, sticksChosen; //Scan purpose     int sum;     printf("\n\n\tWELCOME TO GAME OF NIM\t\n\n");         for(i = 0; i < 3; i++)             {                 printf("\nRow %d:", i+1);                 for(j = 0; j < rows[i] ; j++)                     {                         printf("\t|");                     }                 printf("\t|");             }           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);             printf("Please Enter Number of sticks: ");             scanf("%d", &sticksChosen);     rows[rowChosen -1] -= sticksChosen;     sum -= sticksChosen;     return 0; }```
This is my code.
• 10-04-2012
You dropped this part:

Code:

```    sum=rows[0]+rows[1]+rows[2];     while(sum>0) {         //All your other code goes in here.   }```
Did you leave anything else out? Try it with the while loop back in place.
• 10-04-2012
ervinako
Quote:

Originally Posted by Adak
You dropped this part:

Code:

```    sum=rows[0]+rows[1]+rows[2];     while(sum>0) {         //All your other code goes in here.   }```
Did you leave anything else out? Try it with the while loop back in place.

What you mean the while loop in my other thread?
• 10-04-2012
I mean the while loop that is in #16 of this thread.
• 10-04-2012
ervinako
Quote:

Originally Posted by Adak
I mean the while loop that is in #16 of this thread.

let me code it first :)
• 10-04-2012