Mert Us Matthew Hannay Logan Starr

Dependencies:   mbed 4DGL-uLCD-SE

Committer:
mhannay3
Date:
Wed Nov 09 21:07:14 2022 +0000
Revision:
4:5798e4062350
Parent:
2:80f1d1056ae4
Child:
5:b553c51b3b85
split the class into a state class and a graphics class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mhannay3 1:cd78922f70fa 1 #include <bitset>
mhannay3 1:cd78922f70fa 2
mhannay3 1:cd78922f70fa 3 enum Piece : u_int8_t {e, wK, bK, wQ, bQ, wR, bR, wB, bB, wN, bN, w, b};
mhannay3 1:cd78922f70fa 4
mhannay3 1:cd78922f70fa 5 struct coord {
mhannay3 1:cd78922f70fa 6 uint8_t x;
mhannay3 1:cd78922f70fa 7 uint8_t y;
mhannay3 1:cd78922f70fa 8 }
mhannay3 1:cd78922f70fa 9
mhannay3 4:5798e4062350 10 class BoardState {
mhannay3 4:5798e4062350 11 std::bitset<256> boardStateBits;
mhannay3 4:5798e4062350 12
mhannay3 4:5798e4062350 13 // calculates the advantage difference for the board state
mhannay3 4:5798e4062350 14 float calculateBoardState() {
mhannay3 4:5798e4062350 15 return 0.0;
mhannay3 4:5798e4062350 16 }
mhannay3 4:5798e4062350 17
mhannay3 4:5798e4062350 18 // returns the piece at a given location
mhannay3 4:5798e4062350 19 Piece getPiece(int row, int column) {
mhannay3 4:5798e4062350 20 return (boardStateBits >> (4 * (column + 8 * row))) & 15u;
mhannay3 4:5798e4062350 21 }
mhannay3 4:5798e4062350 22
mhannay3 4:5798e4062350 23 /* puts the bit representation of a piece at the set position of the board
mhannay3 4:5798e4062350 24 assumes that the position of the board is emptied beforehand
mhannay3 4:5798e4062350 25 */
mhannay3 4:5798e4062350 26 void placePiece(Piece piece, int row, int column) {
mhannay3 4:5798e4062350 27 boardStateBits = boardStateBits | (piece << (4 * (column + 8 * row)));
mhannay3 4:5798e4062350 28 }
mhannay3 4:5798e4062350 29
mhannay3 4:5798e4062350 30 /* removes a piece from the set position of the board
mhannay3 4:5798e4062350 31 returns the bit representation of the piece
mhannay3 4:5798e4062350 32 */
mhannay3 4:5798e4062350 33 Piece removePiece(int row, int column) {
mhannay3 4:5798e4062350 34 Piece removedPiece = (boardStateBits >> (4 * (column + 8 * row))) & 15u;
mhannay3 4:5798e4062350 35 boardStateBits = boardStateBits & ~(15u << (4 * (column + 8 * row)));
mhannay3 4:5798e4062350 36 }
mhannay3 4:5798e4062350 37
mhannay3 4:5798e4062350 38 /* moves a piece from one position to another
mhannay3 4:5798e4062350 39 returns the captured piece
mhannay3 4:5798e4062350 40 */
mhannay3 4:5798e4062350 41 Piece movePiece(int startRow, int startColumn, int endRow, int endColumn) {
mhannay3 4:5798e4062350 42 Piece movingPiece = removePiece(startRow, startColumn);
mhannay3 4:5798e4062350 43 Piece capturedPiece = removePiece(endRow, endColumn);
mhannay3 4:5798e4062350 44 placePiece(movingPiece, endRow, endColumn);
mhannay3 4:5798e4062350 45 return capturedPiece;
mhannay3 4:5798e4062350 46 }
mhannay3 4:5798e4062350 47 }
mhannay3 4:5798e4062350 48
mhannay3 4:5798e4062350 49 class GameBoard {
mhannay3 1:cd78922f70fa 50 private:
mhannay3 4:5798e4062350 51 BoardState boardState;
mhannay3 1:cd78922f70fa 52 const uint32_t BOARD_DARK_COLOR = 0x769656;
mhannay3 1:cd78922f70fa 53 const uint32_t BOARD_LIGHT_COLOR = 0xbaca44;
mhannay3 1:cd78922f70fa 54 const uint32_t HOVER_COLOR = 0x0000ff;
mhannay3 1:cd78922f70fa 55 const uint32_t SELECTED_COLOR = 0xff8800;
mhannay3 1:cd78922f70fa 56
mhannay3 1:cd78922f70fa 57 // piece sprites (12 x 12)
mhannay3 4:5798e4062350 58 static uint32_t* spritePawn(bool white, bool light) {
mhannay3 1:cd78922f70fa 59 uint32_t X = white ? 0xffffff : 0x000000;
mhannay3 1:cd78922f70fa 60 uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 2:80f1d1056ae4 61 return {_, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 2:80f1d1056ae4 62 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 2:80f1d1056ae4 63 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 2:80f1d1056ae4 64 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 2:80f1d1056ae4 65 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 1:cd78922f70fa 66 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 1:cd78922f70fa 67 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 1:cd78922f70fa 68 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 1:cd78922f70fa 69 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 1:cd78922f70fa 70 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 2:80f1d1056ae4 71 _, _, X, X, X, X, X, X, X, X, _, _,
mhannay3 2:80f1d1056ae4 72 _, _, X, X, X, X, X, X, X, X, _, _};
mhannay3 1:cd78922f70fa 73 }
mhannay3 1:cd78922f70fa 74
mhannay3 4:5798e4062350 75 static uint32_t* spriteRook(bool white, bool light) {
mhannay3 1:cd78922f70fa 76 uint32_t X = white ? 0xffffff : 0x000000;
mhannay3 1:cd78922f70fa 77 uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 2:80f1d1056ae4 78 return {X, X, _, X, X, _, _, X, X, _, X, X,
mhannay3 2:80f1d1056ae4 79 X, X, _, X, X, _, _, X, X, _, X, X,
mhannay3 2:80f1d1056ae4 80 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 2:80f1d1056ae4 81 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 2:80f1d1056ae4 82 _, X, X, X, _, X, X, _, X, X, X, _,
mhannay3 2:80f1d1056ae4 83 _, X, X, X, _, X, X, _, X, X, X, _,
mhannay3 2:80f1d1056ae4 84 _, _, X, X, _, X, X, _, X, X, _, _,
mhannay3 2:80f1d1056ae4 85 _, _, X, X, _, X, X, _, X, X, _, _,
mhannay3 2:80f1d1056ae4 86 _, _, X, X, _, X, X, _, X, X, _, _,
mhannay3 2:80f1d1056ae4 87 _, X, X, X, X, X, X, X, X, X, X, _,
mhannay3 1:cd78922f70fa 88 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 2:80f1d1056ae4 89 X, X, X, X, X, X, X, X, X, X, X, X};
mhannay3 2:80f1d1056ae4 90 }
mhannay3 2:80f1d1056ae4 91
mhannay3 4:5798e4062350 92 static uint32_t* spriteKnight(bool white, bool light) {
mhannay3 2:80f1d1056ae4 93 uint32_t X = white ? 0xffffff : 0x000000;
mhannay3 2:80f1d1056ae4 94 uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 2:80f1d1056ae4 95 return {_, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 2:80f1d1056ae4 96 _, _, _, _, _, X, X, _, X, X, _, _,
mhannay3 2:80f1d1056ae4 97 _, _, _, _, _, X, X, _, X, X, _, _,
mhannay3 2:80f1d1056ae4 98 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 2:80f1d1056ae4 99 _, _, X, X, X, X, X, _, X, _, _, _,
mhannay3 2:80f1d1056ae4 100 _, _, X, X, X, X, X, X, X, _, _, _,
mhannay3 2:80f1d1056ae4 101 _, _, _, _, _, X, X, X, X, _, _, _,
mhannay3 2:80f1d1056ae4 102 _, _, _, _, X, X, X, X, X, _, _, _,
mhannay3 2:80f1d1056ae4 103 _, _, _, X, X, X, X, X, X, X, _, _,
mhannay3 2:80f1d1056ae4 104 _, _, X, X, X, X, X, X, X, X, _, _,
mhannay3 2:80f1d1056ae4 105 _, X, X, X, X, X, X, X, X, X, X, _,
mhannay3 2:80f1d1056ae4 106 _, X, X, X, X, X, X, X, X, X, X, _};
mhannay3 2:80f1d1056ae4 107 }
mhannay3 2:80f1d1056ae4 108
mhannay3 4:5798e4062350 109 static uint32_t* spriteBishop(bool white, bool light) {
mhannay3 2:80f1d1056ae4 110 uint32_t X = white ? 0xffffff : 0x000000;
mhannay3 2:80f1d1056ae4 111 uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 2:80f1d1056ae4 112 return {_, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 2:80f1d1056ae4 113 _, _, _, _, X, X, X, _, _, _, _, _,
mhannay3 2:80f1d1056ae4 114 _, _, _, X, X, X, _, _, X, _, _, _,
mhannay3 2:80f1d1056ae4 115 _, _, _, X, X, _, _, X, X, _, _, _,
mhannay3 2:80f1d1056ae4 116 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 2:80f1d1056ae4 117 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 2:80f1d1056ae4 118 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 2:80f1d1056ae4 119 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 2:80f1d1056ae4 120 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 2:80f1d1056ae4 121 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 2:80f1d1056ae4 122 _, _, X, X, X, X, X, X, X, X, _, _,
mhannay3 2:80f1d1056ae4 123 _, _, X, X, X, X, X, X, X, X, _, _};
mhannay3 2:80f1d1056ae4 124 }
mhannay3 2:80f1d1056ae4 125
mhannay3 4:5798e4062350 126 static uint32_t* spriteQueen(bool white, bool light) {
mhannay3 2:80f1d1056ae4 127 uint32_t X = white ? 0xffffff : 0x000000;
mhannay3 2:80f1d1056ae4 128 uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 2:80f1d1056ae4 129 return {_, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 2:80f1d1056ae4 130 _, _, X, _, _, X, X, _, _, X, _, _,
mhannay3 2:80f1d1056ae4 131 X, _, X, X, _, X, X, _, X, X, _, X,
mhannay3 2:80f1d1056ae4 132 X, _, X, X, _, X, X, _, X, X, _, X,
mhannay3 2:80f1d1056ae4 133 X, _, X, X, _, X, X, _, X, X, _, X,
mhannay3 2:80f1d1056ae4 134 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 2:80f1d1056ae4 135 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 2:80f1d1056ae4 136 X, X, _, X, X, X, X, X, X, _, X, X,
mhannay3 2:80f1d1056ae4 137 X, X, X, X, _, X, X, _, X, X, X, X,
mhannay3 2:80f1d1056ae4 138 _, X, X, X, X, X, X, X, X, X, X, _,
mhannay3 2:80f1d1056ae4 139 _, _, X, X, X, X, X, X, X, X, _, _,
mhannay3 2:80f1d1056ae4 140 _, X, X, X, X, X, X, X, X, X, X, _};
mhannay3 2:80f1d1056ae4 141 }
mhannay3 2:80f1d1056ae4 142
mhannay3 4:5798e4062350 143 static uint32_t* spriteKing(bool white, bool light) {
mhannay3 2:80f1d1056ae4 144 uint32_t X = white ? 0xffffff : 0x000000;
mhannay3 2:80f1d1056ae4 145 uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 2:80f1d1056ae4 146 return {_, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 2:80f1d1056ae4 147 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 2:80f1d1056ae4 148 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 2:80f1d1056ae4 149 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 2:80f1d1056ae4 150 X, X, _, _, _, X, X, _, _, _, X, X,
mhannay3 2:80f1d1056ae4 151 X, X, X, X, _, X, X, _, X, X, X, X,
mhannay3 2:80f1d1056ae4 152 X, _, X, X, X, X, X, X, X, X, _, X,
mhannay3 2:80f1d1056ae4 153 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 2:80f1d1056ae4 154 X, X, X, _, X, X, X, X, _, X, X, X,
mhannay3 2:80f1d1056ae4 155 _, X, X, X, X, X, X, X, X, X, X, _,
mhannay3 2:80f1d1056ae4 156 _, _, X, X, X, _, _, X, X, X, _, _,
mhannay3 2:80f1d1056ae4 157 _, X, X, X, X, X, X, X, X, X, X, _};
mhannay3 1:cd78922f70fa 158 }
mhannay3 1:cd78922f70fa 159
mhannay3 1:cd78922f70fa 160 public:
mhannay3 4:5798e4062350 161 BoardState getBoardState() {
mhannay3 1:cd78922f70fa 162 return boardState;
mhannay3 1:cd78922f70fa 163 }
mhannay3 1:cd78922f70fa 164
mhannay3 4:5798e4062350 165 void setBoardState(BoardState newBoardState) {
mhannay3 1:cd78922f70fa 166 boardState = newBoardState;
mhannay3 1:cd78922f70fa 167 }
mus3 0:d16cc59110a3 168
mhannay3 1:cd78922f70fa 169 // initializes the starting board state
mhannay3 4:5798e4062350 170 GameBoard() {
mhannay3 1:cd78922f70fa 171 placePiece(wR, 0, 0);
mhannay3 1:cd78922f70fa 172 placePiece(wN, 0, 1);
mhannay3 1:cd78922f70fa 173 placePiece(wB, 0, 2);
mhannay3 1:cd78922f70fa 174 placePiece(wQ, 0, 3);
mhannay3 1:cd78922f70fa 175 placePiece(wK, 0, 4);
mhannay3 1:cd78922f70fa 176 placePiece(wB, 0, 5);
mhannay3 1:cd78922f70fa 177 placePiece(wN, 0, 6);
mhannay3 1:cd78922f70fa 178 placePiece(wR, 0, 7);
mhannay3 1:cd78922f70fa 179 placePiece(bR, 7, 0);
mhannay3 1:cd78922f70fa 180 placePiece(bN, 7, 1);
mhannay3 1:cd78922f70fa 181 placePiece(bB, 7, 2);
mhannay3 1:cd78922f70fa 182 placePiece(bQ, 7, 3);
mhannay3 1:cd78922f70fa 183 placePiece(bK, 7, 4);
mhannay3 1:cd78922f70fa 184 placePiece(bB, 7, 5);
mhannay3 1:cd78922f70fa 185 placePiece(bN, 7, 6);
mhannay3 1:cd78922f70fa 186 placePiece(bR, 7, 7);
mhannay3 1:cd78922f70fa 187 for (int i = 0; i < 8; i++) {
mhannay3 1:cd78922f70fa 188 placePiece(w, 1, i);
mhannay3 1:cd78922f70fa 189 placePiece(b, 6, i);
mhannay3 1:cd78922f70fa 190 }
mhannay3 1:cd78922f70fa 191 }
mhannay3 4:5798e4062350 192
mhannay3 4:5798e4062350 193 // gets the pixel coordinates of the top left of the square
mhannay3 4:5798e4062350 194 coord getTopLeftOfSquare(int row, int column) {
mhannay3 4:5798e4062350 195 coord topLeft;
mhannay3 4:5798e4062350 196 topLeft.x = 16 * column;
mhannay3 4:5798e4062350 197 topLeft.y = 112 - 16 * row;
mhannay3 4:5798e4062350 198 }
mhannay3 4:5798e4062350 199
mhannay3 4:5798e4062350 200 // PIECE MOVEMENT AND GRAPHICS FUNCTIONS
mhannay3 4:5798e4062350 201
mhannay3 1:cd78922f70fa 202 // returns the piece at a given location
mhannay3 1:cd78922f70fa 203 Piece getPiece(int row, int column) {
mhannay3 4:5798e4062350 204 return boardState.getPiece(row, column);
mhannay3 1:cd78922f70fa 205 }
mhannay3 1:cd78922f70fa 206
mhannay3 1:cd78922f70fa 207 /* puts the bit representation of a piece at the set position of the board
mhannay3 1:cd78922f70fa 208 assumes that the position of the board is emptied beforehand
mhannay3 1:cd78922f70fa 209 */
mhannay3 1:cd78922f70fa 210 void placePiece(Piece piece, int row, int column) {
mhannay3 4:5798e4062350 211 boardState.placePiece(piece, row, column);
mhannay3 4:5798e4062350 212 // draw
mhannay3 1:cd78922f70fa 213 }
mhannay3 1:cd78922f70fa 214
mhannay3 1:cd78922f70fa 215 /* removes a piece from the set position of the board
mhannay3 1:cd78922f70fa 216 returns the bit representation of the piece
mhannay3 1:cd78922f70fa 217 */
mhannay3 1:cd78922f70fa 218 Piece removePiece(int row, int column) {
mhannay3 4:5798e4062350 219 Piece removedPiece = boardState.placePiece(piece, row, column);
mhannay3 4:5798e4062350 220 // draw
mhannay3 4:5798e4062350 221 return removedPiece;
mhannay3 1:cd78922f70fa 222 }
mhannay3 1:cd78922f70fa 223
mhannay3 1:cd78922f70fa 224 /* moves a piece from one position to another
mhannay3 1:cd78922f70fa 225 returns the captured piece
mhannay3 1:cd78922f70fa 226 */
mhannay3 1:cd78922f70fa 227 Piece movePiece(int startRow, int startColumn, int endRow, int endColumn) {
mhannay3 4:5798e4062350 228 Piece capturedPiece = boardState.movePiece(startRow, startColumn, endRow, endColumn);
mhannay3 4:5798e4062350 229 // draw
mhannay3 1:cd78922f70fa 230 return capturedPiece;
mhannay3 1:cd78922f70fa 231 }
mhannay3 1:cd78922f70fa 232
mhannay3 4:5798e4062350 233 // SQUARE BORDER GRAPHICS FUNCTIONS
mhannay3 4:5798e4062350 234
mhannay3 1:cd78922f70fa 235 // removes selection border around square
mhannay3 1:cd78922f70fa 236 void unselectSquare(int row, int column) {
mhannay3 1:cd78922f70fa 237 coord tl = getTopLeftOfSquare(row, column);
mhannay3 1:cd78922f70fa 238 uint64_t color;
mhannay3 1:cd78922f70fa 239 if ((row + column) % 2 == 0) {
mhannay3 1:cd78922f70fa 240 color = BOARD_DARK_COLOR;
mhannay3 1:cd78922f70fa 241 } else {
mhannay3 1:cd78922f70fa 242 color = BOARD_LIGHT_COLOR;
mhannay3 1:cd78922f70fa 243 }
mhannay3 1:cd78922f70fa 244 //uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, color);
mhannay3 1:cd78922f70fa 245 }
mhannay3 1:cd78922f70fa 246
mhannay3 1:cd78922f70fa 247 void hoverSquare(int row, int column) {
mhannay3 1:cd78922f70fa 248 coord tl = getTopLeftOfSquare(row, column);
mhannay3 1:cd78922f70fa 249 //uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, HOVER_COLOR);
mhannay3 1:cd78922f70fa 250 }
mhannay3 1:cd78922f70fa 251
mhannay3 1:cd78922f70fa 252 // draws selection border around square
mhannay3 1:cd78922f70fa 253 void selectSquare(int row, int column) {
mhannay3 1:cd78922f70fa 254 coord tl = getTopLeftOfSquare(row, column);
mhannay3 1:cd78922f70fa 255 //uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, SELECTED_COLOR);
mhannay3 1:cd78922f70fa 256 }
mhannay3 1:cd78922f70fa 257 }
mus3 0:d16cc59110a3 258
mus3 0:d16cc59110a3 259 int main() {
mhannay3 1:cd78922f70fa 260 return 0;
mhannay3 1:cd78922f70fa 261 }