Mert Us Matthew Hannay Logan Starr
Dependencies: mbed 4DGL-uLCD-SE
Diff: main.cpp
- Revision:
- 9:1f33c0f299ae
- Parent:
- 8:928d5d33258f
- Child:
- 10:8f73a917b239
- Child:
- 11:43c89579ac52
--- a/main.cpp Thu Nov 17 18:18:21 2022 +0000 +++ b/main.cpp Fri Nov 18 19:06:03 2022 +0000 @@ -5,8 +5,10 @@ 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}; +std::vector<Piece> whitePieces; +std::vector<Piece> blackPieces; -enum GameState {whiteSelecting, whitePickedUp, blackSelecting, blackPickedUp}; +enum GameState {whiteSelecting, whitePickedUp, whiteAI, blackSelecting, blackPickedUp, blackAI}; struct pixelCoord { uint8_t x; @@ -16,6 +18,11 @@ struct boardPos { uint8_t row; uint8_t column; + + bool operator==(const boardPos &other) const + { + return row == other.row && column && other.column; + } }; class BoardState @@ -64,6 +71,10 @@ // generates a list of possible moves for a piece // returns moves + std::vector<boardPos> getMoves(boardPos pos) + { + return getMoves(pos.row, pos.column); + } std::vector<boardPos> getMoves(int row, int column) { std::vector<boardPos> moves; @@ -169,7 +180,7 @@ } return moves; } - + // returns a vector of board positions that a piece can move in a line std::vector<boardPos> movesInLine(bool isMovingPieceWhite, int row, int column, int rowChange, int columnChange) { @@ -181,11 +192,10 @@ row + i * rowChange, column + i * columnChange }); // if piece is capturable, stop moving beyond it - if (getPiece(row + i * rowChange, column + i * columnChange) != e) - { + if (getPiece(row + i * rowChange, column + i * columnChange) != e) { break; } - // if unable to move, break out + // if unable to move, break out } else { break; } @@ -545,13 +555,13 @@ pixelCoord tl = getTopLeftOfSquare(row, column); uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, SELECTED_COLOR); } - + // draws the movement border around square - void moveSquare(boardPos pos) + void movementSquare(boardPos pos) { - moveSquare(pos.row, pos.column); + movementSquare(pos.row, pos.column); } - void moveSquare(int row, int column) + void movementSquare(int row, int column) { pixelCoord tl = getTopLeftOfSquare(row, column); uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, MOVE_COLOR); @@ -566,73 +576,125 @@ 3, 4 }; boardPos selectedPos; +Piece selectedPiece; +std::vector<boardPos> possibleMoves; // callbacks +void moveCursor(int rowChange, int columnChange) +{ + // calculate new positoin that is within board bounds + int newRow = cursorPos.row + rowChange; + newRow = newRow <= 7 ? newRow : 7; + newRow = newRow >= 0 ? newRow : 0; + int newColumn = cursorPos.column + columnChange; + newColumn = newColumn <= 7 ? newColumn : 7; + newColumn = newColumn >= 0 ? newColumn : 0; + boardPos newPos = (boardPos) { + newRow, newColumn + }; + + // draw border around square that should be there after moving cursor off + if (cursorPos == selectedPos) { + gameBoard.selectSquare(cursorPos); + } else if (std::find(possibleMoves.begin(), possibleMoves.end(), cursorPos) != possibleMoves.end()) { + gameBoard.movementSquare(cursorPos); + } else { + gameBoard.unselectSquare(cursorPos); + } + + // draw hover rectangle over new square + cursorPos = newPos; + gameBoard.hoverSquare(cursorPos); +} + void joyStickUp() { - if (cursorPos.row < 7) { - boardPos nextPos; - nextPos.row = cursorPos.row + 1; - nextPos.column = cursorPos.column; - gameBoard.unselectSquare(cursorPos); - gameBoard.hoverSquare(nextPos); - cursorPos = nextPos; - } + moveCursor(1, 0); } void joyStickDown() { - if (cursorPos.row > 0) { - boardPos nextPos; - nextPos.row = cursorPos.row - 1; - nextPos.column = cursorPos.column; - gameBoard.unselectSquare(cursorPos); - gameBoard.hoverSquare(nextPos); - cursorPos = nextPos; - } + moveCursor(-1, 0); } void joyStickLeft() { - if (cursorPos.column > 0) { - boardPos nextPos; - nextPos.row = cursorPos.row; - nextPos.column = cursorPos.column - 1; - gameBoard.unselectSquare(cursorPos); - gameBoard.hoverSquare(nextPos); - cursorPos = nextPos; - } + moveCursor(0, -1); } void joyStickRight() { - if (cursorPos.column < 7) { - boardPos nextPos; - nextPos.row = cursorPos.row; - nextPos.column = cursorPos.column + 1; - gameBoard.unselectSquare(cursorPos); - gameBoard.hoverSquare(nextPos); - cursorPos = nextPos; - } + moveCursor(0, 1); } void joyStickPressed() { switch(state) { case whiteSelecting: + case blackSelecting: { selectedPos = cursorPos; - break; - case whitePickedUp: + Piece tempPiece = gameBoard.getPiece(cursorPos); + std::vector<Piece> pickablePieces = state == whiteSelecting ? whitePieces : blackPieces; + // check that piece is white and able to be picked up + if (std::find(pickablePieces.begin(), pickablePieces.end(), tempPiece) != pickablePieces.end()) { + selectedPiece = tempPiece; + possibleMoves = gameBoard.getBoardState().getMoves(cursorPos); + // draw movement squares + for (std::vector<boardPos>::iterator it = possibleMoves.begin(); it != possibleMoves.end(); ++it) { + gameBoard.movementSquare(*it); + } + gameBoard.selectSquare(selectedPos); + // transistion state + state = state == whiteSelecting ? whitePickedUp : blackPickedUp; + } break; - case blackSelecting: + } + case whitePickedUp: + case blackPickedUp: { + // check if move is valid + if (std::find(possibleMoves.begin(), possibleMoves.end(), cursorPos) != possibleMoves.end()) { + // move the piece + Piece capturedPiece = gameBoard.movePieceAndDraw(selectedPos, cursorPos); + // check for king capture + if (state == whitePickedUp && capturedPiece == bK || state == blackPickedUp && capturedPiece == wK) { + // game end + } + // transition state + state = state == whitePickedUp ? blackSelecting : whiteSelecting; + // check if placing piece back down + } else if (cursorPos == selectedPos) { + // transition state + state = state == whitePickedUp ? whiteSelecting : blackSelecting; + } + // unselect movement squares + for (std::vector<boardPos>::iterator it = possibleMoves.begin(); it != possibleMoves.end(); ++it) { + gameBoard.unselectSquare(*it); + } + gameBoard.unselectSquare(selectedPos); + gameBoard.hoverSquare(cursorPos); break; - case blackPickedUp: + } + case whiteAI: + case blackAI: { break; + } } } int main() { gameBoard = GameBoard(); + whitePieces.push_back(wK); + whitePieces.push_back(wQ); + whitePieces.push_back(wB); + whitePieces.push_back(wN); + whitePieces.push_back(wR); + whitePieces.push_back(w); + blackPieces.push_back(bK); + blackPieces.push_back(bQ); + blackPieces.push_back(bB); + blackPieces.push_back(bN); + blackPieces.push_back(bR); + blackPieces.push_back(b); return 0; } \ No newline at end of file