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

Upload New File

parent d14ce983
No related branches found
No related tags found
No related merge requests found
1.3.cpp 0 → 100644
#include "amoebawidget.hpp"
#include "button.hpp"
using namespace genv;
AmoebaWidget::AmoebaWidget(Window* window, int pos_x, int pos_y, int width, int height)
: OSWidget(window, pos_x, pos_y, width, height) {
cellSize = (width - 20) / pathSize;
cellPadding = 2;
currentPlayer = 0;
gameEnded = false;
for (int i = 0; i < pathSize; ++i) {
for (int j = 0; j < pathSize; ++j) {
path[i][j] = '.';
}
}
winnerButton = new Button(window, 400, 50, 150, 40, "");
}
void AmoebaWidget::draw() const {
if (gameEnded) {
winnerButton->draw();
} else {
for (int i = 0; i < pathSize; ++i) {
for (int j = 0; j < pathSize; ++j) {
int x = _pos_x + j * (cellSize + cellPadding);
int y = _pos_y + i * (cellSize + cellPadding);
gout << move_to(x, y) << color(200, 200, 200) << box(cellSize, cellSize);
if (path[i][j] == 'X') {
gout << color(255, 0, 0);
gout << move_to(x + cellSize / 4, y + cellSize / 4) << line(cellSize / 2, cellSize / 2);
gout << move_to(x + cellSize / 4, y + cellSize * 3 / 4) << line(cellSize / 2, -cellSize / 2);
} else if (path[i][j] == 'O') {
gout << color(0, 0, 255);
gout << move_to(x + cellSize / 3, y + cellSize / 4) << text("O");
}
}
}
}
gout << refresh;
}
void AmoebaWidget::handle(genv::event ev) {
if (!gameEnded && ev.type == ev_mouse && ev.button == btn_left) {
int cellX = (ev.pos_x - _pos_x) / (cellSize + cellPadding);
int cellY = (ev.pos_y - _pos_y) / (cellSize + cellPadding);
if (cellX >= 0 && cellX < pathSize && cellY >= 0 && cellY < pathSize && path[cellY][cellX] == '.') {
if (currentPlayer == 0) {
path[cellY][cellX] = 'X';
} else {
path[cellY][cellX] = 'O';
}
currentPlayer = (currentPlayer + 1) % 2;
draw();
if (checkWin()) {
gameEnded = true;
int playerNumber;
if (currentPlayer == 0) {
playerNumber = 2;
} else {
playerNumber = 1;
}
std::string winnerLabel = "Player " + std::to_string(playerNumber) + " nyert!";
winnerButton->setLabel(winnerLabel);
}
}
}
}
bool AmoebaWidget::checkWin() const {
for (int i = 0; i < pathSize; ++i) {
for (int j = 0; j < pathSize - 4; ++j) {
if (path[i][j] != '.' && path[i][j] == path[i][j + 1] && path[i][j] == path[i][j + 2] &&
path[i][j] == path[i][j + 3] && path[i][j] == path[i][j + 4]) {
return true;
}
if (path[j][i] != '.' && path[j][i] == path[j + 1][i] && path[j][i] == path[j + 2][i] &&
path[j][i] == path[j + 3][i] && path[j][i] == path[j + 4][i]) {
return true;
}
}
}
for (int i = 0; i < pathSize - 4; ++i) {
for (int j = 0; j < pathSize - 4; ++j) {
if (path[i][j] != '.' && path[i][j] == path[i + 1][j + 1] && path[i][j] == path[i + 2][j + 2] &&
path[i][j] == path[i + 3][j + 3] && path[i][j] == path[i + 4][j + 4]) {
return true;
}
if (path[i][j + 4] != '.' && path[i][j + 4] == path[i + 1][j + 3] && path[i][j + 4] == path[i + 2][j + 2] &&
path[i][j + 4] == path[i + 3][j + 1] && path[i][j + 4] == path[i + 4][j]) {
return true;
}
}
}
return false;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment