0

I am working on a little puzzle game ( Gem Puzzle).

I have a puzzle Piece object for each piece on the board that has the following attributes:

Position Pcorrect_;
Position Pactuel_;
bool estvide_;

(the 3rd attribute is irrelevant for this question)

The Position is simple structure consisting of:

unsigned ligne;
unsigned colonne;

Each Piece is stored in a vector of a vector.

std::vector<std::vector<Piece>> board_;

The Pieces eventually get mixed around so the correct attribute (location) does not match the actual attribute (location).

I am stuck on a method that should sort the board.The actual position has to match the current position for each piece of the board.

Is there an elegant way of doing this with a sort function ?My current approach is using 4 loops and lots of conditions which is probably the wrong way of doing it.

user2302702
  • 95
  • 1
  • 7
  • Have a look at the `std::sort` function. – Jabberwocky Mar 27 '14 at 13:14
  • Also if you use std::sort you might have to implement a comparison (<) operator inside your Piece class. See http://stackoverflow.com/questions/1380463/sorting-a-vector-of-custom-objects – Silouane Gerin Mar 27 '14 at 13:15
  • Use `std::sort`, and provide it with a function that takes `std::vector` and returns a `bool`. You can then use `std::sort` again within that function to define how you want to compare them. – OMGtechy Mar 27 '14 at 13:23

1 Answers1

0

if you use C++11, you can create a tuple and sort it.

You can define a custom comparison, like in this example:

http://www.hongliangjie.com/2011/10/10/sortin-tuples-in-c/

madduci
  • 2,635
  • 1
  • 32
  • 51