# Game of Nim (PROJECT HELP!)

Show 80 post(s) from this thread on one page
Page 4 of 6 First 123456 Last
• 10-04-2012
ervinako
This is my final code.. Just one problem when the blank row.. T.T How to fix it..
when the row is empty it should not be counted anymore T.T help help any solutions..

Code:

#include <stdio.h>

int main ()
{

char ans;

do{
int rows[] = { 3, 5, 7};
int i, j;
int player =1;
int rowChosen, sticksChosen;
int sum;

printf("\n\n\t\t\WELCOME TO GAME OF NIM\t\t\t\t\n\n");
printf("\t\tBy:Ervin And Orven\t\t\t\t\n\n\n\n");

sum=rows[0]+rows[1]+rows[2];

while(sum>0)
{

for(i = 0; i < 3; i++)
{
printf("\nRow %d:", i+1);

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

if(rowChosen > 3 || rowChosen < 1)
{
printf("Error! Enter number from 1-3!\n");

while (rowChosen > 3|| rowChosen < 1)
{
printf("Please Enter a Row Again:  ");
scanf("%d", &rowChosen);
}
}

printf("Please Enter Number of sticks to remove: ");
scanf("%d", &sticksChosen);

switch(rowChosen)
{
case 1:
{
if(sticksChosen > 3 ||sticksChosen < 1)
printf("Error! Enter number from 1-3!\n");

while(sticksChosen > 3||sticksChosen < 1)
{
printf("Please Enter Number of sticks to remove again:");
scanf("%d", &sticksChosen);
}
}

break;

case 2:
{
if(sticksChosen > 5|| sticksChosen < 1)
{
printf("Error! Enter number from 1-5!\n");
}

while(sticksChosen > 5 || sticksChosen < 1)
{
printf("Please Enter Number of sticks to remove again:");
scanf("%d", &sticksChosen);
}
}

break;

case 3:
{
if(sticksChosen > 7 || sticksChosen < 1)
printf("Error! Enter number from 1-7!\n");

while(sticksChosen > 7 || sticksChosen < 1)
{
printf("Please Enter Number of sticks to remove again:");
scanf("%d", &sticksChosen);
}
}

break;

}

rows[rowChosen -1] -= sticksChosen;
sum -= sticksChosen;

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

printf("Do you wanna try again? (Y/N)");
scanf("%s", &ans);

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

return 0;

}

• 10-04-2012
Maybe a test before printing the row number?

Code:

if(rows[i])    //new
printf("\nRow %d:", i+1);

• 10-04-2012
ervinako
Quote:

Maybe a test before printing the row number?

Code:

if(rows[i])    //new
printf("\nRow %d:", i+1);

Where can i put it??
• 10-04-2012
The if(rows[i]) should be on line 27
• 10-04-2012
ervinako
Quote:

The if(rows[i]) should be on line 27

It automatically shut the program..

the header will print but the game will stop..
• 10-04-2012
Quote:

Originally Posted by ervinako
It automatically shut the program..

the header will print but the game will stop..

Post your new code, line 20 through line 40. I need to see what you're doing.
• 10-04-2012
ervinako
Quote:

Post your new code, line 20 through line 40. I need to see what you're doing.

Code:

while(sum>0)
{

if(rows[i])
{
printf("\nRow %d:", i+1);
}

for(i = 0; i < 3; i++)
{
printf("\nRow %d:", i+1);

for(j = 0; j < rows[i] ; j++)
{
printf("\t|");
}

• 10-04-2012
From post #46, above:

Code:

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

printf("\n");
} //end of new if statement
}

• 10-04-2012
ervinako
Quote:

From post #46, above:

Code:

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

printf("\n");
} //end of new if statement
}

i will delete the code? from #26 to #37 and put it in #46?..
• 10-04-2012
ervinako
Quote:

Originally Posted by ervinako
i will delete the code? from #26 to #37 and put it in #46?..

Code:

#include <stdio.h>

int main ()
{

char ans;

do{

int rows[] = { 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[0]+rows[1]+rows[2];

while(sum>0)
{

if(rows[i])
{
printf("\nRow %d:", i+1);
}

printf("\nPlayer %d's turn! \n\n",player);
player++;
if(player == 3)
{
player = player - 2;
}

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

if(rowChosen > 3 || rowChosen < 1)
{
printf("Error! Enter number from 1-3!\n");

while (rowChosen > 3|| rowChosen < 1)
{
printf("Please Enter a Row Again:  ");
scanf("%d", &rowChosen);
}
}

printf("Please Enter Number of sticks to remove: ");
scanf("%d", &sticksChosen);

switch(rowChosen)
{
case 1:
{
if(sticksChosen > 3 ||sticksChosen < 1)
printf("Error! Enter number from 1-3!\n");

while(sticksChosen > 3||sticksChosen < 1)
{
printf("Please Enter Number of sticks to remove again:");
scanf("%d", &sticksChosen);
}
}

break;

case 2:
{
if(sticksChosen > 5|| sticksChosen < 1)
{
printf("Error! Enter number from 1-5!\n");
}

while(sticksChosen > 5 || sticksChosen < 1)
{
printf("Please Enter Number of sticks to remove again:");
scanf("%d", &sticksChosen);
}
}

break;

case 3:
{
if(sticksChosen > 7 || sticksChosen < 1)
printf("Error! Enter number from 1-7!\n");

while(sticksChosen > 7 || sticksChosen < 1)
{
printf("Please Enter Number of sticks to remove again:");
scanf("%d", &sticksChosen);
}
}

break;

}

rows[rowChosen -1] -= sticksChosen;
sum -= sticksChosen;

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

printf("Do you wanna try again? (Y/N)");
scanf("%s", &ans);

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

return 0;

}

is this what you mean or what?..

it crashes the program..
• 10-04-2012
Quote:

Originally Posted by ervinako
i will delete the code? from #26 to #37 and put it in #46?..

NO!

Then you will add the lines I marked in red above, just as they are in my post, right into your outer for loop, just like I have it.

Not above the first for loop, but BETWEEN the two for loops.
• 10-04-2012
ervinako
Is this what you mean? :)) Sorry for not getting..
It will delete the row1? but if i wanted not to delete row 1? just let it stay there but
if the person chooses the row one it will promt error! the row have no more sticks anymore.

like this

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

Enter row: 1
Opps Row 1 have no more sticks anymore, please try again.

Code:

#include <stdio.h>

int main ()
{

char ans;

do{

int rows[] = { 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[0]+rows[1]+rows[2];

while(sum>0)
{

for(i = 0; i < 3; i++)
{
if(rows[i])
{
printf("\nRow %d:", i+1);
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);

if(rowChosen > 3 || rowChosen < 1)
{
printf("Error! Enter number from 1-3!\n");

while (rowChosen > 3|| rowChosen < 1)
{
printf("Please Enter a Row Again:  ");
scanf("%d", &rowChosen);
}
}

printf("Please Enter Number of sticks to remove: ");
scanf("%d", &sticksChosen);

switch(rowChosen)
{
case 1:
{
if(sticksChosen > 3 ||sticksChosen < 1)
printf("Error! Enter number from 1-3!\n");

while(sticksChosen > 3||sticksChosen < 1)
{
printf("Please Enter Number of sticks to remove again:");
scanf("%d", &sticksChosen);
}
}

break;

case 2:
{
if(sticksChosen > 5|| sticksChosen < 1)
{
printf("Error! Enter number from 1-5!\n");
}

while(sticksChosen > 5 || sticksChosen < 1)
{
printf("Please Enter Number of sticks to remove again:");
scanf("%d", &sticksChosen);
}
}

break;

case 3:
{
if(sticksChosen > 7 || sticksChosen < 1)
printf("Error! Enter number from 1-7!\n");

while(sticksChosen > 7 || sticksChosen < 1)
{
printf("Please Enter Number of sticks to remove again:");
scanf("%d", &sticksChosen);
}
}

break;

}

rows[rowChosen -1] -= sticksChosen;
sum -= sticksChosen;

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

printf("Do you wanna try again? (Y/N)");
scanf("%s", &ans);

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

return 0;

}

• 10-04-2012
garbag
Solution...
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);

}

• 10-04-2012
ervinako
Quote:

Originally Posted by garbag
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);

}

This code is actually the same as mine and it make me so confuse.

what I'm trying to say is: For example row1: has no sticks anymore.
if the user will choose the row 1 it will print error cause theirs no more sticks in it.

like this:

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

Enter a row: 2
Opps!, that row has no sticsk anymore please try again:
Enter a row: 3
How many sticks to remove:

Thats what i'm trying to code..
• 10-04-2012
ervinako
Help.
Show 80 post(s) from this thread on one page
Page 4 of 6 First 123456 Last