Mert Us Matthew Hannay Logan Starr

Dependencies:   mbed 4DGL-uLCD-SE

Committer:
mhannay3
Date:
Mon Nov 07 20:47:53 2022 +0000
Revision:
1:cd78922f70fa
Parent:
0:d16cc59110a3
Child:
2:80f1d1056ae4
initial commit

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 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 1:cd78922f70fa 22 return {_, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 1:cd78922f70fa 23 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 1:cd78922f70fa 24 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 1:cd78922f70fa 25 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 1:cd78922f70fa 26 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 1:cd78922f70fa 27 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 1:cd78922f70fa 28 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 1:cd78922f70fa 29 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 1:cd78922f70fa 30 _, _, _, X, X, X, X, X, X, X, _, _,
mhannay3 1:cd78922f70fa 31 _, X, X, X, X, X, X, X, X, X, X, _,
mhannay3 1:cd78922f70fa 32 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 1:cd78922f70fa 33 X, X, X, X, 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 1:cd78922f70fa 39 return {X, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 1:cd78922f70fa 40 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 1:cd78922f70fa 41 X, X, _, _, _, _, _, _, _, _, _, _,
mhannay3 1:cd78922f70fa 42 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 1:cd78922f70fa 43 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 1:cd78922f70fa 44 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 1:cd78922f70fa 45 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 1:cd78922f70fa 46 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 1:cd78922f70fa 47 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 1:cd78922f70fa 48 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 1:cd78922f70fa 49 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 1:cd78922f70fa 50 _, _, _, _, _, _, _, _, _, _, _, _};
mhannay3 1:cd78922f70fa 51 }
mhannay3 1:cd78922f70fa 52
mhannay3 1:cd78922f70fa 53 public:
mhannay3 1:cd78922f70fa 54 std::bitset<256> getBoardState() {
mhannay3 1:cd78922f70fa 55 return boardState;
mhannay3 1:cd78922f70fa 56 }
mhannay3 1:cd78922f70fa 57
mhannay3 1:cd78922f70fa 58 void setBoardState(std::bitset<256> newBoardState) {
mhannay3 1:cd78922f70fa 59 boardState = newBoardState;
mhannay3 1:cd78922f70fa 60 }
mus3 0:d16cc59110a3 61
mhannay3 1:cd78922f70fa 62 // initializes the starting board state
mhannay3 1:cd78922f70fa 63 Board() {
mhannay3 1:cd78922f70fa 64 placePiece(wR, 0, 0);
mhannay3 1:cd78922f70fa 65 placePiece(wN, 0, 1);
mhannay3 1:cd78922f70fa 66 placePiece(wB, 0, 2);
mhannay3 1:cd78922f70fa 67 placePiece(wQ, 0, 3);
mhannay3 1:cd78922f70fa 68 placePiece(wK, 0, 4);
mhannay3 1:cd78922f70fa 69 placePiece(wB, 0, 5);
mhannay3 1:cd78922f70fa 70 placePiece(wN, 0, 6);
mhannay3 1:cd78922f70fa 71 placePiece(wR, 0, 7);
mhannay3 1:cd78922f70fa 72 placePiece(bR, 7, 0);
mhannay3 1:cd78922f70fa 73 placePiece(bN, 7, 1);
mhannay3 1:cd78922f70fa 74 placePiece(bB, 7, 2);
mhannay3 1:cd78922f70fa 75 placePiece(bQ, 7, 3);
mhannay3 1:cd78922f70fa 76 placePiece(bK, 7, 4);
mhannay3 1:cd78922f70fa 77 placePiece(bB, 7, 5);
mhannay3 1:cd78922f70fa 78 placePiece(bN, 7, 6);
mhannay3 1:cd78922f70fa 79 placePiece(bR, 7, 7);
mhannay3 1:cd78922f70fa 80 for (int i = 0; i < 8; i++) {
mhannay3 1:cd78922f70fa 81 placePiece(w, 1, i);
mhannay3 1:cd78922f70fa 82 placePiece(b, 6, i);
mhannay3 1:cd78922f70fa 83 }
mhannay3 1:cd78922f70fa 84 }
mhannay3 1:cd78922f70fa 85
mhannay3 1:cd78922f70fa 86 // returns the piece at a given location
mhannay3 1:cd78922f70fa 87 Piece getPiece(int row, int column) {
mhannay3 1:cd78922f70fa 88 return (boardState >> (4 * (column + 8 * row))) & 15u;
mhannay3 1:cd78922f70fa 89 }
mhannay3 1:cd78922f70fa 90
mhannay3 1:cd78922f70fa 91 /* puts the bit representation of a piece at the set position of the board
mhannay3 1:cd78922f70fa 92 assumes that the position of the board is emptied beforehand
mhannay3 1:cd78922f70fa 93 */
mhannay3 1:cd78922f70fa 94 void placePiece(Piece piece, int row, int column) {
mhannay3 1:cd78922f70fa 95 boardState = boardState | (piece << (4 * (column + 8 * row)));
mhannay3 1:cd78922f70fa 96 }
mhannay3 1:cd78922f70fa 97
mhannay3 1:cd78922f70fa 98 /* removes a piece from the set position of the board
mhannay3 1:cd78922f70fa 99 returns the bit representation of the piece
mhannay3 1:cd78922f70fa 100 */
mhannay3 1:cd78922f70fa 101 Piece removePiece(int row, int column) {
mhannay3 1:cd78922f70fa 102 Piece removedPiece = (boardState >> (4 * (column + 8 * row))) & 15u;
mhannay3 1:cd78922f70fa 103 boardState = boardState & ~(15u << (4 * (column + 8 * row)));
mhannay3 1:cd78922f70fa 104 }
mhannay3 1:cd78922f70fa 105
mhannay3 1:cd78922f70fa 106 /* moves a piece from one position to another
mhannay3 1:cd78922f70fa 107 returns the captured piece
mhannay3 1:cd78922f70fa 108 */
mhannay3 1:cd78922f70fa 109 Piece movePiece(int startRow, int startColumn, int endRow, int endColumn) {
mhannay3 1:cd78922f70fa 110 Piece movingPiece = removePiece(startRow, startColumn);
mhannay3 1:cd78922f70fa 111 Piece capturedPiece = removePiece(endRow, endColumn);
mhannay3 1:cd78922f70fa 112 placePiece(movingPiece, endRow, endColumn);
mhannay3 1:cd78922f70fa 113 return capturedPiece;
mhannay3 1:cd78922f70fa 114 }
mhannay3 1:cd78922f70fa 115
mhannay3 1:cd78922f70fa 116 // calculates the advantage difference for the board state
mhannay3 1:cd78922f70fa 117 float calculateBoardState() {
mhannay3 1:cd78922f70fa 118 return 0.0;
mhannay3 1:cd78922f70fa 119 }
mhannay3 1:cd78922f70fa 120
mhannay3 1:cd78922f70fa 121 coord getTopLeftOfSquare(int row, int column) {
mhannay3 1:cd78922f70fa 122 coord topLeft;
mhannay3 1:cd78922f70fa 123 topLeft.x = 16 * column;
mhannay3 1:cd78922f70fa 124 topLeft.y = 112 - 16 * row;
mhannay3 1:cd78922f70fa 125 }
mhannay3 1:cd78922f70fa 126
mhannay3 1:cd78922f70fa 127 // removes selection border around square
mhannay3 1:cd78922f70fa 128 void unselectSquare(int row, int column) {
mhannay3 1:cd78922f70fa 129 coord tl = getTopLeftOfSquare(row, column);
mhannay3 1:cd78922f70fa 130 uint64_t color;
mhannay3 1:cd78922f70fa 131 if ((row + column) % 2 == 0) {
mhannay3 1:cd78922f70fa 132 color = BOARD_DARK_COLOR;
mhannay3 1:cd78922f70fa 133 } else {
mhannay3 1:cd78922f70fa 134 color = BOARD_LIGHT_COLOR;
mhannay3 1:cd78922f70fa 135 }
mhannay3 1:cd78922f70fa 136 //uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, color);
mhannay3 1:cd78922f70fa 137 }
mhannay3 1:cd78922f70fa 138
mhannay3 1:cd78922f70fa 139 void hoverSquare(int row, int column) {
mhannay3 1:cd78922f70fa 140 coord tl = getTopLeftOfSquare(row, column);
mhannay3 1:cd78922f70fa 141 //uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, HOVER_COLOR);
mhannay3 1:cd78922f70fa 142 }
mhannay3 1:cd78922f70fa 143
mhannay3 1:cd78922f70fa 144 // draws selection border around square
mhannay3 1:cd78922f70fa 145 void selectSquare(int row, int column) {
mhannay3 1:cd78922f70fa 146 coord tl = getTopLeftOfSquare(row, column);
mhannay3 1:cd78922f70fa 147 //uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, SELECTED_COLOR);
mhannay3 1:cd78922f70fa 148 }
mhannay3 1:cd78922f70fa 149 }
mus3 0:d16cc59110a3 150
mus3 0:d16cc59110a3 151 int main() {
mhannay3 1:cd78922f70fa 152 return 0;
mhannay3 1:cd78922f70fa 153 }