#include "board.h" #include "scores.h" Board::Board(int ro, int co) { rows = 2*ro-1; cols = 2*co-1; board = new char*[rows]; for (int i = 0; i < rows; i++) { board[i] = new char[cols]; for (int j = 0; j < cols; j++) { board[i][j] = (i % 2 == 0 && j % 2 == 0) ? '.' : ' '; } } } Board::~Board() { if (board) { for (int i = 0; i < rows; i++) { delete[] board[i]; } delete[] board; } } //Checking if memory being accessed is in or out of bounds to prevent segmentation faults bool Board::isOutOfBounds(int ro, int co, int max_ro, int max_co) { if (ro<0 || ro>=max_ro || co<0 || co>=max_co) { return false; } else { return true; } } bool Board::oddRowBoxComplete(point temp, Scores& scoreList, int max_ro, int max_co) { bool leftComplete = true, rightComplete = true; int ro = temp.x; int co = temp.y; // Check the left box if (isOutOfBounds(ro, co - 2, max_ro, max_co) && isOutOfBounds(ro - 1, co - 1, max_ro, max_co) && isOutOfBounds(ro + 1, co - 1, max_ro, max_co)) { leftComplete &= (board[temp.x][temp.y-2] != ' '); // Left horizontal line leftComplete &= (board[temp.x-1][temp.y-1] != ' '); // Top vertical line leftComplete &= (board[temp.x+1][temp.y-1] != ' '); // Bottom vertical line if (leftComplete) { board[temp.x][temp.y-1] = toupper(temp.player); scoreList.updateScore(temp.player); } } // Check the right box if (isOutOfBounds(ro, co + 2, max_ro, max_co) && isOutOfBounds(ro - 1, co + 1, max_ro, max_co) && isOutOfBounds(ro + 1, co + 1, max_ro, max_co)) { rightComplete &= (board[temp.x][temp.y+2] != ' '); // Right horizontal line rightComplete &= (board[temp.x-1][temp.y+1] != ' '); // Top vertical line rightComplete &= (board[temp.x+1][temp.y+1] != ' '); // Bottom vertical line if (rightComplete) { board[temp.x][temp.y + 1] = toupper(temp.player); scoreList.updateScore(temp.player); } } return leftComplete && rightComplete; } bool Board::evenRowBoxComplete(point temp, Scores& scoreList, int max_ro, int max_co) { bool upComplete = true, downComplete = true; int ro = temp.x; int co = temp.y; // Check the upper box if (isOutOfBounds(ro - 2, co, max_ro, max_co) && isOutOfBounds(ro - 1, co - 1, max_ro, max_co) && isOutOfBounds(ro - 1, co + 1, max_ro, max_co)) { upComplete &= (board[temp.x-2][temp.y] != ' '); // Upper horizontal line upComplete &= (board[temp.x-1][temp.y-1] != ' '); // Left vertical line upComplete &= (board[temp.x-1][temp.y+1] != ' '); // Right vertical line if (upComplete) { board[temp.x-1][temp.y] = toupper(temp.player); scoreList.updateScore(temp.player); } } // Check the lower box if (isOutOfBounds(ro + 2, co, max_ro, max_co) && isOutOfBounds(ro + 1, co - 1, max_ro, max_co) && isOutOfBounds(ro + 1, co + 1, max_ro, max_co)) { downComplete &= (board[temp.x+2][temp.y] != ' '); // Lower horizontal line downComplete &= (board[temp.x+1][temp.y-1] != ' '); // Left vertical line downComplete &= (board[temp.x+1][temp.y+1] != ' '); // Right vertical line if (downComplete) { board[temp.x + 1][temp.y] = toupper(temp.player); scoreList.updateScore(temp.player); } } return upComplete && downComplete; } void Board::updateBoard(char player, int x, int y) { board[x][y] = tolower(player); } void Board::printBoard() { std::cout << " "; for (int col=0;colboard; }