Upload files to "/"

This commit is contained in:
lakhia13 2024-11-20 02:43:28 +00:00
commit 17a654ea37
5 changed files with 351 additions and 0 deletions

126
board.cxx Normal file

@ -0,0 +1,126 @@
#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;
}

38
board.h Normal file

@ -0,0 +1,38 @@
//
// Created by pranshav on 11/18/24.
//
#ifndef BOARD_H
#define BOARD_H
#include "scores.h"
#include "points.h"
class Board {
private:
char** board;
int rows;
int cols;
public:
Board(int ro, int co);
~Board();
//Checking if memory being accessed is in or out of bounds to prevent segmentation faults
bool isOutOfBounds(int ro, int co, int max_ro, int max_co);
bool oddRowBoxComplete(point temp, Scores& scoreList, int max_ro, int max_co);
bool evenRowBoxComplete(point temp, Scores& scoreList, int max_ro, int max_co);
void updateBoard(char player, int x, int y);
void printBoard();
char** getBoard();
friend class Gameplay;
};
#endif //BOARD_H

63
gameplay.cxx Normal file

@ -0,0 +1,63 @@
#include "gameplay.h"
Gameplay::Gameplay() {};
Gameplay::~Gameplay() {};
int Gameplay::randomFirst(char c1, char c2, int rows, int cols, RandomPlayer r_player, StrategicPlayer s_player, Board* board, Points* stepList, Scores* scoreList, Points* temp) {
point move = r_player.randomMove(*temp);
move.player = c1;
cout<<move.player<<" "<<move.x<<" "<<move.y<<endl;
stepList->push(move);
board->updateBoard(move.player, move.x, move.y);
bool newTurn = board->evenRowBoxComplete(move, *scoreList, 2*rows-1, 2*cols-1) || board->oddRowBoxComplete(move, *scoreList, 2*rows-1, 2*cols-1);
if(newTurn) {
randomFirst(c1, c2, rows, cols, r_player, s_player, board, stepList, scoreList, temp);
}
if (temp->getSize()==0) {
cout<<"END"<<endl;
return 0;
} else {
point move = s_player.strategicMove(*board, *temp);
move.player = c2;
cout<<move.player<<" "<<move.x<<" "<<move.y<<endl;
stepList->push(move);
board->updateBoard(move.player, move.x, move.y);
newTurn = board->evenRowBoxComplete(move, *scoreList, 2*rows-1, 2*cols-1) || board->oddRowBoxComplete(move, *scoreList, 2*rows-1, 2*cols-1);
if (newTurn) {
strategicFirst(c1, c2, rows, cols, r_player, s_player, board, stepList, scoreList, temp);
}
}
return 0;
}
int Gameplay::strategicFirst(char c1, char c2, int rows, int cols, RandomPlayer r_player, StrategicPlayer s_player, Board* board, Points* stepList, Scores* scoreList, Points* temp) {
point move = s_player.strategicMove(*board, *temp);
move.player = c1;
cout<<move.player<<" "<<move.x<<" "<<move.y<<endl;
stepList->push(move);
board->updateBoard(move.player, move.x, move.y);
bool newTurn = board->evenRowBoxComplete(move, *scoreList, 2*rows-1, 2*cols-1) || board->oddRowBoxComplete(move, *scoreList, 2*rows-1, 2*cols-1);
if (newTurn) {
strategicFirst(c1, c2, rows, cols, r_player, s_player, board, stepList, scoreList, temp);
}
if (temp->getSize()==0) {
cout<<"END"<<endl;
return 0;
} else {
point move = r_player.randomMove(*temp);
move.player = c2;
cout<<move.player<<" "<<move.x<<" "<<move.y<<endl;
stepList->push(move);
board->updateBoard(move.player, move.x, move.y);
board->evenRowBoxComplete(move, *scoreList, 2*rows-1, 2*cols-1);
board->oddRowBoxComplete(move, *scoreList, 2*rows-1, 2*cols-1);
newTurn = board->evenRowBoxComplete(move, *scoreList, 2*rows-1, 2*cols-1) || board->oddRowBoxComplete(move, *scoreList, 2*rows-1, 2*cols-1);
if (newTurn) {
randomFirst(c1, c2, rows, cols, r_player, s_player, board, stepList, scoreList, temp);
}
}
return 0;
}

