]> Shamusworld >> Repos - warehouse-man-deluxe/blobdiff - src/gameboard.cpp
Added MOAR boards.
[warehouse-man-deluxe] / src / gameboard.cpp
index 0c4a89ec148a0e3f65dbb7a8eadf990a5e4479ea..e39d2e56b22e2f202326c6e73915d9bc7eb2d5e7 100644 (file)
@@ -31,8 +31,7 @@ GameBoard::GameBoard(int boardNumber)
        boardLength = width * height;
        board = new char[boardLength];
        initialBoard = new char[boardLength];
-//     initialX = boardToUse->x;
-//     initialY = boardToUse->y;
+       name = (const char *)&(boardToUse->state[boardLength]);
 
        for(int i=0; i<boardLength; i++)
        {
@@ -48,11 +47,13 @@ GameBoard::GameBoard(int boardNumber)
                        initialBoard[i] = GTBoxSpot;
                else if (c == '+')
                        initialBoard[i] = GTBox | GTBoxSpot;
-               else if (c == 'o')
+               else if (c == 'o' || c == '*')
                {
-                       initialBoard[i] = GTSpace;
+                       initialBoard[i] = (c == '*' ? GTBoxSpot : GTSpace);
                        initialX = i % width, initialY = i / width;
                }
+               else
+                       initialBoard[i] = GTNull;
        }
 
        ResetGame();
@@ -88,6 +89,7 @@ void GameBoard::ResetGame(void)
 {
        memcpy(board, initialBoard, boardLength);
        playerX = initialX, playerY = initialY;
+       numMoves = 0;
 }
 
 
@@ -100,7 +102,7 @@ int GameBoard::MovePlayerN(void)
        char cell1 = board[playerX + ((playerY - 1) * width)] & ~GTBoxSpot;
        char cell2 = board[playerX + ((playerY - 2) * width)] & ~GTBoxSpot;
 
-       return Move(playerY, -1, cell1, cell2);
+       return Move(0, -1, cell1, cell2);
 }
 
 
@@ -113,7 +115,7 @@ int GameBoard::MovePlayerS(void)
        char cell1 = board[playerX + ((playerY + 1) * width)] & ~GTBoxSpot;
        char cell2 = board[playerX + ((playerY + 2) * width)] & ~GTBoxSpot;
 
-       return Move(playerY, +1, cell1, cell2);
+       return Move(0, +1, cell1, cell2);
 }
 
 
@@ -126,7 +128,7 @@ int GameBoard::MovePlayerE(void)
        char cell1 = board[(playerX + 1) + (playerY * width)] & ~GTBoxSpot;
        char cell2 = board[(playerX + 2) + (playerY * width)] & ~GTBoxSpot;
 
-       return Move(playerX, +1, cell1, cell2);
+       return Move(+1, 0, cell1, cell2);
 }
 
 
@@ -139,26 +141,30 @@ int GameBoard::MovePlayerW(void)
        char cell1 = board[(playerX - 1) + (playerY * width)] & ~GTBoxSpot;
        char cell2 = board[(playerX - 2) + (playerY * width)] & ~GTBoxSpot;
 
-       return Move(playerX, -1, cell1, cell2);
+       return Move(-1, 0, cell1, cell2);
 }
 
 
-int GameBoard::Move(int & var, int direction, char cell1, char cell2)
+int GameBoard::Move(int dx, int dy, char cell1, char cell2)
 {
        // Player is moving into an unoccupied space...
        if (cell1 == GTSpace)
        {
-               var += direction;
+               playerX += dx, playerY += dy;
+               undo[numMoves].dx = dx, undo[numMoves].dy = dy, undo[numMoves].type = PMWalk;
+               numMoves++;
                return PMWalk;
        }
        // Player is pushing a box into an unoccupied space...
        else if ((cell1 == GTBox) && (cell2 == GTSpace))
        {
-               var += direction;
+               playerX += dx, playerY += dy;
                board[playerX + (playerY * width)] &= GTBoxSpot;
-               var += direction;
+               playerX += dx, playerY += dy;
                board[playerX + (playerY * width)] |= GTBox;
-               var -= direction;
+               playerX -= dx, playerY -= dy;
+               undo[numMoves].dx = dx, undo[numMoves].dy = dy, undo[numMoves].type = PMPush;
+               numMoves++;
                return PMPush;
        }
 
@@ -166,3 +172,68 @@ int GameBoard::Move(int & var, int direction, char cell1, char cell2)
        return PMInvalid;
 }
 
+
+bool GameBoard::IsBoxNOfPlayer(void)
+{
+       if ((board[playerX + ((playerY - 1) * width)] & ~GTBoxSpot) == GTBox)
+               return true;
+
+       return false;
+}
+
+
+bool GameBoard::IsBoxSOfPlayer(void)
+{
+       if ((board[playerX + ((playerY + 1) * width)] & ~GTBoxSpot) == GTBox)
+               return true;
+
+       return false;
+}
+
+
+bool GameBoard::IsBoxEOfPlayer(void)
+{
+       if ((board[(playerX + 1) + (playerY * width)] & ~GTBoxSpot) == GTBox)
+               return true;
+
+       return false;
+}
+
+
+bool GameBoard::IsBoxWOfPlayer(void)
+{
+       if ((board[(playerX - 1) + (playerY * width)] & ~GTBoxSpot) == GTBox)
+               return true;
+
+       return false;
+}
+
+
+bool GameBoard::UndoLastMove(int & dx, int & dy, int & type)
+{
+       if (numMoves == 0)
+               return false;
+
+       numMoves--;
+       dx = undo[numMoves].dx;
+       dy = undo[numMoves].dy;
+       type = undo[numMoves].type;
+
+       // Undo the box's move (if any)
+       if (type == PMPush)
+       {
+               int boxPosition = (playerX + dx) + ((playerY + dy) * width);
+               int newBoxPosition = playerX + (playerY * width);
+
+               board[boxPosition] &= GTBoxSpot;
+//             board[newBoxPosition] &= GTBoxSpot;     // This is extraneous
+               board[newBoxPosition] |= GTBox;
+       }
+
+       // Undo the player's move
+       playerX += -dx;
+       playerY += -dy;
+
+       return true;
+}
+