cmpsc330hw2/main.cpp

399 lines
9.2 KiB
C++
Raw Normal View History

#include <algorithm>
2024-10-09 23:39:24 +00:00
#include <iostream>
#include <sstream>
2024-10-10 22:37:13 +00:00
#include <cstdio>
2024-10-09 23:39:24 +00:00
#define pt cout <<
2024-10-10 15:51:38 +00:00
#define pter cerr <<
2024-10-10 16:13:46 +00:00
#define nl "\n"
2024-10-09 23:39:24 +00:00
#define in cin >>
using namespace std;
// -------- START RAII HELPERS--------
unsigned long allocatedMem = 0;
2024-10-09 23:39:24 +00:00
void *alloc(size_t size) {
void *ptr = malloc(size);
2024-10-10 22:37:13 +00:00
if (!ptr) {
2024-10-10 15:51:38 +00:00
pter "Memory allocation failed" << nl;
2024-10-10 22:37:13 +00:00
return NULL;
2024-10-09 23:39:24 +00:00
}
allocatedMem += 1;
2024-10-09 23:39:24 +00:00
return ptr;
}
void dealloc(void *ptr) {
if (ptr) free(ptr);
allocatedMem -= 1;
2024-10-09 23:39:24 +00:00
}
bool assert_alloc() {
return !allocatedMem;
2024-10-09 23:39:24 +00:00
}
// -------- END RAII HELPERS--------
2024-10-10 22:37:13 +00:00
// -------- START SIMPLE VEC IMPLEMENTATION --------
template<typename T>
class myVec {
T *data;
size_t capacity;
size_t length;
void resize() {
if (length == capacity || !length) {
capacity = capacity ? capacity << 1 : 1;
T *newData = static_cast<T *>(alloc(capacity * sizeof(T)));
for (size_t i = 0; i < length; i++) {
newData[i] = data[i];
}
dealloc(data);
data = newData;
}
}
public:
myVec() {
capacity = 0;
length = 0;
}
void push_back(const T &val) {
resize();
data[length++] = val;
}
void pop_back() {
if (length) {
length--;
}
}
T &operator[](size_t index) {
return data[index];
}
int size() {
return length;
}
};
// -------- END SIMPLE VEC IMPLEMENTATION --------
2024-10-09 23:39:24 +00:00
struct Move {
char player;
2024-10-10 00:36:29 +00:00
int moveX;
int moveY;
2024-10-09 23:39:24 +00:00
};
struct Moves {
2024-10-10 00:36:29 +00:00
int x;
int y;
2024-10-10 22:37:13 +00:00
myVec<Move> moves;
2024-10-09 23:39:24 +00:00
};
bool isValidPlayer(char c) {
return c >= 'A' && c <= 'Z' && c != 'X';
}
2024-10-09 23:39:24 +00:00
Moves serialize(const string &s) {
2024-10-10 15:51:38 +00:00
// TODO: add check for impossible board sizes
2024-10-09 23:39:24 +00:00
Moves result;
istringstream iss(s);
iss >> result.x >> result.y;
2024-10-10 00:36:29 +00:00
result.x <<= 1;
result.y <<= 1;
2024-10-09 23:39:24 +00:00
string player;
int x, y;
while (iss >> player >> x >> y) {
if (player == "END") break;
if (!isValidPlayer(player[0])) {
string err = "Invalid player: " + player;
2024-10-10 02:00:20 +00:00
throw invalid_argument(err);
}
2024-10-10 22:16:59 +00:00
Move move;
move.player = player[0];
move.moveX = x;
move.moveY = y;
result.moves.push_back(move);
2024-10-09 23:39:24 +00:00
}
return result;
}
2024-10-10 15:51:38 +00:00
char **allocBoard(int x, int y) {
char **board = static_cast<char **>(alloc(x * sizeof(char *)));
for (int i = 0; i < x; ++i) {
board[i] = static_cast<char *>(alloc(y * sizeof(char)));
}
return board;
}
2024-10-10 00:36:29 +00:00
void initBoard(char **board, int x, int y) {
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; j++) {
board[i][j] = (j & 1 || i & 1) ? ' ' : '.';
2024-10-10 00:36:29 +00:00
}
}
2024-10-09 23:39:24 +00:00
}
2024-10-10 00:36:29 +00:00
char **allocAndInitBoard(int x, int y) {
2024-10-10 15:51:38 +00:00
char **board = allocBoard(x, y);
2024-10-10 00:36:29 +00:00
initBoard(board, x, y);
return board;
}
void deallocBoard(char **board, int x) {
for (int i = 0; i < x; ++i) {
dealloc(board[i]);
2024-10-09 23:39:24 +00:00
}
2024-10-10 00:36:29 +00:00
dealloc(board);
}
void printBoard(char **board, int x, int y) {
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
pt board[i][j];
}
pt nl;
}
}
2024-10-09 23:39:24 +00:00
char toLowerCase(char c) {
return c | 32;
}
2024-10-10 16:13:46 +00:00
int normalize(char c) {
return toLowerCase(c) - 'a';
}
2024-10-10 16:13:46 +00:00
void swap(pair<char, int> *a, int i, int j) {
pair<char, int> temp = a[i];
a[i] = a[j];
a[j] = temp;
}
2024-10-10 16:13:46 +00:00
void bubbleSort(pair<char, int> *a) {
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 25 - i; j++) {
if (a[j].second > a[j + 1].second)
swap(a, j, j + 1);
}
}
}
2024-10-10 16:13:46 +00:00
void printScores(int *points) {
pair<char, int> *arr = static_cast<pair<char, int> *>(alloc(26 * sizeof(pair<char, int>)));
for (int i = 0; i < 26; i++) {
2024-10-10 22:16:59 +00:00
pair<char, int> p;
p.first = static_cast<char>(i + 'a');
p.second = points[i];
arr[i] = p;
2024-10-10 16:13:46 +00:00
}
bubbleSort(arr);
int prev = -1;
for (int i = 0; i < 26; i++) {
if (arr[i].second != -1) {
2024-10-10 16:13:56 +00:00
pt arr[i].first << ": " << arr[i].second << " boxes" << (
2024-10-10 16:14:29 +00:00
i == 25 || prev == arr[i].second ? " (win)\n" : nl);
2024-10-10 16:13:46 +00:00
prev = arr[i].second;
}
}
dealloc(arr);
}
bool isValidMove(int x, int y) {
2024-10-10 22:16:59 +00:00
return (x & 1) != (y & 1);
}
2024-10-10 15:51:38 +00:00
void addPoint(
2024-10-10 16:13:46 +00:00
int *points,
2024-10-10 15:51:38 +00:00
char **board,
int x,
int y,
char player
) {
if (board[x][y] == ' ') {
2024-10-10 16:13:46 +00:00
if (points[normalize(player)] == -1) {
points[normalize(player)] = 1;
} else {
points[normalize(player)]++;
}
2024-10-10 15:51:38 +00:00
board[x][y] = player;
}
}
void solveVertical(
int moveX,
int moveY,
char **board,
2024-10-10 16:13:46 +00:00
int *points,
2024-10-10 15:51:38 +00:00
int x,
int y,
char player
) {
// TODO: fix ambiguity:
// -
// |
// -
// in the condition above, we do not know what to do at the end of the board
// need to check for x-1, x+1, y-1, y+1
// check for any box below current move
if (moveX < x - 3 && board[moveX + 2][moveY] != ' ' && board[moveX + 1][moveY - 1] != ' ') {
int rightEdge = moveY == y - 1 ? 1 : board[moveX + 1][moveY + 1] != ' ';
if (rightEdge) {
addPoint(points, board, moveX + 1, moveY, player);
}
}
// check for any box above current move
if (moveX > 1 && board[moveX - 2][moveY] != ' ' && board[moveX - 1][moveY - 1] != ' ') {
int rightEdge = moveY == y - 1 ? 1 : board[moveX - 1][moveY + 1] != ' ';
if (rightEdge) {
addPoint(points, board, moveX - 1, moveY, player);
}
}
}
void solveHorizontal(
int moveX,
int moveY,
char **board,
2024-10-10 16:13:46 +00:00
int *points,
2024-10-10 15:51:38 +00:00
int y,
char player
) {
if (moveY) {
if (moveY == y - 1) {
if (board[moveX][moveY - 2] != ' ' && (
board[moveX + 1][moveY - 1] != ' ' && board[moveX - 1][moveY - 1] != ' ')) {
addPoint(points, board, moveX, moveY - 1, player);
}
} else {
if (board[moveX][moveY - 2] != ' ' || board[moveX][moveY + 2] != ' ') {
if (board[moveX + 1][moveY - 1] != ' ' && board[moveX - 1][moveY - 1] != ' ') {
addPoint(points, board, moveX, moveY - 1, player);
}
if (board[moveX + 1][moveY + 1] != ' ' && board[moveX - 1][moveY + 1] != ' ') {
addPoint(points, board, moveX, moveY + 1, player);
}
}
}
} else {
if (board[moveX][moveY + 2] != ' ' && (board[moveX + 1][moveY + 1] != ' ' && board[moveX - 1][moveY + 1] !=
' ')) {
addPoint(points, board, moveX, moveY + 1, player);
}
}
}
2024-10-10 00:36:29 +00:00
void solve(Moves moves, char **board) {
2024-10-10 16:13:46 +00:00
int *points = static_cast<int *>(alloc(26 * (sizeof(int))));
for (int i = 0; i < 26; i++) {
points[i] = -1;
}
2024-10-10 22:16:59 +00:00
for (int i = 0; i < moves.moves.size(); i++) {
Move move = moves.moves[i];
int moveX = move.moveX;
int moveY = move.moveY;
char player = move.player;
if (!isValidMove(moveX, moveY)) {
pt "Invalid move by: " << player << " at (x, y): (" << moveX << ", " << moveY << ")" << nl;
board[moveX][moveY] = 'X';
printBoard(board, moves.x, moves.y);
printScores(points);
pt "Exiting.." << nl;
return;
}
board[moveX][moveY] = toLowerCase(player);
// TODO:
if (moveX % 2 == 0 && moveY % 2 == 1) {
2024-10-10 15:51:38 +00:00
solveVertical(moveX, moveY, board, points, moves.x, moves.y, player);
} else if (moveX % 2 == 1 && moveY % 2 == 0) {
2024-10-10 15:51:38 +00:00
solveHorizontal(moveX, moveY, board, points, moves.y, player);
} else {
pt "Invalid move by: " << player << " at (x, y): (" << moveX << ", " << moveY << ")" << nl;
board[moveX][moveY] = 'X';
printBoard(board, moves.x, moves.y);
2024-10-10 00:36:29 +00:00
2024-10-10 15:51:38 +00:00
printScores(points);
pt "Exiting.." << nl;
return;
}
}
printBoard(board, moves.x, moves.y);
2024-10-10 16:13:46 +00:00
printScores(points);
dealloc(points);
2024-10-10 00:36:29 +00:00
}
int init(char *fileName) {
// --- IO START ---
2024-10-09 23:39:24 +00:00
FILE *inp = fopen(fileName, "r");
2024-10-10 22:37:13 +00:00
if (!inp) {
2024-10-10 15:51:38 +00:00
pter "File not found" << nl;
2024-10-09 23:39:24 +00:00
return 1;
}
fseek(inp, 0L, SEEK_END);
2024-10-10 00:36:29 +00:00
const long sz = ftell(inp);
2024-10-09 23:39:24 +00:00
fseek(inp, 0L, SEEK_SET);
char *fileInpPtr = static_cast<char *>(alloc(sz + 1 * sizeof(char)));
fileInpPtr[sz] = '\0';
for (int i = 0; i < sz; ++i) {
fscanf(inp, "%c", &fileInpPtr[i]);
}
2024-10-10 00:36:29 +00:00
// --- IO END ---
2024-10-09 23:39:24 +00:00
2024-10-10 00:36:29 +00:00
// --- ALGORITHM START ---
Moves moves = serialize(fileInpPtr);
char **board = allocAndInitBoard(moves.x, moves.y);
solve(moves, board);
// --- ALGORITHM END ---
2024-10-09 23:39:24 +00:00
2024-10-10 00:36:29 +00:00
// --- CLEANUP START ---
2024-10-09 23:39:24 +00:00
dealloc(fileInpPtr);
2024-10-10 00:36:29 +00:00
deallocBoard(board, moves.x);
2024-10-09 23:39:24 +00:00
fclose(inp);
if (!assert_alloc()) {
2024-10-10 15:51:38 +00:00
pter "Memory leak detected" << nl;
2024-10-09 23:39:24 +00:00
return 1;
}
2024-10-10 00:36:29 +00:00
// --- CLEANUP END ---
2024-10-09 23:39:24 +00:00
return 0;
}
2024-10-10 00:36:29 +00:00
int main(int argc, char **argv) {
if (argc < 2) {
2024-10-10 15:51:38 +00:00
pter "No input file provided" << nl;
2024-10-10 22:16:59 +00:00
pter "Usage: dotsandboxes /path/to/input.txt" << nl;
return 1;
}
try {
return init(argv[1]);
2024-10-10 02:00:20 +00:00
} catch (const invalid_argument &e) {
2024-10-10 15:51:38 +00:00
pter e.what() << nl;
2024-10-10 00:36:29 +00:00
return 1;
}
}