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

gram is in RAPTOR RM Design a program that allows two players to play a game of

ID: 3607150 • Letter: G

Question

gram is in RAPTOR RM Design a program that allows two players to play a game of tic-tac-toe. One approach would use a two- dimensional String array with three rows and three columns as the game board. Or, you might use a one dimensional array with 9 elements. Each element of the array should be initialized with a character or string like r". Another way to initialize the array would be with numeric characters like "1", "2", "3", etc. This would display the board so the user can choose the number corresponding to the location where they want to place their "X" or "O". The single quotes are used for single characters, the double quotes are used for strings. In this program, it doesn't matter whether you use characters or strings, but you must stay consistent through the entire program or it will not work. The program should run a loop that does the following: Displays the contents of the board array Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and column number (Or enter the cell number if using a 1D array) Allows player 2 to select a location on the board for an O. The program should ask the user to enter the row and column number (Or enter the cell number if using a ID array) Determines whether a player has won or if a tie has occurred. If a player has won, the program should declare that player the winner and end. If a tie has occurred, the program should say so and end. Player 1 wins when there are three Xs in a row on the game board. Player 2 wins when there are three Os in a row on the game board. The winning Xs or Ox can appear in a row, in a column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is no winner. To check for a winner, the easiest approach is to create a series of 8 selection (if) statements that checks for each possible winning combination. Unfortunately, there is not easy approach to use a loop or shorter method for checking for a winner.

Explanation / Answer

#include<bits/stdc++.h>

using namespace std;

#define COMPUTER 1

#define HUMAN 2

#define SIDE 3

#define COMPUTERMOVE 'O'

#define HUMANMOVE 'X'

void showBoard(char board[][SIDE])

{

printf(" ");

printf(" %c | %c | %c ", board[0][0],

board[0][1], board[0][2]);

printf(" -------------- ");

printf(" %c | %c | %c ", board[1][0],

board[1][1], board[1][2]);

printf(" -------------- ");

printf(" %c | %c | %c ", board[2][0],

board[2][1], board[2][2]);

return;

}

void showInstructions()

{

printf(" Tic-Tac-Toe ");

printf("Choose a cell numbered from 1 to 9 as below"

" and play ");

printf(" 1 | 2 | 3 ");

printf(" -------------- ");

printf(" 4 | 5 | 6 ");

printf(" -------------- ");

printf(" 7 | 8 | 9 ");

printf("- - - - - - - - - - ");

return;

}

void initialise(char board[][SIDE], int moves[])

{

srand(time(NULL));

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

{

for (int j=0; j<SIDE; j++)

board[i][j] = ' ';

}

for (int i=0; i<SIDE*SIDE; i++)

moves[i] = i;

random_shuffle(moves, moves + SIDE*SIDE);

return;

}

void declareWinner(int whoseTurn)

{

if (whoseTurn == COMPUTER)

printf("COMPUTER has won ");

else

printf("HUMAN has won ");

return;

}

bool rowCrossed(char board[][SIDE])

{

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

{

if (board[i][0] == board[i][1] &&

board[i][1] == board[i][2] &&

board[i][0] != ' ')

return (true);

}

return(false);

}

bool columnCrossed(char board[][SIDE])

{

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

{

if (board[0][i] == board[1][i] &&

board[1][i] == board[2][i] &&

board[0][i] != ' ')

return (true);

}

return(false);

}

bool diagonalCrossed(char board[][SIDE])

{

if (board[0][0] == board[1][1] &&

board[1][1] == board[2][2] &&

board[0][0] != ' ')

return(true);

if (board[0][2] == board[1][1] &&

board[1][1] == board[2][0] &&

board[0][2] != ' ')

return(true);

return(false);

}

bool gameOver(char board[][SIDE])

{

return(rowCrossed(board) || columnCrossed(board)

|| diagonalCrossed(board) );

}

void playTicTacToe(int whoseTurn)

{

char board[SIDE][SIDE];

int moves[SIDE*SIDE];

initialise(board, moves);

showInstructions();

int moveIndex = 0, x, y;

while (gameOver(board) == false &&

moveIndex != SIDE*SIDE)

{

if (whoseTurn == COMPUTER)

{

x = moves[moveIndex] / SIDE;

y = moves[moveIndex] % SIDE;

board[x][y] = COMPUTERMOVE;

printf("COMPUTER has put a %c in cell %d ",

COMPUTERMOVE, moves[moveIndex]+1);

showBoard(board);

moveIndex ++;

whoseTurn = HUMAN;

}

else if (whoseTurn == HUMAN)

{

x = moves[moveIndex] / SIDE;

y = moves[moveIndex] % SIDE;

board[x][y] = HUMANMOVE;

printf ("HUMAN has put a %c in cell %d ",

HUMANMOVE, moves[moveIndex]+1);

showBoard(board);

moveIndex ++;

whoseTurn = COMPUTER;

}

}

if (gameOver(board) == false &&

moveIndex == SIDE * SIDE)

printf("It's a draw ");

else

{

if (whoseTurn == COMPUTER)

whoseTurn = HUMAN;

else if (whoseTurn == HUMAN)

whoseTurn = COMPUTER;

declareWinner(whoseTurn);

}

return;

}

int main()

{

playTicTacToe(COMPUTER);

return (0);

}