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

Upload New File

parent 4b50d170
No related branches found
No related tags found
No related merge requests found
#include "gamelogic.hpp"
GameLogic::GameLogic() {
restartGame();
}
void GameLogic::restartGame() {
currentPlayer = 0;
for (int i = 0; i < pathSize; ++i) {
for (int j = 0; j < pathSize; ++j) {
path[i][j] = '.';
}
}
}
bool GameLogic::makeMove(int x, int y, char player) {
if (x >= 0 && x < pathSize && y >= 0 && y < pathSize && path[y][x] == '.') {
path[y][x] = player;
currentPlayer = (currentPlayer + 1) % 2;
return true;
}
return false;
}
char GameLogic::getCurrentPlayer() const {
if (currentPlayer == 0) {
return 'X';
} else {
return 'O';
}
}
char GameLogic::getCell(int x, int y) const {
return path[y][x];
}
bool GameLogic::isPathFull() const {
for (int i = 0; i < pathSize; ++i) {
for (int j = 0; j < pathSize; ++j) {
if (path[i][j] == '.') {
return false;
}
}
}
return true;
}
bool GameLogic::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