23
gameplay.h Normal file

@ -0,0 +1,23 @@
#ifndef GAMEPLAY_H
#define GAMEPLAY_H
#include <iostream>
#include "points.h"
#include "board.h"
#include "scores.h"
#include "randomplayer.h"
#include "strategicplayer.h"
class Gameplay {
public:
Gameplay();
~Gameplay();
int randomFirst(char c1, char c2, int rows, int cols, RandomPlayer r_player, StrategicPlayer s_player, Board* board, Points* stepList, Scores* scoreList, Points* temp);
int strategicFirst(char c1, char c2, int rows, int cols, RandomPlayer r_player, StrategicPlayer s_player, Board* board, Points* stepList, Scores* scoreList, Points* temp);
};
#endif //GAMEPLAY_H

101
main.cxx Normal file

@ -0,0 +1,101 @@
//
// Created by pranshav on 11/6/24.
//
#include "gameplay.h"
using namespace std;
void createPlayer(string s){
if (s=="Random"){
RandomPlayer player1 = RandomPlayer();
} else {
StrategicPlayer player1 = StrategicPlayer();
}
}
int main() {
//Input rows and cols
int rows, cols;
cin>>rows>>cols;
char c1,c2;
string p1,p2;
Scores *scoreList = new Scores();
//Input player 1
cin>>c1>>p1;
scoreList->addScore(c1);
//Input player 2
cin>>c2>>p2;
scoreList->addScore(c2);
Points *temp = new Points(2*rows-1, 2*cols-1);
Points *stepList = new Points();
RandomPlayer
if (p1=="Random"){
RandomPlayer player1 = RandomPlayer();
} else {
StrategicPlayer player1 = StrategicPlayer();
}
if (p2=="Random"){
RandomPlayer player2 = RandomPlayer();
} else {
StrategicPlayer player2 = StrategicPlayer();
}
Board *board = new Board(rows, cols);
// board->printBoard();
Gameplay gameplay = Gameplay();
while(true) {
if (temp->getSize()==0) {
cout<<"END"<<endl;
break;
} else {
if (p1=="Random" && p2=="Random") {
} else if (p1=="Random" && p2=="Strategic") {
j2:
RandomPlayer player1 = RandomPlayer();
point move = player1.randomMove(*temp);
move.player = c1;
cout<<move.player<<" "<<move.x<<" "<<move.y<<endl;
stepList->push(move);
board->updateBoard(move.player, move.x, move.y);
bool newTurn = board->evenRowBoxComplete(move, *scoreList, 2*rows-1, 2*cols-1) || board->oddRowBoxComplete(move, *scoreList, 2*rows-1, 2*cols-1);
if(newTurn) {
goto j2;
}
if (temp->getSize()==0) {
cout<<"END"<<endl;
break;
} else {
point move = player2.strategicMove(*board, *temp);
move.player = c2;
cout<<move.player<<" "<<move.x<<" "<<move.y<<endl;
stepList->push(move);
board->updateBoard(move.player, move.x, move.y);
newTurn = board->evenRowBoxComplete(move, *scoreList, 2*rows-1, 2*cols-1) || board->oddRowBoxComplete(move, *scoreList, 2*rows-1, 2*cols-1);
if (newTurn) {
}
}
} else if (p1=="Strategic" && p2=="Random") {
gameplay.randomFirst(c1, c2, rows, cols, player1, player2, board, stepList, scoreList, temp);
} else if (p1=="Strategic" && p2=="Strategic") {
}
}
}
// temp->printArray();
board->printBoard();
scoreList->printResults();
delete board;
delete scoreList;
delete temp;
delete stepList;
return 0;
}