add raii stuff

This commit is contained in:
Sandipsinh Rathod 2024-11-19 19:46:02 -05:00
parent 273662fde0
commit 0c92128fcf
6 changed files with 77 additions and 43 deletions

@ -14,4 +14,5 @@ add_executable(hw4 main.cpp
gameplay.cpp
gameplay.cpp
player.h
io.cpp)
io.cpp
raii.h)

@ -2,13 +2,15 @@
#include <iostream>
#include <cctype>
#include "raii.h"
using namespace std;
Board::Board(int rows, int cols) : rows(rows), cols(cols) {
// Allocate memory for the 2D board
board = new char*[2 * rows - 1];
board = (char**)alloc((2 * rows - 1) * sizeof(char*));
for (int i = 0; i < 2 * rows - 1; i++) {
board[i] = new char[2 * cols - 1];
board[i] = (char*)alloc((2 * cols - 1) * sizeof(char));
for (int j = 0; j < 2 * cols - 1; j++) {
// Initialize with dots at intersections and spaces elsewhere
board[i][j] = (i % 2 == 0 && j % 2 == 0) ? '.' : ' ';
@ -17,12 +19,11 @@ Board::Board(int rows, int cols) : rows(rows), cols(cols) {
}
Board::~Board() {
// TODO: use free
// Free dynamically allocated memory
for (int i = 0; i < 2 * rows - 1; i++) {
delete[] board[i];
dealloc(board[i]);
}
delete[] board;
dealloc(board);
}
char Board::get(int row, int col) const {
@ -119,14 +120,14 @@ void Board::printBoard() const {
cout << " "; // For spaces between dots
}
}
cout << endl;
cout << '\n';
// Print the ones place of the column numbers
cout << " "; // Space for row numbers
for (int col = 0; col < cols * 2 - 1; ++col) {
cout << (col) % 10; // Print ones place
}
cout << endl;
cout << '\n';
// Print the board rows with row numbers
for (int row = 0; row < rows * 2 - 1; ++row) {
@ -150,8 +151,10 @@ void Board::printBoard() const {
for (int col = 0; col < cols * 2 - 1; ++col) {
cout << board[row][col];
}
cout << endl;
cout << '\n';
}
cout.flush();
}
int Board::getRows() const {

17
defs.h Normal file

@ -0,0 +1,17 @@
//
// Created by ssdd on 11/19/24.
//
#ifndef DEFS_H
#define DEFS_H
#include <iostream>
#include <cstdlib>
using namespace std;
#define pt cout <<
#define in cin >>
#define nl endl
#define pter std::cerr <<
#define ll long long
#endif // DEFS_H

@ -6,22 +6,9 @@ Gameplay::Gameplay(int rows, int cols, Player *player1, Player *player2): board(
}
Gameplay::~Gameplay() {
// TODO: needs revie
free(this->player1);
free(this->player2);
}
void Gameplay::playGame() {
ofstream outputFile("output.txt");
if (!outputFile) {
cerr << "Error: Could not open output file for writing." << endl;
return;
}
// Write board dimensions to the output file
outputFile << board.getRows() << " " << board.getCols() << endl;
cout << board.getRows() << " " << board.getCols() << endl;
cout << board.getRows() << " " << board.getCols() << '\n';
char currentPlayer = player1->getName(); // Start with the RandomPlayer
while (true) {
int row, col;
@ -34,8 +21,7 @@ void Gameplay::playGame() {
// Validate and place the move
if (board.isLineValid(row, col)) {
board.placeLine(row, col, currentPlayer);
outputFile << currentPlayer << " " << row << " " << col << std::endl;
std::cout << currentPlayer << " " << row << " " << col << std::endl;
cout << currentPlayer << " " << row << " " << col << '\n';
int boxesCompleted = board.checkAndMarkBox(row, col, currentPlayer);
if (boxesCompleted == 0) {
// Switch to the next player if no boxes are earned
@ -44,13 +30,12 @@ void Gameplay::playGame() {
: player1->getName();
}
} else {
std::cout << currentPlayer << " made an invalid move at (" << row << ", " << col << ")!" << std::endl;
cout << currentPlayer << " made an invalid move at (" << row << ", " << col << ")!" << '\n';
return;
}
// Check if the board is full
if (board.isFull()) {
outputFile << "END" << std::endl;
cout<<"END"<<endl;
break;
}
@ -61,7 +46,18 @@ void Gameplay::playGame() {
// Determine the winner and display results
determineWinner();
outputFile.close();
}
Gameplay::~Gameplay() {
// Free the player objects
free(this->player1);
free(this->player2);
// Alternatively
/*
delete this->player1;
delete this->player2;
*/
}
void Gameplay::determineWinner() {
@ -80,16 +76,14 @@ void Gameplay::determineWinner() {
}
// Display results
std::cout << "Player " << player1->getName() << " has " << randomPlayerBoxes << " boxes." << std::endl;
std::cout << "Player " << player2->getName() << " has " << strategicPlayerBoxes << " boxes." << std::endl;
cout << "Player " << player1->getName() << " has " << randomPlayerBoxes << " boxes." << '\n';
cout << "Player " << player2->getName() << " has " << strategicPlayerBoxes << " boxes." << '\n';
if (randomPlayerBoxes > strategicPlayerBoxes) {
std::cout << "Player " << player1->getName() << " (wins)" << std::endl;
cout << "Player " << player1->getName() << " (wins)" << '\n';
} else if (strategicPlayerBoxes > randomPlayerBoxes) {
std::cout << "Player " << player2->getName() << " (wins)" << std::endl;
cout << "Player " << player2->getName() << " (wins)" << '\n';
} else {
std::cout << "The game is a tie!" << std::endl;
cout << "The game is a tie!" << '\n';
}
}
}

7
io.cpp

@ -1,7 +0,0 @@
#include <iostream>
#include <string>
template<typename T>
void print(T val) {
std::cout << val;
}

26
raii.h Normal file

@ -0,0 +1,26 @@
#ifndef RAII_H
#define RAII_H
#include "defs.h"
unsigned long allocatedMem = 0;
void *alloc(size_t size) {
void *ptr = malloc(size);
if (!ptr) {
pter "Memory allocation failed" << nl;
return NULL;
}
allocatedMem += 1;
return ptr;
}
void dealloc(void *ptr) {
if (ptr) free(ptr);
allocatedMem -= 1;
}
bool assert_alloc() {
return !allocatedMem;
}
#endif // RAII_H