Commit 584a7746 authored by Loic Guegan's avatar Loic Guegan
Browse files

Integrate editor move and model move

parent 9321dd69
Pipeline #217899260 passed with stage
in 9 minutes and 24 seconds
......@@ -25,7 +25,8 @@ void PGNEditor::Render(State *state) {
// Start Drawing
this->DrawHintBar();
this->DrawLine(state->moveline);
if(state->moveline != nullptr)
this->DrawLine(state->moveline);
this->DrawScrollBars();
}
......
......@@ -6,7 +6,7 @@
*/
#include "Editor.hpp"
#include "utils/pgn.hpp"
namespace ochess {
namespace gui {
......@@ -16,29 +16,11 @@ Editor::Editor(wxFrame *parent) :
this->SetBackgroundColour(*wxWHITE);
state = new State();
pgneditor::Move *w1 = new pgneditor::Move("e5");
pgneditor::Move *b1 = new pgneditor::Move("e6");
w1->next = b1;
pgneditor::Move *w2 = new pgneditor::Move("d4");
b1->next = w2;
pgneditor::Move *b2 = new pgneditor::Move("d5");
w2->next = b2;
pgneditor::Move *Varw1 = new pgneditor::Move("d3");
pgneditor::Move *Varb1 = new pgneditor::Move("a5");
pgneditor::Move *Varw2 = new pgneditor::Move("a3");
pgneditor::Move *Varb2 = new pgneditor::Move("b5");
Varw1->next=Varb1;
Varb1->next=Varw2;
Varw2->next=Varb2;
w1->variations.push_back(Varw1);
b1->variations.push_back(Varw1);
ochess::pgn::PGN pgn("/home/loic/test.pgn");
pgn.parseNextGame();
state->moveline = w1;
state->moveline = &(pgn.game->GetMoveLine()->editorMove);
......
......@@ -182,20 +182,29 @@ MoveType Algorithm::GetMoveType(Coord src, Coord dst) {
return (UNKNOWN);
}
Coord Algorithm::FindSrc(string dst, char col){
Coord Algorithm::FindSrc(string dst, char hint,bool isPawn,char piece){
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);
PPiece ppiece=(*BRD)[c];
if(GetMoveType(c, dst) != UNKNOWN){
if(isPawn){
if(ppiece->IsA('p'))
srcs.push_back(c);
}
else{
if(ppiece->IsA(piece)){
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]){
if(hint==c.GetXY()[0] || hint==c.GetXY()[1]){
return(c);
}
}
......
......@@ -97,7 +97,7 @@ public:
* @return The coordinate of the next promoting pawn if exists.
*/
boost::optional<Coord> GetNextPromotingPawn();
Coord FindSrc(string dst, char col);
Coord FindSrc(string dst, char hint,bool isPawn, char piece='?');
};
} // namespace model
......
......@@ -72,10 +72,15 @@ 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));
bool Game::Move(ochess::model::Move *m){
if(m->isLongCastle || m->isShortCastle){
std::cout << "Castle" << std::endl;
return(Castle(m->isLongCastle));
}
std::cout << " dst: " << m->dst << " ispawn: " << m->isPawn << " piece: " << m->piece<< std::endl <<std::flush;
Coord src=A.FindSrc(m->dst, m->col,m->isPawn,m->piece);
return(this->Move(src.GetXY(), m->dst));
}
void Game::Next() {
......
......@@ -110,7 +110,7 @@ public:
* @return true if the move was legal and worked.
*/
bool Move(Coord src, Coord dst);
bool Move(ochess::model::Move m);
bool Move(ochess::model::Move *m);
/**
* @brief Check if a square is empty (usefull for the gui).
* @param c
......@@ -189,6 +189,44 @@ public:
std::string GetFen() {
return (H.GetFen());
}
ochess::model::Move* GetMoveLine(){
return(H.GetInitialState());
}
void PlayLine(ochess::model::Move* line){
if(!Move(line)){
std::cout << "fail" << std::endl << std::flush;
}
if(line->variations.size()>0){
Previous();
for(auto move: line->variations){
std::cout << "play var!" << std::endl;
PlayLine(move);
}
Next();
}
if(line->main!=nullptr)
PlayLine(line->main);
Previous();
}
bool Castle(bool isLong){
if(State.ActiveColor==WHITE){
if(isLong){
return(Move("e1","c1"));
}
else{
return(Move("e1","g1"));
}
}
else{
if(isLong){
return(Move("e8","c8"));
}
else{
return(Move("e8","g8"));
}
}
}
};
} // namespace model
......
......@@ -98,6 +98,9 @@ public:
* @param state FenState to synchronize with the new CurrentState
*/
void Erase(Board<PPiece> *board, FenState *state);
Move* GetInitialState(){
return(&(this->InitialState));
}
};
} // namespace model
......
......@@ -18,7 +18,8 @@ namespace ochess
Move::Move() : parent(nullptr), main(nullptr), isCheck(false), isPromotion(false),
isCheckMate(false), isPawn(false),
isCapture(false), isLongCastle(false), isShortCastle(false), promoteTo('?'), col('?'),
clk{0}, egt{0}, emt{0}, mct{0} {}
clk{0}, egt{0}, emt{0}, mct{0},piece('?'),moveId(1) {
}
void Move::dump()
{
cout << "isCheck:" << isCheck << " isPromotion:" << isPromotion << " isCheckMate:" << isCheckMate << " isPawn:" << isPawn << " isCapture:" << isCapture << " isLongCastle:" << isLongCastle << " isShortCastle:" << isShortCastle << " dst:" << dst << " promoteTo:" << promoteTo << " col:" << col << " clk:" << clk[0] << ":" << clk[1] << ":" << clk[2];
......@@ -51,10 +52,16 @@ namespace ochess
}
void Move::expand(Move *m)
{
if(this->main==nullptr)
this->editorMove.move=this->SANMove;
if(this->main==nullptr){
this->main=m;
else
this->editorMove.next=&(m->editorMove);
}
else{
this->variations.push_back(m);
this->editorMove.variations.push_back(&m->editorMove);
}
m->parent=this;
}
bool Move::operator==(const Move &m){
return(
......
......@@ -4,7 +4,7 @@
#include <string>
#include <iostream>
#include "Coord.hpp"
#include "editor/Move.hpp"
using namespace std;
namespace ochess
......@@ -24,6 +24,7 @@ namespace ochess
Coord Dst;
Move *parent;
Move *main;
pgneditor::Move editorMove;
vector<Move *> variations;
string SANMove;
string dst;
......
......@@ -89,8 +89,7 @@ namespace ochess
else if (NEXTCHAR() == '1')
{
Move *line = parseLine(nullptr);
(void)line;
game->Move(*line);
game->PlayLine(line);
break; // First game parsed to leave
}
if (NEXTCHAR() != EOF)
......
......@@ -43,11 +43,11 @@ namespace ochess
/**
* @brief Parse a given chess line (recursive function)
*/
Move *parseLine(Move *parent);
ochess::model::Move *parseLine(ochess::model::Move *parent);
/**
* @brief Parse a PGN comment that may contains clock informations
*/
void parseComment(Move *line);
void parseComment(ochess::model::Move *line);
/**
* @brief Go to the next non space character (see @a IS_SPACE macro)
*/
......@@ -65,7 +65,7 @@ namespace ochess
* @param SANMove the move to parse
* @param move the NodeMove to store the parsed data
*/
void parseMove(string SANMove, Move *move);
void parseMove(string SANMove, ochess::model::Move *move);
/**
* @brief Extract clock data that follow the "HH:MM:SS" schema from a string
* @param data the string to analyze
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment