Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I built a game to play mancala, however, I want to make it so the player can cho

ID: 3566631 • Letter: I

Question

I built a game to play mancala, however, I want to make it so the player can choose before every move if they want to save the game to come back later as well as quit the game if they wish to. I know how to change the code to quit the game, however, I don't know who to add an I/O file in to allow them to save and quit. Please help!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int pos,x,curPos, players, player, player_1, position, cur_position,s;


//============================================================

int main(void)
{ int board[14];
board[0] = 4;
board[1] = 4;
board[2] = 4;
board[3] = 4;
board[4] = 4;
board[5] = 4;
board[6] = 0;
board[7] = 4;
board[8] = 4;
board[9] = 4;
board[10] = 4;
board[11] = 4;
board[12] = 4;
board[13] = 0;
//welcome users, set again='y' and enter while loop
printf(" Welcome to Mancala! ");
char again='y';
int players;
while (again=='y' || again=='Y')
   {
   //ask if one or two user game
   printf("How many players will be playing?");
   printf(" Options: 1. One Player 2. Two Players ");
   scanf("%d", &players);
   //if 1 player, enter function playing with computer
   if (players==1)
       {
       one_player(board);
       }
   //if 2 players enter function for 2 user input
   if (players==2)
       {
       two_player(board);
       }
   //ask if want to play again after completing a game  
   printf("Do you want to play again? Y or N ");
   scanf("%s", &again);
   }  
return 0;
}

//=================================================================

int one_player(int board[])
{

board[0] = 4;
board[1] = 4;
board[2] = 4;
board[3] = 4;
board[4] = 4;
board[5] = 4;
board[6] = 0;
board[7] = 4;
board[8] = 4;
board[9] = 4;
board[10] = 4;
board[11] = 4;
board[12] = 4;
board[13] = 0;
int position, s, cur_position;
int sum = board[6] + board[13];
//enter function to determine who will move first
int player_1=first_player();

//display the current look of the board
cur_board(board);
//while all the marble are not in the mancala
while (sum != 48)
{
   //with computer player_1=0, complete computer's move
   if (player_1==0)
   {
      
       printf("Computer Turn ");
       //select position through random generator and check if it fit parameter
       position=run();

       while (position!=6 && position!=7 && position!=8 && position!=9 && position!=10 && position!=11)
           {
           position=run();  
           }
       printf("Moved position %d ", position);
       position +=1;
       //enter function to complete the move
       cur_position=move_counter(board, position, player_1);
       //display updated board and position from which the move was made
       cur_board(board);
       s=complete(player_1, board);
      
       //check if the player can go again, if so, allow them another turn
       while(cur_position==13 && s==2)
       {
           printf(" *************************** ");
           printf(" COMPUTER GOES AGAIN ");
           printf("*************************** ");
           printf("Computer Turn ");

                   position=run();

           while (position!=6 && position!=7 && position!=8 && position!=9 && position!=10 && position!=11)
               {
               position=run();  
               }
           printf("Moved position %d ", position);
           position +=1;  
           //enter function to make another move
           cur_position=move_counter(board, position, player_1);
           //display updated board           
           cur_board(board);
           s=complete(player_1, board);
       }
       //add 1 so next player can go
       player_1 +=1;
   }
  
   //while it is second player
   if (player_1==1)
       {
           printf("Player BLUE ");
           printf("Pick a position corresponding to the integer: ");
           scanf("%d", &position);

           while (position!=0 && position!=1 && position!=2 && position!=3 && position!=4 && position!=5)
               {
               printf(" Invalid input, you have to select a bowl from your side. Please select another position ");
               scanf("%d",&position);  
               }
           cur_position=move_counter(board, position, player_1);
           //printf("The current position is = %d ", cur_position);
           s=complete(player_1, board);
           cur_board(board);
           //printf("S = %d ", s);
       while(cur_position==6 && s==2)
           {
           printf("*************************** ");
           printf(" BLUE GOES AGAIN ");
           printf("*************************** ");
           printf("Player BLUE ");
           printf("Pick a position corresponding to the integer: ");
           scanf("%d", &position);

           while (position!=0 && position!=1 && position!=2 && position!=3 && position!=4 && position!=5)
               {
               printf(" Invalid input, you have to select a bowl from your side. Please select another position ");
               scanf("%d",&position);  
               }
               cur_position=move_counter(board, position, player_1);
               cur_board(board);
               s=complete(player_1, board);
           }
      
       player_1 -=1;
       }
   sum = board[6] + board[13];
}
printf("Final Board Outcome: ");
cur_board(board);
winner_one(board);
return 0;
}

