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