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;
94 int GameBoard::MovePlayerN(void)
96 // Sanity check (player trying to move into wall)
100 char cell1 = board[playerX + ((playerY - 1) * width)] & ~GTBoxSpot;
101 char cell2 = board[playerX + ((playerY - 2) * width)] & ~GTBoxSpot;
103 return Move(playerY, -1, cell1, cell2);
107 int GameBoard::MovePlayerS(void)
109 // Sanity check (player trying to move into wall)
110 if (playerY == (height - 2))
113 char cell1 = board[playerX + ((playerY + 1) * width)] & ~GTBoxSpot;
114 char cell2 = board[playerX + ((playerY + 2) * width)] & ~GTBoxSpot;
116 return Move(playerY, +1, cell1, cell2);
120 int GameBoard::MovePlayerE(void)
122 // Sanity check (player trying to move into wall)
123 if (playerX == (width - 2))
126 char cell1 = board[(playerX + 1) + (playerY * width)] & ~GTBoxSpot;
127 char cell2 = board[(playerX + 2) + (playerY * width)] & ~GTBoxSpot;
129 return Move(playerX, +1, cell1, cell2);
133 int GameBoard::MovePlayerW(void)
135 // Sanity check (player trying to move into wall)
139 char cell1 = board[(playerX - 1) + (playerY * width)] & ~GTBoxSpot;
140 char cell2 = board[(playerX - 2) + (playerY * width)] & ~GTBoxSpot;
142 return Move(playerX, -1, cell1, cell2);
146 int GameBoard::Move(int & var, int direction, char cell1, char cell2)
148 // Player is moving into an unoccupied space...
149 if (cell1 == GTSpace)
154 // Player is pushing a box into an unoccupied space...
155 else if ((cell1 == GTBox) && (cell2 == GTSpace))
158 board[playerX + (playerY * width)] &= GTBoxSpot;
160 board[playerX + (playerY * width)] |= GTBox;
165 // All other combinations are no good
170 bool GameBoard::IsBoxNOfPlayer(void)
172 if ((board[playerX + ((playerY - 1) * width)] & ~GTBoxSpot) == GTBox)
179 bool GameBoard::IsBoxSOfPlayer(void)
181 if ((board[playerX + ((playerY + 1) * width)] & ~GTBoxSpot) == GTBox)
188 bool GameBoard::IsBoxEOfPlayer(void)
190 if ((board[(playerX + 1) + (playerY * width)] & ~GTBoxSpot) == GTBox)
197 bool GameBoard::IsBoxWOfPlayer(void)
199 if ((board[(playerX - 1) + (playerY * width)] & ~GTBoxSpot) == GTBox)