Mert Us Matthew Hannay Logan Starr
Dependencies: mbed 4DGL-uLCD-SE
main.cpp@2:80f1d1056ae4, 2022-11-08 (annotated)
- Committer:
- mhannay3
- Date:
- Tue Nov 08 01:17:17 2022 +0000
- Revision:
- 2:80f1d1056ae4
- Parent:
- 1:cd78922f70fa
- Child:
- 3:0f979d8d3fa1
- Child:
- 4:5798e4062350
completed sprites
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:cd78922f70fa | 10 | class Board { |
mhannay3 | 1:cd78922f70fa | 11 | private: |
mhannay3 | 1:cd78922f70fa | 12 | std::bitset<256> boardState; |
mhannay3 | 1:cd78922f70fa | 13 | const uint32_t BOARD_DARK_COLOR = 0x769656; |
mhannay3 | 1:cd78922f70fa | 14 | const uint32_t BOARD_LIGHT_COLOR = 0xbaca44; |
mhannay3 | 1:cd78922f70fa | 15 | const uint32_t HOVER_COLOR = 0x0000ff; |
mhannay3 | 1:cd78922f70fa | 16 | const uint32_t SELECTED_COLOR = 0xff8800; |
mhannay3 | 1:cd78922f70fa | 17 | |
mhannay3 | 1:cd78922f70fa | 18 | // piece sprites (12 x 12) |
mhannay3 | 1:cd78922f70fa | 19 | uint32_t* spritePawn(bool white, bool light) { |
mhannay3 | 1:cd78922f70fa | 20 | uint32_t X = white ? 0xffffff : 0x000000; |
mhannay3 | 1:cd78922f70fa | 21 | uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; |
mhannay3 | 2:80f1d1056ae4 | 22 | return {_, _, _, _, _, _, _, _, _, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 23 | _, _, _, _, _, _, _, _, _, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 24 | _, _, _, _, _, _, _, _, _, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 25 | _, _, _, _, _, _, _, _, _, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 26 | _, _, _, _, _, X, X, _, _, _, _, _, |
mhannay3 | 1:cd78922f70fa | 27 | _, _, _, _, X, X, X, X, _, _, _, _, |
mhannay3 | 1:cd78922f70fa | 28 | _, _, _, _, X, X, X, X, _, _, _, _, |
mhannay3 | 1:cd78922f70fa | 29 | _, _, _, _, _, X, X, _, _, _, _, _, |
mhannay3 | 1:cd78922f70fa | 30 | _, _, _, _, _, X, X, _, _, _, _, _, |
mhannay3 | 1:cd78922f70fa | 31 | _, _, _, _, X, X, X, X, _, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 32 | _, _, X, X, X, X, X, X, X, X, _, _, |
mhannay3 | 2:80f1d1056ae4 | 33 | _, _, X, X, X, X, X, X, X, X, _, _}; |
mhannay3 | 1:cd78922f70fa | 34 | } |
mhannay3 | 1:cd78922f70fa | 35 | |
mhannay3 | 1:cd78922f70fa | 36 | uint32_t* spriteRook(bool white, bool light) { |
mhannay3 | 1:cd78922f70fa | 37 | uint32_t X = white ? 0xffffff : 0x000000; |
mhannay3 | 1:cd78922f70fa | 38 | uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; |
mhannay3 | 2:80f1d1056ae4 | 39 | return {X, X, _, X, X, _, _, X, X, _, X, X, |
mhannay3 | 2:80f1d1056ae4 | 40 | X, X, _, X, X, _, _, X, X, _, X, X, |
mhannay3 | 2:80f1d1056ae4 | 41 | X, X, X, X, X, X, X, X, X, X, X, X, |
mhannay3 | 2:80f1d1056ae4 | 42 | X, X, X, X, X, X, X, X, X, X, X, X, |
mhannay3 | 2:80f1d1056ae4 | 43 | _, X, X, X, _, X, X, _, X, X, X, _, |
mhannay3 | 2:80f1d1056ae4 | 44 | _, X, X, X, _, X, X, _, X, X, X, _, |
mhannay3 | 2:80f1d1056ae4 | 45 | _, _, X, X, _, X, X, _, X, X, _, _, |
mhannay3 | 2:80f1d1056ae4 | 46 | _, _, X, X, _, X, X, _, X, X, _, _, |
mhannay3 | 2:80f1d1056ae4 | 47 | _, _, X, X, _, X, X, _, X, X, _, _, |
mhannay3 | 2:80f1d1056ae4 | 48 | _, X, X, X, X, X, X, X, X, X, X, _, |
mhannay3 | 1:cd78922f70fa | 49 | X, X, X, X, X, X, X, X, X, X, X, X, |
mhannay3 | 2:80f1d1056ae4 | 50 | X, X, X, X, X, X, X, X, X, X, X, X}; |
mhannay3 | 2:80f1d1056ae4 | 51 | } |
mhannay3 | 2:80f1d1056ae4 | 52 | |
mhannay3 | 2:80f1d1056ae4 | 53 | uint32_t* spriteKnight(bool white, bool light) { |
mhannay3 | 2:80f1d1056ae4 | 54 | uint32_t X = white ? 0xffffff : 0x000000; |
mhannay3 | 2:80f1d1056ae4 | 55 | uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; |
mhannay3 | 2:80f1d1056ae4 | 56 | return {_, _, _, _, _, _, _, _, _, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 57 | _, _, _, _, _, X, X, _, X, X, _, _, |
mhannay3 | 2:80f1d1056ae4 | 58 | _, _, _, _, _, X, X, _, X, X, _, _, |
mhannay3 | 2:80f1d1056ae4 | 59 | _, _, _, X, X, X, X, X, X, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 60 | _, _, X, X, X, X, X, _, X, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 61 | _, _, X, X, X, X, X, X, X, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 62 | _, _, _, _, _, X, X, X, X, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 63 | _, _, _, _, X, X, X, X, X, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 64 | _, _, _, X, X, X, X, X, X, X, _, _, |
mhannay3 | 2:80f1d1056ae4 | 65 | _, _, X, X, X, X, X, X, X, X, _, _, |
mhannay3 | 2:80f1d1056ae4 | 66 | _, X, X, X, X, X, X, X, X, X, X, _, |
mhannay3 | 2:80f1d1056ae4 | 67 | _, X, X, X, X, X, X, X, X, X, X, _}; |
mhannay3 | 2:80f1d1056ae4 | 68 | } |
mhannay3 | 2:80f1d1056ae4 | 69 | |
mhannay3 | 2:80f1d1056ae4 | 70 | uint32_t* spriteBishop(bool white, bool light) { |
mhannay3 | 2:80f1d1056ae4 | 71 | uint32_t X = white ? 0xffffff : 0x000000; |
mhannay3 | 2:80f1d1056ae4 | 72 | uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; |
mhannay3 | 2:80f1d1056ae4 | 73 | return {_, _, _, _, _, X, X, _, _, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 74 | _, _, _, _, X, X, X, _, _, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 75 | _, _, _, X, X, X, _, _, X, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 76 | _, _, _, X, X, _, _, X, X, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 77 | _, _, _, X, X, X, X, X, X, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 78 | _, _, _, _, X, X, X, X, _, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 79 | _, _, _, _, _, X, X, _, _, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 80 | _, _, _, _, X, X, X, X, _, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 81 | _, _, _, X, X, X, X, X, X, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 82 | _, _, _, 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, X, X, _, _}; |
mhannay3 | 2:80f1d1056ae4 | 85 | } |
mhannay3 | 2:80f1d1056ae4 | 86 | |
mhannay3 | 2:80f1d1056ae4 | 87 | uint32_t* spriteQueen(bool white, bool light) { |
mhannay3 | 2:80f1d1056ae4 | 88 | uint32_t X = white ? 0xffffff : 0x000000; |
mhannay3 | 2:80f1d1056ae4 | 89 | uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; |
mhannay3 | 2:80f1d1056ae4 | 90 | return {_, _, _, _, _, X, X, _, _, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 91 | _, _, X, _, _, X, X, _, _, X, _, _, |
mhannay3 | 2:80f1d1056ae4 | 92 | X, _, X, X, _, X, X, _, X, X, _, X, |
mhannay3 | 2:80f1d1056ae4 | 93 | X, _, X, X, _, X, X, _, X, X, _, X, |
mhannay3 | 2:80f1d1056ae4 | 94 | X, _, X, X, _, X, X, _, X, X, _, X, |
mhannay3 | 2:80f1d1056ae4 | 95 | X, X, X, X, X, X, X, X, X, X, X, X, |
mhannay3 | 2:80f1d1056ae4 | 96 | X, X, X, X, X, X, X, X, X, X, X, X, |
mhannay3 | 2:80f1d1056ae4 | 97 | X, X, _, X, X, X, X, X, X, _, X, X, |
mhannay3 | 2:80f1d1056ae4 | 98 | X, X, X, X, _, X, X, _, X, X, X, X, |
mhannay3 | 2:80f1d1056ae4 | 99 | _, X, X, X, X, X, X, X, X, X, X, _, |
mhannay3 | 2:80f1d1056ae4 | 100 | _, _, X, X, X, X, X, X, X, X, _, _, |
mhannay3 | 2:80f1d1056ae4 | 101 | _, X, X, X, X, X, X, X, X, X, X, _}; |
mhannay3 | 2:80f1d1056ae4 | 102 | } |
mhannay3 | 2:80f1d1056ae4 | 103 | |
mhannay3 | 2:80f1d1056ae4 | 104 | uint32_t* spriteKing(bool white, bool light) { |
mhannay3 | 2:80f1d1056ae4 | 105 | uint32_t X = white ? 0xffffff : 0x000000; |
mhannay3 | 2:80f1d1056ae4 | 106 | uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; |
mhannay3 | 2:80f1d1056ae4 | 107 | return {_, _, _, _, _, X, X, _, _, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 108 | _, _, _, _, _, X, X, _, _, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 109 | _, _, _, X, X, X, X, X, X, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 110 | _, _, _, X, X, X, X, X, X, _, _, _, |
mhannay3 | 2:80f1d1056ae4 | 111 | X, X, _, _, _, X, X, _, _, _, X, X, |
mhannay3 | 2:80f1d1056ae4 | 112 | X, X, X, X, _, X, X, _, X, X, X, X, |
mhannay3 | 2:80f1d1056ae4 | 113 | X, _, X, X, X, X, X, X, X, X, _, X, |
mhannay3 | 2:80f1d1056ae4 | 114 | X, X, X, X, X, X, X, X, X, X, X, X, |
mhannay3 | 2:80f1d1056ae4 | 115 | X, X, X, _, X, X, X, X, _, X, X, X, |
mhannay3 | 2:80f1d1056ae4 | 116 | _, X, X, X, X, X, X, X, X, X, X, _, |
mhannay3 | 2:80f1d1056ae4 | 117 | _, _, X, X, X, _, _, X, X, X, _, _, |
mhannay3 | 2:80f1d1056ae4 | 118 | _, X, X, X, X, X, X, X, X, X, X, _}; |
mhannay3 | 1:cd78922f70fa | 119 | } |
mhannay3 | 1:cd78922f70fa | 120 | |
mhannay3 | 1:cd78922f70fa | 121 | public: |
mhannay3 | 1:cd78922f70fa | 122 | std::bitset<256> getBoardState() { |
mhannay3 | 1:cd78922f70fa | 123 | return boardState; |
mhannay3 | 1:cd78922f70fa | 124 | } |
mhannay3 | 1:cd78922f70fa | 125 | |
mhannay3 | 1:cd78922f70fa | 126 | void setBoardState(std::bitset<256> newBoardState) { |
mhannay3 | 1:cd78922f70fa | 127 | boardState = newBoardState; |
mhannay3 | 1:cd78922f70fa | 128 | } |
mus3 | 0:d16cc59110a3 | 129 | |
mhannay3 | 1:cd78922f70fa | 130 | // initializes the starting board state |
mhannay3 | 1:cd78922f70fa | 131 | Board() { |
mhannay3 | 1:cd78922f70fa | 132 | placePiece(wR, 0, 0); |
mhannay3 | 1:cd78922f70fa | 133 | placePiece(wN, 0, 1); |
mhannay3 | 1:cd78922f70fa | 134 | placePiece(wB, 0, 2); |
mhannay3 | 1:cd78922f70fa | 135 | placePiece(wQ, 0, 3); |
mhannay3 | 1:cd78922f70fa | 136 | placePiece(wK, 0, 4); |
mhannay3 | 1:cd78922f70fa | 137 | placePiece(wB, 0, 5); |
mhannay3 | 1:cd78922f70fa | 138 | placePiece(wN, 0, 6); |
mhannay3 | 1:cd78922f70fa | 139 | placePiece(wR, 0, 7); |
mhannay3 | 1:cd78922f70fa | 140 | placePiece(bR, 7, 0); |
mhannay3 | 1:cd78922f70fa | 141 | placePiece(bN, 7, 1); |
mhannay3 | 1:cd78922f70fa | 142 | placePiece(bB, 7, 2); |
mhannay3 | 1:cd78922f70fa | 143 | placePiece(bQ, 7, 3); |
mhannay3 | 1:cd78922f70fa | 144 | placePiece(bK, 7, 4); |
mhannay3 | 1:cd78922f70fa | 145 | placePiece(bB, 7, 5); |
mhannay3 | 1:cd78922f70fa | 146 | placePiece(bN, 7, 6); |
mhannay3 | 1:cd78922f70fa | 147 | placePiece(bR, 7, 7); |
mhannay3 | 1:cd78922f70fa | 148 | for (int i = 0; i < 8; i++) { |
mhannay3 | 1:cd78922f70fa | 149 | placePiece(w, 1, i); |
mhannay3 | 1:cd78922f70fa | 150 | placePiece(b, 6, i); |
mhannay3 | 1:cd78922f70fa | 151 | } |
mhannay3 | 1:cd78922f70fa | 152 | } |
mhannay3 | 1:cd78922f70fa | 153 | |
mhannay3 | 1:cd78922f70fa | 154 | // returns the piece at a given location |
mhannay3 | 1:cd78922f70fa | 155 | Piece getPiece(int row, int column) { |
mhannay3 | 1:cd78922f70fa | 156 | return (boardState >> (4 * (column + 8 * row))) & 15u; |
mhannay3 | 1:cd78922f70fa | 157 | } |
mhannay3 | 1:cd78922f70fa | 158 | |
mhannay3 | 1:cd78922f70fa | 159 | /* puts the bit representation of a piece at the set position of the board |
mhannay3 | 1:cd78922f70fa | 160 | assumes that the position of the board is emptied beforehand |
mhannay3 | 1:cd78922f70fa | 161 | */ |
mhannay3 | 1:cd78922f70fa | 162 | void placePiece(Piece piece, int row, int column) { |
mhannay3 | 1:cd78922f70fa | 163 | boardState = boardState | (piece << (4 * (column + 8 * row))); |
mhannay3 | 1:cd78922f70fa | 164 | } |
mhannay3 | 1:cd78922f70fa | 165 | |
mhannay3 | 1:cd78922f70fa | 166 | /* removes a piece from the set position of the board |
mhannay3 | 1:cd78922f70fa | 167 | returns the bit representation of the piece |
mhannay3 | 1:cd78922f70fa | 168 | */ |
mhannay3 | 1:cd78922f70fa | 169 | Piece removePiece(int row, int column) { |
mhannay3 | 1:cd78922f70fa | 170 | Piece removedPiece = (boardState >> (4 * (column + 8 * row))) & 15u; |
mhannay3 | 1:cd78922f70fa | 171 | boardState = boardState & ~(15u << (4 * (column + 8 * row))); |
mhannay3 | 1:cd78922f70fa | 172 | } |
mhannay3 | 1:cd78922f70fa | 173 | |
mhannay3 | 1:cd78922f70fa | 174 | /* moves a piece from one position to another |
mhannay3 | 1:cd78922f70fa | 175 | returns the captured piece |
mhannay3 | 1:cd78922f70fa | 176 | */ |
mhannay3 | 1:cd78922f70fa | 177 | Piece movePiece(int startRow, int startColumn, int endRow, int endColumn) { |
mhannay3 | 1:cd78922f70fa | 178 | Piece movingPiece = removePiece(startRow, startColumn); |
mhannay3 | 1:cd78922f70fa | 179 | Piece capturedPiece = removePiece(endRow, endColumn); |
mhannay3 | 1:cd78922f70fa | 180 | placePiece(movingPiece, endRow, endColumn); |
mhannay3 | 1:cd78922f70fa | 181 | return capturedPiece; |
mhannay3 | 1:cd78922f70fa | 182 | } |
mhannay3 | 1:cd78922f70fa | 183 | |
mhannay3 | 1:cd78922f70fa | 184 | // calculates the advantage difference for the board state |
mhannay3 | 1:cd78922f70fa | 185 | float calculateBoardState() { |
mhannay3 | 1:cd78922f70fa | 186 | return 0.0; |
mhannay3 | 1:cd78922f70fa | 187 | } |
mhannay3 | 1:cd78922f70fa | 188 | |
mhannay3 | 1:cd78922f70fa | 189 | coord getTopLeftOfSquare(int row, int column) { |
mhannay3 | 1:cd78922f70fa | 190 | coord topLeft; |
mhannay3 | 1:cd78922f70fa | 191 | topLeft.x = 16 * column; |
mhannay3 | 1:cd78922f70fa | 192 | topLeft.y = 112 - 16 * row; |
mhannay3 | 1:cd78922f70fa | 193 | } |
mhannay3 | 1:cd78922f70fa | 194 | |
mhannay3 | 1:cd78922f70fa | 195 | // removes selection border around square |
mhannay3 | 1:cd78922f70fa | 196 | void unselectSquare(int row, int column) { |
mhannay3 | 1:cd78922f70fa | 197 | coord tl = getTopLeftOfSquare(row, column); |
mhannay3 | 1:cd78922f70fa | 198 | uint64_t color; |
mhannay3 | 1:cd78922f70fa | 199 | if ((row + column) % 2 == 0) { |
mhannay3 | 1:cd78922f70fa | 200 | color = BOARD_DARK_COLOR; |
mhannay3 | 1:cd78922f70fa | 201 | } else { |
mhannay3 | 1:cd78922f70fa | 202 | color = BOARD_LIGHT_COLOR; |
mhannay3 | 1:cd78922f70fa | 203 | } |
mhannay3 | 1:cd78922f70fa | 204 | //uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, color); |
mhannay3 | 1:cd78922f70fa | 205 | } |
mhannay3 | 1:cd78922f70fa | 206 | |
mhannay3 | 1:cd78922f70fa | 207 | void hoverSquare(int row, int column) { |
mhannay3 | 1:cd78922f70fa | 208 | coord tl = getTopLeftOfSquare(row, column); |
mhannay3 | 1:cd78922f70fa | 209 | //uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, HOVER_COLOR); |
mhannay3 | 1:cd78922f70fa | 210 | } |
mhannay3 | 1:cd78922f70fa | 211 | |
mhannay3 | 1:cd78922f70fa | 212 | // draws selection border around square |
mhannay3 | 1:cd78922f70fa | 213 | void selectSquare(int row, int column) { |
mhannay3 | 1:cd78922f70fa | 214 | coord tl = getTopLeftOfSquare(row, column); |
mhannay3 | 1:cd78922f70fa | 215 | //uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, SELECTED_COLOR); |
mhannay3 | 1:cd78922f70fa | 216 | } |
mhannay3 | 1:cd78922f70fa | 217 | } |
mus3 | 0:d16cc59110a3 | 218 | |
mus3 | 0:d16cc59110a3 | 219 | int main() { |
mhannay3 | 1:cd78922f70fa | 220 | return 0; |
mhannay3 | 1:cd78922f70fa | 221 | } |