Commit 8615b9db authored by Frederik Schwarzer's avatar Frederik Schwarzer
Browse files

A Slide is not a Path and not an (int, int, int ,int).

parent 787ccef8
......@@ -707,18 +707,18 @@ void Board::paintEvent(QPaintEvent * e)
p.end();
}
void Board::reverseSlide(TilePos const & tilePos, int slideX1, int slideY1, int slideX2, int slideY2)
void Board::reverseSlide(TilePos const & tilePos, Slide const & slide)
{
// slide[XY]2 is the current location of the last tile to slide
// slide[XY]1 is its destination
// calculate the offset for the tiles to slide
auto const dx = slideX1 - slideX2;
auto const dy = slideY1 - slideY2;
auto const dx = slide.front().x() - slide.back().x();
auto const dy = slide.front().y() - slide.back().y();
auto currentTile = 0;
// move all tiles between slideX2, slideY2 and x, y to slide with that offset
if (dx == 0) {
if (tilePos.y() < slideY2) {
for (auto i = tilePos.y() + 1; i <= slideY2; ++i) {
if (tilePos.y() < slide.back().y()) {
for (auto i = tilePos.y() + 1; i <= slide.back().y(); ++i) {
currentTile = field(TilePos(tilePos.x(), i));
if (currentTile == EMPTY) {
continue;
......@@ -729,7 +729,7 @@ void Board::reverseSlide(TilePos const & tilePos, int slideX1, int slideY1, int
updateField(TilePos(tilePos.x(), i + dy));
}
} else {
for (auto i = tilePos.y() - 1; i >= slideY2; --i) {
for (auto i = tilePos.y() - 1; i >= slide.back().y(); --i) {
currentTile = field(TilePos(tilePos.x(), i));
if (currentTile == EMPTY) {
continue;
......@@ -741,8 +741,8 @@ void Board::reverseSlide(TilePos const & tilePos, int slideX1, int slideY1, int
}
}
} else if (dy == 0) {
if (tilePos.x() < slideX2) {
for (auto i = tilePos.x() + 1; i <= slideX2; ++i) {
if (tilePos.x() < slide.back().x()) {
for (auto i = tilePos.x() + 1; i <= slide.back().x(); ++i) {
currentTile = field(TilePos(i, tilePos.y()));
if (currentTile == EMPTY) {
continue;
......@@ -753,7 +753,7 @@ void Board::reverseSlide(TilePos const & tilePos, int slideX1, int slideY1, int
updateField(TilePos(i + dx, tilePos.y()));
}
} else {
for (auto i = tilePos.x() - 1; i >= slideX2; --i) {
for (auto i = tilePos.x() - 1; i >= slide.back().x(); --i) {
currentTile = field(TilePos(i, tilePos.y()));
if (currentTile == EMPTY) {
continue;
......@@ -767,7 +767,7 @@ void Board::reverseSlide(TilePos const & tilePos, int slideX1, int slideY1, int
}
}
void Board::performSlide(TilePos const & tilePos, Path const & slide)
void Board::performSlide(TilePos const & tilePos, Slide const & slide)
{
// check if there is something to slide
if (slide.empty()) {
......@@ -1024,10 +1024,10 @@ bool Board::canMakePath(TilePos const & tilePos1, TilePos const & tilePos2) cons
return false;
}
bool Board::canSlideTiles(TilePos const & tilePos1, TilePos const & tilePos2, Path & path) const
bool Board::canSlideTiles(TilePos const & tilePos1, TilePos const & tilePos2, Slide & slide) const
{
auto distance = -1;
path.clear();
slide.clear();
if (tilePos1.x() == tilePos2.x()) {
if (tilePos1.y() > tilePos2.y()) {
distance = tilePos1.y() - tilePos2.y();
......@@ -1058,9 +1058,9 @@ bool Board::canSlideTiles(TilePos const & tilePos1, TilePos const & tilePos2, Pa
// so we can slide of start_free - end_free, compare this to the distance
if (distance <= (startFree - endFree)) {
// first position of the last slided tile
path.push_back(TilePos(tilePos1.x(), startFree + 1));
slide.push_back(TilePos(tilePos1.x(), startFree + 1));
// final position of the last slided tile
path.push_back(TilePos(tilePos1.x(), startFree + 1 - distance));
slide.push_back(TilePos(tilePos1.x(), startFree + 1 - distance));
return true;
}
return false;
......@@ -1094,9 +1094,9 @@ bool Board::canSlideTiles(TilePos const & tilePos1, TilePos const & tilePos2, Pa
// so we can slide of end_free - start_free, compare this to the distance
if (distance <= (endFree - startFree)) {
// first position of the last slidden tile
path.push_back(TilePos(tilePos1.x(), startFree - 1));
slide.push_back(TilePos(tilePos1.x(), startFree - 1));
// final position of the last slidden tile
path.push_back(TilePos(tilePos1.x(), startFree - 1 + distance));
slide.push_back(TilePos(tilePos1.x(), startFree - 1 + distance));
return true;
}
return false;
......@@ -1135,9 +1135,9 @@ bool Board::canSlideTiles(TilePos const & tilePos1, TilePos const & tilePos2, Pa
// so we can slide of start_free - end_free, compare this to the distance
if (distance <= (startFree - endFree)) {
// first position of the last slidden tile
path.push_back(TilePos(startFree + 1, tilePos1.y()));
slide.push_back(TilePos(startFree + 1, tilePos1.y()));
// final position of the last slidden tile
path.push_back(TilePos(startFree + 1 - distance, tilePos1.y()));
slide.push_back(TilePos(startFree + 1 - distance, tilePos1.y()));
return true;
}
return false;
......@@ -1171,9 +1171,9 @@ bool Board::canSlideTiles(TilePos const & tilePos1, TilePos const & tilePos2, Pa
// so we can slide of endFree - startFree, compare this to the distance
if (distance <= (endFree - startFree)) {
// first position of the last slidden tile
path.push_back(TilePos(startFree - 1, tilePos1.y()));
slide.push_back(TilePos(startFree - 1, tilePos1.y()));
// final position of the last slidden tile
path.push_back(TilePos(startFree - 1 + distance, tilePos1.y()));
slide.push_back(TilePos(startFree - 1 + distance, tilePos1.y()));
return true;
}
return false;
......@@ -1240,26 +1240,26 @@ int Board::findSimplePath(TilePos const & tilePos1, TilePos const & tilePos2, Po
// I isolate the special code when tiles can slide even if it duplicates code for now
// Can we make a path sliding tiles ?, the slide move is always first, then a normal path
if (m_tilesCanSlideFlag) {
Path slidePath;
Slide slide;
// Find path of 2 segments (route A)
if (canSlideTiles(tilePos1, TilePos(tilePos2.x(), tilePos1.y()), slidePath)
if (canSlideTiles(tilePos1, TilePos(tilePos2.x(), tilePos1.y()), slide)
&& canMakePath(TilePos(tilePos2.x(), tilePos1.y()), tilePos2)) {
path.clear();
path.push_back(tilePos1);
path.push_back(TilePos(tilePos2.x(), tilePos1.y()));
path.push_back(tilePos2);
possibleMoves.push_back(PossibleMove(path, slidePath));
possibleMoves.push_back(PossibleMove(path, slide));
++numberOfPaths;
}
// Find path of 2 segments (route B)
if (canSlideTiles(tilePos1, TilePos(tilePos1.x(), tilePos2.y()), slidePath)
if (canSlideTiles(tilePos1, TilePos(tilePos1.x(), tilePos2.y()), slide)
&& canMakePath(TilePos(tilePos1.x(), tilePos2.y()), tilePos2)) {
path.clear();
path.push_back(tilePos1);
path.push_back(TilePos(tilePos1.x(), tilePos2.y()));
path.push_back(tilePos2);
possibleMoves.push_back(PossibleMove(path, slidePath));
possibleMoves.push_back(PossibleMove(path, slide));
++numberOfPaths;
}
}
......@@ -1404,13 +1404,13 @@ int Board::delay() const
return m_delay;
}
void Board::madeMove(TilePos const & tilePos1, TilePos const & tilePos2, Path slide)
void Board::madeMove(TilePos const & tilePos1, TilePos const & tilePos2, Slide slide)
{
Move * move;
if (slide.empty()) {
move = new Move(tilePos1, tilePos2, field(tilePos1), field(tilePos2));
} else {
move = new Move(tilePos1, tilePos2, field(tilePos1), field(tilePos2), slide.front().x(), slide.front().y(), slide.back().x(), slide.back().y());
move = new Move(tilePos1, tilePos2, field(tilePos1), field(tilePos2), slide);
}
m_undo.push_back(move);
while (m_redo.size() != 0) {
......@@ -1599,7 +1599,7 @@ void Board::undo()
qCDebug(KSHISEN_General) << "[undo] reversing slide";
// then undo the slide to put the tiles back to their original location
reverseSlide(TilePos(move->x1(), move->y1()), move->slideX1(), move->slideY1(), move->slideX2(), move->slideY2());
reverseSlide(TilePos(move->x1(), move->y1()), move->slide());
} else {
qCDebug(KSHISEN_General) << "[undo] gravity from vertical slide";
......@@ -1625,7 +1625,7 @@ void Board::undo()
// undo slide if any
if (move->hasSlide()) {
// perform the slide in reverse
reverseSlide(TilePos(move->x1(), move->y1()), move->slideX1(), move->slideY1(), move->slideX2(), move->slideY2());
reverseSlide(TilePos(move->x1(), move->y1()), move->slide());
}
}
......@@ -1647,7 +1647,7 @@ void Board::redo()
auto move = m_redo.takeFirst();
// redo the slide if any
if (move->hasSlide()) {
Path s;
Slide s;
s.push_back(TilePos(move->slideX1(), move->slideY1()));
s.push_back(TilePos(move->slideX2(), move->slideY2()));
performSlide(TilePos(move->x1(), move->y1()), s);
......
......@@ -241,9 +241,9 @@ private: // functions
/** Checks if the tile at \p tilePos1 can be slid to \p tilePos2.
* @param tilePos1 coordinates of the slide's initial position
* @param tilePos2 coordinates of the slide's final position
* @param path The movement of the last tile slided will be stored in the path
* @param slide The movement of the last tile slid will be stored in @p slide
*/
bool canSlideTiles(TilePos const & tilePos1, TilePos const & tilePos2, Path & path) const;
bool canSlideTiles(TilePos const & tilePos1, TilePos const & tilePos2, Slide & slide) const;
/** Checks if a path between two tiles can be made with 2 or 3 lines.
* @param tilePos1 coordinates of the first tile
......@@ -261,8 +261,8 @@ private: // functions
*/
int findSimplePath(TilePos const & tilePos1, TilePos const & tilePos2, PossibleMoves & possibleMoves) const;
void performMove(PossibleMove & possibleMoves);
void performSlide(TilePos const & tilePos, Path const & slide);
void reverseSlide(TilePos const & tilePos, int slideX1, int slideY1, int slideX2, int slideY2);
void performSlide(TilePos const & tilePos, Slide const & slide);
void reverseSlide(TilePos const & tilePos, Slide const & slide);
bool isTileHighlighted(TilePos const & tilePos) const;
void drawConnection();
void drawPossibleMoves(bool b);
......@@ -273,7 +273,7 @@ private: // functions
QPoint midCoord(TilePos const & tilePos) const;
void unmarkTile();
void marked(TilePos const & tilePos);
void madeMove(TilePos const & tilePos1, TilePos const & tilePos2, Path slide = Path());
void madeMove(TilePos const & tilePos1, TilePos const & tilePos2, Slide slide = Slide());
/** Applies gravity to all columns.
* @param tilePos1 coordinates of the first tile
......
......@@ -27,23 +27,17 @@ Move::Move(TilePos const & tilePos1, TilePos const & tilePos2, int tile1, int ti
, m_tile1(tile1)
, m_tile2(tile2)
, m_hasSlide(false)
, m_slideX1(-1)
, m_slideY1(-1)
, m_slideX2(-1)
, m_slideY2(-1)
, m_slide(Slide())
{
}
Move::Move(TilePos const & tilePos1, TilePos const & tilePos2, int tile1, int tile2, int slideX1, int slideY1, int slideX2, int slideY2)
Move::Move(TilePos const & tilePos1, TilePos const & tilePos2, int tile1, int tile2, Slide const & slide)
: m_tilePos1(tilePos1)
, m_tilePos2(tilePos2)
, m_tile1(tile1)
, m_tile2(tile2)
, m_hasSlide(true)
, m_slideX1(slideX1)
, m_slideY1(slideY1)
, m_slideX2(slideX2)
, m_slideY2(slideY2)
, m_slide(slide)
{
}
......@@ -82,24 +76,29 @@ bool Move::hasSlide() const
return m_hasSlide;
}
Slide Move::slide() const
{
return m_slide;
}
int Move::slideX1() const
{
return m_slideX1;
return m_slide.front().x();
}
int Move::slideY1() const
{
return m_slideY1;
return m_slide.front().y();
}
int Move::slideX2() const
{
return m_slideX2;
return m_slide.back().x();
}
int Move::slideY2() const
{
return m_slideY2;
return m_slide.back().y();
}
void Move::swapTiles()
......
......@@ -32,7 +32,7 @@ class Move
{
public:
Move(TilePos const & tilePos1, TilePos const & tilePos2, int tile1, int tile2);
Move(TilePos const & tilePos1, TilePos const & tilePos2, int tile1, int tile2, int slideX1, int slideY1, int slideX2, int slideY2);
Move(TilePos const & tilePos1, TilePos const & tilePos2, int tile1, int tile2, Slide const & slide);
int x1() const;
int y1() const;
......@@ -41,6 +41,7 @@ public:
int tile1() const;
int tile2() const;
bool hasSlide() const;
Slide slide() const;
int slideX1() const;
int slideY1() const;
int slideX2() const;
......@@ -59,10 +60,7 @@ private:
int m_tile1; ///< type of tile at first set of coordinates
int m_tile2; ///< type of tile at second set of coordinates
bool m_hasSlide; ///< if we performed a slide during the move
int m_slideX1; ///< original x coordinate of the last slided tile
int m_slideY1; ///< original y coordinate of the last slided tile
int m_slideX2; ///< final x coordinate of the last slided tile
int m_slideY2; ///< final y coordinate of the last slided tile
Slide m_slide; ///< original x coordinate of the last slided tile
};
}
......
......@@ -64,27 +64,33 @@ void MoveTest::hasSlide() const
QCOMPARE(m_moveWithSlide.hasSlide(), true);
}
void MoveTest::slide() const
{
QCOMPARE(m_moveWithoutSlide.slide(), KShisen::Slide());
QCOMPARE(m_moveWithSlide.slide(), KShisen::Slide() << m_slidePos1 << m_slidePos2);
}
void MoveTest::slideX1() const
{
QCOMPARE(m_moveWithoutSlide.slideX1(), -1);
QCOMPARE(m_moveWithoutSlide.slideX1(), 0);
QCOMPARE(m_moveWithSlide.slideX1(), 15);
}
void MoveTest::slideY1() const
{
QCOMPARE(m_moveWithoutSlide.slideY1(), -1);
QCOMPARE(m_moveWithoutSlide.slideY1(), 0);
QCOMPARE(m_moveWithSlide.slideY1(), 16);
}
void MoveTest::slideX2() const
{
QCOMPARE(m_moveWithoutSlide.slideX2(), -1);
QCOMPARE(m_moveWithoutSlide.slideX2(), 0);
QCOMPARE(m_moveWithSlide.slideX2(), 17);
}
void MoveTest::slideY2() const
{
QCOMPARE(m_moveWithoutSlide.slideY2(), -1);
QCOMPARE(m_moveWithoutSlide.slideY2(), 0);
QCOMPARE(m_moveWithSlide.slideY2(), 18);
}
......
......@@ -37,6 +37,7 @@ private slots:
void tile1() const;
void tile2() const;
void hasSlide() const;
void slide() const;
void slideX1() const;
void slideY1() const;
void slideX2() const;
......@@ -50,8 +51,10 @@ private:
KShisen::TilePos m_tile2{3, 4};
KShisen::TilePos m_tile3{5, 6};
KShisen::TilePos m_tile4{7, 8};
KShisen::TilePos m_slidePos1{15, 16};
KShisen::TilePos m_slidePos2{17, 18};
KShisen::Move m_moveWithoutSlide{m_tile1, m_tile2, 12, 34};
KShisen::Move m_moveWithSlide{m_tile3, m_tile4, 56, 78, 15, 16, 17, 18};
KShisen::Move m_moveWithSlide{m_tile3, m_tile4, 56, 78, KShisen::Slide() << m_slidePos1 << m_slidePos2};
};
#endif // MOVETEST_H
......@@ -30,6 +30,8 @@ using TilePos = QPoint;
* A list of positions (at least 2) makes a Path
*/
using Path = QList<TilePos>;
using Slide = QList<TilePos>;
}
#endif // KSHISEN_TYPES_H
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment