In this project, your goal is to program the game TIC-TAC-TOE using what you hav
ID: 3884977 • Letter: I
Question
In this project, your goal is to program the game TIC-TAC-TOE using what you have used in C as well as what you have learned so far in C++. The game is not a hard one to play, but here is a refresher on the rules:
Rules: 1. There are two players per game
2. The game is played on a 3 by 3 grid.
3. The first player picks a position in the grid and places an X.
4. The second player picks a position in the grid not previously chosen and places an O.
5. The first player and second player go back and forth picking spots not previously chosen until one of two things happen: A winning situation where there are 3 Xs or 3 Os in a row, column, or diagonally. A Tie (or Cat) game where all spaces are taken but there is no winner.
Figure 1: The TIC-TAC-TOE grid. Columns are in letters, the rows are in numbers. Here you will do the following tasks:
1. Create a display function for the TIC-TAC-TOE game board that displays not only the 3 by 3 grid, but also the Xs, Os, and blank spaces. Use letters for the Columns, and numbers for the rows. The function needs to accept the board you created in main.
2. Ask the user for a Column and Row. Check to make sure that the information put in by the user is a possible playable position. For examples, the users can input a. A row or column outside the scope of the grid b. A character when asked for a number or a number when asked for a character (we won’t try strings until later). c. A position already taken by an X or an O Let the player know if they made an error in their input (out of scope, wrong input, position already taken). Place an X in the position chosen.
3. The program will select a random position in the grid. If the position has already been taken, the program will select a random position until the grid position chosen is available.
4. Tasks 2 and 3 repeat until either a winner is present OR all of the positions in the grid are taken. When this happens, the game is over and the program ends.
Recommendations:
1. Think about using a 2 dimensional character array of 3 by 3 size for the grid. Fill this character array with spaces (‘ ‘). This way you can check if the grid[row][column] == ‘ ‘ for availability. Display it using your display function, but this should make the game design a little easier. Every time the player puts in a position, grid[row][column] = ‘X’ or grid[row][column] = ‘O’ can be called.
2. Create functions checking if the grid is full and if there is a winner. You’ll be checking for this several times and it just makes it easier to write functions that return a boolean value. The winner checker can be done with If –else if – else statements. The function that checks to see if the grid is full can be done with nested for loops.
3. When reading in information from the user, read it in a character. This way, you can read in ‘A’, ‘B’, ‘C’, ‘1’, ‘2’, and ‘3’ and not worry about > or < comparisons. Take these characters and convert them to 0-2 integer values corresponding to column and rows in the 3 by 3 array grid.
Grading:
main.cpp and makefile
Program displays blank 3 by 3 grid in readable format.
Display is done through a function
Grid display shows labeled columns (Letters) and rows (Numbers).
Program asks user for Column and Row
Program will re-ask user for column and/or row if wrong information type is put in.
Program will re-ask for column and row if a position chosen is already taken
Program places an X at the location the user chooses.
Program randomly chooses location within the grid for O
Program verifies that location for O is not already taken. If the position is taken, program randomly chooses location until location is not already taken.
Program displays grid with updated playing field.
Program keeps running until winner condition is satisfied or when playing grid is full. Appendix: Comment Header
Explanation / Answer
// A C++ Program to play tic-tac-toe
#include<bits/stdc++.h>
using namespace std;
#define COMPUTER 1
#define HUMAN 2
#define SIDE 3 // Length of the board
// Computer will move with 'O'
// and human with 'X'
#define COMPUTERMOVE 'O'
#define HUMANMOVE 'X'
// A function to show the current board status
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;
}
// A function to show the instructions
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;
}
// A function to initialise the game
void initialise(char board[][SIDE], int moves[])
{
// Initiate the random number generator so that
// the same configuration doesn't arises
srand(time(NULL));
// Initially the board is empty
for (int i=0; i<SIDE; i++)
{
for (int j=0; j<SIDE; j++)
board[i][j] = ' ';
}
// Fill the moves with numbers
for (int i=0; i<SIDE*SIDE; i++)
moves[i] = i;
// randomise the moves
random_shuffle(moves, moves + SIDE*SIDE);
return;
}
// A function to declare the winner of the game
void declareWinner(int whoseTurn)
{
if (whoseTurn == COMPUTER)
printf("COMPUTER has won ");
else
printf("HUMAN has won ");
return;
}
// A function that returns true if any of the row
// is crossed with the same player's move
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);
}
// A function that returns true if any of the column
// is crossed with the same player's move
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);
}
// A function that returns true if any of the diagonal
// is crossed with the same player's move
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);
}
// A function that returns true if the game is over
// else it returns a false
bool gameOver(char board[][SIDE])
{
return(rowCrossed(board) || columnCrossed(board)
|| diagonalCrossed(board) );
}
// A function to play Tic-Tac-Toe
void playTicTacToe(int whoseTurn)
{
// A 3*3 Tic-Tac-Toe board for playing
char board[SIDE][SIDE];
int moves[SIDE*SIDE];
// Initialise the game
initialise(board, moves);
// Show the instructions before playing
showInstructions();
int moveIndex = 0, x, y;
// Keep playing till the game is over or it is a draw
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 the game has drawn
if (gameOver(board) == false &&
moveIndex == SIDE * SIDE)
printf("It's a draw ");
else
{
// Toggling the user to declare the actual
// winner
if (whoseTurn == COMPUTER)
whoseTurn = HUMAN;
else if (whoseTurn == HUMAN)
whoseTurn = COMPUTER;
// Declare the winner
declareWinner(whoseTurn);
}
return;
}
// Driver program
int main()
{
// Let us play the game with COMPUTER starting first
playTicTacToe(COMPUTER);
return (0);
}
// A C++ Program to play tic-tac-toe
#include<bits/stdc++.h>
using namespace std;
#define COMPUTER 1
#define HUMAN 2
#define SIDE 3 // Length of the board
// Computer will move with 'O'
// and human with 'X'
#define COMPUTERMOVE 'O'
#define HUMANMOVE 'X'
// A function to show the current board status
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;
}
// A function to show the instructions
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;
}
// A function to initialise the game
void initialise(char board[][SIDE], int moves[])
{
// Initiate the random number generator so that
// the same configuration doesn't arises
srand(time(NULL));
// Initially the board is empty
for (int i=0; i<SIDE; i++)
{
for (int j=0; j<SIDE; j++)
board[i][j] = ' ';
}
// Fill the moves with numbers
for (int i=0; i<SIDE*SIDE; i++)
moves[i] = i;
// randomise the moves
random_shuffle(moves, moves + SIDE*SIDE);
return;
}
// A function to declare the winner of the game
void declareWinner(int whoseTurn)
{
if (whoseTurn == COMPUTER)
printf("COMPUTER has won ");
else
printf("HUMAN has won ");
return;
}
// A function that returns true if any of the row
// is crossed with the same player's move
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);
}
// A function that returns true if any of the column
// is crossed with the same player's move
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);
}
// A function that returns true if any of the diagonal
// is crossed with the same player's move
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);
}
// A function that returns true if the game is over
// else it returns a false
bool gameOver(char board[][SIDE])
{
return(rowCrossed(board) || columnCrossed(board)
|| diagonalCrossed(board) );
}
// A function to play Tic-Tac-Toe
void playTicTacToe(int whoseTurn)
{
// A 3*3 Tic-Tac-Toe board for playing
char board[SIDE][SIDE];
int moves[SIDE*SIDE];
// Initialise the game
initialise(board, moves);
// Show the instructions before playing
showInstructions();
int moveIndex = 0, x, y;
// Keep playing till the game is over or it is a draw
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 the game has drawn
if (gameOver(board) == false &&
moveIndex == SIDE * SIDE)
printf("It's a draw ");
else
{
// Toggling the user to declare the actual
// winner
if (whoseTurn == COMPUTER)
whoseTurn = HUMAN;
else if (whoseTurn == HUMAN)
whoseTurn = COMPUTER;
// Declare the winner
declareWinner(whoseTurn);
}
return;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.