Mert Us Matthew Hannay Logan Starr
Dependencies: mbed 4DGL-uLCD-SE
Diff: main.cpp
- Revision:
- 4:5798e4062350
- Parent:
- 2:80f1d1056ae4
- Child:
- 5:b553c51b3b85
diff -r 80f1d1056ae4 -r 5798e4062350 main.cpp --- a/main.cpp Tue Nov 08 01:17:17 2022 +0000 +++ b/main.cpp Wed Nov 09 21:07:14 2022 +0000 @@ -7,16 +7,55 @@ uint8_t y; } -class Board { +class BoardState { + std::bitset<256> boardStateBits; + + // calculates the advantage difference for the board state + float calculateBoardState() { + return 0.0; + } + + // returns the piece at a given location + Piece getPiece(int row, int column) { + return (boardStateBits >> (4 * (column + 8 * row))) & 15u; + } + + /* puts the bit representation of a piece at the set position of the board + assumes that the position of the board is emptied beforehand + */ + void placePiece(Piece piece, int row, int column) { + boardStateBits = boardStateBits | (piece << (4 * (column + 8 * row))); + } + + /* removes a piece from the set position of the board + returns the bit representation of the piece + */ + Piece removePiece(int row, int column) { + Piece removedPiece = (boardStateBits >> (4 * (column + 8 * row))) & 15u; + boardStateBits = boardStateBits & ~(15u << (4 * (column + 8 * row))); + } + + /* moves a piece from one position to another + returns the captured piece + */ + Piece movePiece(int startRow, int startColumn, int endRow, int endColumn) { + Piece movingPiece = removePiece(startRow, startColumn); + Piece capturedPiece = removePiece(endRow, endColumn); + placePiece(movingPiece, endRow, endColumn); + return capturedPiece; + } +} + +class GameBoard { private: - std::bitset<256> boardState; + BoardState boardState; const uint32_t BOARD_DARK_COLOR = 0x769656; const uint32_t BOARD_LIGHT_COLOR = 0xbaca44; const uint32_t HOVER_COLOR = 0x0000ff; const uint32_t SELECTED_COLOR = 0xff8800; // piece sprites (12 x 12) - uint32_t* spritePawn(bool white, bool light) { + static uint32_t* spritePawn(bool white, bool light) { uint32_t X = white ? 0xffffff : 0x000000; uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; return {_, _, _, _, _, _, _, _, _, _, _, _, @@ -33,7 +72,7 @@ _, _, X, X, X, X, X, X, X, X, _, _}; } - uint32_t* spriteRook(bool white, bool light) { + static uint32_t* spriteRook(bool white, bool light) { uint32_t X = white ? 0xffffff : 0x000000; uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; return {X, X, _, X, X, _, _, X, X, _, X, X, @@ -50,7 +89,7 @@ X, X, X, X, X, X, X, X, X, X, X, X}; } - uint32_t* spriteKnight(bool white, bool light) { + static uint32_t* spriteKnight(bool white, bool light) { uint32_t X = white ? 0xffffff : 0x000000; uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; return {_, _, _, _, _, _, _, _, _, _, _, _, @@ -67,7 +106,7 @@ _, X, X, X, X, X, X, X, X, X, X, _}; } - uint32_t* spriteBishop(bool white, bool light) { + static uint32_t* spriteBishop(bool white, bool light) { uint32_t X = white ? 0xffffff : 0x000000; uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; return {_, _, _, _, _, X, X, _, _, _, _, _, @@ -84,7 +123,7 @@ _, _, X, X, X, X, X, X, X, X, _, _}; } - uint32_t* spriteQueen(bool white, bool light) { + static uint32_t* spriteQueen(bool white, bool light) { uint32_t X = white ? 0xffffff : 0x000000; uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; return {_, _, _, _, _, X, X, _, _, _, _, _, @@ -101,7 +140,7 @@ _, X, X, X, X, X, X, X, X, X, X, _}; } - uint32_t* spriteKing(bool white, bool light) { + static uint32_t* spriteKing(bool white, bool light) { uint32_t X = white ? 0xffffff : 0x000000; uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; return {_, _, _, _, _, X, X, _, _, _, _, _, @@ -119,16 +158,16 @@ } public: - std::bitset<256> getBoardState() { + BoardState getBoardState() { return boardState; } - void setBoardState(std::bitset<256> newBoardState) { + void setBoardState(BoardState newBoardState) { boardState = newBoardState; } // initializes the starting board state - Board() { + GameBoard() { placePiece(wR, 0, 0); placePiece(wN, 0, 1); placePiece(wB, 0, 2); @@ -150,48 +189,49 @@ placePiece(b, 6, i); } } - + + // gets the pixel coordinates of the top left of the square + coord getTopLeftOfSquare(int row, int column) { + coord topLeft; + topLeft.x = 16 * column; + topLeft.y = 112 - 16 * row; + } + + // PIECE MOVEMENT AND GRAPHICS FUNCTIONS + // returns the piece at a given location Piece getPiece(int row, int column) { - return (boardState >> (4 * (column + 8 * row))) & 15u; + return boardState.getPiece(row, column); } /* puts the bit representation of a piece at the set position of the board assumes that the position of the board is emptied beforehand */ void placePiece(Piece piece, int row, int column) { - boardState = boardState | (piece << (4 * (column + 8 * row))); + boardState.placePiece(piece, row, column); + // draw } /* removes a piece from the set position of the board returns the bit representation of the piece */ Piece removePiece(int row, int column) { - Piece removedPiece = (boardState >> (4 * (column + 8 * row))) & 15u; - boardState = boardState & ~(15u << (4 * (column + 8 * row))); + Piece removedPiece = boardState.placePiece(piece, row, column); + // draw + return removedPiece; } /* moves a piece from one position to another returns the captured piece */ Piece movePiece(int startRow, int startColumn, int endRow, int endColumn) { - Piece movingPiece = removePiece(startRow, startColumn); - Piece capturedPiece = removePiece(endRow, endColumn); - placePiece(movingPiece, endRow, endColumn); + Piece capturedPiece = boardState.movePiece(startRow, startColumn, endRow, endColumn); + // draw return capturedPiece; } - // calculates the advantage difference for the board state - float calculateBoardState() { - return 0.0; - } - - coord getTopLeftOfSquare(int row, int column) { - coord topLeft; - topLeft.x = 16 * column; - topLeft.y = 112 - 16 * row; - } - + // SQUARE BORDER GRAPHICS FUNCTIONS + // removes selection border around square void unselectSquare(int row, int column) { coord tl = getTopLeftOfSquare(row, column);