Skip to content

Commit

Permalink
Standardize include guard names and member variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolarevelant committed Jan 25, 2024
1 parent d4d19af commit 6fb91b4
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 77 deletions.
6 changes: 3 additions & 3 deletions src/ChessboardGrid/ChessboardGrid.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2023 Nicola Revelant

#ifndef ITALIAN_DRAUGHTS_CHESSBOARD_GRID_H
#define ITALIAN_DRAUGHTS_CHESSBOARD_GRID_H
#ifndef CHESSBOARD_GRID_H
#define CHESSBOARD_GRID_H

#include "ChessboardSquare/ChessboardSquare.h"
#include "GameUtils/GameUtils.h"
Expand Down Expand Up @@ -78,4 +78,4 @@ class ChessboardGrid : public wxPanel {
};


#endif //ITALIAN_DRAUGHTS_CHESSBOARD_GRID_H
#endif // CHESSBOARD_GRID_H
6 changes: 3 additions & 3 deletions src/ChessboardSquare/ChessboardSquare.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2023 Nicola Revelant

#ifndef ITALIAN_DRAUGHTS_CHESSBOARD_SQUARE_H
#define ITALIAN_DRAUGHTS_CHESSBOARD_SQUARE_H
#ifndef CHESSBOARD_SQUARE_H
#define CHESSBOARD_SQUARE_H

#include <wx/wx.h>

Expand Down Expand Up @@ -58,4 +58,4 @@ class ChessboardSquare : public wxWindow {
};


#endif //ITALIAN_DRAUGHTS_CHESSBOARD_SQUARE_H
#endif // CHESSBOARD_SQUARE_H
6 changes: 3 additions & 3 deletions src/Frame/Frame.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2023 Nicola Revelant

#ifndef ITALIAN_DRAUGHTS_FRAME_H
#define ITALIAN_DRAUGHTS_FRAME_H
#ifndef FRAME_H
#define FRAME_H

#include "Resources/Resources.h"
#include "MatchManager/MatchManager.h"
Expand Down Expand Up @@ -94,4 +94,4 @@ class Frame : public wxFrame {
void aboutClicked(wxCommandEvent &);
};

#endif //ITALIAN_DRAUGHTS_FRAME_H
#endif // FRAME_H
6 changes: 3 additions & 3 deletions src/GameUtils/GameUtils.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2023 Nicola Revelant

#ifndef ITALIAN_DRAUGHTS_MINIMAX_THREAD_H
#define ITALIAN_DRAUGHTS_MINIMAX_THREAD_H
#ifndef GAME_UTILS_H
#define GAME_UTILS_H

#include "wx/wx.h"

Expand Down Expand Up @@ -108,4 +108,4 @@ class GameUtils {
static int minimax(const GameUtils::Move *start_move, int oldScore, bool maximizing, int depth, int alpha, int beta);
};

#endif // ITALIAN_DRAUGHTS_MINIMAX_THREAD_H
#endif // GAME_UTILS_H
104 changes: 52 additions & 52 deletions src/MatchManager/MatchManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,57 @@ MatchManager::MatchManager(ChessboardGrid *chessboard) {
chessboard->Bind(wxEVT_LEFT_UP, &MatchManager::onChessboardSquareClick, this);
chessboard->Bind(wxEVT_MENU, &MatchManager::onThreadFinish, this, THREAD_ID);

chessboardGrid = chessboard;
algorithmThread = nullptr;
mChessboardGrid = chessboard;
mAlgorithmThread = nullptr;
mIsPlaying = false;
mIsEnd = false;
mIsPcFirstPlayer = false;
}

