2 // gameboard.cpp: Game board class implementation
5 // © 2014 Underground Software
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 03/01/2014 Created this file
14 #include "gameboard.h"
16 //#include <stdio.h> // for printf()
20 GameBoard::GameBoard(int boardNumber)
23 if (boardNumber > NUMBER_OF_BOARDS)
24 boardNumber = NUMBER_OF_BOARDS;
26 // boardNumber is 1-based
27 Board * boardToUse = (Board *)boards[boardNumber - 1];
29 width = boardToUse->width;
30 height = boardToUse->height;
31 boardLength = width * height;
32 board = new char[boardLength];
33 initialBoard = new char[boardLength];
34 // initialX = boardToUse->x;
35 // initialY = boardToUse->y;
37 for(int i=0; i<boardLength; i++)
39 char c = boardToUse->state[i];
42 initialBoard[i] = GTWall;
44 initialBoard[i] = GTSpace;
46 initialBoard[i] = GTBox;
48 initialBoard[i] = GTBoxSpot;
50 initialBoard[i] = GTBox | GTBoxSpot;
53 initialBoard[i] = GTSpace;
54 initialX = i % width, initialY = i / width;
62 GameBoard::~GameBoard()
68 delete[] initialBoard;
72 bool GameBoard::GameHasBeenWon(void)
74 // Check to see if all boxes are on their spots
75 for(int i=0; i<boardLength; i++)
77 char cell = board[i] & ~GTBoxSpot;
79 if ((cell == GTBox) && !(board[i] & GTBoxSpot))
87 void GameBoard::ResetGame(void)
89 memcpy(board, initialBoard, boardLength);
90 playerX = initialX, playerY = initialY;
95 int GameBoard::MovePlayerN(void)
97 // Sanity check (player trying to move into wall)
101 char cell1 = board[playerX + ((playerY - 1) * width)] & ~GTBoxSpot;
102 char cell2 = board[playerX + ((playerY - 2) * width)] & ~GTBoxSpot;
104 return Move(0, -1, cell1, cell2);
108 int GameBoard::MovePlayerS(void)
110 // Sanity check (player trying to move into wall)
111 if (playerY == (height - 2))
114 char cell1 = board[playerX + ((playerY + 1) * width)] & ~GTBoxSpot;
115 char cell2 = board[playerX + ((playerY + 2) * width)] & ~GTBoxSpot;
117 return Move(0, +1, cell1, cell2);
121 int GameBoard::MovePlayerE(void)
123 // Sanity check (player trying to move into wall)
124 if (playerX == (width - 2))
127 char cell1 = board[(playerX + 1) + (playerY * width)] & ~GTBoxSpot;
128 char cell2 = board[(playerX + 2) + (playerY * width)] & ~GTBoxSpot;
130 return Move(+1, 0, cell1, cell2);
134 int GameBoard::MovePlayerW(void)
136 // Sanity check (player trying to move into wall)
140 char cell1 = board[(playerX - 1) + (playerY * width)] & ~GTBoxSpot;
141 char cell2 = board[(playerX - 2) + (playerY * width)] & ~GTBoxSpot;
143 return Move(-1, 0, cell1, cell2);
147 int GameBoard::Move(int dx, int dy, char cell1, char cell2)
149 // Player is moving into an unoccupied space...
150 if (cell1 == GTSpace)
153 playerX += dx, playerY += dy;
154 undo[numMoves].dx = dx, undo[numMoves].dy = dy, undo[numMoves].type = PMWalk;
158 // Player is pushing a box into an unoccupied space...
159 else if ((cell1 == GTBox) && (cell2 == GTSpace))
162 playerX += dx, playerY += dy;
163 board[playerX + (playerY * width)] &= GTBoxSpot;
165 playerX += dx, playerY += dy;
166 board[playerX + (playerY * width)] |= GTBox;
168 playerX -= dx, playerY -= dy;
169 undo[numMoves].dx = dx, undo[numMoves].dy = dy, undo[numMoves].type = PMPush;
174 // All other combinations are no good
179 bool GameBoard::IsBoxNOfPlayer(void)
181 if ((board[playerX + ((playerY - 1) * width)] & ~GTBoxSpot) == GTBox)
188 bool GameBoard::IsBoxSOfPlayer(void)
190 if ((board[playerX + ((playerY + 1) * width)] & ~GTBoxSpot) == GTBox)
197 bool GameBoard::IsBoxEOfPlayer(void)
199 if ((board[(playerX + 1) + (playerY * width)] & ~GTBoxSpot) == GTBox)
206 bool GameBoard::IsBoxWOfPlayer(void)
208 if ((board[(playerX - 1) + (playerY * width)] & ~GTBoxSpot) == GTBox)
215 bool GameBoard::UndoLastMove(int & dx, int & dy, int & type)
221 dx = undo[numMoves].dx;
222 dy = undo[numMoves].dy;
223 type = undo[numMoves].type;
225 // Undo the box's move (if any)
228 int boxPosition = (playerX + dx) + ((playerY + dy) * width);
229 int newBoxPosition = playerX + (playerY * width);
231 board[boxPosition] &= GTBoxSpot;
232 // board[newBoxPosition] &= GTBoxSpot; // This is extraneous
233 board[newBoxPosition] |= GTBox;
236 // Undo the player's move