From f1592d88fae20695efa303b18c06246ff61d7f1b Mon Sep 17 00:00:00 2001 From: Matuush Date: Sat, 26 Jul 2025 22:19:23 +0200 Subject: [PATCH] Remove unneeded block place parameters + refactor --- global.hpp | 6 ++--- recursive.cpp | 73 +++++++++++++++++++++++++++++---------------------- 2 files changed, 44 insertions(+), 35 deletions(-) diff --git a/global.hpp b/global.hpp index 7ab5f28..3af9415 100644 --- a/global.hpp +++ b/global.hpp @@ -53,9 +53,9 @@ struct Block { void undo(deque &prev); }; -void update(int &time, deque &turn, Block* root, Rectangle &dst); -void undo(Block *root, deque &prev, Rectangle &dst, int &time); -bool valid(Block* root, Rectangle &dst, deque &turn); +void update(Block* root, int time, deque &turn); +void undo(Block *root, deque &prev, int time); +bool valid(Block* root, Rectangle play_area, deque &turn); class Renderer { array textures; diff --git a/recursive.cpp b/recursive.cpp index 06a31b5..4346e0d 100644 --- a/recursive.cpp +++ b/recursive.cpp @@ -23,14 +23,14 @@ deque to_deque(Vector2 v, int depth, int size) { } -void save_state(Block* root, int depth, int time, deque &turn) { +void save_state(Block* root, int time, deque &turn) { vector state = {}; root->save(&state); string filename; std::cin >> filename; std::ofstream outfile("saves/" + filename); - outfile << depth << "\n" << time << "\n" << turn.size() << "\n"; + outfile << root->depth << "\n" << time << "\n" << turn.size() << "\n"; // Save turns for(int play : turn) @@ -40,48 +40,57 @@ void save_state(Block* root, int depth, int time, deque &turn) { for(tile t : state) outfile << int(t) << std::endl; } -void load_state(Block** root, deque &turn, int &depth, int &time, char* argv) { - depth = atoi(argv); - if(depth) +void load_state(Block** root, deque &turn, int &time, char* argv) { + int depth = atoi(argv); + if(depth) { *root = new Block(depth, {0, 0, SCREEN, SCREEN}); - else { - string path = "saves/" + string(argv); - std::ifstream infile(path); - infile >> depth >> time; - int K; infile >> K; - for(int k = 0; k < K; k++) { - int play; infile >> play; - turn.push_back(play); - cout << play; - } - vector state = {}; - long long pw = 1; - for(int i = 0; i < depth; i++) pw *= 9; - for(int i = 0; i < pw; i++) { - int next; infile >> next; - state.push_back((tile)next); - } - *root = new Block(depth, {0, 0, SCREEN, SCREEN}); - (*root)->load_state(state, 0); + return; } + + // Load file + string path = "saves/" + string(argv); + std::ifstream infile(path); + infile >> depth >> time; + + // Load turns + int K; infile >> K; + for(int k = 0; k < K; k++) { + int play; infile >> play; + turn.push_back(play); + cout << play; + } + + // Load board + vector state = {}; + + long long pw = 1; + for(int i = 0; i < depth; i++) + pw *= 9; + + for(int i = 0; i < pw; i++) { + int next; infile >> next; + state.push_back((tile)next); + } + + *root = new Block(depth, {0, 0, SCREEN, SCREEN}); + (*root)->load_state(state, 0); } -void update(int &time, deque &turn, Block* root, Rectangle &dst) { +void update(Block *root, int time, deque &turn) { time++; turn.erase(turn.begin()); int rval_2 = root->play(turn, pas(time)); for(int i = 0; i <= rval_2; i++) turn.pop_back(); - dst = root->getRect(turn); } -void undo(Block *root, deque &prev, Rectangle &dst, int &time) { +void undo(Block *root, deque &prev, int time) { if(prev.size() == 0) return; time--; root->undo(prev); // TODO } -bool valid(Block* root, Rectangle &dst, deque &turn) { - return CheckCollisionRecs(root->getRect(turn), dst) && root->playable(turn); +bool valid(Block *root, Rectangle play_area, deque &turn) { + return CheckCollisionRecs(root->getRect(turn), play_area) && root->playable(turn); } @@ -95,7 +104,7 @@ int main(int argc, char** argv) { root = new Block(depth, {0, 0, SCREEN, SCREEN}); } else - load_state(&root, turn, depth, time, argv[1]); + load_state(&root, turn, time, argv[1]); Rectangle play_area = root->getRect(turn); @@ -124,7 +133,7 @@ int main(int argc, char** argv) { end = true; break; case OKAY: - update(time, turn, root, play_area); + update(root, time, turn); pturn = turn; break; } @@ -132,7 +141,7 @@ int main(int argc, char** argv) { // Key presses if(IsKeyPressed(KEY_X)) - save_state(root, depth, time, turn), end = true; + save_state(root, time, turn), end = true; else if(IsKeyPressed(KEY_Z)) {} // TODO undo