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