Skip to content
Snippets Groups Projects
Commit 03443360 authored by Susa Márk's avatar Susa Márk
Browse files

Upload New File

parent 8f82998f
Branches
No related tags found
No related merge requests found
#include "amoeba.hpp"
#include "graphics.hpp"
using namespace genv;
Amoeba::Amoeba(Window* window, int pos_x, int pos_y, int width, int height)
: OSWidget(window, pos_x, pos_y, width, height),
gameLogic(),
widget((width - 20) / GameLogic::pathSize, 2),
cellSize((width - 20) / GameLogic::pathSize),
cellPadding(2),
gameStarted(false),
gameEnded(false) {
startButton = new Button(window, 400, 385, 95, 30, "START");
startButton->setOnClick([this]() {
gameStarted = true;
draw();
});
restartButton = new Button(window, 400, 730, 120, 30, "RESTART");
restartButton->setOnClick([this]() {
restartGame();
draw();
});
winnerButton = new Button(window, 370, 50, 160, 30, "");
}
void Amoeba::restartGame() {
gameLogic.restartGame();
gameStarted = true;
gameEnded = false;
}
void Amoeba::draw() const {
if (!gameStarted) {
if (startButton) {
startButton->draw();
}
} else {
if (gameEnded) {
winnerButton->draw();
restartButton->draw();
} else {
widget.draw(gameLogic, _pos_x, _pos_y, _width, _height);
}
}
}
void Amoeba::handle(genv::event ev) {
if (!gameStarted) {
if (startButton) {
startButton->handle(ev);
}
} else if (!gameEnded && ev.type == ev_mouse && ev.button == btn_left) {
if (ev.pos_x >= _pos_x && ev.pos_x < _pos_x + _width && ev.pos_y >= _pos_y && ev.pos_y < _pos_y + _height) {
int cellX = (ev.pos_x - _pos_x) / (cellSize + cellPadding);
int cellY = (ev.pos_y - _pos_y) / (cellSize + cellPadding);
if (gameLogic.makeMove(cellX, cellY, gameLogic.getCurrentPlayer())) {
draw();
if (gameLogic.checkWin()) {
gameEnded = true;
int player;
if (gameLogic.getCurrentPlayer() == 'X') {
player = 2;
} else {
player = 1;
}
std::string winnerLabel = "Player " + std::to_string(player) + " nyert!";
winnerButton->setLabel(winnerLabel);
} else if (gameLogic.isPathFull()) {
gameEnded = true;
winnerButton->setLabel(" Dontetlen :)");
}
}
}
} else if (gameEnded && ev.type == ev_mouse && ev.button == btn_left) {
restartButton->handle(ev);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment