Mert Us Matthew Hannay Logan Starr

Dependencies:   mbed 4DGL-uLCD-SE

Committer:
mhannay3
Date:
Fri Dec 02 22:47:38 2022 +0000
Revision:
12:7ef20deb9d5e
Parent:
10:8f73a917b239
Parent:
11:43c89579ac52
Child:
13:3c6da0590428
merge

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mhannay3 5:b553c51b3b85 1 #include "mbed.h"
mhannay3 5:b553c51b3b85 2 #include "uLCD_4DGL.h"
mhannay3 6:1c4dd5e24b8d 3 #include <vector>
mhannay3 1:cd78922f70fa 4
mhannay3 5:b553c51b3b85 5 uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin;
mhannay3 5:b553c51b3b85 6
mhannay3 5:b553c51b3b85 7 enum Piece {e, wK, bK, wQ, bQ, wR, bR, wB, bB, wN, bN, w, b};
mhannay3 9:1f33c0f299ae 8 std::vector<Piece> whitePieces;
mhannay3 9:1f33c0f299ae 9 std::vector<Piece> blackPieces;
mhannay3 1:cd78922f70fa 10
mhannay3 9:1f33c0f299ae 11 enum GameState {whiteSelecting, whitePickedUp, whiteAI, blackSelecting, blackPickedUp, blackAI};
mhannay3 7:9e01c9a334dc 12
mhannay3 6:1c4dd5e24b8d 13 struct pixelCoord {
mhannay3 1:cd78922f70fa 14 uint8_t x;
mhannay3 1:cd78922f70fa 15 uint8_t y;
mhannay3 5:b553c51b3b85 16 };
mhannay3 1:cd78922f70fa 17
mhannay3 6:1c4dd5e24b8d 18 struct boardPos {
mhannay3 6:1c4dd5e24b8d 19 uint8_t row;
mhannay3 6:1c4dd5e24b8d 20 uint8_t column;
mhannay3 9:1f33c0f299ae 21
mhannay3 9:1f33c0f299ae 22 bool operator==(const boardPos &other) const
mhannay3 9:1f33c0f299ae 23 {
mus3 11:43c89579ac52 24 return row == other.row && column == other.column;
mhannay3 9:1f33c0f299ae 25 }
mhannay3 6:1c4dd5e24b8d 26 };
mhannay3 6:1c4dd5e24b8d 27
mus3 11:43c89579ac52 28 class Nav_Switch
mus3 11:43c89579ac52 29 {
mus3 11:43c89579ac52 30 public:
mus3 11:43c89579ac52 31 Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire);
mus3 11:43c89579ac52 32 int read();
mus3 11:43c89579ac52 33 //boolean functions to test each switch
mus3 11:43c89579ac52 34 bool up();
mus3 11:43c89579ac52 35 bool down();
mus3 11:43c89579ac52 36 bool left();
mus3 11:43c89579ac52 37 bool right();
mus3 11:43c89579ac52 38 bool fire();
mus3 11:43c89579ac52 39 //automatic read on RHS
mus3 11:43c89579ac52 40 operator int ();
mus3 11:43c89579ac52 41 //index to any switch array style
mus3 11:43c89579ac52 42 bool operator[](int index) {
mus3 11:43c89579ac52 43 return _pins[index];
mus3 11:43c89579ac52 44 };
mus3 11:43c89579ac52 45 private:
mus3 11:43c89579ac52 46 BusIn _pins;
mus3 11:43c89579ac52 47
mus3 11:43c89579ac52 48 };
mus3 11:43c89579ac52 49 Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire):
mus3 11:43c89579ac52 50 _pins(up, down, left, right, fire)
mus3 11:43c89579ac52 51 {
mus3 11:43c89579ac52 52 _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise
mus3 11:43c89579ac52 53 wait(0.001); //delays just a bit for pullups to pull inputs high
mus3 11:43c89579ac52 54 }
mus3 11:43c89579ac52 55 inline bool Nav_Switch::up()
mus3 11:43c89579ac52 56 {
mus3 11:43c89579ac52 57 return !(_pins[0]);
mus3 11:43c89579ac52 58 }
mus3 11:43c89579ac52 59 inline bool Nav_Switch::down()
mus3 11:43c89579ac52 60 {
mus3 11:43c89579ac52 61 return !(_pins[1]);
mus3 11:43c89579ac52 62 }
mus3 11:43c89579ac52 63 inline bool Nav_Switch::left()
mus3 11:43c89579ac52 64 {
mus3 11:43c89579ac52 65 return !(_pins[2]);
mus3 11:43c89579ac52 66 }
mus3 11:43c89579ac52 67 inline bool Nav_Switch::right()
mus3 11:43c89579ac52 68 {
mus3 11:43c89579ac52 69 return !(_pins[3]);
mus3 11:43c89579ac52 70 }
mus3 11:43c89579ac52 71 inline bool Nav_Switch::fire()
mus3 11:43c89579ac52 72 {
mus3 11:43c89579ac52 73 return !(_pins[4]);
mus3 11:43c89579ac52 74 }
mus3 11:43c89579ac52 75 inline int Nav_Switch::read()
mus3 11:43c89579ac52 76 {
mus3 11:43c89579ac52 77 return _pins.read();
mus3 11:43c89579ac52 78 }
mus3 11:43c89579ac52 79 inline Nav_Switch::operator int ()
mus3 11:43c89579ac52 80 {
mus3 11:43c89579ac52 81 return _pins.read();
mus3 11:43c89579ac52 82 }
mus3 11:43c89579ac52 83
mhannay3 5:b553c51b3b85 84 class BoardState
mhannay3 5:b553c51b3b85 85 {
mhannay3 5:b553c51b3b85 86 private:
mhannay3 5:b553c51b3b85 87 Piece array[64];
mhannay3 5:b553c51b3b85 88 public:
mhannay3 4:5798e4062350 89 // calculates the advantage difference for the board state
mhannay3 5:b553c51b3b85 90 float calculateBoardState()
mhannay3 5:b553c51b3b85 91 {
mhannay3 4:5798e4062350 92 return 0.0;
mhannay3 4:5798e4062350 93 }
mhannay3 5:b553c51b3b85 94
mhannay3 4:5798e4062350 95 // returns the piece at a given location
mhannay3 5:b553c51b3b85 96 Piece getPiece(int row, int column)
mhannay3 5:b553c51b3b85 97 {
mhannay3 5:b553c51b3b85 98 return array[column + 8 * row];
mhannay3 4:5798e4062350 99 }
mhannay3 4:5798e4062350 100
mhannay3 5:b553c51b3b85 101 // puts a piece at a given location
mhannay3 5:b553c51b3b85 102 void placePiece(Piece piece, int row, int column)
mhannay3 5:b553c51b3b85 103 {
mhannay3 5:b553c51b3b85 104 array[column + 8 * row] = piece;
mhannay3 4:5798e4062350 105 }
mhannay3 4:5798e4062350 106
mhannay3 4:5798e4062350 107 /* removes a piece from the set position of the board
mhannay3 4:5798e4062350 108 returns the bit representation of the piece
mhannay3 4:5798e4062350 109 */
mhannay3 5:b553c51b3b85 110 Piece removePiece(int row, int column)
mhannay3 5:b553c51b3b85 111 {
mhannay3 5:b553c51b3b85 112 Piece removedPiece = array[column + 8 * row];
mhannay3 5:b553c51b3b85 113 array[column + 8 * row] = e;
mhannay3 4:5798e4062350 114 return removedPiece;
mhannay3 1:cd78922f70fa 115 }
mhannay3 1:cd78922f70fa 116
mhannay3 1:cd78922f70fa 117 /* moves a piece from one position to another
mhannay3 1:cd78922f70fa 118 returns the captured piece
mhannay3 1:cd78922f70fa 119 */
mhannay3 5:b553c51b3b85 120 Piece movePiece(int startRow, int startColumn, int endRow, int endColumn)
mhannay3 5:b553c51b3b85 121 {
mhannay3 5:b553c51b3b85 122 Piece movingPiece = removePiece(startRow, startColumn);
mhannay3 5:b553c51b3b85 123 Piece capturedPiece = removePiece(endRow, endColumn);
mhannay3 5:b553c51b3b85 124 placePiece(movingPiece, endRow, endColumn);
mhannay3 1:cd78922f70fa 125 return capturedPiece;
mhannay3 1:cd78922f70fa 126 }
mhannay3 6:1c4dd5e24b8d 127
mhannay3 6:1c4dd5e24b8d 128 // generates a list of possible moves for a piece
mhannay3 6:1c4dd5e24b8d 129 // returns moves
mhannay3 9:1f33c0f299ae 130 std::vector<boardPos> getMoves(boardPos pos)
mhannay3 9:1f33c0f299ae 131 {
mhannay3 9:1f33c0f299ae 132 return getMoves(pos.row, pos.column);
mhannay3 9:1f33c0f299ae 133 }
mhannay3 6:1c4dd5e24b8d 134 std::vector<boardPos> getMoves(int row, int column)
mhannay3 6:1c4dd5e24b8d 135 {
mhannay3 6:1c4dd5e24b8d 136 std::vector<boardPos> moves;
mhannay3 8:928d5d33258f 137 std::vector<boardPos> lineMoves;
mhannay3 6:1c4dd5e24b8d 138 Piece movingPiece = getPiece(row, column);
mus3 11:43c89579ac52 139 uint8_t rowIndex;
mus3 11:43c89579ac52 140 uint8_t columnIndex;
mhannay3 6:1c4dd5e24b8d 141 bool isWhite;
mhannay3 6:1c4dd5e24b8d 142 switch(movingPiece) {
mhannay3 6:1c4dd5e24b8d 143 case wK:
mhannay3 6:1c4dd5e24b8d 144 case bK:
mhannay3 6:1c4dd5e24b8d 145 isWhite = movingPiece == wK;
mus3 11:43c89579ac52 146 if (validMove(isWhite, row + 1, column)) {
mus3 11:43c89579ac52 147 moves.push_back((boardPos) {
mus3 11:43c89579ac52 148 row + 1, column
mus3 11:43c89579ac52 149 });
mus3 11:43c89579ac52 150 }
mus3 11:43c89579ac52 151 if (validMove(isWhite, row, column + 1)) {
mus3 11:43c89579ac52 152 moves.push_back((boardPos) {
mus3 11:43c89579ac52 153 row, column + 1
mus3 11:43c89579ac52 154 });
mus3 11:43c89579ac52 155 }
mus3 11:43c89579ac52 156 if (validMove(isWhite, row - 1, column)) {
mus3 11:43c89579ac52 157 moves.push_back((boardPos) {
mus3 11:43c89579ac52 158 row - 1, column
mus3 11:43c89579ac52 159 });
mus3 11:43c89579ac52 160 }
mus3 11:43c89579ac52 161 if (validMove(isWhite, row, column - 1)) {
mus3 11:43c89579ac52 162 moves.push_back((boardPos) {
mus3 11:43c89579ac52 163 row, column - 1
mus3 11:43c89579ac52 164 });
mus3 11:43c89579ac52 165 }
mus3 11:43c89579ac52 166 if (validMove(isWhite, row + 1, column + 1)) {
mus3 11:43c89579ac52 167 moves.push_back((boardPos) {
mus3 11:43c89579ac52 168 row + 1, column + 1
mus3 11:43c89579ac52 169 });
mus3 11:43c89579ac52 170 }
mus3 11:43c89579ac52 171 if (validMove(isWhite, row - 1, column + 1)) {
mus3 11:43c89579ac52 172 moves.push_back((boardPos) {
mus3 11:43c89579ac52 173 row - 1, column + 1
mus3 11:43c89579ac52 174 });
mus3 11:43c89579ac52 175 }
mus3 11:43c89579ac52 176 if (validMove(isWhite, row - 1, column - 1)) {
mus3 11:43c89579ac52 177 moves.push_back((boardPos) {
mus3 11:43c89579ac52 178 row - 1, column - 1
mus3 11:43c89579ac52 179 });
mus3 11:43c89579ac52 180 }
mus3 11:43c89579ac52 181 if (validMove(isWhite, row + 1, column - 1)) {
mus3 11:43c89579ac52 182 moves.push_back((boardPos) {
mus3 11:43c89579ac52 183 row + 1, column - 1
mus3 11:43c89579ac52 184 });
mhannay3 6:1c4dd5e24b8d 185 }
mhannay3 6:1c4dd5e24b8d 186 break;
mhannay3 6:1c4dd5e24b8d 187 case wQ:
mhannay3 6:1c4dd5e24b8d 188 case bQ:
mhannay3 6:1c4dd5e24b8d 189 isWhite = movingPiece == wQ;
mus3 11:43c89579ac52 190 rowIndex = row + 1;
mus3 11:43c89579ac52 191 columnIndex = column + 1;
mus3 11:43c89579ac52 192 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 193 moves.push_back((boardPos) {
mus3 11:43c89579ac52 194 rowIndex, columnIndex
mus3 11:43c89579ac52 195 });
mus3 11:43c89579ac52 196 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 197 break;
mus3 11:43c89579ac52 198 }
mus3 11:43c89579ac52 199 rowIndex++;
mus3 11:43c89579ac52 200 columnIndex++;
mus3 11:43c89579ac52 201 }
mus3 11:43c89579ac52 202 rowIndex = row - 1;
mus3 11:43c89579ac52 203 columnIndex = column + 1;
mus3 11:43c89579ac52 204 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 205 moves.push_back((boardPos) {
mus3 11:43c89579ac52 206 rowIndex, columnIndex
mus3 11:43c89579ac52 207 });
mus3 11:43c89579ac52 208 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 209 break;
mus3 11:43c89579ac52 210 }
mus3 11:43c89579ac52 211 rowIndex--;
mus3 11:43c89579ac52 212 columnIndex++;
mus3 11:43c89579ac52 213 }
mus3 11:43c89579ac52 214 rowIndex = row + 1;
mus3 11:43c89579ac52 215 columnIndex = column - 1;
mus3 11:43c89579ac52 216 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 217 moves.push_back((boardPos) {
mus3 11:43c89579ac52 218 rowIndex, columnIndex
mus3 11:43c89579ac52 219 });
mus3 11:43c89579ac52 220 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 221 break;
mus3 11:43c89579ac52 222 }
mus3 11:43c89579ac52 223 rowIndex++;
mus3 11:43c89579ac52 224 columnIndex--;
mus3 11:43c89579ac52 225 }
mus3 11:43c89579ac52 226 rowIndex = row - 1;
mus3 11:43c89579ac52 227 columnIndex = column - 1;
mus3 11:43c89579ac52 228 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 229 moves.push_back((boardPos) {
mus3 11:43c89579ac52 230 rowIndex, columnIndex
mus3 11:43c89579ac52 231 });
mus3 11:43c89579ac52 232 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 233 break;
mus3 11:43c89579ac52 234 }
mus3 11:43c89579ac52 235 rowIndex--;
mus3 11:43c89579ac52 236 columnIndex--;
mus3 11:43c89579ac52 237 }
mus3 11:43c89579ac52 238 rowIndex = row + 1;
mus3 11:43c89579ac52 239 columnIndex = column;
mus3 11:43c89579ac52 240 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 241 moves.push_back((boardPos) {
mus3 11:43c89579ac52 242 rowIndex, columnIndex
mus3 11:43c89579ac52 243 });
mus3 11:43c89579ac52 244 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 245 break;
mus3 11:43c89579ac52 246 }
mus3 11:43c89579ac52 247 rowIndex++;
mus3 11:43c89579ac52 248 }
mus3 11:43c89579ac52 249 rowIndex = row - 1;
mus3 11:43c89579ac52 250 columnIndex = column;
mus3 11:43c89579ac52 251 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 252 moves.push_back((boardPos) {
mus3 11:43c89579ac52 253 rowIndex, columnIndex
mus3 11:43c89579ac52 254 });
mus3 11:43c89579ac52 255 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 256 break;
mus3 11:43c89579ac52 257 }
mus3 11:43c89579ac52 258 rowIndex--;
mus3 11:43c89579ac52 259 }
mus3 11:43c89579ac52 260 rowIndex = row;
mus3 11:43c89579ac52 261 columnIndex = column + 1;
mus3 11:43c89579ac52 262 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 263 moves.push_back((boardPos) {
mus3 11:43c89579ac52 264 rowIndex, columnIndex
mus3 11:43c89579ac52 265 });
mus3 11:43c89579ac52 266 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 267 break;
mus3 11:43c89579ac52 268 }
mus3 11:43c89579ac52 269 columnIndex++;
mus3 11:43c89579ac52 270 }
mus3 11:43c89579ac52 271 rowIndex = row;
mus3 11:43c89579ac52 272 columnIndex = column - 1;
mus3 11:43c89579ac52 273 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 274 moves.push_back((boardPos) {
mus3 11:43c89579ac52 275 rowIndex, columnIndex
mus3 11:43c89579ac52 276 });
mus3 11:43c89579ac52 277 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 278 break;
mus3 11:43c89579ac52 279 }
mus3 11:43c89579ac52 280 columnIndex--;
mus3 11:43c89579ac52 281 }
mhannay3 6:1c4dd5e24b8d 282 break;
mhannay3 6:1c4dd5e24b8d 283 case wB:
mhannay3 6:1c4dd5e24b8d 284 case bB:
mhannay3 6:1c4dd5e24b8d 285 isWhite = movingPiece == wB;
mus3 11:43c89579ac52 286 rowIndex = row + 1;
mus3 11:43c89579ac52 287 columnIndex = column + 1;
mus3 11:43c89579ac52 288 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 289 moves.push_back((boardPos) {
mus3 11:43c89579ac52 290 rowIndex, columnIndex
mus3 11:43c89579ac52 291 });
mus3 11:43c89579ac52 292 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 293 break;
mus3 11:43c89579ac52 294 }
mus3 11:43c89579ac52 295 rowIndex++;
mus3 11:43c89579ac52 296 columnIndex++;
mus3 11:43c89579ac52 297 }
mus3 11:43c89579ac52 298 rowIndex = row - 1;
mus3 11:43c89579ac52 299 columnIndex = column + 1;
mus3 11:43c89579ac52 300 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 301 moves.push_back((boardPos) {
mus3 11:43c89579ac52 302 rowIndex, columnIndex
mus3 11:43c89579ac52 303 });
mus3 11:43c89579ac52 304 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 305 break;
mus3 11:43c89579ac52 306 }
mus3 11:43c89579ac52 307 rowIndex--;
mus3 11:43c89579ac52 308 columnIndex++;
mus3 11:43c89579ac52 309 }
mus3 11:43c89579ac52 310 rowIndex = row + 1;
mus3 11:43c89579ac52 311 columnIndex = column - 1;
mus3 11:43c89579ac52 312 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 313 moves.push_back((boardPos) {
mus3 11:43c89579ac52 314 rowIndex, columnIndex
mus3 11:43c89579ac52 315 });
mus3 11:43c89579ac52 316 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 317 break;
mus3 11:43c89579ac52 318 }
mus3 11:43c89579ac52 319 rowIndex++;
mus3 11:43c89579ac52 320 columnIndex--;
mus3 11:43c89579ac52 321 }
mus3 11:43c89579ac52 322 rowIndex = row - 1;
mus3 11:43c89579ac52 323 columnIndex = column - 1;
mus3 11:43c89579ac52 324 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 325 moves.push_back((boardPos) {
mus3 11:43c89579ac52 326 rowIndex, columnIndex
mus3 11:43c89579ac52 327 });
mus3 11:43c89579ac52 328 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 329 break;
mus3 11:43c89579ac52 330 }
mus3 11:43c89579ac52 331 rowIndex--;
mus3 11:43c89579ac52 332 columnIndex--;
mus3 11:43c89579ac52 333 }
mhannay3 6:1c4dd5e24b8d 334 break;
mhannay3 6:1c4dd5e24b8d 335 case wN:
mhannay3 6:1c4dd5e24b8d 336 case bN:
mhannay3 6:1c4dd5e24b8d 337 isWhite = movingPiece == wN;
mus3 11:43c89579ac52 338 if (validMove(isWhite, row + 2, column + 1)) {
mus3 11:43c89579ac52 339 moves.push_back((boardPos) {
mus3 11:43c89579ac52 340 row + 2, column + 1
mus3 11:43c89579ac52 341 });
mus3 11:43c89579ac52 342 }
mus3 11:43c89579ac52 343 if (validMove(isWhite, row + 2, column - 1)) {
mus3 11:43c89579ac52 344 moves.push_back((boardPos) {
mus3 11:43c89579ac52 345 row + 2, column - 1
mus3 11:43c89579ac52 346 });
mus3 11:43c89579ac52 347 }
mus3 11:43c89579ac52 348 if (validMove(isWhite, row - 2, column - 1)) {
mus3 11:43c89579ac52 349 moves.push_back((boardPos) {
mus3 11:43c89579ac52 350 row - 2, column - 1
mus3 11:43c89579ac52 351 });
mus3 11:43c89579ac52 352 }
mus3 11:43c89579ac52 353 if (validMove(isWhite, row - 2, column + 1)) {
mus3 11:43c89579ac52 354 moves.push_back((boardPos) {
mus3 11:43c89579ac52 355 row - 2, column + 1
mus3 11:43c89579ac52 356 });
mus3 11:43c89579ac52 357 }
mus3 11:43c89579ac52 358 if (validMove(isWhite, row + 1, column + 2)) {
mus3 11:43c89579ac52 359 moves.push_back((boardPos) {
mus3 11:43c89579ac52 360 row + 1, column + 2
mus3 11:43c89579ac52 361 });
mus3 11:43c89579ac52 362 }
mus3 11:43c89579ac52 363 if (validMove(isWhite, row - 1, column + 2)) {
mus3 11:43c89579ac52 364 moves.push_back((boardPos) {
mus3 11:43c89579ac52 365 row - 1, column + 2
mus3 11:43c89579ac52 366 });
mus3 11:43c89579ac52 367 }
mus3 11:43c89579ac52 368 if (validMove(isWhite, row - 1, column - 2)) {
mus3 11:43c89579ac52 369 moves.push_back((boardPos) {
mus3 11:43c89579ac52 370 row - 1, column - 2
mus3 11:43c89579ac52 371 });
mus3 11:43c89579ac52 372 }
mus3 11:43c89579ac52 373 if (validMove(isWhite, row + 1, column - 2)) {
mus3 11:43c89579ac52 374 moves.push_back((boardPos) {
mus3 11:43c89579ac52 375 row + 1, column - 2
mus3 11:43c89579ac52 376 });
mhannay3 6:1c4dd5e24b8d 377 }
mhannay3 6:1c4dd5e24b8d 378 break;
mhannay3 6:1c4dd5e24b8d 379 case wR:
mhannay3 6:1c4dd5e24b8d 380 case bR:
mhannay3 6:1c4dd5e24b8d 381 isWhite = movingPiece == wR;
mus3 11:43c89579ac52 382 rowIndex = row + 1;
mus3 11:43c89579ac52 383 columnIndex = column;
mus3 11:43c89579ac52 384 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 385 moves.push_back((boardPos) {
mus3 11:43c89579ac52 386 rowIndex, columnIndex
mus3 11:43c89579ac52 387 });
mus3 11:43c89579ac52 388 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 389 break;
mus3 11:43c89579ac52 390 }
mus3 11:43c89579ac52 391 rowIndex++;
mus3 11:43c89579ac52 392 }
mus3 11:43c89579ac52 393 rowIndex = row - 1;
mus3 11:43c89579ac52 394 columnIndex = column;
mus3 11:43c89579ac52 395 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 396 moves.push_back((boardPos) {
mus3 11:43c89579ac52 397 rowIndex, columnIndex
mus3 11:43c89579ac52 398 });
mus3 11:43c89579ac52 399 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 400 break;
mus3 11:43c89579ac52 401 }
mus3 11:43c89579ac52 402 rowIndex--;
mus3 11:43c89579ac52 403 }
mus3 11:43c89579ac52 404 rowIndex = row;
mus3 11:43c89579ac52 405 columnIndex = column + 1;
mus3 11:43c89579ac52 406 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 407 moves.push_back((boardPos) {
mus3 11:43c89579ac52 408 rowIndex, columnIndex
mus3 11:43c89579ac52 409 });
mus3 11:43c89579ac52 410 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 411 break;
mus3 11:43c89579ac52 412 }
mus3 11:43c89579ac52 413 columnIndex++;
mus3 11:43c89579ac52 414 }
mus3 11:43c89579ac52 415 rowIndex = row;
mus3 11:43c89579ac52 416 columnIndex = column - 1;
mus3 11:43c89579ac52 417 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 418 moves.push_back((boardPos) {
mus3 11:43c89579ac52 419 rowIndex, columnIndex
mus3 11:43c89579ac52 420 });
mus3 11:43c89579ac52 421 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 422 break;
mus3 11:43c89579ac52 423 }
mus3 11:43c89579ac52 424 columnIndex--;
mus3 11:43c89579ac52 425 }
mhannay3 6:1c4dd5e24b8d 426 break;
mhannay3 6:1c4dd5e24b8d 427 case w:
mhannay3 6:1c4dd5e24b8d 428 case b:
mhannay3 6:1c4dd5e24b8d 429 isWhite = movingPiece == w;
mhannay3 6:1c4dd5e24b8d 430 int rowChange = isWhite ? 1 : -1;
mus3 11:43c89579ac52 431 if (validMove(isWhite, row + rowChange, column) && getPiece(row + rowChange, column) == e) {
mhannay3 6:1c4dd5e24b8d 432 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 433 row + rowChange, column
mhannay3 6:1c4dd5e24b8d 434 });
mus3 11:43c89579ac52 435 // The case for pawns moving two squares at the start
mus3 11:43c89579ac52 436 if (((isWhite && row == 1) || (!isWhite && row == 6)) && validMove(isWhite, row + rowChange + rowChange, column) && getPiece(row + rowChange + rowChange, column) == e) {
mus3 11:43c89579ac52 437 moves.push_back((boardPos) {
mus3 11:43c89579ac52 438 row + rowChange + rowChange, column
mus3 11:43c89579ac52 439 });
mus3 11:43c89579ac52 440 }
mhannay3 6:1c4dd5e24b8d 441 }
mhannay3 6:1c4dd5e24b8d 442 if (validMove(isWhite, row + rowChange, column + 1) && getPiece(row + rowChange, column + 1) != e) {
mhannay3 6:1c4dd5e24b8d 443 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 444 row + rowChange, column + 1
mhannay3 6:1c4dd5e24b8d 445 });
mhannay3 6:1c4dd5e24b8d 446 }
mhannay3 6:1c4dd5e24b8d 447 if (validMove(isWhite, row + rowChange, column - 1) && getPiece(row + rowChange, column - 1) != e) {
mhannay3 6:1c4dd5e24b8d 448 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 449 row + rowChange, column - 1
mhannay3 6:1c4dd5e24b8d 450 });
mhannay3 6:1c4dd5e24b8d 451 }
mhannay3 6:1c4dd5e24b8d 452 break;
mus3 11:43c89579ac52 453 default:
mus3 11:43c89579ac52 454 break;
mhannay3 6:1c4dd5e24b8d 455 }
mhannay3 6:1c4dd5e24b8d 456 return moves;
mhannay3 6:1c4dd5e24b8d 457 }
mhannay3 9:1f33c0f299ae 458
mhannay3 8:928d5d33258f 459 // returns a vector of board positions that a piece can move in a line
mhannay3 8:928d5d33258f 460 std::vector<boardPos> movesInLine(bool isMovingPieceWhite, int row, int column, int rowChange, int columnChange)
mhannay3 8:928d5d33258f 461 {
mhannay3 8:928d5d33258f 462 std::vector<boardPos> moves;
mhannay3 8:928d5d33258f 463 for (int i = 1; ; i++) {
mhannay3 8:928d5d33258f 464 // check if piece can move to location
mhannay3 8:928d5d33258f 465 if (validMove(isMovingPieceWhite, row + i * rowChange, column + i * columnChange)) {
mhannay3 8:928d5d33258f 466 moves.push_back((boardPos) {
mhannay3 8:928d5d33258f 467 row + i * rowChange, column + i * columnChange
mhannay3 8:928d5d33258f 468 });
mhannay3 8:928d5d33258f 469 // if piece is capturable, stop moving beyond it
mhannay3 9:1f33c0f299ae 470 if (getPiece(row + i * rowChange, column + i * columnChange) != e) {
mhannay3 8:928d5d33258f 471 break;
mhannay3 8:928d5d33258f 472 }
mhannay3 9:1f33c0f299ae 473 // if unable to move, break out
mhannay3 8:928d5d33258f 474 } else {
mhannay3 8:928d5d33258f 475 break;
mhannay3 8:928d5d33258f 476 }
mhannay3 8:928d5d33258f 477 }
mhannay3 8:928d5d33258f 478 return moves;
mhannay3 8:928d5d33258f 479 }
mhannay3 6:1c4dd5e24b8d 480
mhannay3 8:928d5d33258f 481 // returns if a piece can move to a given location
mhannay3 6:1c4dd5e24b8d 482 bool validMove(bool isMovingPieceWhite, int row, int column)
mhannay3 6:1c4dd5e24b8d 483 {
mhannay3 6:1c4dd5e24b8d 484 if (row < 0 || row > 7 || column < 0 || column > 7) {
mhannay3 6:1c4dd5e24b8d 485 return false;
mhannay3 6:1c4dd5e24b8d 486 }
mhannay3 6:1c4dd5e24b8d 487 Piece capturedPiece = getPiece(row, column);
mhannay3 6:1c4dd5e24b8d 488 switch(capturedPiece) {
mhannay3 6:1c4dd5e24b8d 489 case wK:
mhannay3 6:1c4dd5e24b8d 490 case wQ:
mhannay3 6:1c4dd5e24b8d 491 case wR:
mhannay3 6:1c4dd5e24b8d 492 case wB:
mhannay3 6:1c4dd5e24b8d 493 case wN:
mhannay3 6:1c4dd5e24b8d 494 case w:
mhannay3 6:1c4dd5e24b8d 495 return !isMovingPieceWhite;
mhannay3 6:1c4dd5e24b8d 496 case bK:
mhannay3 6:1c4dd5e24b8d 497 case bQ:
mhannay3 6:1c4dd5e24b8d 498 case bR:
mhannay3 6:1c4dd5e24b8d 499 case bB:
mhannay3 6:1c4dd5e24b8d 500 case bN:
mhannay3 6:1c4dd5e24b8d 501 case b:
mhannay3 6:1c4dd5e24b8d 502 return isMovingPieceWhite;
mhannay3 6:1c4dd5e24b8d 503 case e:
mhannay3 6:1c4dd5e24b8d 504 return true;
mhannay3 6:1c4dd5e24b8d 505 }
mhannay3 6:1c4dd5e24b8d 506 return false;
mhannay3 6:1c4dd5e24b8d 507 }
mhannay3 5:b553c51b3b85 508 };
mhannay3 1:cd78922f70fa 509
mhannay3 5:b553c51b3b85 510 class GameBoard
mhannay3 5:b553c51b3b85 511 {
mhannay3 5:b553c51b3b85 512 private:
mhannay3 5:b553c51b3b85 513 BoardState boardState;
mhannay3 5:b553c51b3b85 514 static const int BOARD_DARK_COLOR = 0x769656;
mhannay3 5:b553c51b3b85 515 static const int BOARD_LIGHT_COLOR = 0xbaca44;
mhannay3 5:b553c51b3b85 516 static const int HOVER_COLOR = 0x0000ff;
mhannay3 5:b553c51b3b85 517 static const int SELECTED_COLOR = 0xff8800;
mhannay3 6:1c4dd5e24b8d 518 static const int MOVE_COLOR = 0xff00ff;
mus3 11:43c89579ac52 519
mhannay3 12:7ef20deb9d5e 520 <<<<<<< working copy
mhannay3 10:8f73a917b239 521 // gets the pixel coordinates of the top left of the square
mhannay3 10:8f73a917b239 522 static pixelCoord getTopLeftOfSquare(boardPos pos)
mhannay3 10:8f73a917b239 523 {
mhannay3 10:8f73a917b239 524 return getTopLeftOfSquare(pos.row, pos.column);
mhannay3 10:8f73a917b239 525 }
mhannay3 10:8f73a917b239 526 static pixelCoord getTopLeftOfSquare(int row, int column)
mhannay3 10:8f73a917b239 527 {
mhannay3 10:8f73a917b239 528 pixelCoord topLeft;
mhannay3 10:8f73a917b239 529 topLeft.x = 16 * column;
mhannay3 10:8f73a917b239 530 topLeft.y = 112 - 16 * row;
mhannay3 10:8f73a917b239 531 return topLeft;
mhannay3 10:8f73a917b239 532 }
mhannay3 5:b553c51b3b85 533
mhannay3 5:b553c51b3b85 534 // piece sprites (12 x 12)
mhannay3 10:8f73a917b239 535 static void drawPawn(int row, int column, bool white, bool light)
mhannay3 5:b553c51b3b85 536 {
mhannay3 5:b553c51b3b85 537 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 538 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 539 int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 5:b553c51b3b85 540 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 5:b553c51b3b85 541 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 5:b553c51b3b85 542 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 5:b553c51b3b85 543 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 544 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 5:b553c51b3b85 545 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 5:b553c51b3b85 546 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 547 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 548 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 5:b553c51b3b85 549 _, _, X, X, X, X, X, X, X, X, _, _,
mhannay3 5:b553c51b3b85 550 _, _, X, X, X, X, X, X, X, X, _, _
mhannay3 5:b553c51b3b85 551 };
mhannay3 10:8f73a917b239 552 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 10:8f73a917b239 553 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mhannay3 5:b553c51b3b85 554 }
mhannay3 5:b553c51b3b85 555
mhannay3 10:8f73a917b239 556 static void drawRook(int row, int column, bool white, bool light)
mhannay3 5:b553c51b3b85 557 {
mhannay3 5:b553c51b3b85 558 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 559 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 560 int sprite[144] = {X, X, _, X, X, _, _, X, X, _, X, X,
mhannay3 5:b553c51b3b85 561 X, X, _, X, X, _, _, X, X, _, X, X,
mhannay3 5:b553c51b3b85 562 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 563 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 564 _, X, X, X, _, X, X, _, X, X, X, _,
mhannay3 5:b553c51b3b85 565 _, X, X, X, _, X, X, _, X, X, X, _,
mhannay3 5:b553c51b3b85 566 _, _, X, X, _, X, X, _, X, X, _, _,
mhannay3 5:b553c51b3b85 567 _, _, X, X, _, X, X, _, X, X, _, _,
mhannay3 5:b553c51b3b85 568 _, _, X, X, _, X, X, _, X, X, _, _,
mhannay3 5:b553c51b3b85 569 _, X, X, X, X, X, X, X, X, X, X, _,
mhannay3 5:b553c51b3b85 570 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 571 X, X, X, X, X, X, X, X, X, X, X, X
mhannay3 5:b553c51b3b85 572 };
mhannay3 10:8f73a917b239 573 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 10:8f73a917b239 574 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mhannay3 12:7ef20deb9d5e 575 =======
mus3 11:43c89579ac52 576 // gets the pixel coordinates of the top left of the square
mus3 11:43c89579ac52 577 static pixelCoord getTopLeftOfSquare(boardPos pos)
mus3 11:43c89579ac52 578 {
mus3 11:43c89579ac52 579 return getTopLeftOfSquare(pos.row, pos.column);
mus3 11:43c89579ac52 580 }
mus3 11:43c89579ac52 581 static pixelCoord getTopLeftOfSquare(int row, int column)
mus3 11:43c89579ac52 582 {
mus3 11:43c89579ac52 583 pixelCoord topLeft;
mus3 11:43c89579ac52 584 topLeft.x = 16 * column;
mus3 11:43c89579ac52 585 topLeft.y = 112 - 16 * row;
mus3 11:43c89579ac52 586 return topLeft;
mhannay3 12:7ef20deb9d5e 587 >>>>>>> merge rev
mhannay3 5:b553c51b3b85 588 }
mhannay3 5:b553c51b3b85 589
mhannay3 12:7ef20deb9d5e 590 <<<<<<< working copy
mhannay3 10:8f73a917b239 591 static void drawKnight(int row, int column, bool white, bool light)
mhannay3 5:b553c51b3b85 592 {
mhannay3 5:b553c51b3b85 593 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 594 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 595 int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 5:b553c51b3b85 596 _, _, _, _, _, X, X, _, X, X, _, _,
mhannay3 5:b553c51b3b85 597 _, _, _, _, _, X, X, _, X, X, _, _,
mhannay3 5:b553c51b3b85 598 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 599 _, _, X, X, X, X, X, _, X, _, _, _,
mhannay3 5:b553c51b3b85 600 _, _, X, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 601 _, _, _, _, _, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 602 _, _, _, _, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 603 _, _, _, X, X, X, X, X, X, X, _, _,
mhannay3 5:b553c51b3b85 604 _, _, X, X, X, X, X, X, X, X, _, _,
mhannay3 5:b553c51b3b85 605 _, X, X, X, X, X, X, X, X, X, X, _,
mhannay3 5:b553c51b3b85 606 _, X, X, X, X, X, X, X, X, X, X, _
mhannay3 5:b553c51b3b85 607 };
mhannay3 10:8f73a917b239 608 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 10:8f73a917b239 609 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mhannay3 5:b553c51b3b85 610 }
mhannay3 5:b553c51b3b85 611
mhannay3 10:8f73a917b239 612 static void drawBishop(int row, int column, bool white, bool light)
mhannay3 5:b553c51b3b85 613 {
mhannay3 5:b553c51b3b85 614 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 615 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 616 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 617 _, _, _, _, X, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 618 _, _, _, X, X, X, _, _, X, _, _, _,
mhannay3 5:b553c51b3b85 619 _, _, _, X, X, _, _, X, X, _, _, _,
mhannay3 5:b553c51b3b85 620 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 621 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 5:b553c51b3b85 622 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 623 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 5:b553c51b3b85 624 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 625 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 626 _, _, X, X, X, X, X, X, X, X, _, _,
mhannay3 5:b553c51b3b85 627 _, _, X, X, X, X, X, X, X, X, _, _
mhannay3 5:b553c51b3b85 628 };
mhannay3 10:8f73a917b239 629 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 10:8f73a917b239 630 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mhannay3 5:b553c51b3b85 631 }
mhannay3 5:b553c51b3b85 632
mhannay3 10:8f73a917b239 633 static void drawQueen(int row, int column, bool white, bool light)
mhannay3 5:b553c51b3b85 634 {
mhannay3 5:b553c51b3b85 635 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 636 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 637 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 638 _, _, X, _, _, X, X, _, _, X, _, _,
mhannay3 5:b553c51b3b85 639 X, _, X, X, _, X, X, _, X, X, _, X,
mhannay3 5:b553c51b3b85 640 X, _, X, X, _, X, X, _, X, X, _, X,
mhannay3 5:b553c51b3b85 641 X, _, X, X, _, X, X, _, X, X, _, X,
mhannay3 5:b553c51b3b85 642 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 643 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 644 X, X, _, X, X, X, X, X, X, _, X, X,
mhannay3 5:b553c51b3b85 645 X, X, X, X, _, X, X, _, X, X, X, X,
mhannay3 5:b553c51b3b85 646 _, X, X, X, X, X, X, X, X, X, X, _,
mhannay3 5:b553c51b3b85 647 _, _, X, X, X, X, X, X, X, X, _, _,
mhannay3 5:b553c51b3b85 648 _, X, X, X, X, X, X, X, X, X, X, _
mhannay3 5:b553c51b3b85 649 };
mhannay3 10:8f73a917b239 650 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 10:8f73a917b239 651 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mhannay3 5:b553c51b3b85 652 }
mhannay3 5:b553c51b3b85 653
mhannay3 10:8f73a917b239 654 static void drawKing(int row, int column, bool white, bool light)
mhannay3 5:b553c51b3b85 655 {
mhannay3 5:b553c51b3b85 656 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 657 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 658 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 659 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 660 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 661 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 662 X, X, _, _, _, X, X, _, _, _, X, X,
mhannay3 5:b553c51b3b85 663 X, X, X, X, _, X, X, _, X, X, X, X,
mhannay3 5:b553c51b3b85 664 X, _, X, X, X, X, X, X, X, X, _, X,
mhannay3 5:b553c51b3b85 665 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 666 X, X, X, _, X, X, X, X, _, X, X, X,
mhannay3 5:b553c51b3b85 667 _, X, X, X, X, X, X, X, X, X, X, _,
mhannay3 5:b553c51b3b85 668 _, _, X, X, X, _, _, X, X, X, _, _,
mhannay3 5:b553c51b3b85 669 _, X, X, X, X, X, X, X, X, X, X, _
mhannay3 5:b553c51b3b85 670 };
mhannay3 10:8f73a917b239 671 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 10:8f73a917b239 672 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mhannay3 12:7ef20deb9d5e 673 =======
mus3 11:43c89579ac52 674 // piece sprites (12 x 12)
mus3 11:43c89579ac52 675 static void drawPawn(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 676 {
mus3 11:43c89579ac52 677 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 678 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 679 int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _,
mus3 11:43c89579ac52 680 _, _, _, _, _, _, _, _, _, _, _, _,
mus3 11:43c89579ac52 681 _, _, _, _, _, _, _, _, _, _, _, _,
mus3 11:43c89579ac52 682 _, _, _, _, _, _, _, _, _, _, _, _,
mus3 11:43c89579ac52 683 _, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 684 _, _, _, _, X, X, X, X, _, _, _, _,
mus3 11:43c89579ac52 685 _, _, _, _, X, X, X, X, _, _, _, _,
mus3 11:43c89579ac52 686 _, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 687 _, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 688 _, _, _, _, X, X, X, X, _, _, _, _,
mus3 11:43c89579ac52 689 _, _, X, X, X, X, X, X, X, X, _, _,
mus3 11:43c89579ac52 690 _, _, X, X, X, X, X, X, X, X, _, _
mus3 11:43c89579ac52 691 };
mus3 11:43c89579ac52 692 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 693 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mus3 11:43c89579ac52 694 }
mus3 11:43c89579ac52 695 static void drawRook(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 696 {
mus3 11:43c89579ac52 697 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 698 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 699 int sprite[144] = {X, X, _, X, X, _, _, X, X, _, X, X,
mus3 11:43c89579ac52 700 X, X, _, X, X, _, _, X, X, _, X, X,
mus3 11:43c89579ac52 701 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 702 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 703 _, X, X, X, _, X, X, _, X, X, X, _,
mus3 11:43c89579ac52 704 _, X, X, X, _, X, X, _, X, X, X, _,
mus3 11:43c89579ac52 705 _, _, X, X, _, X, X, _, X, X, _, _,
mus3 11:43c89579ac52 706 _, _, X, X, _, X, X, _, X, X, _, _,
mus3 11:43c89579ac52 707 _, _, X, X, _, X, X, _, X, X, _, _,
mus3 11:43c89579ac52 708 _, X, X, X, X, X, X, X, X, X, X, _,
mus3 11:43c89579ac52 709 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 710 X, X, X, X, X, X, X, X, X, X, X, X
mus3 11:43c89579ac52 711 };
mus3 11:43c89579ac52 712 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 713 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mus3 11:43c89579ac52 714 }
mus3 11:43c89579ac52 715 static void drawKnight(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 716 {
mus3 11:43c89579ac52 717 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 718 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 719 int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _,
mus3 11:43c89579ac52 720 _, _, _, _, _, X, X, _, X, X, _, _,
mus3 11:43c89579ac52 721 _, _, _, _, _, X, X, _, X, X, _, _,
mus3 11:43c89579ac52 722 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 723 _, _, X, X, X, X, X, _, X, _, _, _,
mus3 11:43c89579ac52 724 _, _, X, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 725 _, _, _, _, _, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 726 _, _, _, _, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 727 _, _, _, X, X, X, X, X, X, X, _, _,
mus3 11:43c89579ac52 728 _, _, X, X, X, X, X, X, X, X, _, _,
mus3 11:43c89579ac52 729 _, X, X, X, X, X, X, X, X, X, X, _,
mus3 11:43c89579ac52 730 _, X, X, X, X, X, X, X, X, X, X, _
mus3 11:43c89579ac52 731 };
mus3 11:43c89579ac52 732 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 733 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mus3 11:43c89579ac52 734 }
mus3 11:43c89579ac52 735 static void drawBishop(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 736 {
mus3 11:43c89579ac52 737 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 738 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 739 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 740 _, _, _, _, X, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 741 _, _, _, X, X, X, _, _, X, _, _, _,
mus3 11:43c89579ac52 742 _, _, _, X, X, _, _, X, X, _, _, _,
mus3 11:43c89579ac52 743 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 744 _, _, _, _, X, X, X, X, _, _, _, _,
mus3 11:43c89579ac52 745 _, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 746 _, _, _, _, X, X, X, X, _, _, _, _,
mus3 11:43c89579ac52 747 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 748 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 749 _, _, X, X, X, X, X, X, X, X, _, _,
mus3 11:43c89579ac52 750 _, _, X, X, X, X, X, X, X, X, _, _
mus3 11:43c89579ac52 751 };
mus3 11:43c89579ac52 752 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 753 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mus3 11:43c89579ac52 754 }
mus3 11:43c89579ac52 755 static void drawQueen(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 756 {
mus3 11:43c89579ac52 757 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 758 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 759 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 760 _, _, X, _, _, X, X, _, _, X, _, _,
mus3 11:43c89579ac52 761 X, _, X, X, _, X, X, _, X, X, _, X,
mus3 11:43c89579ac52 762 X, _, X, X, _, X, X, _, X, X, _, X,
mus3 11:43c89579ac52 763 X, _, X, X, _, X, X, _, X, X, _, X,
mus3 11:43c89579ac52 764 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 765 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 766 X, X, _, X, X, X, X, X, X, _, X, X,
mus3 11:43c89579ac52 767 X, X, X, X, _, X, X, _, X, X, X, X,
mus3 11:43c89579ac52 768 _, X, X, X, X, X, X, X, X, X, X, _,
mus3 11:43c89579ac52 769 _, _, X, X, X, X, X, X, X, X, _, _,
mus3 11:43c89579ac52 770 _, X, X, X, X, X, X, X, X, X, X, _
mus3 11:43c89579ac52 771 };
mus3 11:43c89579ac52 772 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 773 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mus3 11:43c89579ac52 774 }
mus3 11:43c89579ac52 775 static void drawKing(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 776 {
mus3 11:43c89579ac52 777 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 778 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 779 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 780 _, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 781 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 782 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 783 X, X, _, _, _, X, X, _, _, _, X, X,
mus3 11:43c89579ac52 784 X, X, X, X, _, X, X, _, X, X, X, X,
mus3 11:43c89579ac52 785 X, _, X, X, X, X, X, X, X, X, _, X,
mus3 11:43c89579ac52 786 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 787 X, X, X, _, X, X, X, X, _, X, X, X,
mus3 11:43c89579ac52 788 _, X, X, X, X, X, X, X, X, X, X, _,
mus3 11:43c89579ac52 789 _, _, X, X, X, _, _, X, X, X, _, _,
mus3 11:43c89579ac52 790 _, X, X, X, X, X, X, X, X, X, X, _
mus3 11:43c89579ac52 791 };
mus3 11:43c89579ac52 792 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 793 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mhannay3 12:7ef20deb9d5e 794 >>>>>>> merge rev
mhannay3 5:b553c51b3b85 795 }
mhannay3 5:b553c51b3b85 796
mhannay3 5:b553c51b3b85 797 public:
mhannay3 5:b553c51b3b85 798 BoardState getBoardState()
mhannay3 5:b553c51b3b85 799 {
mhannay3 5:b553c51b3b85 800 return boardState;
mhannay3 5:b553c51b3b85 801 }
mhannay3 5:b553c51b3b85 802
mhannay3 5:b553c51b3b85 803 void setBoardState(BoardState newBoardState)
mhannay3 5:b553c51b3b85 804 {
mhannay3 5:b553c51b3b85 805 boardState = newBoardState;
mhannay3 5:b553c51b3b85 806 }
mhannay3 5:b553c51b3b85 807
mhannay3 5:b553c51b3b85 808 // initializes the starting board state
mhannay3 5:b553c51b3b85 809 GameBoard()
mhannay3 5:b553c51b3b85 810 {
mhannay3 5:b553c51b3b85 811 // draw board
mhannay3 5:b553c51b3b85 812 for (int row = 0; row < 8; row++) {
mhannay3 5:b553c51b3b85 813 for (int column = 0; column < 8; column++) {
mhannay3 5:b553c51b3b85 814 uint64_t color;
mhannay3 5:b553c51b3b85 815 if ((row + column) % 2 == 0) {
mhannay3 5:b553c51b3b85 816 color = BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 817 } else {
mhannay3 5:b553c51b3b85 818 color = BOARD_LIGHT_COLOR;
mhannay3 5:b553c51b3b85 819 }
mhannay3 6:1c4dd5e24b8d 820 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 821 uLCD.filled_rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, color);
mhannay3 5:b553c51b3b85 822 }
mhannay3 5:b553c51b3b85 823 }
mhannay3 5:b553c51b3b85 824 // draw pieces
mhannay3 5:b553c51b3b85 825 placePieceAndDraw(wR, 0, 0);
mhannay3 5:b553c51b3b85 826 placePieceAndDraw(wN, 0, 1);
mhannay3 5:b553c51b3b85 827 placePieceAndDraw(wB, 0, 2);
mhannay3 5:b553c51b3b85 828 placePieceAndDraw(wQ, 0, 3);
mhannay3 5:b553c51b3b85 829 placePieceAndDraw(wK, 0, 4);
mhannay3 5:b553c51b3b85 830 placePieceAndDraw(wB, 0, 5);
mhannay3 5:b553c51b3b85 831 placePieceAndDraw(wN, 0, 6);
mhannay3 5:b553c51b3b85 832 placePieceAndDraw(wR, 0, 7);
mhannay3 5:b553c51b3b85 833 placePieceAndDraw(bR, 7, 0);
mhannay3 5:b553c51b3b85 834 placePieceAndDraw(bN, 7, 1);
mhannay3 5:b553c51b3b85 835 placePieceAndDraw(bB, 7, 2);
mhannay3 5:b553c51b3b85 836 placePieceAndDraw(bQ, 7, 3);
mhannay3 5:b553c51b3b85 837 placePieceAndDraw(bK, 7, 4);
mhannay3 5:b553c51b3b85 838 placePieceAndDraw(bB, 7, 5);
mhannay3 5:b553c51b3b85 839 placePieceAndDraw(bN, 7, 6);
mhannay3 5:b553c51b3b85 840 placePieceAndDraw(bR, 7, 7);
mhannay3 5:b553c51b3b85 841 for (int i = 0; i < 8; i++) {
mhannay3 5:b553c51b3b85 842 placePieceAndDraw(w, 1, i);
mhannay3 5:b553c51b3b85 843 placePieceAndDraw(b, 6, i);
mhannay3 5:b553c51b3b85 844 }
mhannay3 5:b553c51b3b85 845 }
mhannay3 5:b553c51b3b85 846
mhannay3 5:b553c51b3b85 847 // PIECE MOVEMENT AND GRAPHICS FUNCTIONS
mhannay3 5:b553c51b3b85 848
mhannay3 5:b553c51b3b85 849 // returns the piece at a given location
mhannay3 7:9e01c9a334dc 850 Piece getPiece(boardPos pos)
mhannay3 7:9e01c9a334dc 851 {
mhannay3 7:9e01c9a334dc 852 return getPiece(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 853 }
mhannay3 5:b553c51b3b85 854 Piece getPiece(int row, int column)
mhannay3 5:b553c51b3b85 855 {
mhannay3 5:b553c51b3b85 856 return boardState.getPiece(row, column);
mhannay3 5:b553c51b3b85 857 }
mhannay3 5:b553c51b3b85 858
mhannay3 5:b553c51b3b85 859 /* puts the bit representation of a piece at the set position of the board
mhannay3 5:b553c51b3b85 860 assumes that the position of the board is emptied beforehand
mhannay3 5:b553c51b3b85 861 */
mhannay3 7:9e01c9a334dc 862 void placePieceAndDraw(Piece piece, boardPos pos)
mhannay3 7:9e01c9a334dc 863 {
mhannay3 7:9e01c9a334dc 864 placePieceAndDraw(piece, pos.row, pos.column);
mhannay3 7:9e01c9a334dc 865 }
mhannay3 5:b553c51b3b85 866 void placePieceAndDraw(Piece piece, int row, int column)
mhannay3 5:b553c51b3b85 867 {
mhannay3 5:b553c51b3b85 868 boardState.placePiece(piece, row, column);
mhannay3 6:1c4dd5e24b8d 869 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 12:7ef20deb9d5e 870 <<<<<<< working copy
mhannay3 5:b553c51b3b85 871 switch(piece) {
mhannay3 5:b553c51b3b85 872 case wK:
mhannay3 5:b553c51b3b85 873 case bK:
mhannay3 10:8f73a917b239 874 drawKing(row, column, piece==wK, (row+column)%2);
mhannay3 5:b553c51b3b85 875 break;
mhannay3 5:b553c51b3b85 876 case wQ:
mhannay3 5:b553c51b3b85 877 case bQ:
mhannay3 10:8f73a917b239 878 drawQueen(row, column, piece==wQ, (row+column)%2);
mhannay3 5:b553c51b3b85 879 break;
mhannay3 5:b553c51b3b85 880 case wB:
mhannay3 5:b553c51b3b85 881 case bB:
mhannay3 10:8f73a917b239 882 drawBishop(row, column, piece==wB, (row+column)%2);
mhannay3 5:b553c51b3b85 883 break;
mhannay3 5:b553c51b3b85 884 case wN:
mhannay3 5:b553c51b3b85 885 case bN:
mhannay3 10:8f73a917b239 886 drawKnight(row, column, piece==wN, (row+column)%2);
mhannay3 5:b553c51b3b85 887 break;
mhannay3 5:b553c51b3b85 888 case wR:
mhannay3 5:b553c51b3b85 889 case bR:
mhannay3 10:8f73a917b239 890 drawRook(row, column, piece==wR, (row+column)%2);
mhannay3 5:b553c51b3b85 891 break;
mhannay3 5:b553c51b3b85 892 case w:
mhannay3 5:b553c51b3b85 893 case b:
mhannay3 10:8f73a917b239 894 drawPawn(row, column, piece==w, (row+column)%2);
mhannay3 5:b553c51b3b85 895 break;
mhannay3 12:7ef20deb9d5e 896 =======
mus3 11:43c89579ac52 897 switch(piece) {
mus3 11:43c89579ac52 898 case wK:
mus3 11:43c89579ac52 899 case bK:
mus3 11:43c89579ac52 900 drawKing(row, column, piece==wK, (row+column)%2);
mus3 11:43c89579ac52 901 break;
mus3 11:43c89579ac52 902 case wQ:
mus3 11:43c89579ac52 903 case bQ:
mus3 11:43c89579ac52 904 drawQueen(row, column, piece==wQ, (row+column)%2);
mus3 11:43c89579ac52 905 break;
mus3 11:43c89579ac52 906 case wB:
mus3 11:43c89579ac52 907 case bB:
mus3 11:43c89579ac52 908 drawBishop(row, column, piece==wB, (row+column)%2);
mus3 11:43c89579ac52 909 break;
mus3 11:43c89579ac52 910 case wN:
mus3 11:43c89579ac52 911 case bN:
mus3 11:43c89579ac52 912 drawKnight(row, column, piece==wN, (row+column)%2);
mus3 11:43c89579ac52 913 break;
mus3 11:43c89579ac52 914 case wR:
mus3 11:43c89579ac52 915 case bR:
mus3 11:43c89579ac52 916 drawRook(row, column, piece==wR, (row+column)%2);
mus3 11:43c89579ac52 917 break;
mus3 11:43c89579ac52 918 case w:
mus3 11:43c89579ac52 919 case b:
mus3 11:43c89579ac52 920 drawPawn(row, column, piece==w, (row+column)%2);
mus3 11:43c89579ac52 921 break;
mhannay3 12:7ef20deb9d5e 922 >>>>>>> merge rev
mhannay3 5:b553c51b3b85 923 }
mhannay3 5:b553c51b3b85 924 }
mhannay3 5:b553c51b3b85 925
mhannay3 5:b553c51b3b85 926 /* removes a piece from the set position of the board
mhannay3 5:b553c51b3b85 927 returns the bit representation of the piece
mhannay3 5:b553c51b3b85 928 */
mhannay3 7:9e01c9a334dc 929 Piece removePieceAndDraw(boardPos pos)
mhannay3 7:9e01c9a334dc 930 {
mhannay3 7:9e01c9a334dc 931 return removePieceAndDraw(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 932 }
mhannay3 5:b553c51b3b85 933 Piece removePieceAndDraw(int row, int column)
mhannay3 5:b553c51b3b85 934 {
mhannay3 5:b553c51b3b85 935 Piece removedPiece = boardState.removePiece(row, column);
mhannay3 6:1c4dd5e24b8d 936 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 1:cd78922f70fa 937 uint64_t color;
mhannay3 1:cd78922f70fa 938 if ((row + column) % 2 == 0) {
mhannay3 1:cd78922f70fa 939 color = BOARD_DARK_COLOR;
mhannay3 1:cd78922f70fa 940 } else {
mhannay3 1:cd78922f70fa 941 color = BOARD_LIGHT_COLOR;
mhannay3 1:cd78922f70fa 942 }
mhannay3 5:b553c51b3b85 943 uLCD.filled_rectangle(tl.x+2, tl.y+2, tl.x + 13, tl.y + 13, color);
mhannay3 5:b553c51b3b85 944 return removedPiece;
mhannay3 5:b553c51b3b85 945 }
mhannay3 5:b553c51b3b85 946
mhannay3 5:b553c51b3b85 947 /* moves a piece from one position to another
mhannay3 5:b553c51b3b85 948 returns the captured piece
mhannay3 5:b553c51b3b85 949 */
mhannay3 7:9e01c9a334dc 950 Piece movePieceAndDraw(boardPos startPos, boardPos endPos)
mhannay3 7:9e01c9a334dc 951 {
mhannay3 7:9e01c9a334dc 952 return movePieceAndDraw(startPos.row, startPos.column, endPos.row, endPos.column);
mhannay3 7:9e01c9a334dc 953 }
mhannay3 5:b553c51b3b85 954 Piece movePieceAndDraw(int startRow, int startColumn, int endRow, int endColumn)
mhannay3 5:b553c51b3b85 955 {
mhannay3 5:b553c51b3b85 956 Piece movingPiece = removePieceAndDraw(startRow, startColumn);
mhannay3 5:b553c51b3b85 957 Piece capturedPiece = boardState.removePiece(endRow, endColumn);
mhannay3 5:b553c51b3b85 958 placePieceAndDraw(movingPiece, endRow, endColumn);
mhannay3 5:b553c51b3b85 959 return capturedPiece;
mhannay3 1:cd78922f70fa 960 }
mhannay3 1:cd78922f70fa 961
mhannay3 5:b553c51b3b85 962 // SQUARE BORDER GRAPHICS FUNCTIONS
mhannay3 5:b553c51b3b85 963
mhannay3 5:b553c51b3b85 964 // removes selection border around square
mhannay3 7:9e01c9a334dc 965 void unselectSquare(boardPos pos)
mhannay3 7:9e01c9a334dc 966 {
mhannay3 7:9e01c9a334dc 967 unselectSquare(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 968 }
mhannay3 5:b553c51b3b85 969 void unselectSquare(int row, int column)
mhannay3 5:b553c51b3b85 970 {
mus3 11:43c89579ac52 971 if (row < 0 || row > 7 || column < 0 || column > 7) {
mus3 11:43c89579ac52 972 return;
mus3 11:43c89579ac52 973 }
mhannay3 6:1c4dd5e24b8d 974 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 975 uint64_t color;
mhannay3 5:b553c51b3b85 976 if ((row + column) % 2 == 0) {
mhannay3 5:b553c51b3b85 977 color = BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 978 } else {
mhannay3 5:b553c51b3b85 979 color = BOARD_LIGHT_COLOR;
mhannay3 5:b553c51b3b85 980 }
mhannay3 5:b553c51b3b85 981 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, color);
mhannay3 5:b553c51b3b85 982 }
mhannay3 5:b553c51b3b85 983
mhannay3 7:9e01c9a334dc 984 // draws the hover border around square
mhannay3 7:9e01c9a334dc 985 void hoverSquare(boardPos pos)
mhannay3 7:9e01c9a334dc 986 {
mhannay3 7:9e01c9a334dc 987 hoverSquare(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 988 }
mhannay3 5:b553c51b3b85 989 void hoverSquare(int row, int column)
mhannay3 5:b553c51b3b85 990 {
mhannay3 6:1c4dd5e24b8d 991 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 992 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, HOVER_COLOR);
mhannay3 1:cd78922f70fa 993 }
mhannay3 1:cd78922f70fa 994
mhannay3 1:cd78922f70fa 995 // draws selection border around square
mhannay3 7:9e01c9a334dc 996 void selectSquare(boardPos pos)
mhannay3 7:9e01c9a334dc 997 {
mhannay3 7:9e01c9a334dc 998 selectSquare(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 999 }
mhannay3 5:b553c51b3b85 1000 void selectSquare(int row, int column)
mhannay3 5:b553c51b3b85 1001 {
mus3 11:43c89579ac52 1002 if (row < 0 || row > 7 || column < 0 || column > 7) {
mus3 11:43c89579ac52 1003 return;
mus3 11:43c89579ac52 1004 }
mhannay3 6:1c4dd5e24b8d 1005 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 1006 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, SELECTED_COLOR);
mhannay3 1:cd78922f70fa 1007 }
mhannay3 9:1f33c0f299ae 1008
mhannay3 8:928d5d33258f 1009 // draws the movement border around square
mhannay3 9:1f33c0f299ae 1010 void movementSquare(boardPos pos)
mhannay3 8:928d5d33258f 1011 {
mhannay3 9:1f33c0f299ae 1012 movementSquare(pos.row, pos.column);
mhannay3 8:928d5d33258f 1013 }
mhannay3 9:1f33c0f299ae 1014 void movementSquare(int row, int column)
mhannay3 8:928d5d33258f 1015 {
mhannay3 8:928d5d33258f 1016 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 8:928d5d33258f 1017 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, MOVE_COLOR);
mhannay3 8:928d5d33258f 1018 }
mhannay3 5:b553c51b3b85 1019 };
mus3 0:d16cc59110a3 1020
mhannay3 7:9e01c9a334dc 1021 // game variables
mhannay3 7:9e01c9a334dc 1022 GameBoard gameBoard;
mhannay3 7:9e01c9a334dc 1023 GameState state = whiteSelecting;
mhannay3 7:9e01c9a334dc 1024 boardPos cursorPos = (boardPos)
mhannay3 7:9e01c9a334dc 1025 {
mhannay3 7:9e01c9a334dc 1026 3, 4
mhannay3 7:9e01c9a334dc 1027 };
mhannay3 8:928d5d33258f 1028 boardPos selectedPos;
mhannay3 9:1f33c0f299ae 1029 Piece selectedPiece;
mhannay3 9:1f33c0f299ae 1030 std::vector<boardPos> possibleMoves;
mhannay3 7:9e01c9a334dc 1031
mhannay3 7:9e01c9a334dc 1032 // callbacks
mhannay3 9:1f33c0f299ae 1033 void moveCursor(int rowChange, int columnChange)
mhannay3 9:1f33c0f299ae 1034 {
mhannay3 9:1f33c0f299ae 1035 // calculate new positoin that is within board bounds
mhannay3 9:1f33c0f299ae 1036 int newRow = cursorPos.row + rowChange;
mhannay3 9:1f33c0f299ae 1037 newRow = newRow <= 7 ? newRow : 7;
mhannay3 9:1f33c0f299ae 1038 newRow = newRow >= 0 ? newRow : 0;
mhannay3 9:1f33c0f299ae 1039 int newColumn = cursorPos.column + columnChange;
mhannay3 9:1f33c0f299ae 1040 newColumn = newColumn <= 7 ? newColumn : 7;
mhannay3 9:1f33c0f299ae 1041 newColumn = newColumn >= 0 ? newColumn : 0;
mhannay3 9:1f33c0f299ae 1042 boardPos newPos = (boardPos) {
mhannay3 9:1f33c0f299ae 1043 newRow, newColumn
mhannay3 9:1f33c0f299ae 1044 };
mhannay3 9:1f33c0f299ae 1045
mhannay3 9:1f33c0f299ae 1046 // draw border around square that should be there after moving cursor off
mhannay3 9:1f33c0f299ae 1047 if (cursorPos == selectedPos) {
mhannay3 9:1f33c0f299ae 1048 gameBoard.selectSquare(cursorPos);
mhannay3 9:1f33c0f299ae 1049 } else if (std::find(possibleMoves.begin(), possibleMoves.end(), cursorPos) != possibleMoves.end()) {
mhannay3 9:1f33c0f299ae 1050 gameBoard.movementSquare(cursorPos);
mhannay3 9:1f33c0f299ae 1051 } else {
mhannay3 9:1f33c0f299ae 1052 gameBoard.unselectSquare(cursorPos);
mhannay3 9:1f33c0f299ae 1053 }
mhannay3 9:1f33c0f299ae 1054
mhannay3 9:1f33c0f299ae 1055 // draw hover rectangle over new square
mhannay3 9:1f33c0f299ae 1056 cursorPos = newPos;
mhannay3 9:1f33c0f299ae 1057 gameBoard.hoverSquare(cursorPos);
mhannay3 9:1f33c0f299ae 1058 }
mhannay3 9:1f33c0f299ae 1059
mhannay3 7:9e01c9a334dc 1060 void joyStickUp()
mhannay3 7:9e01c9a334dc 1061 {
mhannay3 9:1f33c0f299ae 1062 moveCursor(1, 0);
mhannay3 7:9e01c9a334dc 1063 }
mhannay3 7:9e01c9a334dc 1064
mhannay3 7:9e01c9a334dc 1065 void joyStickDown()
mhannay3 7:9e01c9a334dc 1066 {
mhannay3 9:1f33c0f299ae 1067 moveCursor(-1, 0);
mhannay3 7:9e01c9a334dc 1068 }
mhannay3 7:9e01c9a334dc 1069
mhannay3 7:9e01c9a334dc 1070 void joyStickLeft()
mhannay3 7:9e01c9a334dc 1071 {
mhannay3 9:1f33c0f299ae 1072 moveCursor(0, -1);
mhannay3 7:9e01c9a334dc 1073 }
mhannay3 7:9e01c9a334dc 1074
mhannay3 7:9e01c9a334dc 1075 void joyStickRight()
mhannay3 7:9e01c9a334dc 1076 {
mhannay3 9:1f33c0f299ae 1077 moveCursor(0, 1);
mhannay3 7:9e01c9a334dc 1078 }
mhannay3 7:9e01c9a334dc 1079
mhannay3 7:9e01c9a334dc 1080 void joyStickPressed()
mhannay3 7:9e01c9a334dc 1081 {
mhannay3 8:928d5d33258f 1082 switch(state) {
mhannay3 8:928d5d33258f 1083 case whiteSelecting:
mhannay3 9:1f33c0f299ae 1084 case blackSelecting: {
mus3 11:43c89579ac52 1085 possibleMoves.clear();
mhannay3 8:928d5d33258f 1086 selectedPos = cursorPos;
mhannay3 9:1f33c0f299ae 1087 Piece tempPiece = gameBoard.getPiece(cursorPos);
mhannay3 9:1f33c0f299ae 1088 std::vector<Piece> pickablePieces = state == whiteSelecting ? whitePieces : blackPieces;
mhannay3 9:1f33c0f299ae 1089 // check that piece is white and able to be picked up
mhannay3 9:1f33c0f299ae 1090 if (std::find(pickablePieces.begin(), pickablePieces.end(), tempPiece) != pickablePieces.end()) {
mhannay3 9:1f33c0f299ae 1091 selectedPiece = tempPiece;
mhannay3 9:1f33c0f299ae 1092 possibleMoves = gameBoard.getBoardState().getMoves(cursorPos);
mhannay3 9:1f33c0f299ae 1093 // draw movement squares
mhannay3 9:1f33c0f299ae 1094 for (std::vector<boardPos>::iterator it = possibleMoves.begin(); it != possibleMoves.end(); ++it) {
mhannay3 9:1f33c0f299ae 1095 gameBoard.movementSquare(*it);
mhannay3 9:1f33c0f299ae 1096 }
mhannay3 9:1f33c0f299ae 1097 gameBoard.selectSquare(selectedPos);
mus3 11:43c89579ac52 1098 // transition state
mhannay3 9:1f33c0f299ae 1099 state = state == whiteSelecting ? whitePickedUp : blackPickedUp;
mus3 11:43c89579ac52 1100 } else {
mus3 11:43c89579ac52 1101 selectedPos = (boardPos)
mus3 11:43c89579ac52 1102 {
mus3 11:43c89579ac52 1103 10, 10
mus3 11:43c89579ac52 1104 };
mhannay3 9:1f33c0f299ae 1105 }
mhannay3 8:928d5d33258f 1106 break;
mhannay3 9:1f33c0f299ae 1107 }
mhannay3 9:1f33c0f299ae 1108 case whitePickedUp:
mhannay3 9:1f33c0f299ae 1109 case blackPickedUp: {
mhannay3 9:1f33c0f299ae 1110 // check if move is valid
mhannay3 9:1f33c0f299ae 1111 if (std::find(possibleMoves.begin(), possibleMoves.end(), cursorPos) != possibleMoves.end()) {
mhannay3 9:1f33c0f299ae 1112 // move the piece
mhannay3 9:1f33c0f299ae 1113 Piece capturedPiece = gameBoard.movePieceAndDraw(selectedPos, cursorPos);
mhannay3 9:1f33c0f299ae 1114 // check for king capture
mhannay3 9:1f33c0f299ae 1115 if (state == whitePickedUp && capturedPiece == bK || state == blackPickedUp && capturedPiece == wK) {
mhannay3 9:1f33c0f299ae 1116 // game end
mhannay3 9:1f33c0f299ae 1117 }
mhannay3 9:1f33c0f299ae 1118 // transition state
mhannay3 9:1f33c0f299ae 1119 state = state == whitePickedUp ? blackSelecting : whiteSelecting;
mhannay3 9:1f33c0f299ae 1120 // check if placing piece back down
mus3 11:43c89579ac52 1121 } else {
mhannay3 9:1f33c0f299ae 1122 // transition state
mhannay3 9:1f33c0f299ae 1123 state = state == whitePickedUp ? whiteSelecting : blackSelecting;
mhannay3 9:1f33c0f299ae 1124 }
mhannay3 9:1f33c0f299ae 1125 // unselect movement squares
mhannay3 9:1f33c0f299ae 1126 for (std::vector<boardPos>::iterator it = possibleMoves.begin(); it != possibleMoves.end(); ++it) {
mhannay3 9:1f33c0f299ae 1127 gameBoard.unselectSquare(*it);
mhannay3 9:1f33c0f299ae 1128 }
mhannay3 9:1f33c0f299ae 1129 gameBoard.unselectSquare(selectedPos);
mhannay3 9:1f33c0f299ae 1130 gameBoard.hoverSquare(cursorPos);
mus3 11:43c89579ac52 1131 possibleMoves.clear();
mus3 11:43c89579ac52 1132 selectedPos = (boardPos)
mus3 11:43c89579ac52 1133 {
mus3 11:43c89579ac52 1134 10, 10
mus3 11:43c89579ac52 1135 };
mhannay3 8:928d5d33258f 1136 break;
mhannay3 9:1f33c0f299ae 1137 }
mhannay3 9:1f33c0f299ae 1138 case whiteAI:
mhannay3 9:1f33c0f299ae 1139 case blackAI: {
mhannay3 8:928d5d33258f 1140 break;
mhannay3 9:1f33c0f299ae 1141 }
mhannay3 8:928d5d33258f 1142 }
mhannay3 7:9e01c9a334dc 1143 }
mhannay3 7:9e01c9a334dc 1144
mus3 11:43c89579ac52 1145 Nav_Switch myNav(p9, p6, p7, p5, p8); //pin order on Sparkfun breakout
mus3 11:43c89579ac52 1146
mhannay3 5:b553c51b3b85 1147 int main()
mhannay3 5:b553c51b3b85 1148 {
mus3 11:43c89579ac52 1149 //gameBoard = GameBoard();
mhannay3 9:1f33c0f299ae 1150 whitePieces.push_back(wK);
mhannay3 9:1f33c0f299ae 1151 whitePieces.push_back(wQ);
mhannay3 9:1f33c0f299ae 1152 whitePieces.push_back(wB);
mhannay3 9:1f33c0f299ae 1153 whitePieces.push_back(wN);
mhannay3 9:1f33c0f299ae 1154 whitePieces.push_back(wR);
mhannay3 9:1f33c0f299ae 1155 whitePieces.push_back(w);
mhannay3 9:1f33c0f299ae 1156 blackPieces.push_back(bK);
mhannay3 9:1f33c0f299ae 1157 blackPieces.push_back(bQ);
mhannay3 9:1f33c0f299ae 1158 blackPieces.push_back(bB);
mhannay3 9:1f33c0f299ae 1159 blackPieces.push_back(bN);
mhannay3 9:1f33c0f299ae 1160 blackPieces.push_back(bR);
mhannay3 9:1f33c0f299ae 1161 blackPieces.push_back(b);
mus3 11:43c89579ac52 1162 moveCursor(0, 0);
mus3 11:43c89579ac52 1163 while (1) {
mus3 11:43c89579ac52 1164 if (myNav.up()) {
mus3 11:43c89579ac52 1165 joyStickUp();
mus3 11:43c89579ac52 1166 } else if (myNav.down()) {
mus3 11:43c89579ac52 1167 joyStickDown();
mus3 11:43c89579ac52 1168 } else if (myNav.left()) {
mus3 11:43c89579ac52 1169 joyStickLeft();
mus3 11:43c89579ac52 1170 } else if (myNav.right()) {
mus3 11:43c89579ac52 1171 joyStickRight();
mus3 11:43c89579ac52 1172 } else if (myNav.fire()) {
mus3 11:43c89579ac52 1173 joyStickPressed();
mus3 11:43c89579ac52 1174 }
mus3 11:43c89579ac52 1175 wait(0.25);
mus3 11:43c89579ac52 1176 }
mhannay3 1:cd78922f70fa 1177 }