Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed 4DGL-uLCD-SE
main.cpp
- Committer:
- mhannay3
- Date:
- 2022-11-08
- Revision:
- 2:80f1d1056ae4
- Parent:
- 1:cd78922f70fa
- Child:
- 3:0f979d8d3fa1
- Child:
- 4:5798e4062350
File content as of revision 2:80f1d1056ae4:
#include <bitset>
enum Piece : u_int8_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;
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;
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, _, _};
}
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};
}
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, _};
}
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, _, _};
}
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, _};
}
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:
std::bitset<256> getBoardState() {
return boardState;
}
void setBoardState(std::bitset<256> newBoardState) {
boardState = newBoardState;
}
// initializes the starting board state
Board() {
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 >> (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) {
boardState = boardState | (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 = (boardState >> (4 * (column + 8 * row))) & 15u;
boardState = boardState & ~(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;
}
// 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);
}
}
int main() {
return 0;
}