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;
}
printf("Please Enter a Row: ");
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;
}