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.