rekurzivni-piskvorky/recursive.cpp
2025-08-02 23:42:11 +02:00

87 lines
2 KiB
C++

#include "global.hpp"
tile act(int t) { return (t % 2) ? p2 : p1; }
tile pas(int t) { return (t % 2) ? p1 : p2; }
Color col(int t) { return act(t) == p2 ? BLUE : RED; }
deque<int> to_deque(Vector2 v, int depth, int size) {
deque<int> r = {};
int x = v.x, y = v.y;
if(x >= size || y >= size || x < 0 || y < 0)
return {};
for(int i = 0; i < depth; ++i) {
size /= 3;
r.push_back(3*(y/size) + x/size);
x %= size;
y %= size;
}
return r;
}
bool valid(Block *root, Rectangle play_area, deque<int> &turn) {
return CheckCollisionRecs(root->get_rect(turn), play_area) && root->playable(turn);
}
int main(int argc, char** argv) {
int time = 0, depth;
Block* root = nullptr;
depth = argc <= 1 ? 1 : std::atoi(argv[1]);
root = new Block(depth, {0, 0, SCREEN, SCREEN});
Rectangle play_area = root->get_rect({});
Renderer renderer(root, time, play_area);
int board_size = renderer.update_board_size(depth);
bool end = false;
deque<int> pturn = {};
while(!WindowShouldClose()) {
// Update screen size
board_size = renderer.update_board_size(depth);
float start_x = ((float)GetScreenWidth()-board_size)/2;
float start_y = ((float)GetScreenHeight()-board_size)/2;
root->update_rect({start_x, start_y, (float)board_size, (float)board_size});
play_area = root->get_rect(pturn);
// Move
Vector2 touch = GetMousePosition();
deque<int> turn = to_deque({touch.x - start_x, touch.y - start_y}, depth, board_size);
if( IsMouseButtonPressed(0) &&
CheckCollisionPointRec(touch, play_area) &&
valid(root, play_area, turn))
switch(root->play(turn, act(time))) {
case CHANGE:
end = true;
break;
case OKAY:
time++;
turn.erase(turn.begin());
int rval_2 = root->play(turn, pas(time));
for(int i = 0; i <= rval_2; i++)
turn.pop_back();
pturn = turn;
break;
}
// Rendering
renderer.render_all(root, time, play_area, turn);
if(IsKeyDown(KEY_Q))
if(end || IsKeyDown(KEY_Q))
break;
}
CloseWindow();
return 0;
}