MatchManager::~MatchManager() {
for (GameUtils::Move *move: moves) {
for (GameUtils::Move *move: mMoves) {
delete move;
}
}

bool MatchManager::newMatch() {
if (algorithmThread) {
algorithmThread->Delete();
algorithmThread = nullptr;
if (mAlgorithmThread) {
mAlgorithmThread->Delete();
mAlgorithmThread = nullptr;
}

setDefaultLayout();
chessboardGrid->updateDisposition(m_disposition, mIsPcFirstPlayer);
mChessboardGrid->updateDisposition(mDisposition, mIsPcFirstPlayer);

// deletes all moves before re-assignment
for (GameUtils::Move *move: moves)
for (GameUtils::Move *move: mMoves)
delete move;

mIsEnd = false;
if (mIsPcFirstPlayer) {
moves.clear();
mMoves.clear();
makePCMove();
} else {
mIsPlaying = false;
moves = GameUtils::findMoves(m_disposition, true);
mMoves = GameUtils::findMoves(mDisposition, true);
notifyUpdate(TURN_PLAYER);
}

return true;
}

void MatchManager::setOnUpdateListener(const UpdateCallback &updateCB) {
m_onUpdate = updateCB;
mOnUpdate = updateCB;

// if the game is over it doesn't call the listener
if (!mIsEnd) notifyUpdate(algorithmThread ? TURN_PC : TURN_PLAYER);
// if the game is not over notify the current state
if (!mIsEnd) notifyUpdate(mAlgorithmThread ? TURN_PC : TURN_PLAYER);
}

bool MatchManager::changeDifficulty(int newDifficulty) {
if (newDifficulty < minGD || newDifficulty > maxGD)
return false;

gameDifficulty = newDifficulty;
mGameDifficulty = newDifficulty;
newMatch();
return true;
}
Expand All @@ -76,23 +76,23 @@ bool MatchManager::flipFirstPlayer() {
}

int MatchManager::getDifficulty() const {
return gameDifficulty;
return mGameDifficulty;
}

bool MatchManager::isPlaying() const {
return mIsPlaying;
}

void MatchManager::onChessboardSquareClick(wxMouseEvent &event) {
if (algorithmThread || mIsEnd) return;
if (mAlgorithmThread || mIsEnd) return;

int currentPos = event.GetId();

if (selectedPos == selectedNone) {
if ((m_disposition[currentPos] == GameUtils::PLAYER_PAWN || m_disposition[currentPos] == GameUtils::PLAYER_DAME)) {
if (mSelectedPos == selectedNone) {
if ((mDisposition[currentPos] == GameUtils::PLAYER_PAWN || mDisposition[currentPos] == GameUtils::PLAYER_DAME)) {
if (highlightPossibleMoves(currentPos)) {
chessboardGrid->SetSquareSelectedOverlay(currentPos);
selectedPos = currentPos;
mChessboardGrid->SetSquareSelectedOverlay(currentPos);
mSelectedPos = currentPos;
} else {
notifyUpdate(ILLEGAL_SELECTION);
}
Expand All @@ -101,66 +101,66 @@ void MatchManager::onChessboardSquareClick(wxMouseEvent &event) {
}

// change selection
if ((m_disposition[currentPos] == GameUtils::PLAYER_PAWN || m_disposition[currentPos] == GameUtils::PLAYER_DAME)) {
chessboardGrid->ClearSquareOverlay(); // it clears selectedPos and possible moves
if (currentPos == selectedPos) {
selectedPos = selectedNone;
if ((mDisposition[currentPos] == GameUtils::PLAYER_PAWN || mDisposition[currentPos] == GameUtils::PLAYER_DAME)) {
mChessboardGrid->ClearSquareOverlay(); // it clears selectedPos and possible moves
if (currentPos == mSelectedPos) {
mSelectedPos = selectedNone;
return;
}

if (highlightPossibleMoves(currentPos)) {
chessboardGrid->SetSquareSelectedOverlay(currentPos);
selectedPos = currentPos;
mChessboardGrid->SetSquareSelectedOverlay(currentPos);
mSelectedPos = currentPos;
} else {
selectedPos = selectedNone;
mSelectedPos = selectedNone;
notifyUpdate(ILLEGAL_SELECTION);
}

return;
}

// illegal selection, deselect the current selection
if (m_disposition[currentPos] != GameUtils::EMPTY || currentPos % 2 != (currentPos / 8) % 2) {
chessboardGrid->ClearSquareOverlay(); // it clears selectedPos and possible moves
selectedPos = selectedNone;
if (mDisposition[currentPos] != GameUtils::EMPTY || currentPos % 2 != (currentPos / 8) % 2) {
mChessboardGrid->ClearSquareOverlay(); // it clears selectedPos and possible moves
mSelectedPos = selectedNone;
return;
}

GameUtils::Move *move = findPlayerMove(selectedPos, currentPos);
GameUtils::Move *move = findPlayerMove(mSelectedPos, currentPos);
if (move == nullptr) {
// illegal move
chessboardGrid->ClearSquareOverlay();
selectedPos = selectedNone;
mChessboardGrid->ClearSquareOverlay();
mSelectedPos = selectedNone;
notifyUpdate(ILLEGAL_MOVE);
return;
}

// legal move
chessboardGrid->updateDisposition(m_disposition = move->disposition, mIsPcFirstPlayer);
selectedPos = selectedNone;
mChessboardGrid->updateDisposition(mDisposition = move->disposition, mIsPcFirstPlayer);
mSelectedPos = selectedNone;

makePCMove();
}

void MatchManager::makePCMove() {
mIsPlaying = true;
notifyUpdate(TURN_PC);
algorithmThread = new GameUtils::AlgorithmThread(chessboardGrid, m_disposition, gameDifficulty, THREAD_ID);
if (algorithmThread->Create() != wxTHREAD_NO_ERROR || algorithmThread->Run() != wxTHREAD_NO_ERROR) {
mAlgorithmThread = new GameUtils::AlgorithmThread(mChessboardGrid, mDisposition, mGameDifficulty, THREAD_ID);
if (mAlgorithmThread->Create() != wxTHREAD_NO_ERROR || mAlgorithmThread->Run() != wxTHREAD_NO_ERROR) {
std::cerr << "Cannot execute thread" << std::endl;
exit(1);
}
}

void MatchManager::onThreadFinish(wxCommandEvent &evt) {
if (!algorithmThread) {
if (!mAlgorithmThread) {
#ifdef DEBUG
std::cerr << "Unwanted thread finished" << std::endl;
#endif
delete static_cast<GameUtils::Move *>(evt.GetClientData());
exit(1);
}
algorithmThread = nullptr;
mAlgorithmThread = nullptr;

auto *pcMove = static_cast<GameUtils::Move *>(evt.GetClientData());
if (pcMove == nullptr) {
Expand All @@ -171,15 +171,15 @@ void MatchManager::onThreadFinish(wxCommandEvent &evt) {
return;
}

chessboardGrid->updateDisposition(m_disposition = pcMove->disposition, mIsPcFirstPlayer);
mChessboardGrid->updateDisposition(mDisposition = pcMove->disposition, mIsPcFirstPlayer);
delete pcMove;

// deletes all moves before re-assignment
for (GameUtils::Move *move: moves)
for (GameUtils::Move *move: mMoves)
delete move;

moves = GameUtils::findMoves(m_disposition, true);
if (moves.empty()) {
mMoves = GameUtils::findMoves(mDisposition, true);
if (mMoves.empty()) {
// Player cannot do anything, PC won
mIsEnd = true;
mIsPlaying = false;
Expand All @@ -194,14 +194,14 @@ void MatchManager::setDefaultLayout() {
for (int i = 0; i < 64; i++) {
if ((i / 8) % 2 == i % 2) {
if (i < (8 * 3)) {
m_disposition[i] = GameUtils::PC_PAWN;
mDisposition[i] = GameUtils::PC_PAWN;
} else if (i >= (8 * 5)) {
m_disposition[i] = GameUtils::PLAYER_PAWN;
mDisposition[i] = GameUtils::PLAYER_PAWN;
} else {
m_disposition[i] = GameUtils::EMPTY;
mDisposition[i] = GameUtils::EMPTY;
}
} else {
m_disposition[i] = GameUtils::EMPTY;
mDisposition[i] = GameUtils::EMPTY;
}
}
}
Expand All @@ -210,7 +210,7 @@ GameUtils::Move *MatchManager::findPlayerMove(int oldIndex, int newIndex) {
GameUtils::PieceType oldValue, newValue;

// iterates the possible moves
for (GameUtils::Move *move: moves) {
for (GameUtils::Move *move: mMoves) {
oldValue = move->disposition[oldIndex];
if (oldValue != GameUtils::EMPTY)
continue; // this is not the correct move
Expand All @@ -226,13 +226,13 @@ GameUtils::Move *MatchManager::findPlayerMove(int oldIndex, int newIndex) {

bool MatchManager::highlightPossibleMoves(int from) {
bool isValid = false;
for (GameUtils::Move *move: moves) {
for (GameUtils::Move *move: mMoves) {
if (move->disposition[from] != GameUtils::EMPTY) continue; // wrong move
isValid = true;
for (int i = 0; i < 64; i++) {
if (m_disposition[i] == GameUtils::EMPTY && move->disposition[i] != GameUtils::EMPTY) {
if (mDisposition[i] == GameUtils::EMPTY && move->disposition[i] != GameUtils::EMPTY) {
// possible move
chessboardGrid->SetSquarePossibleMoveOverlay(i);
mChessboardGrid->SetSquarePossibleMoveOverlay(i);
}
}
}
Expand All @@ -241,5 +241,5 @@ bool MatchManager::highlightPossibleMoves(int from) {
}

void MatchManager::notifyUpdate(MatchManager::UpdateType updateType) {
if (m_onUpdate != nullptr) m_onUpdate(updateType);
if (mOnUpdate != nullptr) mOnUpdate(updateType);
}
20 changes: 10 additions & 10 deletions src/MatchManager/MatchManager.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2023 Nicola Revelant

#ifndef ITALIAN_DRAUGHTS_DRAUGHTS_MATCH_MANAGER_H
#define ITALIAN_DRAUGHTS_DRAUGHTS_MATCH_MANAGER_H
#ifndef MATCH_MANAGER_H
#define MATCH_MANAGER_H

#include "ChessboardGrid/ChessboardGrid.h"
#include "GameUtils/GameUtils.h"
Expand Down Expand Up @@ -85,14 +85,14 @@ class MatchManager {

private:
MatchManager(const MatchManager &); // prevents copy-constructor
GameUtils::Disposition m_disposition{};
GameUtils::AlgorithmThread *algorithmThread;
ChessboardGrid *chessboardGrid;
GameUtils::MoveList moves{};
GameUtils::Disposition mDisposition{};
GameUtils::AlgorithmThread *mAlgorithmThread;
ChessboardGrid *mChessboardGrid;
GameUtils::MoveList mMoves{};
bool mIsEnd, mIsPlaying, mIsPcFirstPlayer;
int gameDifficulty = DEFAULT_DIFFICULTY, selectedPos = selectedNone;
int mGameDifficulty = DEFAULT_DIFFICULTY, mSelectedPos = selectedNone;

UpdateCallback m_onUpdate;
UpdateCallback mOnUpdate;

void onChessboardSquareClick(wxMouseEvent &evt);

Expand Down Expand Up @@ -126,10 +126,10 @@ class MatchManager {
bool highlightPossibleMoves(int from);

/**
* If onUpdate listener is set, it calls the listener with updateType
* If onUpdate listener is set, notify state change
* @param updateType Update type
*/
void notifyUpdate(UpdateType updateType);
};

#endif //ITALIAN_DRAUGHTS_DRAUGHTS_MATCH_MANAGER_H
#endif // MATCH_MANAGER_H
6 changes: 3 additions & 3 deletions src/Resources/Resources.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2023 Nicola Revelant

#ifndef ITALIAN_DRAUGHTS_STRING_RESOURCES_H
#define ITALIAN_DRAUGHTS_STRING_RESOURCES_H
#ifndef RESOURCES_H
#define RESOURCES_H

#include "wx/wx.h"
#include <bits/stdc++.h>
Expand Down Expand Up @@ -41,4 +41,4 @@ class Resources {
};


#endif //ITALIAN_DRAUGHTS_STRING_RESOURCES_H
#endif // RESOURCES_H

0 comments on commit 6fb91b4

Please sign in to comment.