0

I'm trying to swap to objects of class. It's a puzzle game something like http://www.neos-guide.org/sites/default/files/Fifteen_puzzle.png. I'm using std::swap but it doesn't work. Here's code:

if (puzzle[i-1][j].getNum() == 16) {
  std::swap(puzzle[i][j], puzzle[i-1][j]);
}
else if (puzzle[i+1][j].getNum() == 16) {
  std::swap(puzzle[i+1][j], puzzle[i][j]);
}
else if (puzzle[i][j+1].getNum() == 16 && j != 3) {
  std::swap(puzzle[i][j+1], puzzle[i][j]);
}
else if (puzzle[i][j-1].getNum() == 16 && j != 0)  {
  std::swap(puzzle[i][j-1], puzzle[i][j]);
}

And here's my puzzle class:

class Puzzle {
private:
    int x;
    int y;
    int num;
    ALLEGRO_FONT *font;
public:
    Puzzle();
    ~Puzzle();
    void init(int posx, int posy, ALLEGRO_FONT *font);
    void setNum(int num);
    void draw();
    void setXY(int x, int y);
    int getX();
    int getY();
    int getNum();
    bool click(int mx, int my);
};

I have tried std::swap on 2D array of integers, on two objects of same class, on 1D object array and 2D object array. It worked. It just doesn't work in this implementation. I also tried to comment out ALLEGRO_FONT as I thought that there is the problem. But nothing has changed. cplusplus.com says that I need to include <algorithm> or <utility>. I tried both (not at the same time) but it didn't work. Any suggestions? Thank you :)

timrau
  • 22,578
  • 4
  • 51
  • 64
amUser
  • 27
  • 4

0 Answers0