Mert Us Matthew Hannay Logan Starr

Dependencies:   mbed 4DGL-uLCD-SE

Committer:
mhannay3
Date:
Mon Nov 14 21:36:39 2022 +0000
Revision:
6:1c4dd5e24b8d
Parent:
5:b553c51b3b85
Child:
7:9e01c9a334dc
added piece movement calculation

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 1:cd78922f70fa 8
mhannay3 6:1c4dd5e24b8d 9 struct pixelCoord {
mhannay3 1:cd78922f70fa 10 uint8_t x;
mhannay3 1:cd78922f70fa 11 uint8_t y;
mhannay3 5:b553c51b3b85 12 };
mhannay3 1:cd78922f70fa 13
mhannay3 6:1c4dd5e24b8d 14 struct boardPos {
mhannay3 6:1c4dd5e24b8d 15 uint8_t row;
mhannay3 6:1c4dd5e24b8d 16 uint8_t column;
mhannay3 6:1c4dd5e24b8d 17 };
mhannay3 6:1c4dd5e24b8d 18
mhannay3 5:b553c51b3b85 19 class BoardState
mhannay3 5:b553c51b3b85 20 {
mhannay3 5:b553c51b3b85 21 private:
mhannay3 5:b553c51b3b85 22 Piece array[64];
mhannay3 5:b553c51b3b85 23 public:
mhannay3 4:5798e4062350 24 // calculates the advantage difference for the board state
mhannay3 5:b553c51b3b85 25 float calculateBoardState()
mhannay3 5:b553c51b3b85 26 {
mhannay3 4:5798e4062350 27 return 0.0;
mhannay3 4:5798e4062350 28 }
mhannay3 5:b553c51b3b85 29
mhannay3 4:5798e4062350 30 // returns the piece at a given location
mhannay3 5:b553c51b3b85 31 Piece getPiece(int row, int column)
mhannay3 5:b553c51b3b85 32 {
mhannay3 5:b553c51b3b85 33 return array[column + 8 * row];
mhannay3 4:5798e4062350 34 }
mhannay3 4:5798e4062350 35
mhannay3 5:b553c51b3b85 36 // puts a piece at a given location
mhannay3 5:b553c51b3b85 37 void placePiece(Piece piece, int row, int column)
mhannay3 5:b553c51b3b85 38 {
mhannay3 5:b553c51b3b85 39 array[column + 8 * row] = piece;
mhannay3 4:5798e4062350 40 }
mhannay3 4:5798e4062350 41
mhannay3 4:5798e4062350 42 /* removes a piece from the set position of the board
mhannay3 4:5798e4062350 43 returns the bit representation of the piece
mhannay3 4:5798e4062350 44 */
mhannay3 5:b553c51b3b85 45 Piece removePiece(int row, int column)
mhannay3 5:b553c51b3b85 46 {
mhannay3 5:b553c51b3b85 47 Piece removedPiece = array[column + 8 * row];
mhannay3 5:b553c51b3b85 48 array[column + 8 * row] = e;
mhannay3 4:5798e4062350 49 return removedPiece;
mhannay3 1:cd78922f70fa 50 }
mhannay3 1:cd78922f70fa 51
mhannay3 1:cd78922f70fa 52 /* moves a piece from one position to another
mhannay3 1:cd78922f70fa 53 returns the captured piece
mhannay3 1:cd78922f70fa 54 */
mhannay3 5:b553c51b3b85 55 Piece movePiece(int startRow, int startColumn, int endRow, int endColumn)
mhannay3 5:b553c51b3b85 56 {
mhannay3 5:b553c51b3b85 57 Piece movingPiece = removePiece(startRow, startColumn);
mhannay3 5:b553c51b3b85 58 Piece capturedPiece = removePiece(endRow, endColumn);
mhannay3 5:b553c51b3b85 59 placePiece(movingPiece, endRow, endColumn);
mhannay3 1:cd78922f70fa 60 return capturedPiece;
mhannay3 1:cd78922f70fa 61 }
mhannay3 6:1c4dd5e24b8d 62
mhannay3 6:1c4dd5e24b8d 63 // generates a list of possible moves for a piece
mhannay3 6:1c4dd5e24b8d 64 // returns moves
mhannay3 6:1c4dd5e24b8d 65 std::vector<boardPos> getMoves(int row, int column)
mhannay3 6:1c4dd5e24b8d 66 {
mhannay3 6:1c4dd5e24b8d 67 std::vector<boardPos> moves;
mhannay3 6:1c4dd5e24b8d 68 Piece movingPiece = getPiece(row, column);
mhannay3 6:1c4dd5e24b8d 69 bool isWhite;
mhannay3 6:1c4dd5e24b8d 70 switch(movingPiece) {
mhannay3 6:1c4dd5e24b8d 71 case wK:
mhannay3 6:1c4dd5e24b8d 72 case bK:
mhannay3 6:1c4dd5e24b8d 73 isWhite = movingPiece == wK;
mhannay3 6:1c4dd5e24b8d 74 for (int i = -1; i <= 1; i++) {
mhannay3 6:1c4dd5e24b8d 75 for (int j = -1; j <= 1; j++) {
mhannay3 6:1c4dd5e24b8d 76 if (validMove(isWhite, row + i, column + j)) {
mhannay3 6:1c4dd5e24b8d 77 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 78 row + i, column + j
mhannay3 6:1c4dd5e24b8d 79 });
mhannay3 6:1c4dd5e24b8d 80 }
mhannay3 6:1c4dd5e24b8d 81 }
mhannay3 6:1c4dd5e24b8d 82 }
mhannay3 6:1c4dd5e24b8d 83 break;
mhannay3 6:1c4dd5e24b8d 84 case wQ:
mhannay3 6:1c4dd5e24b8d 85 case bQ:
mhannay3 6:1c4dd5e24b8d 86 isWhite = movingPiece == wQ;
mhannay3 6:1c4dd5e24b8d 87 for (int i = 1; ; i++) {
mhannay3 6:1c4dd5e24b8d 88 if (validMove(isWhite, row + i, column + i)) {
mhannay3 6:1c4dd5e24b8d 89 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 90 row + i, column + i
mhannay3 6:1c4dd5e24b8d 91 });
mhannay3 6:1c4dd5e24b8d 92 } else {
mhannay3 6:1c4dd5e24b8d 93 break;
mhannay3 6:1c4dd5e24b8d 94 }
mhannay3 6:1c4dd5e24b8d 95 }
mhannay3 6:1c4dd5e24b8d 96 for (int i = 1; ; i++) {
mhannay3 6:1c4dd5e24b8d 97 if (validMove(isWhite, row - i, column + i)) {
mhannay3 6:1c4dd5e24b8d 98 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 99 row - i, column + i
mhannay3 6:1c4dd5e24b8d 100 });
mhannay3 6:1c4dd5e24b8d 101 } else {
mhannay3 6:1c4dd5e24b8d 102 break;
mhannay3 6:1c4dd5e24b8d 103 }
mhannay3 6:1c4dd5e24b8d 104 }
mhannay3 6:1c4dd5e24b8d 105 for (int i = 1; ; i++) {
mhannay3 6:1c4dd5e24b8d 106 if (validMove(isWhite, row + i, column - i)) {
mhannay3 6:1c4dd5e24b8d 107 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 108 row + i, column - i
mhannay3 6:1c4dd5e24b8d 109 });
mhannay3 6:1c4dd5e24b8d 110 } else {
mhannay3 6:1c4dd5e24b8d 111 break;
mhannay3 6:1c4dd5e24b8d 112 }
mhannay3 6:1c4dd5e24b8d 113 }
mhannay3 6:1c4dd5e24b8d 114 for (int i = 1; ; i++) {
mhannay3 6:1c4dd5e24b8d 115 if (validMove(isWhite, row - i, column - i)) {
mhannay3 6:1c4dd5e24b8d 116 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 117 row - i, column - i
mhannay3 6:1c4dd5e24b8d 118 });
mhannay3 6:1c4dd5e24b8d 119 } else {
mhannay3 6:1c4dd5e24b8d 120 break;
mhannay3 6:1c4dd5e24b8d 121 }
mhannay3 6:1c4dd5e24b8d 122 }
mhannay3 6:1c4dd5e24b8d 123 for (int i = 1; ; i++) {
mhannay3 6:1c4dd5e24b8d 124 if (validMove(isWhite, row + i, column)) {
mhannay3 6:1c4dd5e24b8d 125 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 126 row + i, column
mhannay3 6:1c4dd5e24b8d 127 });
mhannay3 6:1c4dd5e24b8d 128 } else {
mhannay3 6:1c4dd5e24b8d 129 break;
mhannay3 6:1c4dd5e24b8d 130 }
mhannay3 6:1c4dd5e24b8d 131 }
mhannay3 6:1c4dd5e24b8d 132 for (int i = 1; ; i++) {
mhannay3 6:1c4dd5e24b8d 133 if (validMove(isWhite, row, column + i)) {
mhannay3 6:1c4dd5e24b8d 134 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 135 row, column + i
mhannay3 6:1c4dd5e24b8d 136 });
mhannay3 6:1c4dd5e24b8d 137 } else {
mhannay3 6:1c4dd5e24b8d 138 break;
mhannay3 6:1c4dd5e24b8d 139 }
mhannay3 6:1c4dd5e24b8d 140 }
mhannay3 6:1c4dd5e24b8d 141 for (int i = 1; ; i++) {
mhannay3 6:1c4dd5e24b8d 142 if (validMove(isWhite, row, column - i)) {
mhannay3 6:1c4dd5e24b8d 143 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 144 row, column - i
mhannay3 6:1c4dd5e24b8d 145 });
mhannay3 6:1c4dd5e24b8d 146 } else {
mhannay3 6:1c4dd5e24b8d 147 break;
mhannay3 6:1c4dd5e24b8d 148 }
mhannay3 6:1c4dd5e24b8d 149 }
mhannay3 6:1c4dd5e24b8d 150 for (int i = 1; ; i++) {
mhannay3 6:1c4dd5e24b8d 151 if (validMove(isWhite, row - i, column)) {
mhannay3 6:1c4dd5e24b8d 152 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 153 row - i, column
mhannay3 6:1c4dd5e24b8d 154 });
mhannay3 6:1c4dd5e24b8d 155 } else {
mhannay3 6:1c4dd5e24b8d 156 break;
mhannay3 6:1c4dd5e24b8d 157 }
mhannay3 6:1c4dd5e24b8d 158 }
mhannay3 6:1c4dd5e24b8d 159 break;
mhannay3 6:1c4dd5e24b8d 160 case wB:
mhannay3 6:1c4dd5e24b8d 161 case bB:
mhannay3 6:1c4dd5e24b8d 162 isWhite = movingPiece == wB;
mhannay3 6:1c4dd5e24b8d 163 for (int i = 1; ; i++) {
mhannay3 6:1c4dd5e24b8d 164 if (validMove(isWhite, row + i, column + i)) {
mhannay3 6:1c4dd5e24b8d 165 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 166 row + i, column + i
mhannay3 6:1c4dd5e24b8d 167 });
mhannay3 6:1c4dd5e24b8d 168 } else {
mhannay3 6:1c4dd5e24b8d 169 break;
mhannay3 6:1c4dd5e24b8d 170 }
mhannay3 6:1c4dd5e24b8d 171 }
mhannay3 6:1c4dd5e24b8d 172 for (int i = 1; ; i++) {
mhannay3 6:1c4dd5e24b8d 173 if (validMove(isWhite, row - i, column + i)) {
mhannay3 6:1c4dd5e24b8d 174 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 175 row - i, column + i
mhannay3 6:1c4dd5e24b8d 176 });
mhannay3 6:1c4dd5e24b8d 177 } else {
mhannay3 6:1c4dd5e24b8d 178 break;
mhannay3 6:1c4dd5e24b8d 179 }
mhannay3 6:1c4dd5e24b8d 180 }
mhannay3 6:1c4dd5e24b8d 181 for (int i = 1; ; i++) {
mhannay3 6:1c4dd5e24b8d 182 if (validMove(isWhite, row + i, column - i)) {
mhannay3 6:1c4dd5e24b8d 183 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 184 row + i, column - i
mhannay3 6:1c4dd5e24b8d 185 });
mhannay3 6:1c4dd5e24b8d 186 } else {
mhannay3 6:1c4dd5e24b8d 187 break;
mhannay3 6:1c4dd5e24b8d 188 }
mhannay3 6:1c4dd5e24b8d 189 }
mhannay3 6:1c4dd5e24b8d 190 for (int i = 1; ; i++) {
mhannay3 6:1c4dd5e24b8d 191 if (validMove(isWhite, row - i, column - i)) {
mhannay3 6:1c4dd5e24b8d 192 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 193 row - i, column - i
mhannay3 6:1c4dd5e24b8d 194 });
mhannay3 6:1c4dd5e24b8d 195 } else {
mhannay3 6:1c4dd5e24b8d 196 break;
mhannay3 6:1c4dd5e24b8d 197 }
mhannay3 6:1c4dd5e24b8d 198 }
mhannay3 6:1c4dd5e24b8d 199 break;
mhannay3 6:1c4dd5e24b8d 200 case wN:
mhannay3 6:1c4dd5e24b8d 201 case bN:
mhannay3 6:1c4dd5e24b8d 202 isWhite = movingPiece == wN;
mhannay3 6:1c4dd5e24b8d 203 for (int i = -1; i <= 1; i+=2) {
mhannay3 6:1c4dd5e24b8d 204 for (int j = -1; j <= 1; j+=2) {
mhannay3 6:1c4dd5e24b8d 205 if (validMove(isWhite, row + 2*i, column + j)) {
mhannay3 6:1c4dd5e24b8d 206 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 207 row + 2*i, column + j
mhannay3 6:1c4dd5e24b8d 208 });
mhannay3 6:1c4dd5e24b8d 209 }
mhannay3 6:1c4dd5e24b8d 210 if (validMove(isWhite, row + i, column + 2*j)) {
mhannay3 6:1c4dd5e24b8d 211 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 212 row + i, column + 2*j
mhannay3 6:1c4dd5e24b8d 213 });
mhannay3 6:1c4dd5e24b8d 214 }
mhannay3 6:1c4dd5e24b8d 215 }
mhannay3 6:1c4dd5e24b8d 216 }
mhannay3 6:1c4dd5e24b8d 217 break;
mhannay3 6:1c4dd5e24b8d 218 case wR:
mhannay3 6:1c4dd5e24b8d 219 case bR:
mhannay3 6:1c4dd5e24b8d 220 isWhite = movingPiece == wR;
mhannay3 6:1c4dd5e24b8d 221 for (int i = 1; ; i++) {
mhannay3 6:1c4dd5e24b8d 222 if (validMove(isWhite, row + i, column)) {
mhannay3 6:1c4dd5e24b8d 223 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 224 row + i, column
mhannay3 6:1c4dd5e24b8d 225 });
mhannay3 6:1c4dd5e24b8d 226 } else {
mhannay3 6:1c4dd5e24b8d 227 break;
mhannay3 6:1c4dd5e24b8d 228 }
mhannay3 6:1c4dd5e24b8d 229 }
mhannay3 6:1c4dd5e24b8d 230 for (int i = 1; ; i++) {
mhannay3 6:1c4dd5e24b8d 231 if (validMove(isWhite, row, column + i)) {
mhannay3 6:1c4dd5e24b8d 232 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 233 row, column + i
mhannay3 6:1c4dd5e24b8d 234 });
mhannay3 6:1c4dd5e24b8d 235 } else {
mhannay3 6:1c4dd5e24b8d 236 break;
mhannay3 6:1c4dd5e24b8d 237 }
mhannay3 6:1c4dd5e24b8d 238 }
mhannay3 6:1c4dd5e24b8d 239 for (int i = 1; ; i++) {
mhannay3 6:1c4dd5e24b8d 240 if (validMove(isWhite, row, column - i)) {
mhannay3 6:1c4dd5e24b8d 241 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 242 row, column - i
mhannay3 6:1c4dd5e24b8d 243 });
mhannay3 6:1c4dd5e24b8d 244 } else {
mhannay3 6:1c4dd5e24b8d 245 break;
mhannay3 6:1c4dd5e24b8d 246 }
mhannay3 6:1c4dd5e24b8d 247 }
mhannay3 6:1c4dd5e24b8d 248 for (int i = 1; ; i++) {
mhannay3 6:1c4dd5e24b8d 249 if (validMove(isWhite, row - i, column)) {
mhannay3 6:1c4dd5e24b8d 250 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 251 row - i, column
mhannay3 6:1c4dd5e24b8d 252 });
mhannay3 6:1c4dd5e24b8d 253 } else {
mhannay3 6:1c4dd5e24b8d 254 break;
mhannay3 6:1c4dd5e24b8d 255 }
mhannay3 6:1c4dd5e24b8d 256 }
mhannay3 6:1c4dd5e24b8d 257 break;
mhannay3 6:1c4dd5e24b8d 258 case w:
mhannay3 6:1c4dd5e24b8d 259 case b:
mhannay3 6:1c4dd5e24b8d 260 isWhite = movingPiece == w;
mhannay3 6:1c4dd5e24b8d 261 int rowChange = isWhite ? 1 : -1;
mhannay3 6:1c4dd5e24b8d 262 if (validMove(isWhite, row + rowChange, column)) {
mhannay3 6:1c4dd5e24b8d 263 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 264 row + rowChange, column
mhannay3 6:1c4dd5e24b8d 265 });
mhannay3 6:1c4dd5e24b8d 266 }
mhannay3 6:1c4dd5e24b8d 267 if (validMove(isWhite, row + rowChange, column + 1) && getPiece(row + rowChange, column + 1) != e) {
mhannay3 6:1c4dd5e24b8d 268 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 269 row + rowChange, column + 1
mhannay3 6:1c4dd5e24b8d 270 });
mhannay3 6:1c4dd5e24b8d 271 }
mhannay3 6:1c4dd5e24b8d 272 if (validMove(isWhite, row + rowChange, column - 1) && getPiece(row + rowChange, column - 1) != e) {
mhannay3 6:1c4dd5e24b8d 273 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 274 row + rowChange, column - 1
mhannay3 6:1c4dd5e24b8d 275 });
mhannay3 6:1c4dd5e24b8d 276 }
mhannay3 6:1c4dd5e24b8d 277 break;
mhannay3 6:1c4dd5e24b8d 278 }
mhannay3 6:1c4dd5e24b8d 279 return moves;
mhannay3 6:1c4dd5e24b8d 280 }
mhannay3 6:1c4dd5e24b8d 281
mhannay3 6:1c4dd5e24b8d 282 bool validMove(bool isMovingPieceWhite, int row, int column)
mhannay3 6:1c4dd5e24b8d 283 {
mhannay3 6:1c4dd5e24b8d 284 if (row < 0 || row > 7 || column < 0 || column > 7) {
mhannay3 6:1c4dd5e24b8d 285 return false;
mhannay3 6:1c4dd5e24b8d 286 }
mhannay3 6:1c4dd5e24b8d 287 Piece capturedPiece = getPiece(row, column);
mhannay3 6:1c4dd5e24b8d 288 switch(capturedPiece) {
mhannay3 6:1c4dd5e24b8d 289 case wK:
mhannay3 6:1c4dd5e24b8d 290 case wQ:
mhannay3 6:1c4dd5e24b8d 291 case wR:
mhannay3 6:1c4dd5e24b8d 292 case wB:
mhannay3 6:1c4dd5e24b8d 293 case wN:
mhannay3 6:1c4dd5e24b8d 294 case w:
mhannay3 6:1c4dd5e24b8d 295 return !isMovingPieceWhite;
mhannay3 6:1c4dd5e24b8d 296 case bK:
mhannay3 6:1c4dd5e24b8d 297 case bQ:
mhannay3 6:1c4dd5e24b8d 298 case bR:
mhannay3 6:1c4dd5e24b8d 299 case bB:
mhannay3 6:1c4dd5e24b8d 300 case bN:
mhannay3 6:1c4dd5e24b8d 301 case b:
mhannay3 6:1c4dd5e24b8d 302 return isMovingPieceWhite;
mhannay3 6:1c4dd5e24b8d 303 case e:
mhannay3 6:1c4dd5e24b8d 304 return true;
mhannay3 6:1c4dd5e24b8d 305 }
mhannay3 6:1c4dd5e24b8d 306 return false;
mhannay3 6:1c4dd5e24b8d 307 }
mhannay3 5:b553c51b3b85 308 };
mhannay3 1:cd78922f70fa 309
mhannay3 5:b553c51b3b85 310 class GameBoard
mhannay3 5:b553c51b3b85 311 {
mhannay3 5:b553c51b3b85 312 private:
mhannay3 5:b553c51b3b85 313 BoardState boardState;
mhannay3 5:b553c51b3b85 314 static const int BOARD_DARK_COLOR = 0x769656;
mhannay3 5:b553c51b3b85 315 static const int BOARD_LIGHT_COLOR = 0xbaca44;
mhannay3 5:b553c51b3b85 316 static const int HOVER_COLOR = 0x0000ff;
mhannay3 5:b553c51b3b85 317 static const int SELECTED_COLOR = 0xff8800;
mhannay3 6:1c4dd5e24b8d 318 static const int MOVE_COLOR = 0xff00ff;
mhannay3 5:b553c51b3b85 319
mhannay3 5:b553c51b3b85 320 // piece sprites (12 x 12)
mhannay3 5:b553c51b3b85 321 static void spritePawn(bool white, bool light, int* spriteArray)
mhannay3 5:b553c51b3b85 322 {
mhannay3 5:b553c51b3b85 323 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 324 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 325 int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 5:b553c51b3b85 326 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 5:b553c51b3b85 327 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 5:b553c51b3b85 328 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 5:b553c51b3b85 329 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 330 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 5:b553c51b3b85 331 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 5:b553c51b3b85 332 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 333 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 334 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 5:b553c51b3b85 335 _, _, X, X, X, X, X, X, X, X, _, _,
mhannay3 5:b553c51b3b85 336 _, _, X, X, X, X, X, X, X, X, _, _
mhannay3 5:b553c51b3b85 337 };
mhannay3 5:b553c51b3b85 338 memcpy(sprite, spriteArray, sizeof(sprite));
mhannay3 5:b553c51b3b85 339 }
mhannay3 5:b553c51b3b85 340
mhannay3 5:b553c51b3b85 341 static void spriteRook(bool white, bool light, int* spriteArray)
mhannay3 5:b553c51b3b85 342 {
mhannay3 5:b553c51b3b85 343 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 344 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 345 int sprite[144] = {X, X, _, X, X, _, _, X, X, _, X, X,
mhannay3 5:b553c51b3b85 346 X, X, _, X, X, _, _, X, X, _, X, X,
mhannay3 5:b553c51b3b85 347 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 348 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 349 _, X, X, X, _, X, X, _, X, X, X, _,
mhannay3 5:b553c51b3b85 350 _, X, X, X, _, X, X, _, X, X, X, _,
mhannay3 5:b553c51b3b85 351 _, _, X, X, _, X, X, _, X, X, _, _,
mhannay3 5:b553c51b3b85 352 _, _, X, X, _, X, X, _, X, X, _, _,
mhannay3 5:b553c51b3b85 353 _, _, X, X, _, X, X, _, X, X, _, _,
mhannay3 5:b553c51b3b85 354 _, X, X, X, X, X, X, X, X, X, X, _,
mhannay3 5:b553c51b3b85 355 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 356 X, X, X, X, X, X, X, X, X, X, X, X
mhannay3 5:b553c51b3b85 357 };
mhannay3 5:b553c51b3b85 358 memcpy(sprite, spriteArray, sizeof(sprite));
mhannay3 5:b553c51b3b85 359 }
mhannay3 5:b553c51b3b85 360
mhannay3 5:b553c51b3b85 361 static void spriteKnight(bool white, bool light, int* spriteArray)
mhannay3 5:b553c51b3b85 362 {
mhannay3 5:b553c51b3b85 363 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 364 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 365 int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 5:b553c51b3b85 366 _, _, _, _, _, X, X, _, X, X, _, _,
mhannay3 5:b553c51b3b85 367 _, _, _, _, _, X, X, _, X, X, _, _,
mhannay3 5:b553c51b3b85 368 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 369 _, _, X, X, X, X, X, _, X, _, _, _,
mhannay3 5:b553c51b3b85 370 _, _, X, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 371 _, _, _, _, _, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 372 _, _, _, _, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 373 _, _, _, X, X, X, X, X, X, X, _, _,
mhannay3 5:b553c51b3b85 374 _, _, X, X, X, X, X, X, X, X, _, _,
mhannay3 5:b553c51b3b85 375 _, X, X, X, X, X, X, X, X, X, X, _,
mhannay3 5:b553c51b3b85 376 _, X, X, X, X, X, X, X, X, X, X, _
mhannay3 5:b553c51b3b85 377 };
mhannay3 5:b553c51b3b85 378 memcpy(sprite, spriteArray, sizeof(sprite));
mhannay3 5:b553c51b3b85 379 }
mhannay3 5:b553c51b3b85 380
mhannay3 5:b553c51b3b85 381 static void spriteBishop(bool white, bool light, int* spriteArray)
mhannay3 5:b553c51b3b85 382 {
mhannay3 5:b553c51b3b85 383 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 384 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 385 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 386 _, _, _, _, X, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 387 _, _, _, X, X, X, _, _, X, _, _, _,
mhannay3 5:b553c51b3b85 388 _, _, _, X, X, _, _, X, X, _, _, _,
mhannay3 5:b553c51b3b85 389 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 390 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 5:b553c51b3b85 391 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 392 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 5:b553c51b3b85 393 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 394 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 395 _, _, X, X, X, X, X, X, X, X, _, _,
mhannay3 5:b553c51b3b85 396 _, _, X, X, X, X, X, X, X, X, _, _
mhannay3 5:b553c51b3b85 397 };
mhannay3 5:b553c51b3b85 398 memcpy(sprite, spriteArray, sizeof(sprite));
mhannay3 5:b553c51b3b85 399 }
mhannay3 5:b553c51b3b85 400
mhannay3 5:b553c51b3b85 401 static void spriteQueen(bool white, bool light, int* spriteArray)
mhannay3 5:b553c51b3b85 402 {
mhannay3 5:b553c51b3b85 403 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 404 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 405 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 406 _, _, X, _, _, X, X, _, _, X, _, _,
mhannay3 5:b553c51b3b85 407 X, _, X, X, _, X, X, _, X, X, _, X,
mhannay3 5:b553c51b3b85 408 X, _, X, X, _, X, X, _, X, X, _, X,
mhannay3 5:b553c51b3b85 409 X, _, X, X, _, X, X, _, X, X, _, X,
mhannay3 5:b553c51b3b85 410 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 411 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 412 X, X, _, X, X, X, X, X, X, _, X, X,
mhannay3 5:b553c51b3b85 413 X, X, X, X, _, X, X, _, X, X, X, X,
mhannay3 5:b553c51b3b85 414 _, X, X, X, X, X, X, X, X, X, X, _,
mhannay3 5:b553c51b3b85 415 _, _, X, X, X, X, X, X, X, X, _, _,
mhannay3 5:b553c51b3b85 416 _, X, X, X, X, X, X, X, X, X, X, _
mhannay3 5:b553c51b3b85 417 };
mhannay3 5:b553c51b3b85 418 memcpy(sprite, spriteArray, sizeof(sprite));
mhannay3 5:b553c51b3b85 419 }
mhannay3 5:b553c51b3b85 420
mhannay3 5:b553c51b3b85 421 static void spriteKing(bool white, bool light, int* spriteArray)
mhannay3 5:b553c51b3b85 422 {
mhannay3 5:b553c51b3b85 423 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 424 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 425 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 426 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 427 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 428 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 429 X, X, _, _, _, X, X, _, _, _, X, X,
mhannay3 5:b553c51b3b85 430 X, X, X, X, _, X, X, _, X, X, X, X,
mhannay3 5:b553c51b3b85 431 X, _, X, X, X, X, X, X, X, X, _, X,
mhannay3 5:b553c51b3b85 432 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 433 X, X, X, _, X, X, X, X, _, X, X, X,
mhannay3 5:b553c51b3b85 434 _, X, X, X, X, X, X, X, X, X, X, _,
mhannay3 5:b553c51b3b85 435 _, _, X, X, X, _, _, X, X, X, _, _,
mhannay3 5:b553c51b3b85 436 _, X, X, X, X, X, X, X, X, X, X, _
mhannay3 5:b553c51b3b85 437 };
mhannay3 5:b553c51b3b85 438 memcpy(sprite, spriteArray, sizeof(sprite));
mhannay3 5:b553c51b3b85 439 }
mhannay3 5:b553c51b3b85 440
mhannay3 5:b553c51b3b85 441 public:
mhannay3 5:b553c51b3b85 442 BoardState getBoardState()
mhannay3 5:b553c51b3b85 443 {
mhannay3 5:b553c51b3b85 444 return boardState;
mhannay3 5:b553c51b3b85 445 }
mhannay3 5:b553c51b3b85 446
mhannay3 5:b553c51b3b85 447 void setBoardState(BoardState newBoardState)
mhannay3 5:b553c51b3b85 448 {
mhannay3 5:b553c51b3b85 449 boardState = newBoardState;
mhannay3 5:b553c51b3b85 450 }
mhannay3 5:b553c51b3b85 451
mhannay3 5:b553c51b3b85 452 // initializes the starting board state
mhannay3 5:b553c51b3b85 453 GameBoard()
mhannay3 5:b553c51b3b85 454 {
mhannay3 5:b553c51b3b85 455 // draw board
mhannay3 5:b553c51b3b85 456 for (int row = 0; row < 8; row++) {
mhannay3 5:b553c51b3b85 457 for (int column = 0; column < 8; column++) {
mhannay3 5:b553c51b3b85 458 uint64_t color;
mhannay3 5:b553c51b3b85 459 if ((row + column) % 2 == 0) {
mhannay3 5:b553c51b3b85 460 color = BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 461 } else {
mhannay3 5:b553c51b3b85 462 color = BOARD_LIGHT_COLOR;
mhannay3 5:b553c51b3b85 463 }
mhannay3 6:1c4dd5e24b8d 464 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 465 uLCD.filled_rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, color);
mhannay3 5:b553c51b3b85 466 }
mhannay3 5:b553c51b3b85 467 }
mhannay3 5:b553c51b3b85 468 // draw pieces
mhannay3 5:b553c51b3b85 469 placePieceAndDraw(wR, 0, 0);
mhannay3 5:b553c51b3b85 470 placePieceAndDraw(wN, 0, 1);
mhannay3 5:b553c51b3b85 471 placePieceAndDraw(wB, 0, 2);
mhannay3 5:b553c51b3b85 472 placePieceAndDraw(wQ, 0, 3);
mhannay3 5:b553c51b3b85 473 placePieceAndDraw(wK, 0, 4);
mhannay3 5:b553c51b3b85 474 placePieceAndDraw(wB, 0, 5);
mhannay3 5:b553c51b3b85 475 placePieceAndDraw(wN, 0, 6);
mhannay3 5:b553c51b3b85 476 placePieceAndDraw(wR, 0, 7);
mhannay3 5:b553c51b3b85 477 placePieceAndDraw(bR, 7, 0);
mhannay3 5:b553c51b3b85 478 placePieceAndDraw(bN, 7, 1);
mhannay3 5:b553c51b3b85 479 placePieceAndDraw(bB, 7, 2);
mhannay3 5:b553c51b3b85 480 placePieceAndDraw(bQ, 7, 3);
mhannay3 5:b553c51b3b85 481 placePieceAndDraw(bK, 7, 4);
mhannay3 5:b553c51b3b85 482 placePieceAndDraw(bB, 7, 5);
mhannay3 5:b553c51b3b85 483 placePieceAndDraw(bN, 7, 6);
mhannay3 5:b553c51b3b85 484 placePieceAndDraw(bR, 7, 7);
mhannay3 5:b553c51b3b85 485 for (int i = 0; i < 8; i++) {
mhannay3 5:b553c51b3b85 486 placePieceAndDraw(w, 1, i);
mhannay3 5:b553c51b3b85 487 placePieceAndDraw(b, 6, i);
mhannay3 5:b553c51b3b85 488 }
mhannay3 5:b553c51b3b85 489 }
mhannay3 5:b553c51b3b85 490
mhannay3 5:b553c51b3b85 491 // gets the pixel coordinates of the top left of the square
mhannay3 6:1c4dd5e24b8d 492 pixelCoord getTopLeftOfSquare(int row, int column)
mhannay3 5:b553c51b3b85 493 {
mhannay3 6:1c4dd5e24b8d 494 pixelCoord topLeft;
mhannay3 5:b553c51b3b85 495 topLeft.x = 16 * column;
mhannay3 5:b553c51b3b85 496 topLeft.y = 112 - 16 * row;
mhannay3 5:b553c51b3b85 497 return topLeft;
mhannay3 5:b553c51b3b85 498 }
mhannay3 5:b553c51b3b85 499
mhannay3 5:b553c51b3b85 500 // PIECE MOVEMENT AND GRAPHICS FUNCTIONS
mhannay3 5:b553c51b3b85 501
mhannay3 5:b553c51b3b85 502 // returns the piece at a given location
mhannay3 5:b553c51b3b85 503 Piece getPiece(int row, int column)
mhannay3 5:b553c51b3b85 504 {
mhannay3 5:b553c51b3b85 505 return boardState.getPiece(row, column);
mhannay3 5:b553c51b3b85 506 }
mhannay3 5:b553c51b3b85 507
mhannay3 5:b553c51b3b85 508 /* puts the bit representation of a piece at the set position of the board
mhannay3 5:b553c51b3b85 509 assumes that the position of the board is emptied beforehand
mhannay3 5:b553c51b3b85 510 */
mhannay3 5:b553c51b3b85 511 void placePieceAndDraw(Piece piece, int row, int column)
mhannay3 5:b553c51b3b85 512 {
mhannay3 5:b553c51b3b85 513 boardState.placePiece(piece, row, column);
mhannay3 6:1c4dd5e24b8d 514 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 515 int sprite[144];
mhannay3 5:b553c51b3b85 516 switch(piece) {
mhannay3 5:b553c51b3b85 517 case wK:
mhannay3 5:b553c51b3b85 518 case bK:
mhannay3 5:b553c51b3b85 519 spriteKing(piece==wK, (row+column)%2, sprite);
mhannay3 5:b553c51b3b85 520 break;
mhannay3 5:b553c51b3b85 521 case wQ:
mhannay3 5:b553c51b3b85 522 case bQ:
mhannay3 5:b553c51b3b85 523 spriteQueen(piece==wQ, (row+column)%2, sprite);
mhannay3 5:b553c51b3b85 524 break;
mhannay3 5:b553c51b3b85 525 case wB:
mhannay3 5:b553c51b3b85 526 case bB:
mhannay3 5:b553c51b3b85 527 spriteBishop(piece==wB, (row+column)%2, sprite);
mhannay3 5:b553c51b3b85 528 break;
mhannay3 5:b553c51b3b85 529 case wN:
mhannay3 5:b553c51b3b85 530 case bN:
mhannay3 5:b553c51b3b85 531 spriteKing(piece==wN, (row+column)%2, sprite);
mhannay3 5:b553c51b3b85 532 break;
mhannay3 5:b553c51b3b85 533 case wR:
mhannay3 5:b553c51b3b85 534 case bR:
mhannay3 5:b553c51b3b85 535 spriteKing(piece==wR, (row+column)%2, sprite);
mhannay3 5:b553c51b3b85 536 break;
mhannay3 5:b553c51b3b85 537 case w:
mhannay3 5:b553c51b3b85 538 case b:
mhannay3 5:b553c51b3b85 539 spriteKing(piece==w, (row+column)%2, sprite);
mhannay3 5:b553c51b3b85 540 break;
mhannay3 5:b553c51b3b85 541 }
mhannay3 5:b553c51b3b85 542 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mhannay3 5:b553c51b3b85 543 }
mhannay3 5:b553c51b3b85 544
mhannay3 5:b553c51b3b85 545 /* removes a piece from the set position of the board
mhannay3 5:b553c51b3b85 546 returns the bit representation of the piece
mhannay3 5:b553c51b3b85 547 */
mhannay3 5:b553c51b3b85 548 Piece removePieceAndDraw(int row, int column)
mhannay3 5:b553c51b3b85 549 {
mhannay3 5:b553c51b3b85 550 Piece removedPiece = boardState.removePiece(row, column);
mhannay3 6:1c4dd5e24b8d 551 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 1:cd78922f70fa 552 uint64_t color;
mhannay3 1:cd78922f70fa 553 if ((row + column) % 2 == 0) {
mhannay3 1:cd78922f70fa 554 color = BOARD_DARK_COLOR;
mhannay3 1:cd78922f70fa 555 } else {
mhannay3 1:cd78922f70fa 556 color = BOARD_LIGHT_COLOR;
mhannay3 1:cd78922f70fa 557 }
mhannay3 5:b553c51b3b85 558 uLCD.filled_rectangle(tl.x+2, tl.y+2, tl.x + 13, tl.y + 13, color);
mhannay3 5:b553c51b3b85 559 return removedPiece;
mhannay3 5:b553c51b3b85 560 }
mhannay3 5:b553c51b3b85 561
mhannay3 5:b553c51b3b85 562 /* moves a piece from one position to another
mhannay3 5:b553c51b3b85 563 returns the captured piece
mhannay3 5:b553c51b3b85 564 */
mhannay3 5:b553c51b3b85 565 Piece movePieceAndDraw(int startRow, int startColumn, int endRow, int endColumn)
mhannay3 5:b553c51b3b85 566 {
mhannay3 5:b553c51b3b85 567 Piece movingPiece = removePieceAndDraw(startRow, startColumn);
mhannay3 5:b553c51b3b85 568 Piece capturedPiece = boardState.removePiece(endRow, endColumn);
mhannay3 5:b553c51b3b85 569 placePieceAndDraw(movingPiece, endRow, endColumn);
mhannay3 5:b553c51b3b85 570 return capturedPiece;
mhannay3 1:cd78922f70fa 571 }
mhannay3 1:cd78922f70fa 572
mhannay3 5:b553c51b3b85 573 // SQUARE BORDER GRAPHICS FUNCTIONS
mhannay3 5:b553c51b3b85 574
mhannay3 5:b553c51b3b85 575 // removes selection border around square
mhannay3 5:b553c51b3b85 576 void unselectSquare(int row, int column)
mhannay3 5:b553c51b3b85 577 {
mhannay3 6:1c4dd5e24b8d 578 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 579 uint64_t color;
mhannay3 5:b553c51b3b85 580 if ((row + column) % 2 == 0) {
mhannay3 5:b553c51b3b85 581 color = BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 582 } else {
mhannay3 5:b553c51b3b85 583 color = BOARD_LIGHT_COLOR;
mhannay3 5:b553c51b3b85 584 }
mhannay3 5:b553c51b3b85 585 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, color);
mhannay3 5:b553c51b3b85 586 }
mhannay3 5:b553c51b3b85 587
mhannay3 5:b553c51b3b85 588 void hoverSquare(int row, int column)
mhannay3 5:b553c51b3b85 589 {
mhannay3 6:1c4dd5e24b8d 590 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 591 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, HOVER_COLOR);
mhannay3 1:cd78922f70fa 592 }
mhannay3 1:cd78922f70fa 593
mhannay3 1:cd78922f70fa 594 // draws selection border around square
mhannay3 5:b553c51b3b85 595 void selectSquare(int row, int column)
mhannay3 5:b553c51b3b85 596 {
mhannay3 6:1c4dd5e24b8d 597 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 598 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, SELECTED_COLOR);
mhannay3 1:cd78922f70fa 599 }
mhannay3 5:b553c51b3b85 600 };
mus3 0:d16cc59110a3 601
mhannay3 5:b553c51b3b85 602 int main()
mhannay3 5:b553c51b3b85 603 {
mhannay3 1:cd78922f70fa 604 return 0;
mhannay3 1:cd78922f70fa 605 }