Mert Us Matthew Hannay Logan Starr
Dependencies: mbed 4DGL-uLCD-SE
Diff: main.cpp
- Revision:
- 3:0f979d8d3fa1
- Parent:
- 2:80f1d1056ae4
--- a/main.cpp Tue Nov 08 01:17:17 2022 +0000 +++ b/main.cpp Fri Nov 11 07:13:53 2022 +0000 @@ -1,15 +1,17 @@ #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}; +enum Piece : uint8_t {e, wK, bK, wQ, bQ, wR, bR, wB, bB, wN, bN, w, b}; struct coord { uint8_t x; uint8_t y; -} +}; class Board { private: - std::bitset<256> boardState; + Piece boardState[64]; const uint32_t BOARD_DARK_COLOR = 0x769656; const uint32_t BOARD_LIGHT_COLOR = 0xbaca44; const uint32_t HOVER_COLOR = 0x0000ff; @@ -19,7 +21,7 @@ uint32_t* spritePawn(bool white, bool light) { uint32_t X = white ? 0xffffff : 0x000000; uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; - return {_, _, _, _, _, _, _, _, _, _, _, _, + uint32_t r[] = {_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, @@ -30,13 +32,14 @@ _, _, _, _, _, X, X, _, _, _, _, _, _, _, _, _, X, X, X, X, _, _, _, _, _, _, X, X, X, X, X, X, X, X, _, _, - _, _, X, X, X, X, X, X, X, X, _, _}; + _, _, X, X, X, X, X, X, X, X, _, _}; + return r; } 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, + uint32_t r[] = {X, X, _, X, X, _, _, X, X, _, X, X, X, X, _, X, X, _, _, X, X, _, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, @@ -48,12 +51,13 @@ _, X, X, X, X, X, X, X, X, X, X, _, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X}; + return r; } uint32_t* spriteKnight(bool white, bool light) { uint32_t X = white ? 0xffffff : 0x000000; uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; - return {_, _, _, _, _, _, _, _, _, _, _, _, + uint32_t r[] = {_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, X, X, _, X, X, _, _, _, _, _, _, _, X, X, _, X, X, _, _, _, _, _, X, X, X, X, X, X, _, _, _, @@ -65,12 +69,13 @@ _, _, X, X, X, X, X, X, X, X, _, _, _, X, X, X, X, X, X, X, X, X, X, _, _, X, X, X, X, X, X, X, X, X, X, _}; + return r; } 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, _, _, _, _, _, + uint32_t r[] = {_, _, _, _, _, X, X, _, _, _, _, _, _, _, _, _, X, X, X, _, _, _, _, _, _, _, _, X, X, X, _, _, X, _, _, _, _, _, _, X, X, _, _, X, X, _, _, _, @@ -82,12 +87,13 @@ _, _, _, X, X, X, X, X, X, _, _, _, _, _, X, X, X, X, X, X, X, X, _, _, _, _, X, X, X, X, X, X, X, X, _, _}; + return r; } 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, _, _, _, _, _, + uint32_t r[] = {_, _, _, _, _, X, X, _, _, _, _, _, _, _, X, _, _, X, X, _, _, X, _, _, X, _, X, X, _, X, X, _, X, X, _, X, X, _, X, X, _, X, X, _, X, X, _, X, @@ -99,12 +105,13 @@ _, X, X, X, X, X, X, X, X, X, X, _, _, _, X, X, X, X, X, X, X, X, _, _, _, X, X, X, X, X, X, X, X, X, X, _}; + return r; } 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, _, _, _, _, _, + uint32_t r[] = {_, _, _, _, _, X, X, _, _, _, _, _, _, _, _, _, _, X, X, _, _, _, _, _, _, _, _, X, X, X, X, X, X, _, _, _, _, _, _, X, X, X, X, X, X, _, _, _, @@ -116,19 +123,25 @@ _, X, X, X, X, X, X, X, X, X, X, _, _, _, X, X, X, _, _, X, X, X, _, _, _, X, X, X, X, X, X, X, X, X, X, _}; + return r; } public: - std::bitset<256> getBoardState() { + Piece* getBoardState() { return boardState; } - - void setBoardState(std::bitset<256> newBoardState) { - boardState = newBoardState; + + void setBoardState(Piece newBoardState[]) { + for (int i = 0; i < 64; i++) { + boardState[i] = newBoardState[i]; + } } - + // initializes the starting board state Board() { + for (int i = 0; i < 64; i++) { + placePiece(e, i / 8, i % 8); + } placePiece(wR, 0, 0); placePiece(wN, 0, 1); placePiece(wB, 0, 2); @@ -150,27 +163,28 @@ placePiece(b, 6, i); } } - + // returns the piece at a given location Piece getPiece(int row, int column) { - return (boardState >> (4 * (column + 8 * row))) & 15u; + return boardState[row * 8 + 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[row * 8 + column] = 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 = (boardState >> (4 * (column + 8 * row))) & 15u; - boardState = boardState & ~(15u << (4 * (column + 8 * row))); + Piece removedPiece = getPiece(row, column); + boardState[row * 8 + column] = e; + return removedPiece; } - + /* moves a piece from one position to another returns the captured piece */ @@ -180,18 +194,18 @@ placePiece(movingPiece, endRow, endColumn); 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; } - + // removes selection border around square void unselectSquare(int row, int column) { coord tl = getTopLeftOfSquare(row, column); @@ -203,19 +217,146 @@ } //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) { coord tl = getTopLeftOfSquare(row, column); //uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, SELECTED_COLOR); } +}; + +DigitalOut led(LED1); +uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin; + +void drawPiece(int row, int column, uLCD_4DGL uLCD, uint32_t boardColor, uint32_t pieceColor, uint32_t pieceGraphic[]) { + uLCD.filled_rectangle(column * 16, row * 16, column * 16 + 16, row * 16 + 2, boardColor); + uLCD.filled_rectangle(column * 16, row * 16 + 14, column * 16 + 16, row * 16 + 16, boardColor); + uLCD.filled_rectangle(column * 16, row * 16, column * 16 + 2, row * 16 + 16, boardColor); + uLCD.filled_rectangle(column * 16 + 14, row * 16, column * 16 + 16, row * 16 + 16, boardColor); + for (int x = 0; x < 12; x++) { + for (int y = 0; y < 12; y++) { + if (pieceGraphic[y * 12 + x] == 0x000001) { + uLCD.pixel(column * 16 + x + 2, row * 16 + y + 2, boardColor); + } else { + uLCD.pixel(column * 16 + x + 2, row * 16 + y + 2, pieceColor); + } + } + } } int main() { - return 0; + Board game; + //Piece* bs = game.getBoardState(); + //for (int i = 0; i < 8; i++) { + //uLCD.printf("%d\n", static_cast<uint8_t>(bs[i+8])); + //} + led = 0; + uint32_t X = 0x000002; + uint32_t _ = 0x000001; + uint32_t boardColor; + uint32_t pieceColor; + for (int row = 0; row < 8; row++) { + for (int column = 0; column < 8; column++) { + Piece currPiece = game.getPiece(row, column); + if ((row+column) % 2) { + boardColor = 0x769656; + } else { + boardColor = 0xbaca44; + } + uLCD.filled_rectangle(column * 16, row * 16, column * 16 + 16, row * 16 + 16, boardColor); + if (currPiece == e) { + continue; + } else { + // e, wK, bK, wQ, bQ, wR, bR, wB, bB, wN, bN, w, b + if (currPiece == wK) { + pieceColor = 0xffffff; + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 8, column * 16 + 13, row * 16 + 13, pieceColor); + uLCD.filled_rectangle(column * 16 + 7, row * 16 + 2, column * 16 + 8, row * 16 + 8, pieceColor); + uLCD.filled_rectangle(column * 16 + 5, row * 16 + 4, column * 16 + 10, row * 16 + 5, pieceColor); + } else if (currPiece == bK) { + pieceColor = 0x000000; + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 8, column * 16 + 13, row * 16 + 13, pieceColor); + uLCD.filled_rectangle(column * 16 + 7, row * 16 + 2, column * 16 + 8, row * 16 + 8, pieceColor); + uLCD.filled_rectangle(column * 16 + 5, row * 16 + 4, column * 16 + 10, row * 16 + 5, pieceColor); + } else if (currPiece == wQ) { + pieceColor = 0xffffff; + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 8, column * 16 + 13, row * 16 + 13, pieceColor); + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 4, column * 16 + 3, row * 16 + 8, pieceColor); + uLCD.filled_rectangle(column * 16 + 7, row * 16 + 4, column * 16 + 8, row * 16 + 8, pieceColor); + uLCD.filled_rectangle(column * 16 + 12, row * 16 + 4, column * 16 + 13, row * 16 + 8, pieceColor); + uLCD.filled_circle(column * 16 + 3, row * 16 + 4, 2, pieceColor); + uLCD.filled_circle(column * 16 + 8, row * 16 + 4, 2, pieceColor); + uLCD.filled_circle(column * 16 + 12, row * 16 + 4, 2, pieceColor); + } else if (currPiece == bQ) { + pieceColor = 0x000000; + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 8, column * 16 + 13, row * 16 + 13, pieceColor); + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 4, column * 16 + 3, row * 16 + 8, pieceColor); + uLCD.filled_rectangle(column * 16 + 7, row * 16 + 4, column * 16 + 8, row * 16 + 8, pieceColor); + uLCD.filled_rectangle(column * 16 + 12, row * 16 + 4, column * 16 + 13, row * 16 + 8, pieceColor); + uLCD.filled_circle(column * 16 + 3, row * 16 + 4, 2, pieceColor); + uLCD.filled_circle(column * 16 + 8, row * 16 + 4, 2, pieceColor); + uLCD.filled_circle(column * 16 + 12, row * 16 + 4, 2, pieceColor); + } else if (currPiece == wR) { + pieceColor = 0xffffff; + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 5, column * 16 + 13, row * 16 + 13, pieceColor); + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 2, column * 16 + 4, row * 16 + 5, pieceColor); + uLCD.filled_rectangle(column * 16 + 6, row * 16 + 2, column * 16 + 9, row * 16 + 5, pieceColor); + uLCD.filled_rectangle(column * 16 + 11, row * 16 + 2, column * 16 + 13, row * 16 + 5, pieceColor); + } else if (currPiece == bR) { + pieceColor = 0x000000; + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 5, column * 16 + 13, row * 16 + 13, pieceColor); + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 2, column * 16 + 4, row * 16 + 5, pieceColor); + uLCD.filled_rectangle(column * 16 + 6, row * 16 + 2, column * 16 + 9, row * 16 + 5, pieceColor); + uLCD.filled_rectangle(column * 16 + 11, row * 16 + 2, column * 16 + 13, row * 16 + 5, pieceColor); + } else if (currPiece == wB) { + pieceColor = 0xffffff; + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 11, column * 16 + 13, row * 16 + 13, pieceColor); + uLCD.filled_rectangle(column * 16 + 4, row * 16 + 8, column * 16 + 11, row * 16 + 10, pieceColor); + uLCD.filled_circle(column * 16 + 7, row * 16 + 5, 3, pieceColor); + uLCD.filled_circle(column * 16 + 8, row * 16 + 5, 3, pieceColor); + } else if (currPiece == bB) { + pieceColor = 0x000000; + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 11, column * 16 + 13, row * 16 + 13, pieceColor); + uLCD.filled_rectangle(column * 16 + 4, row * 16 + 8, column * 16 + 11, row * 16 + 10, pieceColor); + uLCD.filled_circle(column * 16 + 7, row * 16 + 5, 3, pieceColor); + uLCD.filled_circle(column * 16 + 8, row * 16 + 5, 3, pieceColor); + } else if (currPiece == wN) { + pieceColor = 0xffffff; + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 11, column * 16 + 13, row * 16 + 13, pieceColor); + uLCD.filled_rectangle(column * 16 + 8, row * 16 + 2, column * 16 + 11, row * 16 + 10, pieceColor); + uLCD.filled_rectangle(column * 16 + 6, row * 16 + 3, column * 16 + 7, row * 16 + 8, pieceColor); + uLCD.filled_rectangle(column * 16 + 4, row * 16 + 4, column * 16 + 5, row * 16 + 7, pieceColor); + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 5, column * 16 + 3, row * 16 + 6, pieceColor); + } else if (currPiece == bN) { + pieceColor = 0x000000; + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 11, column * 16 + 13, row * 16 + 13, pieceColor); + uLCD.filled_rectangle(column * 16 + 8, row * 16 + 2, column * 16 + 11, row * 16 + 10, pieceColor); + uLCD.filled_rectangle(column * 16 + 6, row * 16 + 3, column * 16 + 7, row * 16 + 8, pieceColor); + uLCD.filled_rectangle(column * 16 + 4, row * 16 + 4, column * 16 + 5, row * 16 + 7, pieceColor); + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 5, column * 16 + 3, row * 16 + 6, pieceColor); + } else if (currPiece == w) { + pieceColor = 0xffffff; + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 11, column * 16 + 13, row * 16 + 13, pieceColor); + uLCD.filled_rectangle(column * 16 + 6, row * 16 + 8, column * 16 + 9, row * 16 + 10, pieceColor); + uLCD.filled_circle(column * 16 + 7, row * 16 + 5, 2, pieceColor); + uLCD.filled_circle(column * 16 + 8, row * 16 + 5, 2, pieceColor); + } else if (currPiece == b) { + pieceColor = 0x000000; + uLCD.filled_rectangle(column * 16 + 2, row * 16 + 11, column * 16 + 13, row * 16 + 13, pieceColor); + uLCD.filled_rectangle(column * 16 + 6, row * 16 + 8, column * 16 + 9, row * 16 + 10, pieceColor); + uLCD.filled_circle(column * 16 + 7, row * 16 + 5, 2, pieceColor); + uLCD.filled_circle(column * 16 + 8, row * 16 + 5, 2, pieceColor); + } + } + } + } + while (1) { + led = !led; + wait(.2); + } } \ No newline at end of file