im trying to get my code to display as a 3×3 grid of tic tac toe games but it will only display in a single column i know there will be other issues with this code but only working on getting it to display properly atm thanks.
here is the code i have
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#include "TicTacToe.h"
int main() {
NBTicTacToe game;
game.displayBoards();
return 0;
}
this is the main function and most of the code for this is at the bottom under NBTicTacToe
#ifndef TICTACTOE_H_
#define TICTACTOE_H_
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int BOARDSIZE = 3;
class TicTacToe {
private:
int board[BOARDSIZE][BOARDSIZE];
int noOfMoves;
public:
TicTacToe();
bool isValidMove(int, int);
bool getXMove(int&, int&);
bool getOMove(int&, int&);
void addMove(int, int, int);
int gameStatus();
int play();
void displayBoard();
};
class NBTicTacToe {
private:
TicTacToe boards[BOARDSIZE][BOARDSIZE];
int currentBoardX;
int currentBoardY;
public:
NBTicTacToe();
void displayBoards();
TicTacToe& getCurrentBoard();
void setCurrentBoard(int, int);
void playCurrentBoard();
};
TicTacToe::TicTacToe() {
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
board[row][col] = 0;
noOfMoves = 0;
}
void TicTacToe::displayBoard() {
cout << " 1 2 3" << endl << endl;
for (int i = 0; i < BOARDSIZE; i++) {
cout << i + 1;
for (int j = 0; j < BOARDSIZE; j++) {
char playerSymbol = ' ';
if (board[i][j] == 1)
playerSymbol = 'X';
else if (board[i][j] == -1)
playerSymbol = 'O';
cout << setw(3) << playerSymbol;
if (j != BOARDSIZE -1)
cout << " |";
}
cout << endl;
if (i != BOARDSIZE -1)
cout << " ____|____|____" << endl << " | | " << endl;
}
cout << endl;
}
bool TicTacToe::isValidMove(int x, int y) {
if (board[x][y] == 1 || board[x][y] == -1){
cout << "Please enter a valid move: n";
}
else {
return true;
}
}
bool TicTacToe::getXMove(int &x, int &y) {
if (noOfMoves >= BOARDSIZE * BOARDSIZE)
return false;
int row, col;
do {
row = rand() % BOARDSIZE;
col = rand() % BOARDSIZE;
} while (!isValidMove(row, col));
x = row;
y = col;
return true;
}
bool TicTacToe::getOMove(int &x, int &y) {
if (noOfMoves >= BOARDSIZE * BOARDSIZE)
return false;
int row, col;
do {
cin >> row >> col;
cout << endl;
} while (!isValidMove(row - 1, col - 1));
x = row - 1;
y = col - 1;
return true;
}
void TicTacToe::addMove(int x, int y, int player) {
board[x][y] = player;
}
int TicTacToe::gameStatus() {
//Check rows for a win
for (int i = 0; i < 3; i ++){
if (board[i][0] == 1 && board[i][1] == 1 && board[i][2] == 1) {
return 1;
}else if (board[i][0] == -1 && board[i][1] == -1 && board[i][2] == -1){
return -1;
}
}
//Check columns for a win
for (int i = 0; i < 3; i ++){
if (board[0][i] == 1 && board[1][i] == 1 && board[2][i] == 1) {
return 1;
}else if (board[0][i] == -1 && board[1][i] == -1 && board[2][i] == -1){
return -1;
}
}
//Check diagonals for a win
if (board[0][0] == 1 && board[1][1] == 1 && board[2][2] == 1) {
return 1;
}else if (board[0][2] == 1 && board[1][1] == 1 && board[2][0] == 1){
return 1;
}else if (board[0][0] == -1 && board[1][1] == -1 && board[2][2] == -1) {
return -1;
}else if (board[0][2] == -1 && board[1][1] == -1 && board[2][0] == -1){
return -1;
}
//check for Draw
bool draw = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == 0) {
draw = false;
break;
}
}
}
if (draw)
return 2;
return 0;
}
int TicTacToe::play() {
int player = 1;
displayBoard();
int done = 0;
while (done == 0) {
int x, y;
if (player == 1) {
cout << "player X is moving: n";
getXMove(x, y);
} else {
cout << "please enter your move: ";
getOMove(x, y);
}
addMove(x, y, player);
noOfMoves++;
displayBoard();
done = gameStatus();
if (done == 1) {
cout << "Player X wins!" << endl;
return 1;
} else if (done == -1) {
cout << "Player O wins!" << endl;
return -1;
} else if (done == 2) {
cout << "Draw game!" << endl;
return 0;
}
if (player == 1)
player = -1;
else
player = 1;
}
return 0;
}
NBTicTacToe::NBTicTacToe() : currentBoardX(0), currentBoardY(0) {
for(int i = 0; i < BOARDSIZE; i++) {
for(int j = 0; j < BOARDSIZE; j++) {
boards[i][j] = TicTacToe();
}
}
}
void NBTicTacToe::displayBoards() {
for (int r =0; r < BOARDSIZE; r++){// display rows
for (int c =0; c < BOARDSIZE; c++){
boards[r][c].displayBoard();
if (c <BOARDSIZE) {
cout << " - - - - - - - - n" ;
}
}
if (r < BOARDSIZE){
for (int c = 0; c < BOARDSIZE; c++){
if (c < BOARDSIZE) {
cout << " || ";
}
}
cout << endl;
}
}
}
TicTacToe& NBTicTacToe::getCurrentBoard() {
return boards[currentBoardX][currentBoardY];
}
void NBTicTacToe::setCurrentBoard(int x, int y) {
currentBoardX = x;
currentBoardY = y;
}
void NBTicTacToe::playCurrentBoard() {
getCurrentBoard().play();
}
#endif
should be getting something close to this:
ultimate tic tac toe
4
A way that I would suggest is to work on strings.
Make the class TicTacToe
have a function ‘to_strings’ which returns std::array
or std::vector
of std::string
s.
Then in the the display_board
function, you can get the first 3 TicTacToe boards and then display the first lines ended with an enter.
Then the next 3 boards, and so on.
for example:
void display_boards()
{
for (auto& boards_row : boards)
{
auto board1 = boards_row[0].to_strings();
auto board2 = boards_row[1].to_strings();
auto board3 = boards_row[2].to_strings();
for (int line_idx = 0; line_idx < board1.size(); ++line_idx)
{
std::cout << board1[line_idx] << board2[line_idx] << board3[line_idx] << 'n';
}
}
}