2

I am trying to write a program that simulates a chess game with the FLTK library.

My problem is that I want to do two callbacks on an two dimension array of buttons, I want to click a button, then click another button and when the second button is clicked have the label of the first button switch to the label of the second button and then delete the label of the first button.

I feel like I need someway of storing the value of the the first button that is clicked, for example if I click FBoard[1][2] the i have a variable that is equal to FBoard[1][2] and open the second click replace the label of FBoard[1][2] to blank (assuming FBoard[1][2] is a cell on the board that has a piece on it. But I'm not sure how or even if this is the right approach.

Here is the cpp file:

  #include"Window.h"
  #include"ChessBoard.h"

  const char *  DisplayWindow::LastLabel;
  bool   DisplayWindow::flag;
  DisplayWindow::DisplayWindow(int width, int height, const char*)
  :Fl_Window(800, 650, "Chess"){   


                for (int X = 0; X <= 7; ++X){
                    for (int Y = 0; Y <= 7; ++Y){
                    // Leaves all positions that arent occupied by
                    // a figure at the start of the game blank
                        FBoard[X][Y] = new Fl_Button(10 + 50*Y, 100 + 50*X, 50, 50,"");
                        FBoard[X][Y]->callback((Fl_Callback*)DisplayWindow::ChangeButton);
                    }
                }

         flag = false;
         MakeLabel(); 
         LabelButton(); 
         show();
}


 DisplayWindow::~DisplayWindow(){}

 void DisplayWindow::MakeLabel(){

  for (int X = 0; X <= 7; ++X){
    for (int Y = 0; Y <= 7; ++Y){

            LBoard[X][Y] = ("  ");
    }
  }

  for (int X = 0; X <= 7; ++X){ 
        LBoard[1][X] = ("WP");
    // Occupies second row with white pawns

  }
    LBoard[0][0] = ("WR");
    LBoard[0][1] = ("WH");
    LBoard[0][2] = ("WB");
    LBoard[0][3] = ("WQ");
    LBoard[0][4] = ("WK");
    LBoard[0][5] = ("WB");
    LBoard[0][6] = ("WH");
    LBoard[0][7] = ("WR");


  for (int X = 0; X <= 7; ++X){
        LBoard[6][X] = ("BP");
  }

    LBoard[7][0] = ("BR");
    LBoard[7][1] = ("BH");
    LBoard[7][2] = ("BB");
    LBoard[7][3] = ("BQ");
    LBoard[7][4] = ("BK");
    LBoard[7][5] = ("BB");
    LBoard[7][6] = ("BH");
    LBoard[7][7] = ("BR");
}


void DisplayWindow::LabelButton(){

    for (int X = 0; X <= 7; ++X){ 
        FBoard[1][X]->label(LBoard[1][X]);
    // Occupies second row with white pawns

    }
    FBoard[0][0]->label(LBoard[0][0]);
    FBoard[0][1]->label(LBoard[0][1]);
    FBoard[0][2]->label(LBoard[0][2]);
    FBoard[0][3]->label(LBoard[0][3]);
    FBoard[0][4]->label(LBoard[0][4]);
    FBoard[0][5]->label(LBoard[0][5]);
    FBoard[0][6]->label(LBoard[0][6]);
    FBoard[0][7]->label(LBoard[0][7]);


    for (int X = 0; X <= 7; ++X){
        FBoard[6][X]->label(LBoard[6][X]);
    }

    FBoard[7][0] ->label(LBoard[7][0]);
    FBoard[7][1] ->label(LBoard[7][1]);
    FBoard[7][2] ->label(LBoard[7][2]);
    FBoard[7][3] ->label(LBoard[7][3]);
    FBoard[7][4] ->label(LBoard[7][4]);
    FBoard[7][5] ->label(LBoard[7][5]);
    FBoard[7][6] ->label(LBoard[7][6]);
    FBoard[7][7] ->label(LBoard[7][7]);

    for(int i=0 ; i<=7 ; i++)
    {
        for(int j=0 ; j<=7 ; j++)
        {
            int k=i+j;

                    if(k % 2 != 0 ){
                            FBoard[i][j]->color(FL_WHITE);
                    }

                    else if (k % 2 ==0 ){ 
                            FBoard[i][j]->color(FL_YELLOW); 
                    }
        }
    }
 }
 void DisplayWindow::ChangeButton(Fl_Widget * o, void * v){
    DisplayWindow* Win = (DisplayWindow *) v;
    Fl_Button * NewBoard = (Fl_Button*) o;

      if (Win->flag == false){
        DisplayWindow::LastLabel =  NewBoard->label();
        NewBoard->label("  ");
        Win->flag = true;
      }
      else{
         NewBoard->label(DisplayWindow::LastLabel);
         Win->flag = false;
      }         
 }

And the Header file:

class DisplayWindow: public Fl_Window {
public:

DisplayWindow(int width, int height, const char* title=0);

virtual ~DisplayWindow();

void MakeLabel();
void LabelButton();
static void ChangeButton(Fl_Button * o, void * );
static bool flag;
bool flag1;
static const char * LastLabel;

private:
Fl_Button *  FBoard[8][8];
char * LBoard[8][8];
};

#endif
  • How does ChangeButton know that v is a DisplayWin if the callback does not provide **this** as a parameter? Have you tried running the program to see if the callback works? – cup Dec 11 '15 at 06:49
  • During copying and pasting i may have got a few things mixed up, ?I have posted the amended code below. The callback works but not as i want it to, it deletes the label of the first button as soon as it is clicked, not when the second button is clicked. – RodionRomanovichRaskolnikov Dec 11 '15 at 14:19

1 Answers1

0

cpp:

#include"Window.h"
#include"ChessBoard.h"

const char *  DisplayWindow::LastLabel;
bool   DisplayWindow::flag;
DisplayWindow::DisplayWindow(int width, int height, const char*)
:Fl_Window(800, 650, "Chess"){   


                             for (int X = 0; X <= 7; ++X){
                                    for (int Y = 0; Y <= 7; ++Y){
         // Leaves all positions that arent occupied by
         // a figure at the start of the game blank
                                        FBoard[X][Y] = new Fl_Button(10 + 50*Y, 100 + 50*X, 50, 50,"");
                                        FBoard[X][Y]->callback((Fl_Callback*)DisplayWindow::ChangeButton);


            //FBoard[X][Y]->callback(ChangeButton);
                                    }
                             }
                             flag = false;
                             MakeLabel(); 
                             LabelButton(); 
                             show();
                          }

DisplayWindow::~DisplayWindow(){}

void DisplayWindow::MakeLabel(){

  for (int X = 0; X <= 7; ++X){
    for (int Y = 0; Y <= 7; ++Y){

            LBoard[X][Y] = ("  ");
    }
  }

  for (int X = 0; X <= 7; ++X){ 
        LBoard[1][X] = ("WP");
    // Occupies second row with white pawns

  }
    LBoard[0][0] = ("WR");
    LBoard[0][1] = ("WH");
    LBoard[0][2] = ("WB");
    LBoard[0][3] = ("WQ");
    LBoard[0][4] = ("WK");
    LBoard[0][5] = ("WB");
    LBoard[0][6] = ("WH");
    LBoard[0][7] = ("WR");


  for (int X = 0; X <= 7; ++X){
        LBoard[6][X] = ("BP");
  }

    LBoard[7][0] = ("BR");
    LBoard[7][1] = ("BH");
    LBoard[7][2] = ("BB");
    LBoard[7][3] = ("BQ");
    LBoard[7][4] = ("BK");
    LBoard[7][5] = ("BB");
    LBoard[7][6] = ("BH");
    LBoard[7][7] = ("BR");
}

void DisplayWindow::LabelButton(){

    for (int X = 0; X <= 7; ++X){ 
        FBoard[1][X]->label(LBoard[1][X]);
    // Occupies second row with white pawns

    }
    FBoard[0][0]->label(LBoard[0][0]);
    FBoard[0][1]->label(LBoard[0][1]);
    FBoard[0][2]->label(LBoard[0][2]);
    FBoard[0][3]->label(LBoard[0][3]);
    FBoard[0][4]->label(LBoard[0][4]);
    FBoard[0][5]->label(LBoard[0][5]);
    FBoard[0][6]->label(LBoard[0][6]);
    FBoard[0][7]->label(LBoard[0][7]);


    for (int X = 0; X <= 7; ++X){
        FBoard[6][X]->label(LBoard[6][X]);
    }

    FBoard[7][0] ->label(LBoard[7][0]);
    FBoard[7][1] ->label(LBoard[7][1]);
    FBoard[7][2] ->label(LBoard[7][2]);
    FBoard[7][3] ->label(LBoard[7][3]);
    FBoard[7][4] ->label(LBoard[7][4]);
    FBoard[7][5] ->label(LBoard[7][5]);
    FBoard[7][6] ->label(LBoard[7][6]);
    FBoard[7][7] ->label(LBoard[7][7]);

    for(int i=0 ; i<=7 ; i++)
    {
        for(int j=0 ; j<=7 ; j++)
        {
            int k=i+j;

                    if(k % 2 != 0 ){
                            FBoard[i][j]->color(FL_WHITE);
                    }

                    else if (k % 2 ==0 ){ 
                            FBoard[i][j]->color(FL_YELLOW); 
                    }
        }
    }
}

void DisplayWindow::ChangeButton(Fl_Widget * o, void * v){
DisplayWindow* Win = (DisplayWindow *) v;
Fl_Button * NewBoard = (Fl_Button*) o;

if (Win->flag == false){
    DisplayWindow::LastLabel =  NewBoard->label();
    NewBoard->label("  ");
    Win->flag = true;
}
else{
    NewBoard->label(DisplayWindow::LastLabel);
    Win->flag = false;
    }           
}

h:

    #ifndef WINDOW_H_
#define WINDOW_H_
#include<vector>
#include<limits>
#include<string>
#include<iostream>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>




class DisplayWindow: public Fl_Window {
public:

 DisplayWindow(int width, int height, const char* title=0);

virtual ~DisplayWindow();

 void MakeLabel();
 void LabelButton();
 static void ChangeButton(Fl_Widget * o, void * );
 static bool flag;
 bool flag1;
 static const char * LastLabel;
private:
 Fl_Button *  FBoard[8][8];
 char * LBoard[8][8];
};

#endif