mirror of
https://gitlab.com/manzerbredes/ochess.git
synced 2025-04-07 02:26:29 +02:00
Improve pawn promotion code
This commit is contained in:
parent
840e68807c
commit
b9aa1085df
6 changed files with 48 additions and 11 deletions
|
@ -100,11 +100,11 @@ bool Game::IsCheckmate(bool forBlack){
|
||||||
return !arbiter.IsBlackTurn() && arbiter.IsCheckMate();
|
return !arbiter.IsBlackTurn() && arbiter.IsCheckMate();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Game::Play(std::string move) {
|
bool Game::Play(std::string move,char promotion) {
|
||||||
wxLogDebug("Playing move %s", move);
|
wxLogDebug("Playing move %s", move);
|
||||||
std::string fen = GetFen();
|
std::string fen = GetFen();
|
||||||
arbiter.Setup(fen);
|
arbiter.Setup(fen);
|
||||||
if (arbiter.Play(move)) {
|
if (arbiter.Play(move,promotion)) {
|
||||||
HalfMove *m = new HalfMove(move, arbiter.GetSAN(), arbiter.GetFEN());
|
HalfMove *m = new HalfMove(move, arbiter.GetSAN(), arbiter.GetFEN());
|
||||||
char capture = arbiter.GetCapture();
|
char capture = arbiter.GetCapture();
|
||||||
if (capture != ' ') {
|
if (capture != ' ') {
|
||||||
|
|
|
@ -29,7 +29,7 @@ public:
|
||||||
HalfMove *GetMoves();
|
HalfMove *GetMoves();
|
||||||
std::string GetFen();
|
std::string GetFen();
|
||||||
std::string GetResult();
|
std::string GetResult();
|
||||||
bool Play(std::string move);
|
bool Play(std::string move,char promotion='q');
|
||||||
bool IsBlackToPlay();
|
bool IsBlackToPlay();
|
||||||
bool IsCheckmate(bool forBlack);
|
bool IsCheckmate(bool forBlack);
|
||||||
void Previous();
|
void Previous();
|
||||||
|
|
|
@ -25,6 +25,7 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr<Game> game)
|
||||||
|
|
||||||
// Bind events:
|
// Bind events:
|
||||||
Bind(PLAY_MOVE_EVENT, &GameTabLeftPanel::OnPlay, this, wxID_ANY);
|
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_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->Zoom(-10);}, ZOOM_OUT_BTN);
|
||||||
Bind(wxEVT_BUTTON, [bc=board_canvas](wxCommandEvent &event){bc->Swap();}, SWAP_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) {
|
void GameTabLeftPanel::OnPlay(wxCommandEvent &event) {
|
||||||
wxLogDebug("Game tab received PLAY_MOVE_EVENT");
|
std::string move=event.GetString().ToStdString();
|
||||||
if (game->Play(event.GetString().ToStdString())) {
|
if (game->Play(move)) {
|
||||||
// Notify other classes
|
// Notify other classes
|
||||||
wxCommandEvent event(GAME_CHANGE, GetId());
|
wxCommandEvent event(GAME_CHANGE, GetId());
|
||||||
event.SetEventObject(this);
|
event.SetEventObject(this);
|
||||||
|
|
|
@ -21,6 +21,7 @@ public:
|
||||||
void OnPlay(wxCommandEvent &event);
|
void OnPlay(wxCommandEvent &event);
|
||||||
void OnGotoMove(wxCommandEvent &event);
|
void OnGotoMove(wxCommandEvent &event);
|
||||||
void OnRefreshBoard(wxCommandEvent &event);
|
void OnRefreshBoard(wxCommandEvent &event);
|
||||||
|
void OnPromote(wxCommandEvent &event);
|
||||||
void ApplyPreferences();
|
void ApplyPreferences();
|
||||||
void SetSaveToolEnable(bool state){game_toolbar->EnableTool(0,state);};
|
void SetSaveToolEnable(bool state){game_toolbar->EnableTool(0,state);};
|
||||||
void SetEngineArrows(std::vector<std::string> arrows);
|
void SetEngineArrows(std::vector<std::string> arrows);
|
||||||
|
|
|
@ -9,6 +9,7 @@ tmp=rot_m.TransformPoint(tmp); \
|
||||||
(PT).x+=xsrc;(PT).y+=ysrc;}
|
(PT).x+=xsrc;(PT).y+=ysrc;}
|
||||||
|
|
||||||
wxDEFINE_EVENT(PLAY_MOVE_EVENT, wxCommandEvent);
|
wxDEFINE_EVENT(PLAY_MOVE_EVENT, wxCommandEvent);
|
||||||
|
wxDEFINE_EVENT(PLAY_PROMOTE, wxCommandEvent);
|
||||||
|
|
||||||
BoardCanvas::BoardCanvas(wxFrame *parent)
|
BoardCanvas::BoardCanvas(wxFrame *parent)
|
||||||
: wxPanel(parent), black_side(false), frozen(false),
|
: 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.white_time=new_gs.white_time;
|
||||||
gs.squares_hl=new_gs.squares_hl;
|
gs.squares_hl=new_gs.squares_hl;
|
||||||
gs.arrows=new_gs.arrows;
|
gs.arrows=new_gs.arrows;
|
||||||
|
gs.promotion=new_gs.promotion;
|
||||||
Refresh();
|
Refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -421,11 +423,10 @@ void BoardCanvas::DrawBoard(wxDC &dc) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Ask for promotion
|
// Ask for promotion
|
||||||
/*std::string s="f8";
|
if(gs.promotion.size()==2){
|
||||||
{
|
std::uint8_t sfile = gs.promotion[0]-'a';
|
||||||
std::uint8_t sfile = s[0]-'a';
|
std::uint8_t srank = gs.promotion[1]-'1';
|
||||||
std::uint8_t srank = s[1]-'1';
|
bool is_black_promotion=gs.promotion[1]=='1';
|
||||||
bool is_black_promotion=s[1]=='1';
|
|
||||||
if (!black_side) {
|
if (!black_side) {
|
||||||
srank = 7 - srank;
|
srank = 7 - srank;
|
||||||
sfile = 7 - sfile;
|
sfile = 7 - sfile;
|
||||||
|
@ -451,7 +452,7 @@ void BoardCanvas::DrawBoard(wxDC &dc) {
|
||||||
else
|
else
|
||||||
offset++;
|
offset++;
|
||||||
}
|
}
|
||||||
}*/
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BoardCanvas::MouseEvent(wxMouseEvent &event) {
|
void BoardCanvas::MouseEvent(wxMouseEvent &event) {
|
||||||
|
@ -459,6 +460,34 @@ void BoardCanvas::MouseEvent(wxMouseEvent &event) {
|
||||||
if(frozen)
|
if(frozen)
|
||||||
return;
|
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:
|
// Just redraw if a piece is currently being moved:
|
||||||
if (event.Dragging() && valid_drag) {
|
if (event.Dragging() && valid_drag) {
|
||||||
is_dragging = true;
|
is_dragging = true;
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
|
|
||||||
// Local events
|
// Local events
|
||||||
wxDECLARE_EVENT(PLAY_MOVE_EVENT, wxCommandEvent);
|
wxDECLARE_EVENT(PLAY_MOVE_EVENT, wxCommandEvent);
|
||||||
|
wxDECLARE_EVENT(PLAY_PROMOTE, wxCommandEvent);
|
||||||
|
|
||||||
#define REFRESH_MOUSE_LOCATION() \
|
#define REFRESH_MOUSE_LOCATION() \
|
||||||
{ \
|
{ \
|
||||||
|
@ -74,6 +75,7 @@ typedef struct GameState {
|
||||||
} Square;
|
} Square;
|
||||||
std::string white, black;
|
std::string white, black;
|
||||||
std::string board;
|
std::string board;
|
||||||
|
std::string promotion;
|
||||||
std::map<char, std::uint8_t> captures;
|
std::map<char, std::uint8_t> captures;
|
||||||
std::vector<Square> squares_hl;
|
std::vector<Square> squares_hl;
|
||||||
std::vector<Arrow> arrows;
|
std::vector<Arrow> arrows;
|
||||||
|
|
Loading…
Add table
Reference in a new issue