Code:
#include <stdio.h>
void draw_nim(int row_1, int row_2, int row_3) //function draws the gameboard
{
//Each of the following take the inputted row value and proceeds to output the number of O's that is inputted as well
//for Row 1
printf("Row 1: ");
for(int i = 0; i < row_1; i = i + 1)
{
printf("O");
}
printf("\n");
//Row 2
printf("Row 2: ");
for(int i = 0; i < row_2; i = i + 1)
{
printf("O");
}
printf("\n");
//Row 3
printf("Row 3: ");
for(int i = 0; i < row_3; i = i + 1)
{
printf("O");
}
printf("\n");
}
int play_nim(int rownum, int rocknum, int row_1, int row_2, int row_3) //The play function. This takes the inputted changes to the board and actually impliments them
{
if(row_1 > 0 && row_2 > 0 && row_3 > 0) //checks to make sure all the rows are are greater than zero (still have rocks present)
{
if(rownum == 1) //for row 1 it removes the rocks the player inputs
{
row_1 = row_1 - rocknum;
}
if(rownum == 2) //for row 2 it removes the rocks the player inputs
{
row_2 = row_2 - rocknum;
}
if(rownum == 3) //for row 3 it removes the rocks the player inputs
{
row_3 = row_3 - rocknum;
}
draw_nim(row_1, row_2, row_3);
}
else return 0; //if there are no rocks left on the board the function returns zero
}
int main(void)
{
int row_1, row_2, row_3; //The number of rocks in each row
int rownum, rocknum; //The inputted row number and number of rocks to be removed from said row
int player; //The variable stating which player is currently playing
//Game Setup
printf("Enter the number of rocks in each row: "); //Player imputs how many rocks rows 1, 2 and 3 each have
scanf("%d %d %d", &row_1, &row_2, &row_3);
while(row_1 <= 0 || row_2 <= 0 || row_3 <= 0) //checks to make sure that only a positive value is imputted for each row
{
printf("You must enter in values greater than zero. Try again:");
scanf("%d %d %d", &row_1, &row_2, &row_3); //lets the player input new values if the first are invalid
}
draw_nim(row_1, row_2, row_3); //draws the initial board
//Main Game
player = 1; //makes player 1 is active
if(player = 2)
player = 1;
else player = 1;
while(play_nim(rownum, rocknum, row_1, row_2, row_3) > 0) //will loop until the "play_nim" function returns a zero
{
printf("Player %d - Enter the row and number of rocks to remove: ", player); //Player one picks the row and number of rocks to remove
scanf("%d %d", &rownum, &rocknum);
//Defensive Programming and subsequent reentry of variables in case of invalid inputs
while(rownum > 3 && rownum <= 0) //Makes sure only rows 1-3 are imputted
{
printf("Invalid move. Try again: ");
scanf("%d %d", &rownum, &rocknum);
}
if(rownum == 1)
{
while(rocknum > row_1 || row_1 == 0) //ensures that player 1 cannot take more rocks than row 1 contains
{
printf("You can't take more rocks than there is left in the row! Try again: ");
scanf("%d %d", &rownum, &rocknum);
}
}
if(rownum == 2) //ensures that player 1 cannot take more rocks than in row 2
{
while(rocknum > row_2 || row_2 == 0)
{
printf("You can't take more rocks than there is left in the row! Try again: ");
scanf("%d %d", &rownum, &rocknum);
}
}
if(rownum == 3)
{
while(rocknum > row_3 || row_3 == 0) //player 1 cannot take more rocks than in row 2
{
printf("You can't take more rocks than there is left in the row! Try again: ");
scanf("%d %d", &rownum, &rocknum);
}
}
while(rocknum <= 0) //player 1 cannot take more rocks than in row 3
{
printf("You cannot take zero or negative rocks! Try again: ");
scanf("%d %d", &rownum, &rocknum);
}
play_nim(rownum, rocknum, row_1, row_2, row_3); //takes the inputted values and sticks them in the play function
player = player + 1
printf("Player %d - Enter the row and number of rocks to remove: ", player);
scanf("%d %d", &rownum, &rocknum);
//Defensive Programming (all the lines of code are the exact same as the programming for player 1)
while(rownum > 3 && rownum <= 0)
{
printf("You may only pick rows 1 to 3. Try again: ");
scanf("%d %d", &rownum, &rocknum);
}
if(rownum == 1)
{
while(rocknum > row_1)
{
printf("You can't take more rocks than there is left in the row! Try again: ");
scanf("%d %d", &rownum, &rocknum);
}
}
if(rownum == 2)
{
while(rocknum > row_2)
{
printf("You can't take more rocks than there is left in the row! Try again: ");
scanf("%d %d", &rownum, &rocknum);
}
}
if(rownum == 3)
{
while(rocknum > row_3)
{
printf("You can't take more rocks than there is left in the row! Try again: ");
scanf("%d %d", &rownum, &rocknum);
}
}
while(rocknum <= 0)
{
printf("You cannot take zero or negative rocks! Try again: ");
scanf("%d %d", &rownum, &rocknum);
}
play_nim(rownum, rocknum, row_1, row_2, row_3);
}
printf("Player %d wins!", player); //when the rows all read zero and the play function returns a zero, the game ends. This declares the winner
return 0; //the program then proceeds to die.
}