This commit is contained in:
Sandipsinh Rathod 2024-11-19 20:04:51 -05:00
parent 0c92128fcf
commit 4d69307f8c
12 changed files with 31 additions and 28 deletions

@ -12,7 +12,8 @@ add_executable(hw4 main.cpp
random_player.cpp
strategic_player.cpp
gameplay.cpp
gameplay.cpp
player.h
io.cpp
raii.h)
raii.h
utils.cpp
utils.h
)

@ -1,8 +1,8 @@
#include "board.h"
#include <iostream>
#include <cctype>
#include "raii.h"
#include "utils.h"
using namespace std;
@ -26,27 +26,27 @@ Board::~Board() {
dealloc(board);
}
char Board::get(int row, int col) const {
char Board::get(const int row, const int col) const {
return board[row][col];
}
void Board::set(int row, int col, char value) {
void Board::set(const int row, const int col, const char value) {
board[row][col] = value;
}
bool Board::isLineValid(int row, int col) const {
bool Board::isLineValid(const int row, const int col) const {
// A line is valid if it's in a valid position and not already occupied
return (row > - 1 && row < 2 * rows - 1 && col > -1 && col < 2 * cols - 1) &&
((row&1) != (col&1)) && (board[row][col] == ' ');
}
void Board::placeLine(int row, int col, char player) {
void Board::placeLine(const int row, const int col, const char player) {
if (isLineValid(row, col)) {
board[row][col] = tolower(player); // Place the player's initial in lowercase
board[row][col] = toLowerCase(player); // Place the player's initial in lowercase
}
}
int Board::checkAndMarkBox(int row, int col, char player) {
int Board::checkAndMarkBox(const int row, const int col, const char player) {
int completedBoxes = 0;
// Check if the move is horizontal or vertical

3
defs.h

@ -1,6 +1,3 @@
//
// Created by ssdd on 11/19/24.
//
#ifndef DEFS_H
#define DEFS_H

@ -1,7 +1,10 @@
#include "gameplay.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
Gameplay::Gameplay(int rows, int cols, Player *player1, Player *player2): board(rows, cols), player1(player1), player2(player2) {
}

@ -2,7 +2,6 @@
#define HW4_GAMEPLAY_H
#include "board.h"
#include "random_player.h"
#include "strategic_player.h"
class Gameplay {

@ -1,7 +1,9 @@
#include "gameplay.h"
#include <iostream>
#include <fstream>
#include <cstring>
#include "random_player.h"
#include "utils.h"
using namespace std;
@ -20,14 +22,14 @@ int main() {
cin >> player2Name >> player2Type;
// Determine player types and initialize gameplay
Player* randomPlayerName, *strategicPlayerName;
if (tolower(player1Type[0]) == 'r' && tolower(player2Type[0]) == 's') {
Player *randomPlayerName, *strategicPlayerName;
if (toLowerCase(player1Type[0]) == 'r' && toLowerCase(player2Type[0]) == 's') {
randomPlayerName = new RandomPlayer(player1Name);
strategicPlayerName = new StrategicPlayer(player2Name);
} else if (tolower(player1Type[0]) == 's' && tolower(player2Type[0]) == 'r') {
} else if (toLowerCase(player1Type[0]) == 's' && toLowerCase(player2Type[0]) == 'r') {
randomPlayerName = new RandomPlayer(player2Name);
strategicPlayerName = new StrategicPlayer(player1Name);
}else if (tolower(player1Type[0]) == 'r' && tolower(player2Type[0]) == 'r') {
} else if (toLowerCase(player1Type[0]) == 'r' && toLowerCase(player2Type[0]) == 'r') {
randomPlayerName = new RandomPlayer(player2Name);
strategicPlayerName = new RandomPlayer(player1Name);
} else {
@ -35,8 +37,8 @@ int main() {
strategicPlayerName = new StrategicPlayer(player1Name);
}
Gameplay game(rows, cols, randomPlayerName, strategicPlayerName);
game.playGame();
Gameplay(rows, cols, randomPlayerName, strategicPlayerName)
.playGame();
return 0;
}

@ -1,6 +1,6 @@
#include "random_player.h"
#include <cstdlib>
#include <ctime>
RandomPlayer::RandomPlayer(char name) {
this->name = name;

@ -3,8 +3,6 @@
#include "board.h"
#include "player.h"
#include <cstdlib>
#include <ctime>
class RandomPlayer : public Player {
char name;

@ -1,6 +1,5 @@
#include "strategic_player.h"
StrategicPlayer::StrategicPlayer(char name) {
this->name = name;
}

@ -1,6 +1,3 @@
//
// Created by sapan on 18-11-2024.
//
#ifndef HW4_STRATEGIC_PLAYER_H
#define HW4_STRATEGIC_PLAYER_H

3
utils.cpp Normal file

@ -0,0 +1,3 @@
char toLowerCase(const char c) {
return c | 32;
}

4
utils.h Normal file

@ -0,0 +1,4 @@
#ifndef UTILS_H
#define UTILS_H
char toLowerCase(char c);
#endif //UTILS_H