Problem Statement: ------------------ We want to write an application to play Ti
ID: 670897 • Letter: P
Question
Problem Statement: ------------------ We want to write an application to play Tic-Tac-Toe (Naughts and Crosses, Tic-Tac-Toe, X's and O's). Only provide the design to implement the game. Design considerations: ---------------------- The most common board size in two-dimensional Tic-Tac-Toe is 3x3, meaning a square grid with three rows and three columns. Board sizes larger than 3x3 are also possible: 4x4, 5x5, etc. Board sizes of 1x1 and 2x2 are always won by the first player. Design your game to work properly for any board size starting with 1x1. Make the board size a declared constant in your program. You will be playing against computer. Computer will decide it's moves based on empty locations. You can have three difficluty levels of game: 1. easy, 2. medium, 3. hard - For easy level computer generates random numbers. If a spot is already taken then another number is generated. - For medium level computer checks if it can win in next move. - For hard level computer always tries to pick spots to win the game. Random number generation is not needed for this. You will need some predfined moves based on user moves. - User selects the level of difficulty. - User goes first in first game then it alternates for any subsequent games. Sample output: -------------- Welcome to Tic-Tac-Toe! 1 2 3 +-+-+-+ 1| | | | +-+-+-+ 2| | | | +-+-+-+ 3| | | | +-+-+-+ What is your move? Please type a row number from 1 to 3 and a column number from 1 to 3. 1 1 1 2 3 +-+-+-+ 1|X| | | +-+-+-+ 2| | | | +-+-+-+ 3| | | | +-+-+-+ My move is row 1 column 3. 1 2 3 +-+-+-+ 1|X| |O| +-+-+-+ 2| | | | +-+-+-+ 3| | | | +-+-+-+ What is your move? Please type a row number from 1 to 3 and a column number from 1 to 3. 3 3 1 2 3 +-+-+-+ 1|X| |O| +-+-+-+ 2| | | | +-+-+-+ 3| | |X| +-+-+-+ My move is row 3 column 2. 1 2 3 +-+-+-+ 1|X| |O| +-+-+-+ 2| | | | +-+-+-+ 3| |O|X| +-+-+-+ What is your move? Please type a row number from 1 to 3 and a column number from 1 to 3. 2 2 1 2 3 +-+-+-+ 1|X| |O| +-+-+-+ 2| |X| | +-+-+-+ 3| |O|X| +-+-+-+ Congratulations! You win! Thank you for playing Tic-Tac-Toe.
Explanation / Answer
working c++ code
#include <iostream>
#include <string>
#define GRID_SIZE 9
using namespace std;
void boardprint(char game[]);
void inputToSlot(char game[], int slot, char choice);
void initialiseGame(char game[]);
bool fullcheck(char game[]);
bool emptycheck(char input);
bool checkWon(char game[]);
bool rowwinner(char game[]);
bool columnwinner(char game[]);
bool diagonalwinner(char game[]);
int main()
{
char game[GRID_SIZE];
initialiseGame(game);
int currentTurn = 0;
boardprint(game);
cout << endl;
while (!fullcheck(game)) {
int slotChoice;
currentTurn += 1;
bool isP2 (currentTurn % 2 == 0);
// Prompt
if (isP2) {
cout << "my move is ..." << endl ;
} else {
cout << "what is your move..." << endl;
}
cin >> slotChoice;
while (!emptycheck(game[slotChoice])) {
cout << "Slot occupied. Please select another slot > ";
cin >> slotChoice;
}
// Insert
if (isP2) {
game[slotChoice] = 'o';
} else {
game[slotChoice] = 'x';
}
// Print
boardprint(game);
cout << endl;
// Check Winner
if (checkWon(game)) {
if (isP2) {
cout << "I won the game.";
} else {
cout << "You won the game.";
}
break;
}
}
if (!checkWon(game)) {
cout << "Draw.";
}
}
void initialiseGame(char game[]) {
for (int i = 0; i < GRID_SIZE; ++i) {
game[i] = ' ';
}
}
void boardprint(char game[]) {
cout << " 1 2 3 " << endl;
cout << "+---+---+---+" << endl;
for (int i = 0; i < GRID_SIZE; ++i) {
cout << "| " << game[i] << " ";
if ((i+1) % 3 == 0 && i != 0) {
cout << "|" << endl;
cout << "+---+---+---+" << endl;
}
}
}
void inputToSlot(char game[], int slot, char choice) {
game[slot] = choice;
}
bool fullcheck(char game[]) {
for (int i = 0; i < GRID_SIZE; ++i) {
if (game[i] == ' ') {
return false;
}
}
return true;
}
bool emptycheck(char input) {
if (input == ' '){
return true;
}
return false;
}
bool checkWon(char game[]) {
if (rowwinner(game)) {
return true;
} else if (columnwinner(game)) {
return true;
} else if (diagonalwinner(game)) {
return true;
}
return false;
}
bool rowwinner(char game[]) {
for (int i = 0; i < GRID_SIZE; i += 3) {
char firstInRow = game[i];
char secondInRow = game[i + 1];
char thirdInRow = game[i + 2];
if (!emptycheck(firstInRow) && !emptycheck(secondInRow) && !emptycheck(thirdInRow)) {
if (firstInRow == secondInRow && firstInRow == thirdInRow) {
return true;
}
}
}
return false;
}
bool columnwinner(char game[]) {
for (int i = 0; i < 3; ++i) {
char firstInColumn = game[i];
char secondInColumn = game[i + 3];
char thirdInColumn = game[i + 6];
if (!emptycheck(firstInColumn) && !emptycheck(secondInColumn) && !emptycheck(thirdInColumn)) {
if (firstInColumn == secondInColumn && firstInColumn == thirdInColumn) {
return true;
}
}
}
return false;
}
bool diagonalwinner(char game[]) {
char center = game[4];
if (!emptycheck(center) && !emptycheck(game[0]) && !emptycheck(game[8])) {
if (center == game[0] && center == game[8]) {
return true;
}
} else if (!emptycheck(center) && !emptycheck(game[2]) && !emptycheck(game[6])) {
if (center == game[2] && center == game[6]) {
return true;
}
}
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.