mirror of
https://gitlab.com/manzerbredes/ochess.git
synced 2025-04-06 01:56:28 +02:00
Update chessarbiter and finish the implementation of pawns promotions
This commit is contained in:
parent
b7e48fe351
commit
3bec014a33
8 changed files with 37 additions and 22 deletions
2
TODO.md
2
TODO.md
|
@ -1,7 +1,7 @@
|
|||
# TODO
|
||||
|
||||
## Before releasing v0.1.0
|
||||
- [ ] Implement pawn promotions in BoardCanvas
|
||||
- [x] Implement pawn promotions in BoardCanvas
|
||||
- [x] Debug animations (have a more reliable approach (can segfault when clicking on variations in the editor))
|
||||
- [x] In BoardCanvas search for a workaround of the dynamic allocation of adata.buffer (on canvas resize)
|
||||
- [x] Bind the chess game editor settings to EditorPrefs.hpp
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 6f48becb9186a399e3a4dddb778570e7057752c7
|
||||
Subproject commit 6ebd96825576b2da8e8acf2ac85d0b82a7531424
|
|
@ -100,6 +100,12 @@ bool Game::IsCheckmate(bool forBlack){
|
|||
return !arbiter.IsBlackTurn() && arbiter.IsCheckMate();
|
||||
}
|
||||
|
||||
bool Game::IsPromotionMove(std::string absolute_move){
|
||||
arbiter.Setup(GetFen());
|
||||
arbiter.Play(absolute_move);
|
||||
return(arbiter.WasPawnPromotion());
|
||||
}
|
||||
|
||||
bool Game::Play(std::string move,char promotion) {
|
||||
wxLogDebug("Playing move %s", move);
|
||||
std::string fen = GetFen();
|
||||
|
|
|
@ -32,6 +32,7 @@ public:
|
|||
bool Play(std::string move,char promotion='q');
|
||||
bool IsBlackToPlay();
|
||||
bool IsCheckmate(bool forBlack);
|
||||
bool IsPromotionMove(std::string absolute_move);
|
||||
void Previous();
|
||||
void Next();
|
||||
void DeleteMove(HalfMove *m);
|
||||
|
|
|
@ -25,7 +25,6 @@ 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);
|
||||
|
@ -60,19 +59,28 @@ 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) {
|
||||
std::string move=event.GetString().ToStdString();
|
||||
if (game->Play(move)) {
|
||||
// Notify other classes
|
||||
wxCommandEvent event(GAME_CHANGE, GetId());
|
||||
event.SetEventObject(this);
|
||||
ProcessEvent(event);
|
||||
int size=move.size();
|
||||
char promote=(char)event.GetInt();
|
||||
// First check if it is a promotion move (to prompt the user for a piece)
|
||||
if(size>0 && game->IsPromotionMove(move)){
|
||||
promote_on=move.substr(2,2); // Will trigger the piece prompt on next Notify()
|
||||
promotion_move=move; // Save the move that should be played for the promotion
|
||||
} else {
|
||||
if(size==0){ // It is a promotion move?
|
||||
move=promotion_move; // Play the save promotion move
|
||||
promote_on.clear(); // Clear user prompte (cf. Notify())
|
||||
promotion_move.clear();
|
||||
}
|
||||
if(game->Play(move,promote)){
|
||||
// Notify other classes
|
||||
wxCommandEvent event(GAME_CHANGE, GetId());
|
||||
event.SetEventObject(this);
|
||||
ProcessEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
Notify(true); // Redraw event is move failed! Otherwise piece not resets to it initial position after dragging
|
||||
}
|
||||
|
||||
|
@ -127,6 +135,7 @@ void GameTabLeftPanel::Notify(bool skip_animation) {
|
|||
gs.mat_black=game->IsCheckmate(true);
|
||||
gs.mat_white=game->IsCheckmate(false);
|
||||
gs.arrows=engine_arrows;
|
||||
gs.promotion=promote_on;
|
||||
if(m){
|
||||
// There should be a valid src_hl or dst_hl ortherwise it explode:
|
||||
std::string src_hl, dst_hl;
|
||||
|
|
|
@ -14,6 +14,8 @@ class GameTabLeftPanel : public TabGameLeftPanel {
|
|||
bool repeat;
|
||||
HalfMove *last_move;
|
||||
std::vector<GameState::Arrow> engine_arrows;
|
||||
std::string promote_on;
|
||||
std::string promotion_move;
|
||||
|
||||
public:
|
||||
GameTabLeftPanel(wxFrame *parent, std::shared_ptr<Game> game);
|
||||
|
@ -21,7 +23,6 @@ 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);
|
||||
|
|
|
@ -9,7 +9,6 @@ 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),
|
||||
|
@ -469,17 +468,16 @@ void BoardCanvas::MouseEvent(wxMouseEvent &event) {
|
|||
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());
|
||||
wxCommandEvent event(PLAY_MOVE_EVENT, GetId());
|
||||
event.SetEventObject(this);
|
||||
if(prank==1)
|
||||
event.SetString("r");
|
||||
event.SetInt((int)'r');
|
||||
else if(prank==2)
|
||||
event.SetString("b");
|
||||
event.SetInt((int)'b');
|
||||
else if(prank==3)
|
||||
event.SetString("n");
|
||||
event.SetInt((int)'n');
|
||||
else
|
||||
event.SetString("q");
|
||||
event.SetInt((int)'q');
|
||||
ProcessEvent(event);
|
||||
}
|
||||
}
|
||||
|
@ -531,6 +529,7 @@ void BoardCanvas::MouseEvent(wxMouseEvent &event) {
|
|||
std::to_string(+active_square.y + 1) +
|
||||
((char)('a' + file)) + std::to_string(rank + 1);
|
||||
event.SetString(move);
|
||||
event.SetInt((int)'q'); // Promote to queen by default
|
||||
ProcessEvent(event);
|
||||
} else {
|
||||
// If square not valid just redraw (place piece back to its square)
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
|
||||
// Local events
|
||||
wxDECLARE_EVENT(PLAY_MOVE_EVENT, wxCommandEvent);
|
||||
wxDECLARE_EVENT(PLAY_PROMOTE, wxCommandEvent);
|
||||
|
||||
#define REFRESH_MOUSE_LOCATION() \
|
||||
{ \
|
||||
|
|
Loading…
Add table
Reference in a new issue