Improve pawn promotion code

This commit is contained in:
Loic Guegan 2023-01-10 16:01:26 +01:00
parent 840e68807c
commit b9aa1085df
6 changed files with 48 additions and 11 deletions

View file

@ -100,11 +100,11 @@ bool Game::IsCheckmate(bool forBlack){
return !arbiter.IsBlackTurn() && arbiter.IsCheckMate();
}
bool Game::Play(std::string move) {
bool Game::Play(std::string move,char promotion) {
wxLogDebug("Playing move %s", move);
std::string fen = GetFen();
arbiter.Setup(fen);
if (arbiter.Play(move)) {
if (arbiter.Play(move,promotion)) {
HalfMove *m = new HalfMove(move, arbiter.GetSAN(), arbiter.GetFEN());
char capture = arbiter.GetCapture();
if (capture != ' ') {

View file

@ -29,7 +29,7 @@ public:
HalfMove *GetMoves();
std::string GetFen();
std::string GetResult();
bool Play(std::string move);
bool Play(std::string move,char promotion='q');
bool IsBlackToPlay();
bool IsCheckmate(bool forBlack);
void Previous();

View file

@ -25,6 +25,7 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr<Game> game)
// Bind events:
Bind(PLAY_MOVE_EVENT, &GameTabLeftPanel::OnPlay, this, wxID_ANY);
Bind(PLAY_PROMOTE, &GameTabLeftPanel::OnPromote,this);
Bind(wxEVT_BUTTON, [bc=board_canvas](wxCommandEvent &event){bc->Zoom(10);}, ZOOM_IN_BTN);
Bind(wxEVT_BUTTON, [bc=board_canvas](wxCommandEvent &event){bc->Zoom(-10);}, ZOOM_OUT_BTN);
Bind(wxEVT_BUTTON, [bc=board_canvas](wxCommandEvent &event){bc->Swap();}, SWAP_BTN);
@ -59,10 +60,14 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr<Game> game)
});
}
void GameTabLeftPanel::OnPromote(wxCommandEvent &event){
char piece=event.GetString()[0];
wxLogDebug("Promote to %c",piece);
}
void GameTabLeftPanel::OnPlay(wxCommandEvent &event) {
wxLogDebug("Game tab received PLAY_MOVE_EVENT");
if (game->Play(event.GetString().ToStdString())) {
std::string move=event.GetString().ToStdString();
if (game->Play(move)) {
// Notify other classes
wxCommandEvent event(GAME_CHANGE, GetId());
event.SetEventObject(this);

View file

@ -21,6 +21,7 @@ public:
void OnPlay(wxCommandEvent &event);
void OnGotoMove(wxCommandEvent &event);
void OnRefreshBoard(wxCommandEvent &event);
void OnPromote(wxCommandEvent &event);
void ApplyPreferences();
void SetSaveToolEnable(bool state){game_toolbar->EnableTool(0,state);};
void SetEngineArrows(std::vector<std::string> arrows);

View file

@ -9,6 +9,7 @@ tmp=rot_m.TransformPoint(tmp); \
(PT).x+=xsrc;(PT).y+=ysrc;}
wxDEFINE_EVENT(PLAY_MOVE_EVENT, wxCommandEvent);
wxDEFINE_EVENT(PLAY_PROMOTE, wxCommandEvent);
BoardCanvas::BoardCanvas(wxFrame *parent)
: wxPanel(parent), black_side(false), frozen(false),
@ -134,6 +135,7 @@ void BoardCanvas::SetupBoard(const GameState &new_gs) {
gs.white_time=new_gs.white_time;
gs.squares_hl=new_gs.squares_hl;
gs.arrows=new_gs.arrows;
gs.promotion=new_gs.promotion;
Refresh();
}
@ -421,11 +423,10 @@ void BoardCanvas::DrawBoard(wxDC &dc) {
}
}
// Ask for promotion
/*std::string s="f8";
{
std::uint8_t sfile = s[0]-'a';
std::uint8_t srank = s[1]-'1';
bool is_black_promotion=s[1]=='1';
if(gs.promotion.size()==2){
std::uint8_t sfile = gs.promotion[0]-'a';
std::uint8_t srank = gs.promotion[1]-'1';
bool is_black_promotion=gs.promotion[1]=='1';
if (!black_side) {
srank = 7 - srank;
sfile = 7 - sfile;
@ -451,7 +452,7 @@ void BoardCanvas::DrawBoard(wxDC &dc) {
else
offset++;
}
}*/
}
}
void BoardCanvas::MouseEvent(wxMouseEvent &event) {
@ -459,6 +460,34 @@ void BoardCanvas::MouseEvent(wxMouseEvent &event) {
if(frozen)
return;
// If ask for promotion just wait for the user reply
if(gs.promotion.size()==2){
if(event.LeftDown()){
REFRESH_MOUSE_LOCATION();
INIT_CURRENT_SQUARE();
if (IsCurrentSquareValid) {
if((char)('a' + file)==gs.promotion[0]){
std::uint8_t prank=abs((int)gs.promotion[1]-(int)(char)('1' + rank));
if(prank<=3){
wxLogDebug("%d",(int)prank);
wxCommandEvent event(PLAY_PROMOTE, GetId());
event.SetEventObject(this);
if(prank==1)
event.SetString("r");
else if(prank==2)
event.SetString("b");
else if(prank==3)
event.SetString("n");
else
event.SetString("q");
ProcessEvent(event);
}
}
}
}
return;
}
// Just redraw if a piece is currently being moved:
if (event.Dragging() && valid_drag) {
is_dragging = true;

View file

@ -12,6 +12,7 @@
// Local events
wxDECLARE_EVENT(PLAY_MOVE_EVENT, wxCommandEvent);
wxDECLARE_EVENT(PLAY_PROMOTE, wxCommandEvent);
#define REFRESH_MOUSE_LOCATION() \
{ \
@ -74,6 +75,7 @@ typedef struct GameState {
} Square;
std::string white, black;
std::string board;
std::string promotion;
std::map<char, std::uint8_t> captures;
std::vector<Square> squares_hl;
std::vector<Arrow> arrows;