//=====================================================================

int two_player(int board[])
{

board[0] = 4;
board[1] = 4;
board[2] = 4;
board[3] = 4;
board[4] = 4;
board[5] = 4;
board[6] = 0;
board[7] = 4;
board[8] = 4;
board[9] = 4;
board[10] = 4;
board[11] = 4;
board[12] = 4;
board[13] = 0;
int position, s, cur_position;
int sum;
int player_1;
player_1=first_player();
sum = board[6] + board[13];
cur_board(board);
while (sum != 48)
{
  
   if (player_1==0)
   {
       printf("Player RED ");
       printf("Pick a position corresponding to the integer: ");
       scanf("%d", &position);

       while (position!=6 && position!=7 && position!=8 && position!=9 && position!=10 && position!=11)
           {
           printf(" Invalid input, you have to select a bowl from your side. Please select another position ");
           scanf("%d",&position);  
           }
       position+=1;
       cur_position=move_counter(board, position, player_1);
       s=complete(player_1, board);
       cur_board(board);
       while(cur_position==13 && s==2)
       {
           printf(" *************************** ");
           printf(" RED GOES AGAIN ");
           printf("*************************** ");
           printf("Player RED ");
           printf("Pick a position corresponding to the integer: ");
           scanf("%d", &position);

           while (position!=6 && position!=7 && position!=8 && position!=9 && position!=10 && position!=11)
           {
           printf(" Invalid input, you have to select a bowl from your side. Please select another position ");
           scanf("%d",&position);  
           }
               position +=1;
               cur_position=move_counter(board, position, player_1);
               cur_board(board);
               s=complete(player_1, board);
       }
       player_1 +=1;
   }
  
   //while it is second player
   if (player_1==1)
       {
           printf("Player BLUE ");
           printf("Pick a position corresponding to the integer: ");
           scanf("%d", &position);

           while (position!=0 && position!=1 && position!=2 && position!=3 && position!=4 && position!=5)
               {
               printf(" Invalid input, you have to select a bowl from your side. Please select another position ");
               scanf("%d",&position);  
               }
           cur_position=move_counter(board, position, player_1);
           //printf("The current position is = %d ", cur_position);
           s=complete(player_1, board);
           cur_board(board);
           //printf("S = %d ", s);
       while(cur_position==6 && s==2)
           {
           printf("*************************** ");
           printf(" BLUE GOES AGAIN ");
           printf("*************************** ");
           printf("Player BLUE ");
           printf("Pick a position corresponding to the integer: ");
           scanf("%d", &position);

           while (position!=0 && position!=1 && position!=2 && position!=3 && position!=4 && position!=5)
               {
               printf(" Invalid input, you have to select a bowl from your side. Please select another position ");
               scanf("%d",&position);  
               }
               cur_position=move_counter(board, position, player_1);
               cur_board(board);
               s=complete(player_1, board);
           }
      
       player_1 -=1;
       }
   sum = board[6] + board[13];
}
printf("Final Board Outcome: ");
cur_board(board);
winner_two(board);
return 0;
}

//=============================================================

int winner_one(int board[])
{
if(board[13]>board[6])
   {
   printf("Computer is the winner!");
   printf("Too bad, better luck next time! ");
   }
if(board[13] < board[6])
   {
   printf("The winner: ");
   printf("*************************** ");
   printf("    BLUE ");
   printf("*************************** ");
   printf("Great game! :-D ");
   }
if(board[13]==board[6])
   {
   printf("Looks like you tied");
   printf("Try your luck and play again! ");
   }
return 0;
}

