Mert Us Matthew Hannay Logan Starr
Dependencies: mbed 4DGL-uLCD-SE
Diff: main.cpp
- Revision:
- 6:1c4dd5e24b8d
- Parent:
- 5:b553c51b3b85
- Child:
- 7:9e01c9a334dc
--- a/main.cpp Fri Nov 11 22:48:30 2022 +0000 +++ b/main.cpp Mon Nov 14 21:36:39 2022 +0000 @@ -1,15 +1,21 @@ #include "mbed.h" #include "uLCD_4DGL.h" +#include <vector> 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 { +struct pixelCoord { uint8_t x; uint8_t y; }; +struct boardPos { + uint8_t row; + uint8_t column; +}; + class BoardState { private: @@ -53,6 +59,252 @@ placePiece(movingPiece, endRow, endColumn); return capturedPiece; } + + // generates a list of possible moves for a piece + // returns moves + std::vector<boardPos> getMoves(int row, int column) + { + std::vector<boardPos> moves; + Piece movingPiece = getPiece(row, column); + bool isWhite; + switch(movingPiece) { + case wK: + case bK: + isWhite = movingPiece == wK; + for (int i = -1; i <= 1; i++) { + for (int j = -1; j <= 1; j++) { + if (validMove(isWhite, row + i, column + j)) { + moves.push_back((boardPos) { + row + i, column + j + }); + } + } + } + break; + case wQ: + case bQ: + isWhite = movingPiece == wQ; + for (int i = 1; ; i++) { + if (validMove(isWhite, row + i, column + i)) { + moves.push_back((boardPos) { + row + i, column + i + }); + } else { + break; + } + } + for (int i = 1; ; i++) { + if (validMove(isWhite, row - i, column + i)) { + moves.push_back((boardPos) { + row - i, column + i + }); + } else { + break; + } + } + for (int i = 1; ; i++) { + if (validMove(isWhite, row + i, column - i)) { + moves.push_back((boardPos) { + row + i, column - i + }); + } else { + break; + } + } + for (int i = 1; ; i++) { + if (validMove(isWhite, row - i, column - i)) { + moves.push_back((boardPos) { + row - i, column - i + }); + } else { + break; + } + } + for (int i = 1; ; i++) { + if (validMove(isWhite, row + i, column)) { + moves.push_back((boardPos) { + row + i, column + }); + } else { + break; + } + } + for (int i = 1; ; i++) { + if (validMove(isWhite, row, column + i)) { + moves.push_back((boardPos) { + row, column + i + }); + } else { + break; + } + } + for (int i = 1; ; i++) { + if (validMove(isWhite, row, column - i)) { + moves.push_back((boardPos) { + row, column - i + }); + } else { + break; + } + } + for (int i = 1; ; i++) { + if (validMove(isWhite, row - i, column)) { + moves.push_back((boardPos) { + row - i, column + }); + } else { + break; + } + } + break; + case wB: + case bB: + isWhite = movingPiece == wB; + for (int i = 1; ; i++) { + if (validMove(isWhite, row + i, column + i)) { + moves.push_back((boardPos) { + row + i, column + i + }); + } else { + break; + } + } + for (int i = 1; ; i++) { + if (validMove(isWhite, row - i, column + i)) { + moves.push_back((boardPos) { + row - i, column + i + }); + } else { + break; + } + } + for (int i = 1; ; i++) { + if (validMove(isWhite, row + i, column - i)) { + moves.push_back((boardPos) { + row + i, column - i + }); + } else { + break; + } + } + for (int i = 1; ; i++) { + if (validMove(isWhite, row - i, column - i)) { + moves.push_back((boardPos) { + row - i, column - i + }); + } else { + break; + } + } + break; + case wN: + case bN: + isWhite = movingPiece == wN; + for (int i = -1; i <= 1; i+=2) { + for (int j = -1; j <= 1; j+=2) { + if (validMove(isWhite, row + 2*i, column + j)) { + moves.push_back((boardPos) { + row + 2*i, column + j + }); + } + if (validMove(isWhite, row + i, column + 2*j)) { + moves.push_back((boardPos) { + row + i, column + 2*j + }); + } + } + } + break; + case wR: + case bR: + isWhite = movingPiece == wR; + for (int i = 1; ; i++) { + if (validMove(isWhite, row + i, column)) { + moves.push_back((boardPos) { + row + i, column + }); + } else { + break; + } + } + for (int i = 1; ; i++) { + if (validMove(isWhite, row, column + i)) { + moves.push_back((boardPos) { + row, column + i + }); + } else { + break; + } + } + for (int i = 1; ; i++) { + if (validMove(isWhite, row, column - i)) { + moves.push_back((boardPos) { + row, column - i + }); + } else { + break; + } + } + for (int i = 1; ; i++) { + if (validMove(isWhite, row - i, column)) { + moves.push_back((boardPos) { + row - i, column + }); + } else { + break; + } + } + break; + case w: + case b: + isWhite = movingPiece == w; + int rowChange = isWhite ? 1 : -1; + if (validMove(isWhite, row + rowChange, column)) { + moves.push_back((boardPos) { + row + rowChange, column + }); + } + if (validMove(isWhite, row + rowChange, column + 1) && getPiece(row + rowChange, column + 1) != e) { + moves.push_back((boardPos) { + row + rowChange, column + 1 + }); + } + if (validMove(isWhite, row + rowChange, column - 1) && getPiece(row + rowChange, column - 1) != e) { + moves.push_back((boardPos) { + row + rowChange, column - 1 + }); + } + break; + } + return moves; + } + + bool validMove(bool isMovingPieceWhite, int row, int column) + { + if (row < 0 || row > 7 || column < 0 || column > 7) { + return false; + } + Piece capturedPiece = getPiece(row, column); + switch(capturedPiece) { + case wK: + case wQ: + case wR: + case wB: + case wN: + case w: + return !isMovingPieceWhite; + case bK: + case bQ: + case bR: + case bB: + case bN: + case b: + return isMovingPieceWhite; + case e: + return true; + } + return false; + } }; class GameBoard @@ -63,6 +315,7 @@ static const int BOARD_LIGHT_COLOR = 0xbaca44; static const int HOVER_COLOR = 0x0000ff; static const int SELECTED_COLOR = 0xff8800; + static const int MOVE_COLOR = 0xff00ff; // piece sprites (12 x 12) static void spritePawn(bool white, bool light, int* spriteArray) @@ -208,7 +461,7 @@ } else { color = BOARD_LIGHT_COLOR; } - coord tl = getTopLeftOfSquare(row, column); + pixelCoord tl = getTopLeftOfSquare(row, column); uLCD.filled_rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, color); } } @@ -236,9 +489,9 @@ } // gets the pixel coordinates of the top left of the square - coord getTopLeftOfSquare(int row, int column) + pixelCoord getTopLeftOfSquare(int row, int column) { - coord topLeft; + pixelCoord topLeft; topLeft.x = 16 * column; topLeft.y = 112 - 16 * row; return topLeft; @@ -258,7 +511,7 @@ void placePieceAndDraw(Piece piece, int row, int column) { boardState.placePiece(piece, row, column); - coord tl = getTopLeftOfSquare(row, column); + pixelCoord tl = getTopLeftOfSquare(row, column); int sprite[144]; switch(piece) { case wK: @@ -295,7 +548,7 @@ Piece removePieceAndDraw(int row, int column) { Piece removedPiece = boardState.removePiece(row, column); - coord tl = getTopLeftOfSquare(row, column); + pixelCoord tl = getTopLeftOfSquare(row, column); uint64_t color; if ((row + column) % 2 == 0) { color = BOARD_DARK_COLOR; @@ -322,7 +575,7 @@ // removes selection border around square void unselectSquare(int row, int column) { - coord tl = getTopLeftOfSquare(row, column); + pixelCoord tl = getTopLeftOfSquare(row, column); uint64_t color; if ((row + column) % 2 == 0) { color = BOARD_DARK_COLOR; @@ -334,14 +587,14 @@ void hoverSquare(int row, int column) { - coord tl = getTopLeftOfSquare(row, column); + pixelCoord 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); + pixelCoord tl = getTopLeftOfSquare(row, column); uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, SELECTED_COLOR); } };