Mert Us Matthew Hannay Logan Starr
Dependencies: mbed 4DGL-uLCD-SE
main.cpp
- Committer:
- mus3
- Date:
- 2022-11-11
- Revision:
- 3:0f979d8d3fa1
- Parent:
- 2:80f1d1056ae4
File content as of revision 3:0f979d8d3fa1:
#include <bitset> #include "mbed.h" #include "uLCD_4DGL.h" 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: Piece boardState[64]; 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) { uint32_t X = white ? 0xffffff : 0x000000; uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; 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, _, _}; return r; } uint32_t* spriteRook(bool white, bool light) { uint32_t X = white ? 0xffffff : 0x000000; uint32_t _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; 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, _, X, X, X, _, X, X, _, X, X, X, _, _, X, X, X, _, X, X, _, X, X, X, _, _, _, X, X, _, X, X, _, X, X, _, _, _, _, X, X, _, X, X, _, X, X, _, _, _, _, X, X, _, X, X, _, X, X, _, _, _, X, X, X, X, X, X, X, X, X, X, _, 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; 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, X, X, X, _, _, _, _, X, X, X, X, 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; 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, 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; 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, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, _, X, X, X, X, X, X, _, X, X, X, X, X, X, _, X, X, _, X, X, X, X, _, X, X, X, X, 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; 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, X, _, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, _, X, X, X, X, _, X, X, X, _, X, X, 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: Piece* getBoardState() { return boardState; } 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); 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); } } // returns the piece at a given location Piece getPiece(int row, int column) { 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[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 = getPiece(row, column); boardState[row * 8 + column] = 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 movingPiece = removePiece(startRow, startColumn); Piece capturedPiece = removePiece(endRow, endColumn); 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); 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) { 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() { 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); } }