Extend your basic tic-tac-toe game for the console. The game should now work as
ID: 3680432 • Letter: E
Question
Extend your basic tic-tac-toe game for the console. The game should now work as follows: Ask if the player would like to resume a saved game. If yes, load game state from a text or binary file (your choice of file format). 1. For a new game, ask how many players are playing. 2. For a new game, ask how large the board should be. 3. For a new game, ask what the win sequence count should be (i.e. 3 in a row). 4. For a new game, check to ensure that winning is possible given the criteria specified, and display an error if not. 5. Output a board in the following general format: 1 2 3 1 | | ---+---+--- 2 | | ---+---+--- 3 | | However, you will need to display the correct number of rows/columns based on user input. 6. Ask the user for a row and column number, or if they would like to save and exit. If they choose save/exit, do so, otherwise continue. 7. Put the appropriate symbol for that player (X or O) in the specified spot. Players should be given the following symbols, in order: XOABCDEFGHIJKLMNPQRSTUVWYZ 8. Repeat for the next player until someone has won the game. 9. If someone has won, announce which player won and exit. If the game has tied, announce that and exit. The conditions for winning this new tic-tac-toe game are now as follows: * n in a row/column/diagnoal, where n is the win sequence count
Explanation / Answer
main.cpp
#include <iostream>
#include "TicTacToe.h"
using namespace std;
int main(/*int argc, char* argv[]*/)
{
TicTacToe Game;
int row, col;
char answer;
while (1) //Game Loop
{
while (Game.isWon() == TicTacToe::IN_PROGRESS)
{
system("cls");
Game.print();
cout << "Player " << Game.getPlayer() << ": Enter your move: row,column" << endl;
cin >> row;
cin.ignore(1);
cin >> col;
while (!Game.move(row - 1, col - 1))
{
cout << "Invalid Move" << endl;
cout << "Player " << Game.getPlayer() << ": Enter your move: row,column" << endl;
cin >> row;
cin.ignore(1);
cin >> col;
}
}
system("cls");
Game.print();
if (Game.isWon() == TicTacToe::DRAW)
cout << "The Game Was Drawn";
else if (Game.isWon() == TicTacToe::PLAYER1)
cout << "Player 1 Won!";
else if (Game.isWon() == TicTacToe::PLAYER2)
cout << "Player 2 Won!";
cout << endl << endl;
cout << "Would you like to play again ? (Y/N) > ";
cin >> answer;
if (answer == 'N' || answer == 'n')
break;
else if (answer == 'Y' || answer == 'y')
Game.restart();
}
return 0;
}
TicTacToe.h
#ifndef TICTACTOE_H
#define TICTACTOE_H
//#include <string>
class TicTacToe
{
private:
char board[3][3];
int currentPlayer;
public:
TicTacToe();
~TicTacToe();
int getPlayer() { return currentPlayer; }
enum game_state{IN_PROGRESS, PLAYER1, PLAYER2, DRAW };
void restart();
bool move(int, int);
void print();
int isWon();
};
#endif
TicTacToe.cpp
#include <iostream>
#include "TicTacToe.h"
using namespace std;
TicTacToe::TicTacToe()
{
//Initialize the board, set all squares blank, set player to player 1
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = ' ';
}
}
currentPlayer = PLAYER1;;
}
TicTacToe::~TicTacToe()
{
}
void TicTacToe::restart()
{
//clear the board: set all squares blank, set player to player 1
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = ' ';
}
}
currentPlayer = PLAYER1;;
}
bool TicTacToe::move(int row, int col)
{
if (row > 2 || row < 0 || col > 2 || col < 0) // move must be within the 3x3 board
{
return false; // Invalid move
}
else if (board[row][col] != ' ' ) // block must be empty
{
return false; // Invalid move
}
else
{
if (currentPlayer == PLAYER1)
{
board[row][col] = 'X';
currentPlayer = PLAYER2;
}
else
{
board[row][col] = 'O';
currentPlayer = PLAYER1;
}
return true;
}
}
void TicTacToe::print()
{
cout << " | 1 | 2 | 3 |" << endl;
cout << "1| " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << " | " << endl;
cout << "2| " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " | " << endl;
cout << "3| " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " | " << endl << endl;
}
int TicTacToe::isWon()
{
int player1 = 0, player2 = 0, empty = 0;
//check rows
for (int i = 0; i < 3; i++)
{
player1 = player2 = 0;
for (int j = 0; j < 3; j++)
{
if (board[i][j] == 'X')
++player1;
else if (board[i][j] == 'O')
++player2;
else
++empty;
}
if (player1 == 3)
return PLAYER1;
else if (player2 == 3)
return PLAYER2;
}
//check columns
for (int i = 0; i < 3; i++)
{
player1 = player2 = 0;
for (int j = 0; j < 3; j++)
{
if (board[j][i] == 'X')
++player1;
else if (board[j][i] == 'O')
++player2;
}
if (player1 == 3)
return PLAYER1;
else if (player2 == 3)
return PLAYER2;
}
//check diagonal 1
player1 = player2 = 0;
for (int i = 0; i < 3; i++)
{
if (board[i][i] == 'X')
++player1;
else if (board[i][i] == 'O')
++player2;
}
if (player1 == 3)
return PLAYER1;
else if (player2 == 3)
return PLAYER2;
//check diagonal 2
player1 = player2 = 0;
for (int i = 0; i < 3; i++)
{
if (board[2-i][i] == 'X')
++player1;
else if (board[2-i][i] == 'O')
++player2;
}
if (player1 == 3)
return PLAYER1;
else if (player2 == 3)
return PLAYER2;
if (empty == 0)
return DRAW;
return IN_PROGRESS;
}
output
| 1 | 2 | 3 |
1| | | |
2| | | |
3| | | |
Player 1: Enter your move: row,column
1 1
sh: cls: command not found
| 1 | 2 | 3 |
1| X | | |
2| | | |
3| | | |
Player 2: Enter your move: row,column
2 3
| 1 | 2 | 3 |
1| X | O | |
2| | | O |
3| X | O | X |
Player 1: Enter your move: row,column
1 3
| 1 | 2 | 3 |
1| X | O | X |
2| | | O |
3| X | O | X |
Player 2: Enter your move: row,column
2 1
1| X | O | X |
2| O | | O |
3| X | O | X |
Player 1: Enter your move: row,column
2 2
sh: cls: command not found
| 1 | 2 | 3 |
1| X | O | X |
2| O | X | O |
3| X | O | X |
Player 1 Won!
Would you like to play again ? (Y/N) > n
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.