-
As dumbed down as that was (thanks haha) I still am having difficulty figuring out what ta do! It's also due tonight at midnight (I've been so damn busy with work and midterms that I've had to cram stuff unfortunately) and I still have a whole lab to do for chemistry :( could I get an example involving my program? I find that when im rushing that those sample codes tend to just fly right through my head! (I am not asking for you to write my code with it in however, I just really need a more specific example. Generic pointer info is too much for me at the present :( )
-
So im going to assume that the pointer variables will be the rows. Do i designate the variable in the main function with the * and the point back in my play function with the row_# = row_# - rocknum?
Edit: Newest code
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
}
//Main Game
player = 1; //makes player 1 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);
}
if(player == 1)
player = 2;
else player = 1;
}
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.
}
-
So what's up with this game? Stay around ADH if you can. I may have questions.
-
This is your program, with some reordering of the code.
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");
}
//The play function. This takes the inputted changes to the board and actually impliments them
int play_nim(int row_1,int row_2,int row_3)
{
int player=2;
int rownum, rocknum; //The inputted row number and number of rocks to be removed from said row
draw_nim(row_1, row_2, row_3); //show the inital board setup
while(row_1+row_2+row_3 > 0)
{
if(player ==2)
player = 1;
else player = 2;
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);
}
if(row_1 > 0 || row_2 > 0 || row_3 > 0) //checks to make sure all the rows > 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);
}
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; //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
//Game Setup
printf("Enter the number of rocks in each row [n n n]:"); //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
}
//Start the Game
while(play_nim(row_1, row_2, row_3) > 0); //will loop until the "play_nim" function returns a zero
return 0; //the program then proceeds to die.
}
It was awkward to get it to play, because you had part of the gaming loop in playnim(), and part of it in main().
Barely tested, but seems to play OK, in them.