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

Upload New File

parent 26fb321b
No related branches found
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();
});
winnerButton = new Button(window, 370, 50, 160, 30, "");
}
void Amoeba::draw() const {
if (!gameStarted) {
if (startButton) {
startButton->draw();
}
} else {
if (gameEnded) {
winnerButton->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 playerNumber;
if (gameLogic.getCurrentPlayer() == 'X') {
playerNumber = 2;
} else {
playerNumber = 1;
}
std::string winnerLabel = "Player " + std::to_string(playerNumber) + " nyert!";
winnerButton->setLabel(winnerLabel);
} else if (gameLogic.isPathFull()) {
gameEnded = true;
winnerButton->setLabel(" Dontetlen :)");
}
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment