Mert Us Matthew Hannay Logan Starr
Dependencies: mbed 4DGL-uLCD-SE
Diff: main.cpp
- Revision:
- 11:43c89579ac52
- Parent:
- 9:1f33c0f299ae
- Child:
- 12:7ef20deb9d5e
- Child:
- 14:f390d08e5f92
diff -r 1f33c0f299ae -r 43c89579ac52 main.cpp --- a/main.cpp Fri Nov 18 19:06:03 2022 +0000 +++ b/main.cpp Thu Dec 01 05:53:57 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; } @@ -241,126 +516,140 @@ 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) - { - 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, _, _ - }; - memcpy(sprite, spriteArray, sizeof(sprite)); - } - - static void spriteRook(bool white, bool light, int* spriteArray) - { - 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 - }; - memcpy(sprite, spriteArray, sizeof(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; } - static void spriteKnight(bool white, bool light, int* spriteArray) - { - 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, _ - }; - memcpy(sprite, spriteArray, sizeof(sprite)); - } - - static void spriteBishop(bool white, bool light, int* spriteArray) - { - 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, _, _ - }; - memcpy(sprite, spriteArray, sizeof(sprite)); - } - - static void spriteQueen(bool white, bool light, int* spriteArray) - { - 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, _ - }; - memcpy(sprite, spriteArray, sizeof(sprite)); - } - - static void spriteKing(bool white, bool light, int* spriteArray) - { - 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, _ - }; - memcpy(sprite, spriteArray, sizeof(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); } public: @@ -413,19 +702,6 @@ } } - // gets the pixel coordinates of the top left of the square - pixelCoord getTopLeftOfSquare(boardPos pos) - { - return getTopLeftOfSquare(pos.row, pos.column); - } - pixelCoord getTopLeftOfSquare(int row, int column) - { - pixelCoord topLeft; - topLeft.x = 16 * column; - topLeft.y = 112 - 16 * row; - return topLeft; - } - // PIECE MOVEMENT AND GRAPHICS FUNCTIONS // returns the piece at a given location @@ -449,34 +725,32 @@ { boardState.placePiece(piece, row, column); pixelCoord tl = getTopLeftOfSquare(row, column); - int sprite[144]; - switch(piece) { - case wK: - case bK: - spriteKing(piece==wK, (row+column)%2, sprite); - break; - case wQ: - case bQ: - spriteQueen(piece==wQ, (row+column)%2, sprite); - break; - case wB: - case bB: - spriteBishop(piece==wB, (row+column)%2, sprite); - break; - case wN: - case bN: - spriteKing(piece==wN, (row+column)%2, sprite); - break; - case wR: - case bR: - spriteKing(piece==wR, (row+column)%2, sprite); - break; - case w: - case b: - spriteKing(piece==w, (row+column)%2, sprite); - 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; } - uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite); } /* removes a piece from the set position of the board @@ -524,6 +798,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) { @@ -552,6 +829,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); } @@ -632,6 +912,7 @@ switch(state) { case whiteSelecting: case blackSelecting: { + possibleMoves.clear(); selectedPos = cursorPos; Piece tempPiece = gameBoard.getPiece(cursorPos); std::vector<Piece> pickablePieces = state == whiteSelecting ? whitePieces : blackPieces; @@ -644,8 +925,13 @@ gameBoard.movementSquare(*it); } gameBoard.selectSquare(selectedPos); - // transistion state + // transition state state = state == whiteSelecting ? whitePickedUp : blackPickedUp; + } else { + selectedPos = (boardPos) + { + 10, 10 + }; } break; } @@ -662,7 +948,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; } @@ -672,6 +958,11 @@ } gameBoard.unselectSquare(selectedPos); gameBoard.hoverSquare(cursorPos); + possibleMoves.clear(); + selectedPos = (boardPos) + { + 10, 10 + }; break; } case whiteAI: @@ -681,9 +972,11 @@ } } +Nav_Switch myNav(p9, p6, p7, p5, p8); //pin order on Sparkfun breakout + int main() { - gameBoard = GameBoard(); + //gameBoard = GameBoard(); whitePieces.push_back(wK); whitePieces.push_back(wQ); whitePieces.push_back(wB); @@ -696,5 +989,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