dotboxEngine/board.cxx

127 lines
4.3 KiB
C++
Raw Permalink Normal View History

2024-11-20 02:43:28 +00:00
#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;col<cols;col++) {
if (!(col % 10)) {
std::cout<<col/10;
} else {
std::cout<<" ";
}
}
std::cout<<std::endl;
std::cout<<" ";
for (int col=0;col<cols;col++) {
std::cout<<(col)%10;
}
std::cout << std::endl;
for (int row=0;row<rows;row++) {
if(row%10==0){
std::cout<<row/10;
} else {
std::cout<<" ";
}
std:: cout<<row%10<<" ";
for (int col=0;col<cols;col++) {
std::cout<<board[row][col];
}
std::cout<<std::endl;
}
}
char **Board::getBoard() {
retr
un this->board;
}