# Game of Nim (PROJECT HELP!)

Show 80 post(s) from this thread on one page
Page 1 of 6 123456 Last
• 10-04-2012
ervinako
Game of Nim (PROJECT HELP!)
My Professor want as to make a game that is called game of nim and this will be pass on october 8. i want your help so that we can pass it on time. the game goes like this;

The game will be played by 2players:

Player 1 Turn:

ROW1: I I I
ROW2: I I I I I
ROW3: I I I I I I I

Number of Row: 3
Number of Stick to remove: 5

ROW1: I I I
ROW2: I I I I I
ROW3: I I

Player 2 Turn:

Number of Row: 1
Number of Stick to remove: 3

ROW1:
ROW2: I I I I I
ROW3: I I

Until the one who pick the last stick will be the looser.

P.S: I STARTED THE CODE BUT I DON'T THINK IT IS RIGHT
AND I'M JUST A BEGINNER. SO PLEASE I REALLY NEED YOUR HELP.

Code:

```#include <stdio.h> main() {     int row1;     int row2;     int row3;     int i = 1;     int player=1;     int sum;     int row;     while (i<sum)     {         printf("\nPlayer %d's turn! \n\n",player);         player++;         if(player == 3)             player = player - 2;     printf("\nRow 1: ");         for(row1 = 0; row1 < 3; row1++)             {                 printf("\t|");             }     printf("\nRow 2: ");         for(row2 = 0; row2 <5; row2++)             {                 printf("\t|");             }     printf("\nRow 3: ");         for(row3 = 0; row3 <7; row3++)             {                 printf("\t|");             }         printf("\nEnter row number: ");         scanf("%d", &row);         sum= i* 15;     }     return 0; }```
• 10-04-2012
Salem
Consider something like

Code:

```int rows[] = { 3, 5, 7 }; for ( i = 0 ; i < 3 ; i++ ) {   printf("Row %d:", i+1 );   for ( j = 0 ; j < rows[i] ; j++ ) {     printf("\t|");   }   printf("\n"); }```
• 10-04-2012

Is the program playing, or is it just to show a game two humans are playing? This "smells" like a forced win by the first player, imo. If winning is very important, it could quickly be way over your head to program it.
• 10-04-2012
Matticus
I get the sense from your code that you're just trying to write code all willy-nilly. That is not how you're going to write a successful program

Consider, for instance, your first "while()" loop. "i" is initialized to 1, but "sum' is not initialized, so there's no guarantee (while(i<sum)) that this loop will even execute.

Also consider that, at the end of your loop (the way the code stands now), "sum" will always be equal to 15.

Start with a pen and paper, writing out by hand each step you would take to produce this game. You need to understand how you're going to program it before you can program it.

They way you should approach this (and any program) is to break it down into little steps. First, work on printing out the "game board." Compile and run, and make sure it prints how you want it to. Next, receive the user input, and print that to the screen. Compile and run. Then start working on how the user input will change the game board, implement that, compile and run. Step by step.
• 10-04-2012
AndiPersti
Quote:

This "smells" like a forced win by the first player, imo.

Yes, it is.

Bye, Andreas
• 10-04-2012
Aha!

Reminds me of the old Young German Soldier joke:
Teen aged German soldier radio's that he's captured a Russian soldier.

His officer tells him to bring him in for questioning.

Young soldier radios back: "He won't let me."

;)
• 10-04-2012
ervinako
Quote:

Is the program playing, or is it just to show a game two humans are playing? This "smells" like a forced win by the first player, imo. If winning is very important, it could quickly be way over your head to program it.

Yaah, it always start with 3, 5 , and 7. It Yah the game will be played with two human, and Yah our professor said that a player can take all or any number in a row.

Like

ROW1: I I I
ROW2: I I I I I
ROW3: I I I I I I

Player can take 7 in row 3 for 1st round or 5 in row 2 or 3 in row 1. or just 2 in row 3.. that is the instruction of our teacher..
• 10-04-2012
ervinako
Quote:

Originally Posted by Matticus
I get the sense from your code that you're just trying to write code all willy-nilly. That is not how you're going to write a successful program

Consider, for instance, your first "while()" loop. "i" is initialized to 1, but "sum' is not initialized, so there's no guarantee (while(i<sum)) that this loop will even execute.

Also consider that, at the end of your loop (the way the code stands now), "sum" will always be equal to 15.

Start with a pen and paper, writing out by hand each step you would take to produce this game. You need to understand how you're going to program it before you can program it.

They way you should approach this (and any program) is to break it down into little steps. First, work on printing out the "game board." Compile and run, and make sure it prints how you want it to. Next, receive the user input, and print that to the screen. Compile and run. Then start working on how the user input will change the game board, implement that, compile and run. Step by step.

Thank you for your suggestion. Let me try printing the game board first..
• 10-04-2012
ervinako
Quote:

Originally Posted by Salem
Consider something like

Code:

```int rows[] = { 3, 5, 7 }; for ( i = 0 ; i < 3 ; i++ ) {   printf("Row %d:", i+1 );   for ( j = 0 ; j < rows[i] ; j++ ) {     printf("\t|");   }   printf("\n"); }```

Okie let me try this one..
• 10-04-2012
So that's easy enough. You keep track of how many sticks are in each row, and print them out with for loops.
• 10-04-2012
ervinako
Here is the game board. And now what is next do i need to do?

@Salem Thank you for the suggestion.

Code:

``` #include <stdio.h> int main () {     int rows[] = { 2, 4, 6};     int i, j;     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|");         }     return 0; }```
• 10-04-2012
ervinako
Quote:

So that's easy enough. You keep track of how many sticks are in each row, and print them out with for loops.

I'm just a bigeneer :(
• 10-04-2012
ervinako
This is now my code.. Anyone will continue to help me? PLEASE!!!

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     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("\n\nPlayer %d's turn! \n\n",player);                 player++;                 if(player == 3)                 {                     player = player - 2;                 }     return 0; }```
• 10-04-2012
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; }```
• 10-04-2012
ervinako
Quote:

```#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; }```