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

help me please :( Projec Tic-Tac-Toe Due date: December 22. 2016 Write a C++ pro

ID: 3582669 • Letter: H

Question

help me please :(

Projec Tic-Tac-Toe Due date: December 22. 2016 Write a C++ program that plays the tic-tac-toe game. It starts by displaying the board and numbering the cells from 1 to 9as shown below: 4 5 6 7 I 8 I 9 I Enter you selection 5 The user selection should be a number between l and 9. Your program should check and mark the user selection using the X character. The program should then make its move by choosing one of the empty spots and marking it with the Ocharacter. After the first move, the board should be displayed as shown below, assuming that the program has randomly selected 3 as its move. 3 ia lection Enter your congratulations

Explanation / Answer

Part 1 :

display method : 1D array is used , using loops to print

void display(char board[])
{
   int i,j,k;
   for(i=0;i<10;i++)
       std::cout<<"-";
   std::cout<<endl;
  
   for(i=0;i<3;i++)
   {
       std::cout<<"|";
       for(j=0;j<3;j++)
       {
           std::cout<<board[3*i+j];
           std::cout<<"|";
       }
       //std::cout<<"|";
       std::cout<<endl;
   }
   for(i=0;i<10;i++)
       std::cout<<"-";
   std::cout<<endl;
  
}

-------------------------------------

Part 2:

function validate ():

a move will be invalid if :

i) number entered does not lie between 1 to 9 ( simply use if condition to check for this)

ii) number has already been entered or in other words X or O exist in its place ( use if condition to check like if(board[i] != ''X'") then that "i" entered is validate!

int validate(char board[], int value)
{
/* returning 0 indicates input is invalid
returning 1 indicates that it is valid */
  
if (value>9 || value<1)
return 0;
  
if (board[value] == 'X' || board[value] == 'O' )
return 0;
  
//if the input value does not satisfy the above 2 conditions , then it is valid
return 1;
}

-------------------------------------------------------------------

Part 3:

check_win() : in a tic tac toe game , a player wins if a row , column, diagonal has all 3 X''s or all 3
O''s .

we use if conditions one by one on all possible rows and columns and diagonals to check for all 3 X or all 3 Y

void check_win(char board[])
{

if (board[0] == 'X' && board[1] == 'X' && board[2] == 'X')
{
printf("Player X has WON!");
}
else if (board[3]== 'X' && board[4] == 'X' && board[5]=='X')
{
printf("Player X has WON!");
}
else if (board[6]== 'X' && board[7]== 'X' && gameboard[8]=='X')
{
printf("Player X has WON!");
}
else if (board[0] == 'X' && board[3] == 'X' && board[6]=='X')
{
printf("Player X has WON!");
}
else if (board[1] == 'X' && board[4] == 'X' && board[7]=='X')
{
printf("Player X has WON!");
}
else if (board[2] == 'X' && board[5] == 'X' && board[8]=='X')
{
printf("Player X has WON!");
}
else if (board[0] == 'X' && board[4] == 'X' && board[8]=='X')
{
printf("Player X has WON!");
}
else if (board[2] == 'X' && board[4] == 'X' && gameboard[6]=='X')
{
printf("Player X has WON!");
}


else if (board[0] == 'O' && board[1] == 'O' && board[2]=='O')
{
printf("Player O has WON!");
}
else if (board[3] == 'O' && board[4] == 'O' && board[5]=='O')
{
printf("Player O has WON!");
}
else if (board[6] == 'O' && board[7] == 'O' && board[8]=='O')
{
printf("Player O has WON!");
}
else if (board[0] == 'O' && board[3] == 'O' && board[6]=='O')
{
printf("Player O has WON!");
}
else if (board[1] == 'O' && board[4] == 'O' && board[7]=='O')
{
printf("Player O has WON!");
}
else if (board[2] == 'O' && board[5] == 'O' && board[8]=='O')
{
printf("Player O has WON!");
}
else if (board[0] == 'O' && board[4] == 'O' && board[8]=='O')
{
printf("Player O has WON!");
}
else if (board[2] == 'O' && board[4] == 'O' && board[6]=='O')
{
printf("Player O has WON!");
}
else
{
printf("THE GAME HAS COME TO A DRAW.");
}
}

------------------------------------------------------

part 4 :

full_board()

a full board will ocuur when all slots from 1 to 9 will either have an X or O insted of numbers.

to check this

step 1 : first intialize flag to 1

step 2 : loop through the board array

step 3 : if any i'th element of the array has a number in its value i.e if board[i] = {1..9} then that means that the board is not full yet and hence we set the flag to 0

step 4 : after looping we have

if flag == 0 return 1 to indicate board is not full

if flag ==1 return 0 to indiacate board is full and check for win then

int full_board( char board[])

{

int flag = 1;

for(int i = 0; i<9 ;i++)

{

if(board[i]!= 'X' || board[i]!='O')

{

flag =1;

break;

}

}

if(flag==1)

return 0; // indicates that board is full

else

reutrn 1 ; //indicates that board is not full

}

---------------------------------------------------------------------

i have solved the first 4 parts of this, please try the other 2 parts similary

-----------------------------------------------------------------------

part 5:

to randomly generate you should rand() function in C++

to 'intelligently select ' check if any two slots of a row or column or diagonal has 'O' then immediately select the number which will complete the row etc.

-----------------------------------------------------------

part 6:

to save the game trace to a file, simply save the iterator with its value.

i.e for board[i]= i save like i'th element has a value board[i]

this way we can always trace back our game array and hence are game!

thank you

------------------------------------------------------------------