//==========================================================

int winner_two(int board[])
{

if(board[13]>board[6])
   {
   printf("The winner: ");
   printf("*************************** ");
   printf("    RED ");
   printf("*************************** ");
   printf("Great game! :-D ");
   }
if(board[13] < board[6])
   {
   printf("The winner: ");
   printf("*************************** ");
   printf("    BLUE ");
   printf("*************************** ");
   printf("Great game! :-D ");
   }
if(board[13]==board[6])
   {
   printf("Looks like you tied");
   printf("Try your luck and play again! ");
   }
return 0;
}

//=============================================================

int run(void)
{
int place;

place=rand()%12;
return (place);
}

//=============================================================

int first_player(void)
{  
   int play;
   //to determine who will be player 1
   play=rand()%2;  
   return (play);
}

//=============================================================

int move_counter(int board[], int position, int player_1)
{
int initPos = position;
int i,curPos;
for ( i = 1; i < board[position] + 1; i++)
{
int curPos = position + i;

if (curPos >= 14)
curPos -= 14;

else if (curPos >= 28)
curPos -= 28;

if (player_1 == 0)
{
int * current1 = &(board[curPos]);
if (current1 == &(board[6]))
  
current1 = &(board[7]);
*current1 += 1;
  
}

else if (player_1 == 1)
{
int * current2 = &(board[curPos]);
if (current2 == &(board[13]))
  
current2 = &(board[0]);
*current2 += 1;
  
}
}
  
  
for ( i = 1; i < board[position] + 1; i++)
{
curPos = position + i;
   if(curPos==13 && player_1==1)
       {
       curPos=0;
       }  
   if (curPos==6 && player_1==0)
       {
       curPos=7;
       }
   if (curPos==14 && player_1==0)
       {
       curPos=0;
       }
   }
   board[initPos] = 0;
   return(curPos);
}

//==========================================================================

int complete(int player_1, int board[])
{

if (player_1==0)
   {
       if ((board[12]==0) && (board[7]==0) && board[8]==0 && board[9]==0 && board[10]==0 && board[11]==0 )
           {
           board[13] = board[13]+board[0]+board[1]+board[2]+board[3]+board[4]+board[5];
           board[0]=0;
           board[1]=0;
           board[2]=0;
           board[3]=0;
           board[4]=0;
           board[5]=0;
           return (1);  
           }
   }
if(player_1==1)
   {
   if (board[0]==0 && board[1]==0 && board[2]==0 && board[3]==0 && board[4]==0 && board[5]==0 )
       {
       board[6] = board[6]+board[7]+board[8]+board[9]+board[10]+board[11]+board[12];
       board[7]=0;
       board[8]=0;
       board[9]=0;
       board[10]=0;
       board[11]=0;
       board[12]=0;
       return (1);  
       }
  
   }
return (2);

}

//==========================================================================

int cur_board(int board[])
{
   printf(" 5 4 3 2 1 0   ");
   printf("BLUE ");
   printf("=====[%d] [%d] [%d] [%d] [%d] [%d]----- ", board[5], board[4], board[3], board[2], board[1], board[0]);
   printf("|%d|           |%d| ", board[6], board[13]);
   printf("-----[%d] [%d] [%d] [%d] [%d] [%d]===== ", board[7], board[8], board[9], board[10], board[11], board[12]);
   printf("           RED ");
   printf(" 6 7 8 9 10 11 ");
   return 0;
}

Explanation / Answer

aha, i can help you out.

well it would take me sometime to understand the code and how you implemented the game. so here's a work around, may be helpful.

make a function to write to and read from file, all the variable(s) you are using during the game.

*********************************************************

write:

for read,

do something like this

although you should do it using structures, make a structure object referencing each variable in game.

then you just have to save it and read it

here's an example

saving it would be same as writing string to a file.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote