Commit 5ef228c2 authored by Loic Guegan's avatar Loic Guegan
Browse files

Cleaning source code

parent 56b57ad8
Pipeline #213050087 passed with stage
in 7 minutes and 28 seconds
......@@ -35,15 +35,13 @@ link_directories(${wxWidgets_LIBRARIES_DIRS})
# Define variables
set(SRC ${CMAKE_SOURCE_DIR}/src)
# Model Module
file(GLOB_RECURSE MODEL ${SRC}/model/*.cpp)
# Engine Module
file(GLOB_RECURSE CMD ${SRC}/command/*.cpp)
file(GLOB_RECURSE CNF ${SRC}/config/*.cpp)
file(GLOB_RECURSE ENGINE ${SRC}/engine/*.cpp)
# Tools Module
file(GLOB_RECURSE TOOLS ${SRC}/tools/*.cpp)
# PGN Module
file(GLOB_RECURSE PGN ${SRC}/pgn/*.cpp)
file(GLOB_RECURSE MODEL ${SRC}/model/*.cpp)
file(GLOB_RECURSE UTILS ${SRC}/utils/*.cpp)
# Combine for reusability
add_library(libochess OBJECT ${MODEL} ${TOOLS} ${ENGINE} ${PGN})
add_library(libochess OBJECT ${CMD} ${CNF} ${ENGINE} ${MODEL} ${UTILS})
# GUI Module
file(GLOB_RECURSE GUI ${SRC}/gui/*.cpp)
# Generate Executable
......
This diff is collapsed.
#ifndef MOVE_HPP
#define MOVE_HPP
#include "Command.hpp"
#include "model/Board.hpp"
#include "tools/Command.hpp"
using namespace ochess;
using namespace ochess::model;
......
#ifndef COMMANDMANAGER_HPP
#define COMMANDMANAGER_HPP
#include "Command.hpp"
#include <stack>
#include <memory> // Shared pointer
#include "Command.hpp"
#define RUN_CMD(CMD) ochess::OchessInstance::GetCommandManager().run(std::shared_ptr<Command>(CMD));
#define REDO() ochess::OchessInstance::GetCommandManager().redo();
......
#ifndef CONFIGMANAGER_HPP
#define CONFIGMANAGER_HPP
#include "Configurable.hpp"
#include <boost/filesystem.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <vector>
#include <cstdlib>
#include "Configurable.hpp"
using namespace boost::property_tree;
using namespace std;
......
......@@ -5,7 +5,7 @@
#include <boost/filesystem.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include "../model/Move.hpp"
#include "model/Move.hpp"
#include <map>
#include <stack>
......
#ifndef UCIPROC_HPP
#define UCIPROC_HPP
#include "tools/log.hpp"
#include <boost/process.hpp>
#include <boost/filesystem.hpp>
#include <boost/asio.hpp>
#include "tools/Chrono.hpp"
#include "tools/log.hpp"
#include <boost/log/attributes.hpp>
#include <stack>
#include "log.hpp"
#include "utils/Chrono.hpp"
namespace bp = boost::process;
namespace bfs = boost::filesystem;
......
......@@ -16,7 +16,7 @@ wxEND_EVENT_TABLE()
wxMenu* MainFrame::getPiecesMenu() {
wxMenu *themes = new wxMenu;
short id = 200;
for (auto &theme : CM.ListPiecesThemes()) {
for (auto &theme : CNF.ListPiecesThemes()) {
themes->Append(id, theme, "");
id++;
}
......@@ -26,7 +26,7 @@ wxMenu* MainFrame::getPiecesMenu() {
wxMenu* MainFrame::getSquaresMenu() {
wxMenu *themes = new wxMenu;
short id = 300;
for (auto &theme : CM.ListBoardThemes()) {
for (auto &theme : CNF.ListBoardThemes()) {
themes->Append(id, theme, "");
id++;
}
......@@ -36,7 +36,6 @@ wxMenu* MainFrame::getSquaresMenu() {
MainFrame::MainFrame(const wxString &title, const wxPoint &pos,
const wxSize &size) :
wxFrame(NULL, wxID_ANY, title, pos, size) {
CM = ochess::OchessInstance::GetConfigManager();
wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item");
......
......@@ -7,7 +7,7 @@
#endif
#include "board/BoardView.hpp"
#include "engine/EngineView.hpp"
#include "tools/OchessInstance.hpp"
#include "ochess.hpp"
namespace ochess {
namespace gui {
......@@ -35,7 +35,6 @@ private:
*/
BoardView *p;
EngineView *e;
ConfigManager CM;
vector<View*> OchessViews;
void OnHello(wxCommandEvent &event);
......
......@@ -11,7 +11,7 @@ END_EVENT_TABLE()
BoardPanel::BoardPanel(wxFrame *parent) :
wxPanel(parent), Configurable("BoardView"), C(this) {
UA = std::make_shared<UserActions>();
ochess::OchessInstance::GetConfigManager().AddModule(this);
CNF.AddModule(this);
UA->IsDraggingPiece = false;
UA->DraggingPieceSquare = boost::none;
UA->IsDrawingArrow = false;
......@@ -164,7 +164,7 @@ void BoardPanel::HandleMouse(wxMouseEvent &event) {
boost::optional<char> pieceName = C.GetPromotedPiece(promoted.i,
promoted.j, square.i, square.j, c);
if (pieceName)
C.G.Promote(pieceName.value());
C.G.Promote((char)pieceName.value());
}
} else {
UA->DraggingPieceSquare = C.GetSquareAt(pos.x, pos.y);
......
......@@ -11,10 +11,8 @@
#include <boost/format.hpp>
#include <sstream>
#include <boost/thread/mutex.hpp>
#include "model/commands/CMD_MovePiece.hpp"
#include "tools/config/Configurable.hpp"
#include "tools/OchessInstance.hpp"
#include "config/Configurable.hpp"
#include "ochess.hpp"
using namespace ochess::model;
......
......@@ -9,8 +9,8 @@
#include "engine/Engine.hpp"
#include <boost/filesystem.hpp>
#include "log.hpp"
#include "model/Move.hpp"
#include "tools/log.hpp"
namespace bfs = boost::filesystem;
using namespace ochess;
......
......@@ -6,14 +6,13 @@ namespace gui {
Skin::Skin() {
wxInitAllImageHandlers();
ConfigManager CM = ochess::OchessInstance::GetConfigManager();
for (auto &theme : CM.ListPiecesThemes()) {
for (auto &theme : CNF.ListPiecesThemes()) {
OC_LOG(trace)
<< "Loading default pieces skin: " << theme;
LoadPiecesSkin(theme);
break;
}
for (auto &theme : CM.ListBoardThemes()) {
for (auto &theme : CNF.ListBoardThemes()) {
OC_LOG(trace)
<< "Loading default board skin: " << theme;
LoadSquaresSkin(theme);
......
#ifndef SKIN_HPP
#define SKIN_HPP
#include "tools/OchessInstance.hpp"
#include "tools/log.hpp"
#include "ochess.hpp"
#include <wx/wxprec.h>
#include "log.hpp"
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
......
#ifndef ENGINEPANEL_HPP
#define ENGINEPANEL_HPP
#include "tools/log.hpp"
#include "gui/View.hpp"
#include "controller/EngineController.hpp"
#include "tools/OchessInstance.hpp"
#include "ochess.hpp"
#include "wx/sizer.h"
#include "wx/wx.h"
#include "../../log.hpp"
namespace ochess {
namespace gui {
......
#ifndef ENGINECONTROLLER_HPP
#define ENGINECONTROLLER_HPP
#include "../../../log.hpp"
#include "engine/Engine.hpp"
#include "gui/Controller.hpp"
#include "gui/View.hpp"
#include "tools/log.hpp"
namespace ochess {
namespace gui {
......
......@@ -2,7 +2,7 @@
#define OC_LOG_HPP
#include "boost/date_time/posix_time/posix_time.hpp"
#include "tools/OchessInstance.hpp"
#include "ochess.hpp"
#include <boost/filesystem.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
......@@ -35,7 +35,7 @@ inline void OC_INIT_LOG() {
boost::log::trivial::debug);
#endif
bfs::path logPath = OchessInstance::GetConfigManager().GetLogPath();
bfs::path logPath = CNF.GetLogPath();
// Convenience
typedef sinks::synchronous_sink<sinks::text_ostream_backend> text_sink;
......
......@@ -182,5 +182,26 @@ MoveType Algorithm::GetMoveType(Coord src, Coord dst) {
return (UNKNOWN);
}
Coord Algorithm::FindSrc(string dst, char col){
Coord src;
auto piecesCoord=GetPiecesOfColor(this->State->ActiveColor);
std::vector<Coord> srcs;
for(auto c: piecesCoord){
PPiece piece=(*BRD)[c];
if(piece->IsLegalMove(c, dst))
srcs.push_back(c);
}
if(srcs.size()==1)
src=srcs.front();
else if(srcs.size()>1){
for(auto c: srcs){
if(col==c.GetXY()[0]){
return(c);
}
}
}
return(src);
}
} // namespace model
} // namespace ochess
......@@ -97,6 +97,7 @@ public:
* @return The coordinate of the next promoting pawn if exists.
*/
boost::optional<Coord> GetNextPromotingPawn();
Coord FindSrc(string dst, char col);
};
} // namespace model
......
#ifndef BOARD_HPP
#define BOARD_HPP
#include "../log.hpp"
#include "Coord.hpp"
#include "pieces/Piece.hpp"
#include "tools/log.hpp"
namespace ochess {
namespace model {
......
......@@ -10,11 +10,12 @@
#include "pieces/Queen.hpp"
#include "pieces/Rook.hpp"
#include <boost/algorithm/string.hpp> // Used in fen (split()...)
#include "tools/log.hpp"
#include <functional> // For lambda
#include <regex> // Used in fen
#include <string>
#include <vector>
#include "../log.hpp"
#define FEN_INITIAL "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
namespace ochess {
......
......@@ -9,6 +9,7 @@ Game::Game(std::string fen) :
f.BuildBoard(&BRD);
State = f.GetState();
}
Game::Game():Game(FEN_INITIAL){}
boost::optional<PPiece> Game::GetPiece(Coord c) {
if (!BRD.isEmpty(c))
......@@ -71,6 +72,12 @@ bool Game::Move(Coord src, Coord dst) {
return (true);
}
bool Game::Move(ochess::model::Move m){
Coord src=A.FindSrc(m.dst, m.col);
std::cout << "lkhjc: " << src.GetXY();
return(this->Move(src.GetXY(), m.dst));
}
void Game::Next() {
H.Next(&BRD, &State);
}
......@@ -169,7 +176,7 @@ bool Game::IsDrawByThreefold() {
// If we found a similar state
if (fen.ThreefoldCompare(currentFen))
n++;
// If three simular states is found
// If three similar states is found
if (n >= 3) {
H.RestoreState();
return (true);
......
......@@ -13,10 +13,29 @@
#include "pieces/Knight.hpp"
#include "pieces/Queen.hpp"
#include <boost/optional.hpp>
#include <unordered_map>
#include "Move.hpp"
#include "Coord.hpp"
#include <iostream>
namespace ochess {
namespace model {
typedef struct GameInfos{
bool isDraw;
bool isWhiteWin;
/// @brief Cf the "WhiteClock" PGN tag
short WhiteClock[3];
/// @brief Cf the "BlackClock" PGN tag
short BlackClock[3];
unordered_map<string, string> tags;
}GameInfos;
/**
* @class Game
* @brief The only class that you should care of!
......@@ -74,11 +93,14 @@ private:
}
;
public:
GameInfos infos;
/**
* @brief Create a new game with a initial starting position.
* @param fen Initial Position.
*/
Game(std::string fen);
Game();
/**
* @brief Move the piece at src to dst
*
......@@ -88,6 +110,7 @@ public:
* @return true if the move was legal and worked.
*/
bool Move(Coord src, Coord dst);
bool Move(ochess::model::Move m);
/**
* @brief Check if a square is empty (usefull for the gui).
* @param c
......
#include "History.hpp"
namespace ochess
{
namespace model
{
namespace ochess {
namespace model {
History::History(std::string FEN) : SavedState(NULL)
{
InitialState.FEN = FEN;
CurrentState = &InitialState;
}
bool History::Next(Board<PPiece> *board, FenState *state)
{
if (!CurrentState->IsLeaf())
{
CurrentState = CurrentState->main;
Fen f(CurrentState->FEN);
f.BuildBoard(board);
*state = f.GetState();
return (true);
}
return (false);
}
History::History(std::string FEN) :
SavedState(NULL) {
InitialState.FEN = FEN;
CurrentState = &InitialState;
}
bool History::Next(Board<PPiece> *board, FenState *state) {
if (!CurrentState->IsLeaf()) {
CurrentState = CurrentState->main;
Fen f(CurrentState->FEN);
f.BuildBoard(board);
*state = f.GetState();
return (true);
}
return (false);
}
bool History::Next()
{
if (!CurrentState->IsLeaf())
{
CurrentState = CurrentState->main;
return (true);
}
return (false);
}
bool History::Next() {
if (!CurrentState->IsLeaf()) {
CurrentState = CurrentState->main;
return (true);
}
return (false);
}
void History::SaveState()
{
SavedState = CurrentState;
}
void History::SaveState() {
SavedState = CurrentState;
}
void History::RestoreState()
{
if (SavedState != NULL)
CurrentState = SavedState;
}
void History::RestoreState() {
if (SavedState != NULL)
CurrentState = SavedState;
}
bool History::Previous(Board<PPiece> *board, FenState *state)
{
if (CurrentState->parent != nullptr)
{
CurrentState = CurrentState->parent;
Fen f(CurrentState->FEN);
f.BuildBoard(board);
*state = f.GetState();
return (true);
}
return (false);
}
bool History::Previous(Board<PPiece> *board, FenState *state) {
if (CurrentState->parent != nullptr) {
CurrentState = CurrentState->parent;
Fen f(CurrentState->FEN);
f.BuildBoard(board);
*state = f.GetState();
return (true);
}
return (false);
}
bool History::Previous()
{
if (CurrentState->parent != nullptr)
{
CurrentState = CurrentState->parent;
return (true);
}
return (false);
}
bool History::Previous() {
if (CurrentState->parent != nullptr) {
CurrentState = CurrentState->parent;
return (true);
}
return (false);
}
void History::New(Board<PPiece> *board, FenState *state)
{
Move *newState = new Move;
Fen f(board, *state);
newState->FEN = f.GetFen();
newState->parent = CurrentState;
CurrentState->expand(newState);
CurrentState = newState;
}
void History::New(Board<PPiece> *board, FenState *state) {
Move *newState = new Move;
Fen f(board, *state);
newState->FEN = f.GetFen();
newState->parent = CurrentState;
CurrentState->expand(newState);
CurrentState = newState;
}
void History::Erase(Board<PPiece> *board, FenState *state)
{
if (CurrentState != &InitialState)
{
Move *toErase = CurrentState;
Previous(board, state);
if (CurrentState->main == toErase)
CurrentState->main=nullptr;
else
CurrentState->variations.erase(
std::remove(CurrentState->variations.begin(),
CurrentState->variations.end(), toErase));
SyncSavedState(toErase);
delete toErase;
}
}
void History::Erase(Board<PPiece> *board, FenState *state) {
if (CurrentState != &InitialState) {
Move *toErase = CurrentState;
Previous(board, state);
if (CurrentState->main == toErase)
CurrentState->main = nullptr;
else
CurrentState->variations.erase(
std::remove(CurrentState->variations.begin(),
CurrentState->variations.end(), toErase));
SyncSavedState(toErase);
delete toErase;
}
}
void History::SyncSavedState(Move *state)
{
if (state == SavedState || state->main == SavedState)
SavedState = NULL;
for (auto &cur_state : state->variations)
SyncSavedState(cur_state);
}
void History::SyncSavedState(Move *state) {
if (state == SavedState || state->main == SavedState)
SavedState = NULL;
for (auto &cur_state : state->variations)
SyncSavedState(cur_state);
}
} // namespace model
} // namespace model
} // namespace ochess
......@@ -56,6 +56,13 @@ namespace ochess
else
this->variations.push_back(m);
}
bool Move::operator==(const Move &m){
return(
this->Src==m.Src &&
this->Dst==m.Dst &&
this->FEN==m.FEN
);