Question
#include #include using namespace std; #ifndef TICTACTOE_H #define TICTACTOE_H class TicTacToe { public: TicTacToe(); // Constructor TicTacToe(const TicTacToe& t); TicTacToe& operator=(const TicTacToe& rhs); void displayBoard(ostream&
#include
#ifndef TICTACTOE_H #define TICTACTOE_H
class TicTacToe { public: TicTacToe(); // Constructor
TicTacToe(const TicTacToe& t);
TicTacToe& operator=(const TicTacToe& rhs);
void displayBoard(ostream& outs);
bool placePiece(char piece, int row, int col);
bool winner(char& piece);
void resetBoard();
~TicTacToe();
private: char **board;
static const int SIZE = 3; static const char SPACE = " "; static const char VERTICAL = " | "; static const char DASH = "-"; static const int COL_WIDTH = 3; };
#endif
#include "tictactoe.h"
TicTacToe::TicTacToe(int s) { if (s % 2 == 0) s++; if (s < 3 || s > 9) s = 3; size = s; board = new char*[size];
for (int i = 0; i < size; i++) board[i] = new char[size];
for (int r = 0; r < size; r++) for (int c = 0; c < size; c++) board[r][c] = SPACE; }
TicTacToe::TicTacToe(const TicTacToe& t) { size = t.size; board = new char*[size];
for (int i = 0; i < size; i++) board[i] = new char[size];
for (int r = 0; r < size; r++) for(int c = 0; c < size; c++) board[r][c] = t.board[r][c]; }
TicTacToe& operator=(const TicTacToe& rhs) { if(this != &rhs){ for(int i = 0; i < size; i++) delete [] board [i]; delete [] board; board = new char*[size];
for (int i = 0; i < size; i++) board[i] = new char[size];
for (int r = 0; r < size; r++) for (int c = 0; c < size; c++) board[r][c] = rhs.board[r][c]; } return *this;
}
void displayBoard(ostream& out) { outs << endl << endl; for (int i = 0; i < COL_WIDTH - 1; i++) outs << SPACE; for (int i = 0; i < size; i++) outs << setw(COL_WIDTH) << i; outs << endl; for(int r = 0; r < size; r++){ outs << setw(COL_WIDTH) << r; for (int c = 0; c < size; c++){ outs << SPACE << board[r][c] << VERTICAL; } outs << endl; for (int i = 0; i < COL_WIDTH; i++) outs << SPACE; for (int d = 0; d < size * COL_WIDTH; d++) outs << DASH; outs << endl; } }
bool placePiece(char piece, int row, int col) { if (row >= 0 && row < size && col >= 0 && col < size && board[row][col] == SPACE){ board[row][col] = piece; return true; } else return false; }
bool winner(char& piece) { if(
}
void resetBoard()
~TicTacToe()
I am making a tictactoe game using classes and a two dimensional array, I need help with the functions for the classes.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started