Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USE ONLY THE CODE GIVEN. DO NOT MAKE YOUR OWN CODE AND ONLY EDIT \"BOARD.CC\" Yo

ID: 3730905 • Letter: U

Question

USE ONLY THE CODE GIVEN. DO NOT MAKE YOUR OWN CODE AND ONLY EDIT "BOARD.CC"

You will be implementing a simple version of Tic Tac Toe in C++. All the files you need are included in this tgz. In particular, you should only edit the file board.cc (the other files should not be changed). There are comments detailing what the methods should do – read those carefully to ensure you handle all the possible cases.

board.cc

#include "board.h"
#include // std::exit and EXIT_FAILURE macro
#include // std::cout, std::cin, std::cerr, std::endl

using namespace std;

// Constructor
// size: the size of the grid (i.e. size x size)
// 1. Verify that size is >= 3 and <= 10. If not, print an error message to
// cerr and call exit(EXIT_FAILURE) to terminate the program.
// 2. Dynamically allocate the 2D array named grid_. If there are any
// allocation errors (e.g. bad_alloc exception), print an error
// message to cerr and call exit(EXIT_FAILURE) to terminate the program.
// 3. Initialize all the values within the 2D array to be ' ' (a space).
Board::Board(int size) {
// Your code goes here
}

// Destructor
// 1. De-allocate the 2D array
Board::~Board() {
// Your code goes here
}

// Prints the current board to the screen
// The board should have numbers to indicate the rows and columns, and the
// current values at each position should be displayed (i.e. blank, X, or O).
// This should look something like this (for size=3):
// 0 1 2
// +---+---+---+
// 0| X | | O |
// +---+---+---+
// 1| O | | X |
// +---+---+---+
// 2| | X | O |
// +---+---+---+
void Board::Display() {
// Your code goes here
}

// Marks the player in the given row and column and returns true on success.
// If the position is invalid or already occupied, print an error to cerr and
// return false.
bool Board::Mark(Player player, int row, int column) {
// Your code goes here
}

// Returns the winner if they have won, or indicates if the game is still in
// progress or ended in a stalemate (see enum class Winner for details).
Winner Board::CheckWinner() {
// Your code goes here
}

board.h

#ifndef BOARD_H
#define BOARD_H

enum class Player { X, O };
enum class Winner { STILL_PLAYING, X, O, STALEMATE };

class Board {
public:
// Constructor
// size: the size of the grid (i.e. size x size)
Board(int size);

// Destructor
~Board();

// Prints the current board to the screen
void Display();

// Marks the player in the given row and column and returns true on success.
// If the position is invalid or already occupied, print an error to cerr
// and return false.
bool Mark(Player player, int row, int column);

// Returns the winner if they have won, or indicates if the game is still in
// progress or ended in a stalemate (see enum class Winner for details).
Winner CheckWinner();

private:
int size_; // The size of the grid (i.e. size x size)
char** grid_; // The 2D grid for the board
};

#endif // BOARD_H

tictactoe.cc

#include "board.h"

#include
#include

using namespace std;

int main() {
int size;
cout << "What size board? ";
cin >> size;
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits::max(), ' ');
cout << "Invalid data. What size board? ";
cin >> size;
}
cout << endl;

// Create the board
Board board(size);

// Display the initial empty board
board.Display();

Winner winner;
Player player = Player::X;
do {
int row, column;
cout << "Enter a row and column (space separated): ";
cin >> row >> column;
if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits::max(), ' ');
cerr << "Not a valid number. Please try again." << endl;
continue;
}
cout << endl;
if (board.Mark(player, row, column)) {
// If the action was successful, switch the current player
player = (player == Player::X ? Player::O : Player::X);
}
board.Display();
} while ((winner = board.CheckWinner()) == Winner::STILL_PLAYING);

switch (winner) {
case Winner::X: cout << "Player X won!" << endl; break;
case Winner::O: cout << "Player O won!" << endl; break;
case Winner::STALEMATE: cout << "Stalemate!" << endl; break;
default: break;
}

return 0;
}

Explanation / Answer

here is your board.cc : ---------->>>>>>>>

