Mert Us Matthew Hannay Logan Starr

Dependencies:   mbed 4DGL-uLCD-SE

Committer:
mus3
Date:
Thu Dec 01 05:53:57 2022 +0000
Revision:
11:43c89579ac52
Parent:
9:1f33c0f299ae
Child:
12:7ef20deb9d5e
Child:
14:f390d08e5f92
Added code to read joystick inputs, fixed state machine, revamped possible moves code.

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
mus3 11:43c89579ac52 520 // gets the pixel coordinates of the top left of the square
mus3 11:43c89579ac52 521 static pixelCoord getTopLeftOfSquare(boardPos pos)
mus3 11:43c89579ac52 522 {
mus3 11:43c89579ac52 523 return getTopLeftOfSquare(pos.row, pos.column);
mus3 11:43c89579ac52 524 }
mus3 11:43c89579ac52 525 static pixelCoord getTopLeftOfSquare(int row, int column)
mus3 11:43c89579ac52 526 {
mus3 11:43c89579ac52 527 pixelCoord topLeft;
mus3 11:43c89579ac52 528 topLeft.x = 16 * column;
mus3 11:43c89579ac52 529 topLeft.y = 112 - 16 * row;
mus3 11:43c89579ac52 530 return topLeft;
mhannay3 5:b553c51b3b85 531 }
mhannay3 5:b553c51b3b85 532
mus3 11:43c89579ac52 533 // piece sprites (12 x 12)
mus3 11:43c89579ac52 534 static void drawPawn(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 535 {
mus3 11:43c89579ac52 536 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 537 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 538 int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _,
mus3 11:43c89579ac52 539 _, _, _, _, _, _, _, _, _, _, _, _,
mus3 11:43c89579ac52 540 _, _, _, _, _, _, _, _, _, _, _, _,
mus3 11:43c89579ac52 541 _, _, _, _, _, _, _, _, _, _, _, _,
mus3 11:43c89579ac52 542 _, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 543 _, _, _, _, X, X, X, X, _, _, _, _,
mus3 11:43c89579ac52 544 _, _, _, _, X, X, X, X, _, _, _, _,
mus3 11:43c89579ac52 545 _, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 546 _, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 547 _, _, _, _, X, X, X, X, _, _, _, _,
mus3 11:43c89579ac52 548 _, _, X, X, X, X, X, X, X, X, _, _,
mus3 11:43c89579ac52 549 _, _, X, X, X, X, X, X, X, X, _, _
mus3 11:43c89579ac52 550 };
mus3 11:43c89579ac52 551 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 552 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mus3 11:43c89579ac52 553 }
mus3 11:43c89579ac52 554 static void drawRook(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 555 {
mus3 11:43c89579ac52 556 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 557 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 558 int sprite[144] = {X, X, _, X, X, _, _, X, X, _, X, X,
mus3 11:43c89579ac52 559 X, X, _, X, X, _, _, X, X, _, X, X,
mus3 11:43c89579ac52 560 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 561 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 562 _, X, X, X, _, X, X, _, X, X, X, _,
mus3 11:43c89579ac52 563 _, X, X, X, _, X, X, _, X, X, X, _,
mus3 11:43c89579ac52 564 _, _, X, X, _, X, X, _, X, X, _, _,
mus3 11:43c89579ac52 565 _, _, X, X, _, X, X, _, X, X, _, _,
mus3 11:43c89579ac52 566 _, _, X, X, _, X, X, _, X, X, _, _,
mus3 11:43c89579ac52 567 _, X, X, X, X, X, X, X, X, X, X, _,
mus3 11:43c89579ac52 568 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 569 X, X, X, X, X, X, X, X, X, X, X, X
mus3 11:43c89579ac52 570 };
mus3 11:43c89579ac52 571 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 572 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mus3 11:43c89579ac52 573 }
mus3 11:43c89579ac52 574 static void drawKnight(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 575 {
mus3 11:43c89579ac52 576 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 577 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 578 int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _,
mus3 11:43c89579ac52 579 _, _, _, _, _, X, X, _, X, X, _, _,
mus3 11:43c89579ac52 580 _, _, _, _, _, X, X, _, X, X, _, _,
mus3 11:43c89579ac52 581 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 582 _, _, X, X, X, X, X, _, X, _, _, _,
mus3 11:43c89579ac52 583 _, _, X, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 584 _, _, _, _, _, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 585 _, _, _, _, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 586 _, _, _, X, X, X, X, X, X, X, _, _,
mus3 11:43c89579ac52 587 _, _, X, X, X, X, X, X, X, X, _, _,
mus3 11:43c89579ac52 588 _, X, X, X, X, X, X, X, X, X, X, _,
mus3 11:43c89579ac52 589 _, X, X, X, X, X, X, X, X, X, X, _
mus3 11:43c89579ac52 590 };
mus3 11:43c89579ac52 591 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 592 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mus3 11:43c89579ac52 593 }
mus3 11:43c89579ac52 594 static void drawBishop(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 595 {
mus3 11:43c89579ac52 596 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 597 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 598 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 599 _, _, _, _, X, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 600 _, _, _, X, X, X, _, _, X, _, _, _,
mus3 11:43c89579ac52 601 _, _, _, X, X, _, _, X, X, _, _, _,
mus3 11:43c89579ac52 602 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 603 _, _, _, _, X, X, X, X, _, _, _, _,
mus3 11:43c89579ac52 604 _, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 605 _, _, _, _, X, X, X, X, _, _, _, _,
mus3 11:43c89579ac52 606 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 607 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 608 _, _, X, X, X, X, X, X, X, X, _, _,
mus3 11:43c89579ac52 609 _, _, X, X, X, X, X, X, X, X, _, _
mus3 11:43c89579ac52 610 };
mus3 11:43c89579ac52 611 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 612 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mus3 11:43c89579ac52 613 }
mus3 11:43c89579ac52 614 static void drawQueen(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 615 {
mus3 11:43c89579ac52 616 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 617 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 618 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 619 _, _, X, _, _, X, X, _, _, X, _, _,
mus3 11:43c89579ac52 620 X, _, X, X, _, X, X, _, X, X, _, X,
mus3 11:43c89579ac52 621 X, _, X, X, _, X, X, _, X, X, _, X,
mus3 11:43c89579ac52 622 X, _, X, X, _, X, X, _, X, X, _, X,
mus3 11:43c89579ac52 623 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 624 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 625 X, X, _, X, X, X, X, X, X, _, X, X,
mus3 11:43c89579ac52 626 X, X, X, X, _, X, X, _, X, X, X, X,
mus3 11:43c89579ac52 627 _, X, X, X, X, X, X, X, X, X, X, _,
mus3 11:43c89579ac52 628 _, _, X, X, X, X, X, X, X, X, _, _,
mus3 11:43c89579ac52 629 _, X, X, X, X, X, X, X, X, X, X, _
mus3 11:43c89579ac52 630 };
mus3 11:43c89579ac52 631 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 632 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mus3 11:43c89579ac52 633 }
mus3 11:43c89579ac52 634 static void drawKing(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 635 {
mus3 11:43c89579ac52 636 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 637 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 638 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 639 _, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 640 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 641 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 642 X, X, _, _, _, X, X, _, _, _, X, X,
mus3 11:43c89579ac52 643 X, X, X, X, _, X, X, _, X, X, X, X,
mus3 11:43c89579ac52 644 X, _, X, X, X, X, X, X, X, X, _, X,
mus3 11:43c89579ac52 645 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 646 X, X, X, _, X, X, X, X, _, X, X, X,
mus3 11:43c89579ac52 647 _, X, X, X, X, X, X, X, X, X, X, _,
mus3 11:43c89579ac52 648 _, _, X, X, X, _, _, X, X, X, _, _,
mus3 11:43c89579ac52 649 _, X, X, X, X, X, X, X, X, X, X, _
mus3 11:43c89579ac52 650 };
mus3 11:43c89579ac52 651 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 652 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mhannay3 5:b553c51b3b85 653 }
mhannay3 5:b553c51b3b85 654
mhannay3 5:b553c51b3b85 655 public:
mhannay3 5:b553c51b3b85 656 BoardState getBoardState()
mhannay3 5:b553c51b3b85 657 {
mhannay3 5:b553c51b3b85 658 return boardState;
mhannay3 5:b553c51b3b85 659 }
mhannay3 5:b553c51b3b85 660
mhannay3 5:b553c51b3b85 661 void setBoardState(BoardState newBoardState)
mhannay3 5:b553c51b3b85 662 {
mhannay3 5:b553c51b3b85 663 boardState = newBoardState;
mhannay3 5:b553c51b3b85 664 }
mhannay3 5:b553c51b3b85 665
mhannay3 5:b553c51b3b85 666 // initializes the starting board state
mhannay3 5:b553c51b3b85 667 GameBoard()
mhannay3 5:b553c51b3b85 668 {
mhannay3 5:b553c51b3b85 669 // draw board
mhannay3 5:b553c51b3b85 670 for (int row = 0; row < 8; row++) {
mhannay3 5:b553c51b3b85 671 for (int column = 0; column < 8; column++) {
mhannay3 5:b553c51b3b85 672 uint64_t color;
mhannay3 5:b553c51b3b85 673 if ((row + column) % 2 == 0) {
mhannay3 5:b553c51b3b85 674 color = BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 675 } else {
mhannay3 5:b553c51b3b85 676 color = BOARD_LIGHT_COLOR;
mhannay3 5:b553c51b3b85 677 }
mhannay3 6:1c4dd5e24b8d 678 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 679 uLCD.filled_rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, color);
mhannay3 5:b553c51b3b85 680 }
mhannay3 5:b553c51b3b85 681 }
mhannay3 5:b553c51b3b85 682 // draw pieces
mhannay3 5:b553c51b3b85 683 placePieceAndDraw(wR, 0, 0);
mhannay3 5:b553c51b3b85 684 placePieceAndDraw(wN, 0, 1);
mhannay3 5:b553c51b3b85 685 placePieceAndDraw(wB, 0, 2);
mhannay3 5:b553c51b3b85 686 placePieceAndDraw(wQ, 0, 3);
mhannay3 5:b553c51b3b85 687 placePieceAndDraw(wK, 0, 4);
mhannay3 5:b553c51b3b85 688 placePieceAndDraw(wB, 0, 5);
mhannay3 5:b553c51b3b85 689 placePieceAndDraw(wN, 0, 6);
mhannay3 5:b553c51b3b85 690 placePieceAndDraw(wR, 0, 7);
mhannay3 5:b553c51b3b85 691 placePieceAndDraw(bR, 7, 0);
mhannay3 5:b553c51b3b85 692 placePieceAndDraw(bN, 7, 1);
mhannay3 5:b553c51b3b85 693 placePieceAndDraw(bB, 7, 2);
mhannay3 5:b553c51b3b85 694 placePieceAndDraw(bQ, 7, 3);
mhannay3 5:b553c51b3b85 695 placePieceAndDraw(bK, 7, 4);
mhannay3 5:b553c51b3b85 696 placePieceAndDraw(bB, 7, 5);
mhannay3 5:b553c51b3b85 697 placePieceAndDraw(bN, 7, 6);
mhannay3 5:b553c51b3b85 698 placePieceAndDraw(bR, 7, 7);
mhannay3 5:b553c51b3b85 699 for (int i = 0; i < 8; i++) {
mhannay3 5:b553c51b3b85 700 placePieceAndDraw(w, 1, i);
mhannay3 5:b553c51b3b85 701 placePieceAndDraw(b, 6, i);
mhannay3 5:b553c51b3b85 702 }
mhannay3 5:b553c51b3b85 703 }
mhannay3 5:b553c51b3b85 704
mhannay3 5:b553c51b3b85 705 // PIECE MOVEMENT AND GRAPHICS FUNCTIONS
mhannay3 5:b553c51b3b85 706
mhannay3 5:b553c51b3b85 707 // returns the piece at a given location
mhannay3 7:9e01c9a334dc 708 Piece getPiece(boardPos pos)
mhannay3 7:9e01c9a334dc 709 {
mhannay3 7:9e01c9a334dc 710 return getPiece(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 711 }
mhannay3 5:b553c51b3b85 712 Piece getPiece(int row, int column)
mhannay3 5:b553c51b3b85 713 {
mhannay3 5:b553c51b3b85 714 return boardState.getPiece(row, column);
mhannay3 5:b553c51b3b85 715 }
mhannay3 5:b553c51b3b85 716
mhannay3 5:b553c51b3b85 717 /* puts the bit representation of a piece at the set position of the board
mhannay3 5:b553c51b3b85 718 assumes that the position of the board is emptied beforehand
mhannay3 5:b553c51b3b85 719 */
mhannay3 7:9e01c9a334dc 720 void placePieceAndDraw(Piece piece, boardPos pos)
mhannay3 7:9e01c9a334dc 721 {
mhannay3 7:9e01c9a334dc 722 placePieceAndDraw(piece, pos.row, pos.column);
mhannay3 7:9e01c9a334dc 723 }
mhannay3 5:b553c51b3b85 724 void placePieceAndDraw(Piece piece, int row, int column)
mhannay3 5:b553c51b3b85 725 {
mhannay3 5:b553c51b3b85 726 boardState.placePiece(piece, row, column);
mhannay3 6:1c4dd5e24b8d 727 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 728 switch(piece) {
mus3 11:43c89579ac52 729 case wK:
mus3 11:43c89579ac52 730 case bK:
mus3 11:43c89579ac52 731 drawKing(row, column, piece==wK, (row+column)%2);
mus3 11:43c89579ac52 732 break;
mus3 11:43c89579ac52 733 case wQ:
mus3 11:43c89579ac52 734 case bQ:
mus3 11:43c89579ac52 735 drawQueen(row, column, piece==wQ, (row+column)%2);
mus3 11:43c89579ac52 736 break;
mus3 11:43c89579ac52 737 case wB:
mus3 11:43c89579ac52 738 case bB:
mus3 11:43c89579ac52 739 drawBishop(row, column, piece==wB, (row+column)%2);
mus3 11:43c89579ac52 740 break;
mus3 11:43c89579ac52 741 case wN:
mus3 11:43c89579ac52 742 case bN:
mus3 11:43c89579ac52 743 drawKnight(row, column, piece==wN, (row+column)%2);
mus3 11:43c89579ac52 744 break;
mus3 11:43c89579ac52 745 case wR:
mus3 11:43c89579ac52 746 case bR:
mus3 11:43c89579ac52 747 drawRook(row, column, piece==wR, (row+column)%2);
mus3 11:43c89579ac52 748 break;
mus3 11:43c89579ac52 749 case w:
mus3 11:43c89579ac52 750 case b:
mus3 11:43c89579ac52 751 drawPawn(row, column, piece==w, (row+column)%2);
mus3 11:43c89579ac52 752 break;
mhannay3 5:b553c51b3b85 753 }
mhannay3 5:b553c51b3b85 754 }
mhannay3 5:b553c51b3b85 755
mhannay3 5:b553c51b3b85 756 /* removes a piece from the set position of the board
mhannay3 5:b553c51b3b85 757 returns the bit representation of the piece
mhannay3 5:b553c51b3b85 758 */
mhannay3 7:9e01c9a334dc 759 Piece removePieceAndDraw(boardPos pos)
mhannay3 7:9e01c9a334dc 760 {
mhannay3 7:9e01c9a334dc 761 return removePieceAndDraw(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 762 }
mhannay3 5:b553c51b3b85 763 Piece removePieceAndDraw(int row, int column)
mhannay3 5:b553c51b3b85 764 {
mhannay3 5:b553c51b3b85 765 Piece removedPiece = boardState.removePiece(row, column);
mhannay3 6:1c4dd5e24b8d 766 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 1:cd78922f70fa 767 uint64_t color;
mhannay3 1:cd78922f70fa 768 if ((row + column) % 2 == 0) {
mhannay3 1:cd78922f70fa 769 color = BOARD_DARK_COLOR;
mhannay3 1:cd78922f70fa 770 } else {
mhannay3 1:cd78922f70fa 771 color = BOARD_LIGHT_COLOR;
mhannay3 1:cd78922f70fa 772 }
mhannay3 5:b553c51b3b85 773 uLCD.filled_rectangle(tl.x+2, tl.y+2, tl.x + 13, tl.y + 13, color);
mhannay3 5:b553c51b3b85 774 return removedPiece;
mhannay3 5:b553c51b3b85 775 }
mhannay3 5:b553c51b3b85 776
mhannay3 5:b553c51b3b85 777 /* moves a piece from one position to another
mhannay3 5:b553c51b3b85 778 returns the captured piece
mhannay3 5:b553c51b3b85 779 */
mhannay3 7:9e01c9a334dc 780 Piece movePieceAndDraw(boardPos startPos, boardPos endPos)
mhannay3 7:9e01c9a334dc 781 {
mhannay3 7:9e01c9a334dc 782 return movePieceAndDraw(startPos.row, startPos.column, endPos.row, endPos.column);
mhannay3 7:9e01c9a334dc 783 }
mhannay3 5:b553c51b3b85 784 Piece movePieceAndDraw(int startRow, int startColumn, int endRow, int endColumn)
mhannay3 5:b553c51b3b85 785 {
mhannay3 5:b553c51b3b85 786 Piece movingPiece = removePieceAndDraw(startRow, startColumn);
mhannay3 5:b553c51b3b85 787 Piece capturedPiece = boardState.removePiece(endRow, endColumn);
mhannay3 5:b553c51b3b85 788 placePieceAndDraw(movingPiece, endRow, endColumn);
mhannay3 5:b553c51b3b85 789 return capturedPiece;
mhannay3 1:cd78922f70fa 790 }
mhannay3 1:cd78922f70fa 791
mhannay3 5:b553c51b3b85 792 // SQUARE BORDER GRAPHICS FUNCTIONS
mhannay3 5:b553c51b3b85 793
mhannay3 5:b553c51b3b85 794 // removes selection border around square
mhannay3 7:9e01c9a334dc 795 void unselectSquare(boardPos pos)
mhannay3 7:9e01c9a334dc 796 {
mhannay3 7:9e01c9a334dc 797 unselectSquare(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 798 }
mhannay3 5:b553c51b3b85 799 void unselectSquare(int row, int column)
mhannay3 5:b553c51b3b85 800 {
mus3 11:43c89579ac52 801 if (row < 0 || row > 7 || column < 0 || column > 7) {
mus3 11:43c89579ac52 802 return;
mus3 11:43c89579ac52 803 }
mhannay3 6:1c4dd5e24b8d 804 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 805 uint64_t color;
mhannay3 5:b553c51b3b85 806 if ((row + column) % 2 == 0) {
mhannay3 5:b553c51b3b85 807 color = BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 808 } else {
mhannay3 5:b553c51b3b85 809 color = BOARD_LIGHT_COLOR;
mhannay3 5:b553c51b3b85 810 }
mhannay3 5:b553c51b3b85 811 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, color);
mhannay3 5:b553c51b3b85 812 }
mhannay3 5:b553c51b3b85 813
mhannay3 7:9e01c9a334dc 814 // draws the hover border around square
mhannay3 7:9e01c9a334dc 815 void hoverSquare(boardPos pos)
mhannay3 7:9e01c9a334dc 816 {
mhannay3 7:9e01c9a334dc 817 hoverSquare(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 818 }
mhannay3 5:b553c51b3b85 819 void hoverSquare(int row, int column)
mhannay3 5:b553c51b3b85 820 {
mhannay3 6:1c4dd5e24b8d 821 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 822 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, HOVER_COLOR);
mhannay3 1:cd78922f70fa 823 }
mhannay3 1:cd78922f70fa 824
mhannay3 1:cd78922f70fa 825 // draws selection border around square
mhannay3 7:9e01c9a334dc 826 void selectSquare(boardPos pos)
mhannay3 7:9e01c9a334dc 827 {
mhannay3 7:9e01c9a334dc 828 selectSquare(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 829 }
mhannay3 5:b553c51b3b85 830 void selectSquare(int row, int column)
mhannay3 5:b553c51b3b85 831 {
mus3 11:43c89579ac52 832 if (row < 0 || row > 7 || column < 0 || column > 7) {
mus3 11:43c89579ac52 833 return;
mus3 11:43c89579ac52 834 }
mhannay3 6:1c4dd5e24b8d 835 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 836 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, SELECTED_COLOR);
mhannay3 1:cd78922f70fa 837 }
mhannay3 9:1f33c0f299ae 838
mhannay3 8:928d5d33258f 839 // draws the movement border around square
mhannay3 9:1f33c0f299ae 840 void movementSquare(boardPos pos)
mhannay3 8:928d5d33258f 841 {
mhannay3 9:1f33c0f299ae 842 movementSquare(pos.row, pos.column);
mhannay3 8:928d5d33258f 843 }
mhannay3 9:1f33c0f299ae 844 void movementSquare(int row, int column)
mhannay3 8:928d5d33258f 845 {
mhannay3 8:928d5d33258f 846 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 8:928d5d33258f 847 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, MOVE_COLOR);
mhannay3 8:928d5d33258f 848 }
mhannay3 5:b553c51b3b85 849 };
mus3 0:d16cc59110a3 850
mhannay3 7:9e01c9a334dc 851 // game variables
mhannay3 7:9e01c9a334dc 852 GameBoard gameBoard;
mhannay3 7:9e01c9a334dc 853 GameState state = whiteSelecting;
mhannay3 7:9e01c9a334dc 854 boardPos cursorPos = (boardPos)
mhannay3 7:9e01c9a334dc 855 {
mhannay3 7:9e01c9a334dc 856 3, 4
mhannay3 7:9e01c9a334dc 857 };
mhannay3 8:928d5d33258f 858 boardPos selectedPos;
mhannay3 9:1f33c0f299ae 859 Piece selectedPiece;
mhannay3 9:1f33c0f299ae 860 std::vector<boardPos> possibleMoves;
mhannay3 7:9e01c9a334dc 861
mhannay3 7:9e01c9a334dc 862 // callbacks
mhannay3 9:1f33c0f299ae 863 void moveCursor(int rowChange, int columnChange)
mhannay3 9:1f33c0f299ae 864 {
mhannay3 9:1f33c0f299ae 865 // calculate new positoin that is within board bounds
mhannay3 9:1f33c0f299ae 866 int newRow = cursorPos.row + rowChange;
mhannay3 9:1f33c0f299ae 867 newRow = newRow <= 7 ? newRow : 7;
mhannay3 9:1f33c0f299ae 868 newRow = newRow >= 0 ? newRow : 0;
mhannay3 9:1f33c0f299ae 869 int newColumn = cursorPos.column + columnChange;
mhannay3 9:1f33c0f299ae 870 newColumn = newColumn <= 7 ? newColumn : 7;
mhannay3 9:1f33c0f299ae 871 newColumn = newColumn >= 0 ? newColumn : 0;
mhannay3 9:1f33c0f299ae 872 boardPos newPos = (boardPos) {
mhannay3 9:1f33c0f299ae 873 newRow, newColumn
mhannay3 9:1f33c0f299ae 874 };
mhannay3 9:1f33c0f299ae 875
mhannay3 9:1f33c0f299ae 876 // draw border around square that should be there after moving cursor off
mhannay3 9:1f33c0f299ae 877 if (cursorPos == selectedPos) {
mhannay3 9:1f33c0f299ae 878 gameBoard.selectSquare(cursorPos);
mhannay3 9:1f33c0f299ae 879 } else if (std::find(possibleMoves.begin(), possibleMoves.end(), cursorPos) != possibleMoves.end()) {
mhannay3 9:1f33c0f299ae 880 gameBoard.movementSquare(cursorPos);
mhannay3 9:1f33c0f299ae 881 } else {
mhannay3 9:1f33c0f299ae 882 gameBoard.unselectSquare(cursorPos);
mhannay3 9:1f33c0f299ae 883 }
mhannay3 9:1f33c0f299ae 884
mhannay3 9:1f33c0f299ae 885 // draw hover rectangle over new square
mhannay3 9:1f33c0f299ae 886 cursorPos = newPos;
mhannay3 9:1f33c0f299ae 887 gameBoard.hoverSquare(cursorPos);
mhannay3 9:1f33c0f299ae 888 }
mhannay3 9:1f33c0f299ae 889
mhannay3 7:9e01c9a334dc 890 void joyStickUp()
mhannay3 7:9e01c9a334dc 891 {
mhannay3 9:1f33c0f299ae 892 moveCursor(1, 0);
mhannay3 7:9e01c9a334dc 893 }
mhannay3 7:9e01c9a334dc 894
mhannay3 7:9e01c9a334dc 895 void joyStickDown()
mhannay3 7:9e01c9a334dc 896 {
mhannay3 9:1f33c0f299ae 897 moveCursor(-1, 0);
mhannay3 7:9e01c9a334dc 898 }
mhannay3 7:9e01c9a334dc 899
mhannay3 7:9e01c9a334dc 900 void joyStickLeft()
mhannay3 7:9e01c9a334dc 901 {
mhannay3 9:1f33c0f299ae 902 moveCursor(0, -1);
mhannay3 7:9e01c9a334dc 903 }
mhannay3 7:9e01c9a334dc 904
mhannay3 7:9e01c9a334dc 905 void joyStickRight()
mhannay3 7:9e01c9a334dc 906 {
mhannay3 9:1f33c0f299ae 907 moveCursor(0, 1);
mhannay3 7:9e01c9a334dc 908 }
mhannay3 7:9e01c9a334dc 909
mhannay3 7:9e01c9a334dc 910 void joyStickPressed()
mhannay3 7:9e01c9a334dc 911 {
mhannay3 8:928d5d33258f 912 switch(state) {
mhannay3 8:928d5d33258f 913 case whiteSelecting:
mhannay3 9:1f33c0f299ae 914 case blackSelecting: {
mus3 11:43c89579ac52 915 possibleMoves.clear();
mhannay3 8:928d5d33258f 916 selectedPos = cursorPos;
mhannay3 9:1f33c0f299ae 917 Piece tempPiece = gameBoard.getPiece(cursorPos);
mhannay3 9:1f33c0f299ae 918 std::vector<Piece> pickablePieces = state == whiteSelecting ? whitePieces : blackPieces;
mhannay3 9:1f33c0f299ae 919 // check that piece is white and able to be picked up
mhannay3 9:1f33c0f299ae 920 if (std::find(pickablePieces.begin(), pickablePieces.end(), tempPiece) != pickablePieces.end()) {
mhannay3 9:1f33c0f299ae 921 selectedPiece = tempPiece;
mhannay3 9:1f33c0f299ae 922 possibleMoves = gameBoard.getBoardState().getMoves(cursorPos);
mhannay3 9:1f33c0f299ae 923 // draw movement squares
mhannay3 9:1f33c0f299ae 924 for (std::vector<boardPos>::iterator it = possibleMoves.begin(); it != possibleMoves.end(); ++it) {
mhannay3 9:1f33c0f299ae 925 gameBoard.movementSquare(*it);
mhannay3 9:1f33c0f299ae 926 }
mhannay3 9:1f33c0f299ae 927 gameBoard.selectSquare(selectedPos);
mus3 11:43c89579ac52 928 // transition state
mhannay3 9:1f33c0f299ae 929 state = state == whiteSelecting ? whitePickedUp : blackPickedUp;
mus3 11:43c89579ac52 930 } else {
mus3 11:43c89579ac52 931 selectedPos = (boardPos)
mus3 11:43c89579ac52 932 {
mus3 11:43c89579ac52 933 10, 10
mus3 11:43c89579ac52 934 };
mhannay3 9:1f33c0f299ae 935 }
mhannay3 8:928d5d33258f 936 break;
mhannay3 9:1f33c0f299ae 937 }
mhannay3 9:1f33c0f299ae 938 case whitePickedUp:
mhannay3 9:1f33c0f299ae 939 case blackPickedUp: {
mhannay3 9:1f33c0f299ae 940 // check if move is valid
mhannay3 9:1f33c0f299ae 941 if (std::find(possibleMoves.begin(), possibleMoves.end(), cursorPos) != possibleMoves.end()) {
mhannay3 9:1f33c0f299ae 942 // move the piece
mhannay3 9:1f33c0f299ae 943 Piece capturedPiece = gameBoard.movePieceAndDraw(selectedPos, cursorPos);
mhannay3 9:1f33c0f299ae 944 // check for king capture
mhannay3 9:1f33c0f299ae 945 if (state == whitePickedUp && capturedPiece == bK || state == blackPickedUp && capturedPiece == wK) {
mhannay3 9:1f33c0f299ae 946 // game end
mhannay3 9:1f33c0f299ae 947 }
mhannay3 9:1f33c0f299ae 948 // transition state
mhannay3 9:1f33c0f299ae 949 state = state == whitePickedUp ? blackSelecting : whiteSelecting;
mhannay3 9:1f33c0f299ae 950 // check if placing piece back down
mus3 11:43c89579ac52 951 } else {
mhannay3 9:1f33c0f299ae 952 // transition state
mhannay3 9:1f33c0f299ae 953 state = state == whitePickedUp ? whiteSelecting : blackSelecting;
mhannay3 9:1f33c0f299ae 954 }
mhannay3 9:1f33c0f299ae 955 // unselect movement squares
mhannay3 9:1f33c0f299ae 956 for (std::vector<boardPos>::iterator it = possibleMoves.begin(); it != possibleMoves.end(); ++it) {
mhannay3 9:1f33c0f299ae 957 gameBoard.unselectSquare(*it);
mhannay3 9:1f33c0f299ae 958 }
mhannay3 9:1f33c0f299ae 959 gameBoard.unselectSquare(selectedPos);
mhannay3 9:1f33c0f299ae 960 gameBoard.hoverSquare(cursorPos);
mus3 11:43c89579ac52 961 possibleMoves.clear();
mus3 11:43c89579ac52 962 selectedPos = (boardPos)
mus3 11:43c89579ac52 963 {
mus3 11:43c89579ac52 964 10, 10
mus3 11:43c89579ac52 965 };
mhannay3 8:928d5d33258f 966 break;
mhannay3 9:1f33c0f299ae 967 }
mhannay3 9:1f33c0f299ae 968 case whiteAI:
mhannay3 9:1f33c0f299ae 969 case blackAI: {
mhannay3 8:928d5d33258f 970 break;
mhannay3 9:1f33c0f299ae 971 }
mhannay3 8:928d5d33258f 972 }
mhannay3 7:9e01c9a334dc 973 }
mhannay3 7:9e01c9a334dc 974
mus3 11:43c89579ac52 975 Nav_Switch myNav(p9, p6, p7, p5, p8); //pin order on Sparkfun breakout
mus3 11:43c89579ac52 976
mhannay3 5:b553c51b3b85 977 int main()
mhannay3 5:b553c51b3b85 978 {
mus3 11:43c89579ac52 979 //gameBoard = GameBoard();
mhannay3 9:1f33c0f299ae 980 whitePieces.push_back(wK);
mhannay3 9:1f33c0f299ae 981 whitePieces.push_back(wQ);
mhannay3 9:1f33c0f299ae 982 whitePieces.push_back(wB);
mhannay3 9:1f33c0f299ae 983 whitePieces.push_back(wN);
mhannay3 9:1f33c0f299ae 984 whitePieces.push_back(wR);
mhannay3 9:1f33c0f299ae 985 whitePieces.push_back(w);
mhannay3 9:1f33c0f299ae 986 blackPieces.push_back(bK);
mhannay3 9:1f33c0f299ae 987 blackPieces.push_back(bQ);
mhannay3 9:1f33c0f299ae 988 blackPieces.push_back(bB);
mhannay3 9:1f33c0f299ae 989 blackPieces.push_back(bN);
mhannay3 9:1f33c0f299ae 990 blackPieces.push_back(bR);
mhannay3 9:1f33c0f299ae 991 blackPieces.push_back(b);
mus3 11:43c89579ac52 992 moveCursor(0, 0);
mus3 11:43c89579ac52 993 while (1) {
mus3 11:43c89579ac52 994 if (myNav.up()) {
mus3 11:43c89579ac52 995 joyStickUp();
mus3 11:43c89579ac52 996 } else if (myNav.down()) {
mus3 11:43c89579ac52 997 joyStickDown();
mus3 11:43c89579ac52 998 } else if (myNav.left()) {
mus3 11:43c89579ac52 999 joyStickLeft();
mus3 11:43c89579ac52 1000 } else if (myNav.right()) {
mus3 11:43c89579ac52 1001 joyStickRight();
mus3 11:43c89579ac52 1002 } else if (myNav.fire()) {
mus3 11:43c89579ac52 1003 joyStickPressed();
mus3 11:43c89579ac52 1004 }
mus3 11:43c89579ac52 1005 wait(0.25);
mus3 11:43c89579ac52 1006 }
mhannay3 1:cd78922f70fa 1007 }