6.14 Tic Tac Toe Write a program that allows two players to play a game of tic-t
ID: 3591553 • Letter: 6
Question
6.14 Tic Tac Toe Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (). The program should run a loop that: 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 number and then the column number. Allows player 2 to select a location on the board for an O. The program should ask the user to enter the row number and then the column number. Validates the user's input: Do not let the user input a row outside the range [1,3] Do not let the user input a column outside the range [1,3] ·Do not let the user input into a cell that already has an 'X' or '0' If the user enters an invalid input, just ask them for that input again. . This is best done using nested do-while loops. Below is some pseudo-code that will help with this:Explanation / Answer
Solution:
/* game.h header file for a class that represents a TicTacToe board */
#include <iostream>
using namespace std;
const char human = 'X';
const char ai = 'O';
enum Player { HUMAN, AI };
struct Move {
int x;
int y;
};
class Game {
char board[3][3];
public:
Game();
void printBoard();
// Prints the board pretty-ly
bool gameOver();
// Returns true if a winner has been found or there are no empty spaces
bool checkWin(Player player);
// Checks for a win
void play();
// Primary game driver, loops through turn-by-turn until there's
// a winner or full game board (draw)
void getHumanMove();
// Takes in values from the input stream and places them on the board
// if valid. Expects input in coordinate notation, ex (1,3)
int score();
// Function to score game board states based on their outcome
// Returns 10 for human win, -10 for AI win, 0 for draw
Move minimax(char AIboard[3][3]);
// Returns the best AI move's x, y coords via the minimax algorithm
int minSearch(char AIboard[3][3]);
// minimax helper fn for finding the next move for AI player, chooses the
// move with the least possible score
int maxSearch(char AIboard[3][3]);
// minimax helper fn for finding the next move for human player, chooses
// the move with the least possible score
};
/* game.cpp implementation file for the TicTacToe class */
#include <iostream>
#include <sstream>
#include <iomanip>
#include "game.h"
using namespace std;
Game::Game() {
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
board[i][j] = '-';
}
}
}
void Game::printBoard() {
cout << "-------------------";
for(int i = 0; i < 3; i++) {
cout << ' ' << "|";
for(int j = 0; j < 3; j++) {
cout << setw(3) << board[i][j] << setw(3) << " |";
}
}
cout << ' ' << "-------------------" << ' ';
}
bool Game::gameOver() {
if(checkWin(HUMAN)) return true;
else if(checkWin(AI)) return true;
bool emptySpace = false;
for(int i = 0; i < 3; i++) {
if(board[i][0] == '-' || board[i][1] == '-' || board[i][2] == '-')
emptySpace = true;
}
return !emptySpace;
}
bool Game::checkWin(Player player) {
char playerChar;
if(player == HUMAN) playerChar = human;
else playerChar = ai;
for(int i = 0; i < 3; i++) {
// Check horizontals
if(board[i][0] == playerChar && board[i][1] == playerChar
&& board[i][2] == playerChar)
return true;
// Check verticals
if(board[0][i] == playerChar && board[1][i] == playerChar
&& board[2][i] == playerChar)
return true;
}
// Check diagonals
if (board[0][0] == playerChar && board[1][1] == playerChar
&& board[2][2] == playerChar) {
return true;
} else if (board[0][2] == playerChar && board[1][1] == playerChar
&& board[2][0] == playerChar) {
return true;
}
return false;
}
int Game::score() {
if(checkWin(HUMAN)) { return 10; }
else if(checkWin(AI)) { return -10; }
return 0; // draw
}
Move Game::minimax(char AIboard[3][3]) {
int bestMoveScore = 100; // -100 is arbitrary
Move bestMove;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(AIboard[i][j] == '-') {
AIboard[i][j] = ai;
int tempMoveScore = maxSearch(AIboard);
if(tempMoveScore <= bestMoveScore) {
bestMoveScore = tempMoveScore;
bestMove.x = i;
bestMove.y = j;
}
AIboard[i][j] = '-';
}
}
}
return bestMove;
}
int Game::maxSearch(char AIboard[3][3]) {
if(gameOver()) return score();
Move bestMove;
int bestMoveScore = -1000;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(AIboard[i][j] == '-') {
AIboard[i][j] = human;
int tempMoveScore = minSearch(AIboard);
if(tempMoveScore >= bestMoveScore) {
bestMoveScore = tempMoveScore;
bestMove.x = i;
bestMove.y = j;
}
AIboard[i][j] = '-';
}
}
}
return bestMoveScore;
}
int Game::minSearch(char AIboard[3][3]) {
if(gameOver()) return score();
Move bestMove;
int bestMoveScore = 1000;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(AIboard[i][j] == '-') {
AIboard[i][j] = ai;
int tempMove = maxSearch(AIboard);
if(tempMove <= bestMoveScore) {
bestMoveScore = tempMove;
bestMove.x = i;
bestMove.y = j;
}
AIboard[i][j] = '-';
}
}
}
return bestMoveScore;
}
void Game::getHumanMove() {
int x, y = -1; // arbitrary assignment to init loop
while(x < 0 || x > 2 || y < 0 || y > 2) {
// Loop until a valid move is entered
cout << "Enter your move in coordinate form, ex: (1,3)." << endl;
cout << "Your Move: ";
char c;
string restofline;
cin >> c >> c;
x = c - '0' - 1;
cin >> c >> c;
y = c - '0' - 1;
getline(cin, restofline); // get garbage chars after move
}
board[x][y] = human;
}
void Game::play() {
int turn = 0;
printBoard();
while(!checkWin(HUMAN) && !checkWin(AI) && !gameOver()) {
// human move
if(turn % 2 == 0) {
getHumanMove();
if(checkWin(HUMAN)) cout << "Human Player Wins" << endl;
turn++;
printBoard();
} else {
cout << endl << "Computer Player Move:" << endl;
Move AImove = minimax(board);
board[AImove.x][AImove.y] = ai;
if(checkWin(AI)) cout << "Computer Player Wins" << endl;
turn++;
printBoard();
}
}
}
/* play.cpp code that uses the TicTacToe class to play the game */
#include <iostream>
#include "game.h"
using namespace std;
int main() {
Game tictactoe;
tictactoe.play();
return 0;
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Solution:
in the for loop you need to declare the variable k or you can do it outside of the loop
just replace for(k= 0; k<= n; k++) with for(int k=0; k<=n; k++)
The problem will get resolved if this doesn't work please share the code of all these files and I will correct it and upload.
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.