#include "board.h"
#include<stdexcept>// std::exit and EXIT_FAILURE macro
#include<iostream>// std::cout, std::cin, std::cerr, std::endl
using namespace std;
// Constructor
// size: the size of the grid (i.e. size x size)
// 1. Verify that size is >= 3 and <= 10. If not, print an error message to
// cerr and call exit(EXIT_FAILURE) to terminate the program.
// 2. Dynamically allocate the 2D array named grid_. If there are any
// allocation errors (e.g. bad_alloc exception), print an error
// message to cerr and call exit(EXIT_FAILURE) to terminate the program.
// 3. Initialize all the values within the 2D array to be ' ' (a space).
Board::Board(int size) {
// Your code goes here
if(size < 3 || size > 10){
  cerr<<" Size is not compatible";
  exit(EXIT_FAILURE);
}
this->size_ = size;
try{
  grid_ = new char*[size_];
  for(int i = 0;i<size_;i++){
   grid_[i] = new char[size_];
  }
}catch(bad_alloc e){
  cerr<<" Bad Allocation";
  exit(EXIT_FAILURE);
}

for(int i = 0;i<size_;i++){
  for(int j = 0;j<size_;j++){
   grid_[i][j] = ' ';
  }
}
}
// Destructor
// 1. De-allocate the 2D array
Board::~Board() {
// Your code goes here
for(int i = 0;i<size_;i++){
  delete[] grid_;
}
delete grid_;
}
// Prints the current board to the screen
// The board should have numbers to indicate the rows and columns, and the
// current values at each position should be displayed (i.e. blank, X, or O).
// This should look something like this (for size=3):
// 0 1 2
// +---+---+---+
// 0| X | | O |
// +---+---+---+
// 1| O | | X |
// +---+---+---+
// 2| | X | O |
// +---+---+---+
void Board::Display() {
// Your code goes here
system("cls");
cout<<" +";
for(int k = 0;k<size_;k++){
  cout<<"---+";
}
for(int i = 0;i<size_;i++){
  cout<<" |";
  for(int j = 0;j<size_;j++){
   cout<<" "<<grid_[i][j]<<" |";
  }
  cout<<" +";
  for(int k = 0;k<size_;k++){
   cout<<"---+";
  }
}
cout<<endl<<" ";
}
// Marks the player in the given row and column and returns true on success.
// If the position is invalid or already occupied, print an error to cerr and
// return false.
bool Board::Mark(Player player, int row, int column) {
// Your code goes here
if(row < 0 || row >= size_ || column < 0 || column >= size_){
  return false;
}
if(grid_[row][column] == ' '){
  if(player == Player::X){
   grid_[row][column] = 'X';
   return true;
  }else if(player == Player::O){
   grid_[row][column] = 'O';
   return true;
  }else{
   return false;
  }
}
return false;
}
// Returns the winner if they have won, or indicates if the game is still in
// progress or ended in a stalemate (see enum class Winner for details).
Winner Board::CheckWinner() {
// Your code goes here
//for stale_mate
int st = 0;
for(int i = 0;i<size_;i++){
  for(int j = 0;j<size_;j++){
   if(grid_[i][j] == ' '){
    st = 1;
   }
  }
}
if(st == 0){
  return Winner::STALEMATE;
}
//for rows
st = 0;
char c;
for(int i = 0;i<size_;i++){
  c = grid_[i][0];
  if(c == ' '){
   continue;
  }
  st = 0;
  for(int j = 0;j<size_;j++){
   if(grid_[i][j] != c){
    st = 1;
    break;
   }
  }
  if(st == 0){
   if(c == 'O'){
    return Winner::O;
   }else if(c == 'X'){
    return Winner::X;
   }
  }
}
//for Columns
for(int i = 0;i<size_;i++){
  c = grid_[i][0];
  if(c == ' '){
   continue;
  }
  st = 0;
  for(int j = 0;j<size_;j++){
   if(grid_[j][i] != c){
    st = 1;
    break;
   }
  }
  if(st == 0){
   if(c == 'O'){
    return Winner::O;
   }else if(c == 'X'){
    return Winner::X;
   }
  }
}
//for Diagonals 1
c = grid_[0][0];
st = 0;
for(int i = 0;i<size_;i++){
  if(grid_[i][i] != c){
   st = 1;
   break;
  }
}
if(st == 0){
  if(c == 'O'){
   return Winner::O;
  }else if(c == 'X'){
   return Winner::X;
  }
}
//for Diagonals 2
c = grid_[size_-1][0];
st = 0;
for(int i = 0;i<size_;i++){
  if(grid_[size_-1-i][i] != c){
   st = 1;
   break;
  }
}
if(st == 0){
  if(c == 'O'){
   return Winner::O;
  }else if(c == 'X'){
   return Winner::X;
  }
}


return Winner::STILL_PLAYING;
}