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