Mert Us Matthew Hannay Logan Starr
Dependencies: mbed 4DGL-uLCD-SE
Diff: main.cpp
- Revision:
- 5:b553c51b3b85
- Parent:
- 4:5798e4062350
- Child:
- 6:1c4dd5e24b8d
--- a/main.cpp Wed Nov 09 21:07:14 2022 +0000 +++ b/main.cpp Fri Nov 11 22:48:30 2022 +0000 @@ -1,239 +1,300 @@ -#include <bitset> +#include "mbed.h" +#include "uLCD_4DGL.h" -enum Piece : u_int8_t {e, wK, bK, wQ, bQ, wR, bR, wB, bB, wN, bN, w, b}; +uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin; + +enum Piece {e, wK, bK, wQ, bQ, wR, bR, wB, bB, wN, bN, w, b}; struct coord { uint8_t x; uint8_t y; -} +}; -class BoardState { - std::bitset<256> boardStateBits; - +class BoardState +{ +private: + Piece array[64]; +public: // calculates the advantage difference for the board state - float calculateBoardState() { + 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; + Piece getPiece(int row, int column) + { + return array[column + 8 * row]; } - /* 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))); + // puts a piece at a given location + void placePiece(Piece piece, int row, int column) + { + array[column + 8 * row] = piece; } /* 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: - 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) - static uint32_t* spritePawn(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, X, X, _, _, _, _, - _, _, _, _, _, X, X, _, _, _, _, _, - _, _, _, _, _, X, X, _, _, _, _, _, - _, _, _, _, X, X, X, X, _, _, _, _, - _, _, X, X, X, X, X, X, X, X, _, _, - _, _, X, X, X, X, X, X, X, X, _, _}; - } - - 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, - X, X, _, X, X, _, _, X, X, _, X, X, - X, X, X, X, X, X, X, X, X, X, X, X, - X, X, X, X, X, X, X, X, X, X, X, X, - _, X, X, X, _, X, X, _, X, X, X, _, - _, X, X, X, _, X, X, _, X, X, X, _, - _, _, X, X, _, X, X, _, X, X, _, _, - _, _, X, X, _, X, X, _, X, X, _, _, - _, _, X, X, _, X, X, _, X, X, _, _, - _, X, X, X, X, X, X, X, X, X, X, _, - X, X, X, X, X, X, X, X, X, X, X, X, - X, X, X, X, X, X, X, X, X, X, X, X}; - } - - static uint32_t* spriteKnight(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, _, _, - _, _, _, X, X, X, X, X, X, _, _, _, - _, _, X, X, X, X, X, _, X, _, _, _, - _, _, X, X, X, X, X, X, X, _, _, _, - _, _, _, _, _, X, X, X, X, _, _, _, - _, _, _, _, X, X, X, X, X, _, _, _, - _, _, _, X, X, X, X, X, X, X, _, _, - _, _, X, X, X, X, X, X, X, X, _, _, - _, X, X, X, X, X, X, X, X, X, X, _, - _, X, X, X, X, X, X, X, X, X, X, _}; - } - - 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, _, _, _, _, _, - _, _, _, _, X, X, X, _, _, _, _, _, - _, _, _, X, X, X, _, _, X, _, _, _, - _, _, _, X, X, _, _, X, X, _, _, _, - _, _, _, X, X, X, X, X, X, _, _, _, - _, _, _, _, X, X, X, X, _, _, _, _, - _, _, _, _, _, X, X, _, _, _, _, _, - _, _, _, _, X, X, X, X, _, _, _, _, - _, _, _, X, X, X, X, X, X, _, _, _, - _, _, _, X, X, X, X, X, X, _, _, _, - _, _, X, X, X, X, X, X, X, X, _, _, - _, _, X, X, X, X, X, X, X, X, _, _}; - } - - 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, _, _, _, _, _, - _, _, X, _, _, X, X, _, _, X, _, _, - X, _, X, X, _, X, X, _, X, X, _, X, - X, _, X, X, _, X, X, _, X, X, _, X, - X, _, X, X, _, X, X, _, X, X, _, X, - X, X, X, X, X, X, X, X, X, X, X, X, - X, X, X, X, X, X, X, X, X, X, X, X, - X, X, _, X, X, X, X, X, X, _, X, X, - X, X, X, X, _, X, X, _, X, X, X, X, - _, X, X, X, X, X, X, X, X, X, X, _, - _, _, X, X, X, X, X, X, X, X, _, _, - _, X, X, X, X, X, X, X, X, X, X, _}; - } - - 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, _, _, _, _, _, - _, _, _, _, _, X, X, _, _, _, _, _, - _, _, _, X, X, X, X, X, X, _, _, _, - _, _, _, X, X, X, X, X, X, _, _, _, - X, X, _, _, _, X, X, _, _, _, X, X, - X, X, X, X, _, X, X, _, X, X, X, X, - X, _, X, X, X, X, X, X, X, X, _, X, - X, X, X, X, X, X, X, X, X, X, X, X, - X, X, X, _, X, X, X, X, _, X, X, X, - _, X, X, X, X, X, X, X, X, X, X, _, - _, _, X, X, X, _, _, X, X, X, _, _, - _, X, X, X, X, X, X, X, X, X, X, _}; - } - -public: - BoardState getBoardState() { - return boardState; - } - - void setBoardState(BoardState newBoardState) { - boardState = newBoardState; - } - - // initializes the starting board state - GameBoard() { - placePiece(wR, 0, 0); - placePiece(wN, 0, 1); - placePiece(wB, 0, 2); - placePiece(wQ, 0, 3); - placePiece(wK, 0, 4); - placePiece(wB, 0, 5); - placePiece(wN, 0, 6); - placePiece(wR, 0, 7); - placePiece(bR, 7, 0); - placePiece(bN, 7, 1); - placePiece(bB, 7, 2); - placePiece(bQ, 7, 3); - placePiece(bK, 7, 4); - placePiece(bB, 7, 5); - placePiece(bN, 7, 6); - placePiece(bR, 7, 7); - for (int i = 0; i < 8; i++) { - placePiece(w, 1, i); - 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.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.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.placePiece(piece, row, column); - // draw + Piece removePiece(int row, int column) + { + Piece removedPiece = array[column + 8 * row]; + array[column + 8 * row] = e; 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 capturedPiece = boardState.movePiece(startRow, startColumn, endRow, endColumn); - // draw + 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; } +}; - // SQUARE BORDER GRAPHICS FUNCTIONS - - // removes selection border around square - void unselectSquare(int row, int column) { +class GameBoard +{ +private: + BoardState boardState; + static const int BOARD_DARK_COLOR = 0x769656; + static const int BOARD_LIGHT_COLOR = 0xbaca44; + static const int HOVER_COLOR = 0x0000ff; + static const int SELECTED_COLOR = 0xff8800; + + // piece sprites (12 x 12) + static void spritePawn(bool white, bool light, int* spriteArray) + { + int X = white ? 0xffffff : 0x000000; + int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; + int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, X, X, _, _, _, _, _, + _, _, _, _, X, X, X, X, _, _, _, _, + _, _, _, _, X, X, X, X, _, _, _, _, + _, _, _, _, _, X, X, _, _, _, _, _, + _, _, _, _, _, X, X, _, _, _, _, _, + _, _, _, _, X, X, X, X, _, _, _, _, + _, _, X, X, X, X, X, X, X, X, _, _, + _, _, X, X, X, X, X, X, X, X, _, _ + }; + memcpy(sprite, spriteArray, sizeof(sprite)); + } + + static void spriteRook(bool white, bool light, int* spriteArray) + { + int X = white ? 0xffffff : 0x000000; + int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; + int sprite[144] = {X, X, _, X, X, _, _, X, X, _, X, X, + X, X, _, X, X, _, _, X, X, _, X, X, + X, X, X, X, X, X, X, X, X, X, X, X, + X, X, X, X, X, X, X, X, X, X, X, X, + _, X, X, X, _, X, X, _, X, X, X, _, + _, X, X, X, _, X, X, _, X, X, X, _, + _, _, X, X, _, X, X, _, X, X, _, _, + _, _, X, X, _, X, X, _, X, X, _, _, + _, _, X, X, _, X, X, _, X, X, _, _, + _, X, X, X, X, X, X, X, X, X, X, _, + X, X, X, X, X, X, X, X, X, X, X, X, + X, X, X, X, X, X, X, X, X, X, X, X + }; + memcpy(sprite, spriteArray, sizeof(sprite)); + } + + static void spriteKnight(bool white, bool light, int* spriteArray) + { + int X = white ? 0xffffff : 0x000000; + int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; + int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, X, X, _, X, X, _, _, + _, _, _, _, _, X, X, _, X, X, _, _, + _, _, _, X, X, X, X, X, X, _, _, _, + _, _, X, X, X, X, X, _, X, _, _, _, + _, _, X, X, X, X, X, X, X, _, _, _, + _, _, _, _, _, X, X, X, X, _, _, _, + _, _, _, _, X, X, X, X, X, _, _, _, + _, _, _, X, X, X, X, X, X, X, _, _, + _, _, X, X, X, X, X, X, X, X, _, _, + _, X, X, X, X, X, X, X, X, X, X, _, + _, X, X, X, X, X, X, X, X, X, X, _ + }; + memcpy(sprite, spriteArray, sizeof(sprite)); + } + + static void spriteBishop(bool white, bool light, int* spriteArray) + { + int X = white ? 0xffffff : 0x000000; + int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; + int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _, + _, _, _, _, X, X, X, _, _, _, _, _, + _, _, _, X, X, X, _, _, X, _, _, _, + _, _, _, X, X, _, _, X, X, _, _, _, + _, _, _, X, X, X, X, X, X, _, _, _, + _, _, _, _, X, X, X, X, _, _, _, _, + _, _, _, _, _, X, X, _, _, _, _, _, + _, _, _, _, X, X, X, X, _, _, _, _, + _, _, _, X, X, X, X, X, X, _, _, _, + _, _, _, X, X, X, X, X, X, _, _, _, + _, _, X, X, X, X, X, X, X, X, _, _, + _, _, X, X, X, X, X, X, X, X, _, _ + }; + memcpy(sprite, spriteArray, sizeof(sprite)); + } + + static void spriteQueen(bool white, bool light, int* spriteArray) + { + int X = white ? 0xffffff : 0x000000; + int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; + int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _, + _, _, X, _, _, X, X, _, _, X, _, _, + X, _, X, X, _, X, X, _, X, X, _, X, + X, _, X, X, _, X, X, _, X, X, _, X, + X, _, X, X, _, X, X, _, X, X, _, X, + X, X, X, X, X, X, X, X, X, X, X, X, + X, X, X, X, X, X, X, X, X, X, X, X, + X, X, _, X, X, X, X, X, X, _, X, X, + X, X, X, X, _, X, X, _, X, X, X, X, + _, X, X, X, X, X, X, X, X, X, X, _, + _, _, X, X, X, X, X, X, X, X, _, _, + _, X, X, X, X, X, X, X, X, X, X, _ + }; + memcpy(sprite, spriteArray, sizeof(sprite)); + } + + static void spriteKing(bool white, bool light, int* spriteArray) + { + int X = white ? 0xffffff : 0x000000; + int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; + int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _, + _, _, _, _, _, X, X, _, _, _, _, _, + _, _, _, X, X, X, X, X, X, _, _, _, + _, _, _, X, X, X, X, X, X, _, _, _, + X, X, _, _, _, X, X, _, _, _, X, X, + X, X, X, X, _, X, X, _, X, X, X, X, + X, _, X, X, X, X, X, X, X, X, _, X, + X, X, X, X, X, X, X, X, X, X, X, X, + X, X, X, _, X, X, X, X, _, X, X, X, + _, X, X, X, X, X, X, X, X, X, X, _, + _, _, X, X, X, _, _, X, X, X, _, _, + _, X, X, X, X, X, X, X, X, X, X, _ + }; + memcpy(sprite, spriteArray, sizeof(sprite)); + } + +public: + BoardState getBoardState() + { + return boardState; + } + + void setBoardState(BoardState newBoardState) + { + boardState = newBoardState; + } + + // initializes the starting board state + GameBoard() + { + // draw board + for (int row = 0; row < 8; row++) { + for (int column = 0; column < 8; column++) { + uint64_t color; + if ((row + column) % 2 == 0) { + color = BOARD_DARK_COLOR; + } else { + color = BOARD_LIGHT_COLOR; + } + coord tl = getTopLeftOfSquare(row, column); + uLCD.filled_rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, color); + } + } + // draw pieces + placePieceAndDraw(wR, 0, 0); + placePieceAndDraw(wN, 0, 1); + placePieceAndDraw(wB, 0, 2); + placePieceAndDraw(wQ, 0, 3); + placePieceAndDraw(wK, 0, 4); + placePieceAndDraw(wB, 0, 5); + placePieceAndDraw(wN, 0, 6); + placePieceAndDraw(wR, 0, 7); + placePieceAndDraw(bR, 7, 0); + placePieceAndDraw(bN, 7, 1); + placePieceAndDraw(bB, 7, 2); + placePieceAndDraw(bQ, 7, 3); + placePieceAndDraw(bK, 7, 4); + placePieceAndDraw(bB, 7, 5); + placePieceAndDraw(bN, 7, 6); + placePieceAndDraw(bR, 7, 7); + for (int i = 0; i < 8; i++) { + placePieceAndDraw(w, 1, i); + placePieceAndDraw(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; + return topLeft; + } + + // PIECE MOVEMENT AND GRAPHICS FUNCTIONS + + // returns the piece at a given location + Piece getPiece(int row, int column) + { + 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 placePieceAndDraw(Piece piece, int row, int column) + { + boardState.placePiece(piece, row, column); + coord tl = getTopLeftOfSquare(row, column); + int sprite[144]; + switch(piece) { + case wK: + case bK: + spriteKing(piece==wK, (row+column)%2, sprite); + break; + case wQ: + case bQ: + spriteQueen(piece==wQ, (row+column)%2, sprite); + break; + case wB: + case bB: + spriteBishop(piece==wB, (row+column)%2, sprite); + break; + case wN: + case bN: + spriteKing(piece==wN, (row+column)%2, sprite); + break; + case wR: + case bR: + spriteKing(piece==wR, (row+column)%2, sprite); + break; + case w: + case b: + spriteKing(piece==w, (row+column)%2, sprite); + break; + } + uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); + } + + /* removes a piece from the set position of the board + returns the bit representation of the piece + */ + Piece removePieceAndDraw(int row, int column) + { + Piece removedPiece = boardState.removePiece(row, column); coord tl = getTopLeftOfSquare(row, column); uint64_t color; if ((row + column) % 2 == 0) { @@ -241,21 +302,51 @@ } else { color = BOARD_LIGHT_COLOR; } - //uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, color); + uLCD.filled_rectangle(tl.x+2, tl.y+2, tl.x + 13, tl.y + 13, color); + return removedPiece; + } + + /* moves a piece from one position to another + returns the captured piece + */ + Piece movePieceAndDraw(int startRow, int startColumn, int endRow, int endColumn) + { + Piece movingPiece = removePieceAndDraw(startRow, startColumn); + Piece capturedPiece = boardState.removePiece(endRow, endColumn); + placePieceAndDraw(movingPiece, endRow, endColumn); + return capturedPiece; } - void hoverSquare(int row, int column) { + // SQUARE BORDER GRAPHICS FUNCTIONS + + // removes selection border around square + void unselectSquare(int row, int column) + { coord tl = getTopLeftOfSquare(row, column); - //uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, HOVER_COLOR); + uint64_t color; + if ((row + column) % 2 == 0) { + color = BOARD_DARK_COLOR; + } else { + color = BOARD_LIGHT_COLOR; + } + uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, color); + } + + void hoverSquare(int row, int column) + { + coord tl = getTopLeftOfSquare(row, column); + uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, HOVER_COLOR); } // draws selection border around square - void selectSquare(int row, int column) { + void selectSquare(int row, int column) + { coord tl = getTopLeftOfSquare(row, column); - //uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, SELECTED_COLOR); + uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, SELECTED_COLOR); } -} +}; -int main() { +int main() +{ return 0; } \ No newline at end of file