cmpsc330hw2/main.cpp

152 lines
2.9 KiB
C++
Raw Normal View History

2024-10-09 23:39:24 +00:00
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
2024-10-10 00:36:29 +00:00
#include <vector>
#include <bits/fs_fwd.h>
#include <bits/fs_path.h>
2024-10-09 23:39:24 +00:00
#define pt cout <<
#define nl '\n'
#define in cin >>
using namespace std;
// -------- START RAII HELPERS--------
unsigned long allocated_mem = 0;
void *alloc(size_t size) {
void *ptr = malloc(size);
if (ptr == nullptr) {
cerr << "Memory allocation failed" << endl;
return nullptr;
}
allocated_mem += 1;
return ptr;
}
void dealloc(void *ptr) {
if (ptr) free(ptr);
allocated_mem -= 1;
}
bool assert_alloc() {
return !allocated_mem;
}
// -------- END RAII HELPERS--------
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-09 23:39:24 +00:00
vector<Move> moves;
};
Moves serialize(const string &s) {
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;
result.moves.push_back({player[0], x, y});
}
return result;
}
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-09 23:39:24 +00:00
}
2024-10-10 00:36:29 +00:00
char **allocAndInitBoard(int x, int y) {
char **board = static_cast<char **>(alloc(sizeof(char *) * x));
for (int i = 0; i < x; ++i) {
board[i] = static_cast<char *>(alloc(sizeof(char) * y));
}
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
2024-10-10 00:36:29 +00:00
void solve(Moves moves, char **board) {
}
int init(char *fileName) {
// --- IO START ---
2024-10-09 23:39:24 +00:00
FILE *inp = fopen(fileName, "r");
if (inp == nullptr) {
cerr << "File not found" << endl;
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()) {
cerr << "Memory leak detected" << endl;
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) {
cerr << "No input file provided" << endl;
return 1;
}
return init(argv[1]);
}