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