Mert Us Matthew Hannay Logan Starr
Dependencies: mbed 4DGL-uLCD-SE
Diff: main.cpp
- Revision:
- 12:7ef20deb9d5e
- Parent:
- 10:8f73a917b239
- Parent:
- 11:43c89579ac52
- Child:
- 13:3c6da0590428
diff -r 8f73a917b239 -r 7ef20deb9d5e main.cpp --- a/main.cpp Mon Nov 28 21:08:59 2022 +0000 +++ b/main.cpp Fri Dec 02 22:47:38 2022 +0000 @@ -21,10 +21,66 @@ bool operator==(const boardPos &other) const { - return row == other.row && column && other.column; + return row == other.row && column == other.column; } }; +class Nav_Switch +{ +public: + Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire); + int read(); +//boolean functions to test each switch + bool up(); + bool down(); + bool left(); + bool right(); + bool fire(); +//automatic read on RHS + operator int (); +//index to any switch array style + bool operator[](int index) { + return _pins[index]; + }; +private: + BusIn _pins; + +}; +Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire): + _pins(up, down, left, right, fire) +{ + _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise + wait(0.001); //delays just a bit for pullups to pull inputs high +} +inline bool Nav_Switch::up() +{ + return !(_pins[0]); +} +inline bool Nav_Switch::down() +{ + return !(_pins[1]); +} +inline bool Nav_Switch::left() +{ + return !(_pins[2]); +} +inline bool Nav_Switch::right() +{ + return !(_pins[3]); +} +inline bool Nav_Switch::fire() +{ + return !(_pins[4]); +} +inline int Nav_Switch::read() +{ + return _pins.read(); +} +inline Nav_Switch::operator int () +{ + return _pins.read(); +} + class BoardState { private: @@ -80,91 +136,308 @@ std::vector<boardPos> moves; std::vector<boardPos> lineMoves; Piece movingPiece = getPiece(row, column); + uint8_t rowIndex; + uint8_t columnIndex; 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 - }); - } - } + if (validMove(isWhite, row + 1, column)) { + moves.push_back((boardPos) { + row + 1, column + }); + } + if (validMove(isWhite, row, column + 1)) { + moves.push_back((boardPos) { + row, column + 1 + }); + } + if (validMove(isWhite, row - 1, column)) { + moves.push_back((boardPos) { + row - 1, column + }); + } + if (validMove(isWhite, row, column - 1)) { + moves.push_back((boardPos) { + row, column - 1 + }); + } + if (validMove(isWhite, row + 1, column + 1)) { + moves.push_back((boardPos) { + row + 1, column + 1 + }); + } + if (validMove(isWhite, row - 1, column + 1)) { + moves.push_back((boardPos) { + row - 1, column + 1 + }); + } + if (validMove(isWhite, row - 1, column - 1)) { + moves.push_back((boardPos) { + row - 1, column - 1 + }); + } + if (validMove(isWhite, row + 1, column - 1)) { + moves.push_back((boardPos) { + row + 1, column - 1 + }); } break; case wQ: case bQ: isWhite = movingPiece == wQ; - lineMoves = movesInLine(isWhite, row, column, 1, 0); - moves.insert(moves.end(), lineMoves.begin(), moves.end()); - lineMoves = movesInLine(isWhite, row, column, -1, 0); - moves.insert(moves.end(), lineMoves.begin(), moves.end()); - lineMoves = movesInLine(isWhite, row, column, 0, 1); - moves.insert(moves.end(), lineMoves.begin(), moves.end()); - lineMoves = movesInLine(isWhite, row, column, 0, -1); - moves.insert(moves.end(), lineMoves.begin(), moves.end()); - lineMoves = movesInLine(isWhite, row, column, 1, 1); - moves.insert(moves.end(), lineMoves.begin(), moves.end()); - lineMoves = movesInLine(isWhite, row, column, -1, 1); - moves.insert(moves.end(), lineMoves.begin(), moves.end()); - lineMoves = movesInLine(isWhite, row, column, 1, -1); - moves.insert(moves.end(), lineMoves.begin(), moves.end()); - lineMoves = movesInLine(isWhite, row, column, -1, -1); - moves.insert(moves.end(), lineMoves.begin(), moves.end()); + rowIndex = row + 1; + columnIndex = column + 1; + while (validMove(isWhite, rowIndex, columnIndex)) { + moves.push_back((boardPos) { + rowIndex, columnIndex + }); + if (getPiece(rowIndex, columnIndex) != e) { + break; + } + rowIndex++; + columnIndex++; + } + rowIndex = row - 1; + columnIndex = column + 1; + while (validMove(isWhite, rowIndex, columnIndex)) { + moves.push_back((boardPos) { + rowIndex, columnIndex + }); + if (getPiece(rowIndex, columnIndex) != e) { + break; + } + rowIndex--; + columnIndex++; + } + rowIndex = row + 1; + columnIndex = column - 1; + while (validMove(isWhite, rowIndex, columnIndex)) { + moves.push_back((boardPos) { + rowIndex, columnIndex + }); + if (getPiece(rowIndex, columnIndex) != e) { + break; + } + rowIndex++; + columnIndex--; + } + rowIndex = row - 1; + columnIndex = column - 1; + while (validMove(isWhite, rowIndex, columnIndex)) { + moves.push_back((boardPos) { + rowIndex, columnIndex + }); + if (getPiece(rowIndex, columnIndex) != e) { + break; + } + rowIndex--; + columnIndex--; + } + rowIndex = row + 1; + columnIndex = column; + while (validMove(isWhite, rowIndex, columnIndex)) { + moves.push_back((boardPos) { + rowIndex, columnIndex + }); + if (getPiece(rowIndex, columnIndex) != e) { + break; + } + rowIndex++; + } + rowIndex = row - 1; + columnIndex = column; + while (validMove(isWhite, rowIndex, columnIndex)) { + moves.push_back((boardPos) { + rowIndex, columnIndex + }); + if (getPiece(rowIndex, columnIndex) != e) { + break; + } + rowIndex--; + } + rowIndex = row; + columnIndex = column + 1; + while (validMove(isWhite, rowIndex, columnIndex)) { + moves.push_back((boardPos) { + rowIndex, columnIndex + }); + if (getPiece(rowIndex, columnIndex) != e) { + break; + } + columnIndex++; + } + rowIndex = row; + columnIndex = column - 1; + while (validMove(isWhite, rowIndex, columnIndex)) { + moves.push_back((boardPos) { + rowIndex, columnIndex + }); + if (getPiece(rowIndex, columnIndex) != e) { + break; + } + columnIndex--; + } break; case wB: case bB: isWhite = movingPiece == wB; - lineMoves = movesInLine(isWhite, row, column, 1, 1); - moves.insert(moves.end(), lineMoves.begin(), moves.end()); - lineMoves = movesInLine(isWhite, row, column, -1, 1); - moves.insert(moves.end(), lineMoves.begin(), moves.end()); - lineMoves = movesInLine(isWhite, row, column, 1, -1); - moves.insert(moves.end(), lineMoves.begin(), moves.end()); - lineMoves = movesInLine(isWhite, row, column, -1, -1); - moves.insert(moves.end(), lineMoves.begin(), moves.end()); + rowIndex = row + 1; + columnIndex = column + 1; + while (validMove(isWhite, rowIndex, columnIndex)) { + moves.push_back((boardPos) { + rowIndex, columnIndex + }); + if (getPiece(rowIndex, columnIndex) != e) { + break; + } + rowIndex++; + columnIndex++; + } + rowIndex = row - 1; + columnIndex = column + 1; + while (validMove(isWhite, rowIndex, columnIndex)) { + moves.push_back((boardPos) { + rowIndex, columnIndex + }); + if (getPiece(rowIndex, columnIndex) != e) { + break; + } + rowIndex--; + columnIndex++; + } + rowIndex = row + 1; + columnIndex = column - 1; + while (validMove(isWhite, rowIndex, columnIndex)) { + moves.push_back((boardPos) { + rowIndex, columnIndex + }); + if (getPiece(rowIndex, columnIndex) != e) { + break; + } + rowIndex++; + columnIndex--; + } + rowIndex = row - 1; + columnIndex = column - 1; + while (validMove(isWhite, rowIndex, columnIndex)) { + moves.push_back((boardPos) { + rowIndex, columnIndex + }); + if (getPiece(rowIndex, columnIndex) != e) { + break; + } + rowIndex--; + columnIndex--; + } 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 - }); - } - } + if (validMove(isWhite, row + 2, column + 1)) { + moves.push_back((boardPos) { + row + 2, column + 1 + }); + } + if (validMove(isWhite, row + 2, column - 1)) { + moves.push_back((boardPos) { + row + 2, column - 1 + }); + } + if (validMove(isWhite, row - 2, column - 1)) { + moves.push_back((boardPos) { + row - 2, column - 1 + }); + } + if (validMove(isWhite, row - 2, column + 1)) { + moves.push_back((boardPos) { + row - 2, column + 1 + }); + } + if (validMove(isWhite, row + 1, column + 2)) { + moves.push_back((boardPos) { + row + 1, column + 2 + }); + } + if (validMove(isWhite, row - 1, column + 2)) { + moves.push_back((boardPos) { + row - 1, column + 2 + }); + } + if (validMove(isWhite, row - 1, column - 2)) { + moves.push_back((boardPos) { + row - 1, column - 2 + }); + } + if (validMove(isWhite, row + 1, column - 2)) { + moves.push_back((boardPos) { + row + 1, column - 2 + }); } break; case wR: case bR: isWhite = movingPiece == wR; - lineMoves = movesInLine(isWhite, row, column, 1, 0); - moves.insert(moves.end(), lineMoves.begin(), moves.end()); - lineMoves = movesInLine(isWhite, row, column, -1, 0); - moves.insert(moves.end(), lineMoves.begin(), moves.end()); - lineMoves = movesInLine(isWhite, row, column, 0, 1); - moves.insert(moves.end(), lineMoves.begin(), moves.end()); - lineMoves = movesInLine(isWhite, row, column, 0, -1); - moves.insert(moves.end(), lineMoves.begin(), moves.end()); + rowIndex = row + 1; + columnIndex = column; + while (validMove(isWhite, rowIndex, columnIndex)) { + moves.push_back((boardPos) { + rowIndex, columnIndex + }); + if (getPiece(rowIndex, columnIndex) != e) { + break; + } + rowIndex++; + } + rowIndex = row - 1; + columnIndex = column; + while (validMove(isWhite, rowIndex, columnIndex)) { + moves.push_back((boardPos) { + rowIndex, columnIndex + }); + if (getPiece(rowIndex, columnIndex) != e) { + break; + } + rowIndex--; + } + rowIndex = row; + columnIndex = column + 1; + while (validMove(isWhite, rowIndex, columnIndex)) { + moves.push_back((boardPos) { + rowIndex, columnIndex + }); + if (getPiece(rowIndex, columnIndex) != e) { + break; + } + columnIndex++; + } + rowIndex = row; + columnIndex = column - 1; + while (validMove(isWhite, rowIndex, columnIndex)) { + moves.push_back((boardPos) { + rowIndex, columnIndex + }); + if (getPiece(rowIndex, columnIndex) != e) { + break; + } + columnIndex--; + } break; case w: case b: isWhite = movingPiece == w; int rowChange = isWhite ? 1 : -1; - if (validMove(isWhite, row + rowChange, column)) { + if (validMove(isWhite, row + rowChange, column) && getPiece(row + rowChange, column) == e) { moves.push_back((boardPos) { row + rowChange, column }); + // The case for pawns moving two squares at the start + if (((isWhite && row == 1) || (!isWhite && row == 6)) && validMove(isWhite, row + rowChange + rowChange, column) && getPiece(row + rowChange + rowChange, column) == e) { + moves.push_back((boardPos) { + row + rowChange + rowChange, column + }); + } } if (validMove(isWhite, row + rowChange, column + 1) && getPiece(row + rowChange, column + 1) != e) { moves.push_back((boardPos) { @@ -177,6 +450,8 @@ }); } break; + default: + break; } return moves; } @@ -242,6 +517,7 @@ static const int SELECTED_COLOR = 0xff8800; static const int MOVE_COLOR = 0xff00ff; +<<<<<<< working copy // gets the pixel coordinates of the top left of the square static pixelCoord getTopLeftOfSquare(boardPos pos) { @@ -296,8 +572,22 @@ }; pixelCoord tl = getTopLeftOfSquare(row, column); uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); +======= + // gets the pixel coordinates of the top left of the square + static pixelCoord getTopLeftOfSquare(boardPos pos) + { + return getTopLeftOfSquare(pos.row, pos.column); + } + static pixelCoord getTopLeftOfSquare(int row, int column) + { + pixelCoord topLeft; + topLeft.x = 16 * column; + topLeft.y = 112 - 16 * row; + return topLeft; +>>>>>>> merge rev } +<<<<<<< working copy static void drawKnight(int row, int column, bool white, bool light) { int X = white ? 0xffffff : 0x000000; @@ -380,6 +670,128 @@ }; pixelCoord tl = getTopLeftOfSquare(row, column); uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); +======= + // piece sprites (12 x 12) + static void drawPawn(int row, int column, bool white, bool light) + { + int X = white ? 0xffffff : 0x000000; + int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; + int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, X, X, _, _, _, _, _, + _, _, _, _, X, X, X, X, _, _, _, _, + _, _, _, _, X, X, X, X, _, _, _, _, + _, _, _, _, _, X, X, _, _, _, _, _, + _, _, _, _, _, X, X, _, _, _, _, _, + _, _, _, _, X, X, X, X, _, _, _, _, + _, _, X, X, X, X, X, X, X, X, _, _, + _, _, X, X, X, X, X, X, X, X, _, _ + }; + pixelCoord tl = getTopLeftOfSquare(row, column); + uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); + } + static void drawRook(int row, int column, bool white, bool light) + { + int X = white ? 0xffffff : 0x000000; + int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; + int sprite[144] = {X, X, _, X, X, _, _, X, X, _, X, X, + X, X, _, X, X, _, _, X, X, _, X, X, + X, X, X, X, X, X, X, X, X, X, X, X, + X, X, X, X, X, X, X, X, X, X, X, X, + _, X, X, X, _, X, X, _, X, X, X, _, + _, X, X, X, _, X, X, _, X, X, X, _, + _, _, X, X, _, X, X, _, X, X, _, _, + _, _, X, X, _, X, X, _, X, X, _, _, + _, _, X, X, _, X, X, _, X, X, _, _, + _, X, X, X, X, X, X, X, X, X, X, _, + X, X, X, X, X, X, X, X, X, X, X, X, + X, X, X, X, X, X, X, X, X, X, X, X + }; + pixelCoord tl = getTopLeftOfSquare(row, column); + uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); + } + static void drawKnight(int row, int column, bool white, bool light) + { + int X = white ? 0xffffff : 0x000000; + int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; + int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, X, X, _, X, X, _, _, + _, _, _, _, _, X, X, _, X, X, _, _, + _, _, _, X, X, X, X, X, X, _, _, _, + _, _, X, X, X, X, X, _, X, _, _, _, + _, _, X, X, X, X, X, X, X, _, _, _, + _, _, _, _, _, X, X, X, X, _, _, _, + _, _, _, _, X, X, X, X, X, _, _, _, + _, _, _, X, X, X, X, X, X, X, _, _, + _, _, X, X, X, X, X, X, X, X, _, _, + _, X, X, X, X, X, X, X, X, X, X, _, + _, X, X, X, X, X, X, X, X, X, X, _ + }; + pixelCoord tl = getTopLeftOfSquare(row, column); + uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); + } + static void drawBishop(int row, int column, bool white, bool light) + { + int X = white ? 0xffffff : 0x000000; + int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; + int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _, + _, _, _, _, X, X, X, _, _, _, _, _, + _, _, _, X, X, X, _, _, X, _, _, _, + _, _, _, X, X, _, _, X, X, _, _, _, + _, _, _, X, X, X, X, X, X, _, _, _, + _, _, _, _, X, X, X, X, _, _, _, _, + _, _, _, _, _, X, X, _, _, _, _, _, + _, _, _, _, X, X, X, X, _, _, _, _, + _, _, _, X, X, X, X, X, X, _, _, _, + _, _, _, X, X, X, X, X, X, _, _, _, + _, _, X, X, X, X, X, X, X, X, _, _, + _, _, X, X, X, X, X, X, X, X, _, _ + }; + pixelCoord tl = getTopLeftOfSquare(row, column); + uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); + } + static void drawQueen(int row, int column, bool white, bool light) + { + int X = white ? 0xffffff : 0x000000; + int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; + int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _, + _, _, X, _, _, X, X, _, _, X, _, _, + X, _, X, X, _, X, X, _, X, X, _, X, + X, _, X, X, _, X, X, _, X, X, _, X, + X, _, X, X, _, X, X, _, X, X, _, X, + X, X, X, X, X, X, X, X, X, X, X, X, + X, X, X, X, X, X, X, X, X, X, X, X, + X, X, _, X, X, X, X, X, X, _, X, X, + X, X, X, X, _, X, X, _, X, X, X, X, + _, X, X, X, X, X, X, X, X, X, X, _, + _, _, X, X, X, X, X, X, X, X, _, _, + _, X, X, X, X, X, X, X, X, X, X, _ + }; + pixelCoord tl = getTopLeftOfSquare(row, column); + uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); + } + static void drawKing(int row, int column, bool white, bool light) + { + int X = white ? 0xffffff : 0x000000; + int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR; + int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _, + _, _, _, _, _, X, X, _, _, _, _, _, + _, _, _, X, X, X, X, X, X, _, _, _, + _, _, _, X, X, X, X, X, X, _, _, _, + X, X, _, _, _, X, X, _, _, _, X, X, + X, X, X, X, _, X, X, _, X, X, X, X, + X, _, X, X, X, X, X, X, X, X, _, X, + X, X, X, X, X, X, X, X, X, X, X, X, + X, X, X, _, X, X, X, X, _, X, X, X, + _, X, X, X, X, X, X, X, X, X, X, _, + _, _, X, X, X, _, _, X, X, X, _, _, + _, X, X, X, X, X, X, X, X, X, X, _ + }; + pixelCoord tl = getTopLeftOfSquare(row, column); + uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); +>>>>>>> merge rev } public: @@ -455,6 +867,7 @@ { boardState.placePiece(piece, row, column); pixelCoord tl = getTopLeftOfSquare(row, column); +<<<<<<< working copy switch(piece) { case wK: case bK: @@ -480,6 +893,33 @@ case b: drawPawn(row, column, piece==w, (row+column)%2); break; +======= + switch(piece) { + case wK: + case bK: + drawKing(row, column, piece==wK, (row+column)%2); + break; + case wQ: + case bQ: + drawQueen(row, column, piece==wQ, (row+column)%2); + break; + case wB: + case bB: + drawBishop(row, column, piece==wB, (row+column)%2); + break; + case wN: + case bN: + drawKnight(row, column, piece==wN, (row+column)%2); + break; + case wR: + case bR: + drawRook(row, column, piece==wR, (row+column)%2); + break; + case w: + case b: + drawPawn(row, column, piece==w, (row+column)%2); + break; +>>>>>>> merge rev } } @@ -528,6 +968,9 @@ } void unselectSquare(int row, int column) { + if (row < 0 || row > 7 || column < 0 || column > 7) { + return; + } pixelCoord tl = getTopLeftOfSquare(row, column); uint64_t color; if ((row + column) % 2 == 0) { @@ -556,6 +999,9 @@ } void selectSquare(int row, int column) { + if (row < 0 || row > 7 || column < 0 || column > 7) { + return; + } pixelCoord tl = getTopLeftOfSquare(row, column); uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, SELECTED_COLOR); } @@ -636,6 +1082,7 @@ switch(state) { case whiteSelecting: case blackSelecting: { + possibleMoves.clear(); selectedPos = cursorPos; Piece tempPiece = gameBoard.getPiece(cursorPos); std::vector<Piece> pickablePieces = state == whiteSelecting ? whitePieces : blackPieces; @@ -648,8 +1095,13 @@ gameBoard.movementSquare(*it); } gameBoard.selectSquare(selectedPos); - // transistion state + // transition state state = state == whiteSelecting ? whitePickedUp : blackPickedUp; + } else { + selectedPos = (boardPos) + { + 10, 10 + }; } break; } @@ -666,7 +1118,7 @@ // transition state state = state == whitePickedUp ? blackSelecting : whiteSelecting; // check if placing piece back down - } else if (cursorPos == selectedPos) { + } else { // transition state state = state == whitePickedUp ? whiteSelecting : blackSelecting; } @@ -676,6 +1128,11 @@ } gameBoard.unselectSquare(selectedPos); gameBoard.hoverSquare(cursorPos); + possibleMoves.clear(); + selectedPos = (boardPos) + { + 10, 10 + }; break; } case whiteAI: @@ -685,6 +1142,8 @@ } } +Nav_Switch myNav(p9, p6, p7, p5, p8); //pin order on Sparkfun breakout + int main() { //gameBoard = GameBoard(); @@ -700,5 +1159,19 @@ blackPieces.push_back(bN); blackPieces.push_back(bR); blackPieces.push_back(b); - return 0; + moveCursor(0, 0); + while (1) { + if (myNav.up()) { + joyStickUp(); + } else if (myNav.down()) { + joyStickDown(); + } else if (myNav.left()) { + joyStickLeft(); + } else if (myNav.right()) { + joyStickRight(); + } else if (myNav.fire()) { + joyStickPressed(); + } + wait(0.25); + } } \ No newline at end of file