Mert Us Matthew Hannay Logan Starr

Dependencies:   mbed 4DGL-uLCD-SE

Committer:
mhannay3
Date:
Mon Nov 28 21:08:59 2022 +0000
Revision:
10:8f73a917b239
Parent:
9:1f33c0f299ae
Child:
12:7ef20deb9d5e
fix graphics and double drawing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mhannay3 5:b553c51b3b85 1 #include "mbed.h"
mhannay3 5:b553c51b3b85 2 #include "uLCD_4DGL.h"
mhannay3 6:1c4dd5e24b8d 3 #include <vector>
mhannay3 1:cd78922f70fa 4
mhannay3 5:b553c51b3b85 5 uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin;
mhannay3 5:b553c51b3b85 6
mhannay3 5:b553c51b3b85 7 enum Piece {e, wK, bK, wQ, bQ, wR, bR, wB, bB, wN, bN, w, b};
mhannay3 9:1f33c0f299ae 8 std::vector<Piece> whitePieces;
mhannay3 9:1f33c0f299ae 9 std::vector<Piece> blackPieces;
mhannay3 1:cd78922f70fa 10
mhannay3 9:1f33c0f299ae 11 enum GameState {whiteSelecting, whitePickedUp, whiteAI, blackSelecting, blackPickedUp, blackAI};
mhannay3 7:9e01c9a334dc 12
mhannay3 6:1c4dd5e24b8d 13 struct pixelCoord {
mhannay3 1:cd78922f70fa 14 uint8_t x;
mhannay3 1:cd78922f70fa 15 uint8_t y;
mhannay3 5:b553c51b3b85 16 };
mhannay3 1:cd78922f70fa 17
mhannay3 6:1c4dd5e24b8d 18 struct boardPos {
mhannay3 6:1c4dd5e24b8d 19 uint8_t row;
mhannay3 6:1c4dd5e24b8d 20 uint8_t column;
mhannay3 9:1f33c0f299ae 21
mhannay3 9:1f33c0f299ae 22 bool operator==(const boardPos &other) const
mhannay3 9:1f33c0f299ae 23 {
mhannay3 9:1f33c0f299ae 24 return row == other.row && column && other.column;
mhannay3 9:1f33c0f299ae 25 }
mhannay3 6:1c4dd5e24b8d 26 };
mhannay3 6:1c4dd5e24b8d 27
mhannay3 5:b553c51b3b85 28 class BoardState
mhannay3 5:b553c51b3b85 29 {
mhannay3 5:b553c51b3b85 30 private:
mhannay3 5:b553c51b3b85 31 Piece array[64];
mhannay3 5:b553c51b3b85 32 public:
mhannay3 4:5798e4062350 33 // calculates the advantage difference for the board state
mhannay3 5:b553c51b3b85 34 float calculateBoardState()
mhannay3 5:b553c51b3b85 35 {
mhannay3 4:5798e4062350 36 return 0.0;
mhannay3 4:5798e4062350 37 }
mhannay3 5:b553c51b3b85 38
mhannay3 4:5798e4062350 39 // returns the piece at a given location
mhannay3 5:b553c51b3b85 40 Piece getPiece(int row, int column)
mhannay3 5:b553c51b3b85 41 {
mhannay3 5:b553c51b3b85 42 return array[column + 8 * row];
mhannay3 4:5798e4062350 43 }
mhannay3 4:5798e4062350 44
mhannay3 5:b553c51b3b85 45 // puts a piece at a given location
mhannay3 5:b553c51b3b85 46 void placePiece(Piece piece, int row, int column)
mhannay3 5:b553c51b3b85 47 {
mhannay3 5:b553c51b3b85 48 array[column + 8 * row] = piece;
mhannay3 4:5798e4062350 49 }
mhannay3 4:5798e4062350 50
mhannay3 4:5798e4062350 51 /* removes a piece from the set position of the board
mhannay3 4:5798e4062350 52 returns the bit representation of the piece
mhannay3 4:5798e4062350 53 */
mhannay3 5:b553c51b3b85 54 Piece removePiece(int row, int column)
mhannay3 5:b553c51b3b85 55 {
mhannay3 5:b553c51b3b85 56 Piece removedPiece = array[column + 8 * row];
mhannay3 5:b553c51b3b85 57 array[column + 8 * row] = e;
mhannay3 4:5798e4062350 58 return removedPiece;
mhannay3 1:cd78922f70fa 59 }
mhannay3 1:cd78922f70fa 60
mhannay3 1:cd78922f70fa 61 /* moves a piece from one position to another
mhannay3 1:cd78922f70fa 62 returns the captured piece
mhannay3 1:cd78922f70fa 63 */
mhannay3 5:b553c51b3b85 64 Piece movePiece(int startRow, int startColumn, int endRow, int endColumn)
mhannay3 5:b553c51b3b85 65 {
mhannay3 5:b553c51b3b85 66 Piece movingPiece = removePiece(startRow, startColumn);
mhannay3 5:b553c51b3b85 67 Piece capturedPiece = removePiece(endRow, endColumn);
mhannay3 5:b553c51b3b85 68 placePiece(movingPiece, endRow, endColumn);
mhannay3 1:cd78922f70fa 69 return capturedPiece;
mhannay3 1:cd78922f70fa 70 }
mhannay3 6:1c4dd5e24b8d 71
mhannay3 6:1c4dd5e24b8d 72 // generates a list of possible moves for a piece
mhannay3 6:1c4dd5e24b8d 73 // returns moves
mhannay3 9:1f33c0f299ae 74 std::vector<boardPos> getMoves(boardPos pos)
mhannay3 9:1f33c0f299ae 75 {
mhannay3 9:1f33c0f299ae 76 return getMoves(pos.row, pos.column);
mhannay3 9:1f33c0f299ae 77 }
mhannay3 6:1c4dd5e24b8d 78 std::vector<boardPos> getMoves(int row, int column)
mhannay3 6:1c4dd5e24b8d 79 {
mhannay3 6:1c4dd5e24b8d 80 std::vector<boardPos> moves;
mhannay3 8:928d5d33258f 81 std::vector<boardPos> lineMoves;
mhannay3 6:1c4dd5e24b8d 82 Piece movingPiece = getPiece(row, column);
mhannay3 6:1c4dd5e24b8d 83 bool isWhite;
mhannay3 6:1c4dd5e24b8d 84 switch(movingPiece) {
mhannay3 6:1c4dd5e24b8d 85 case wK:
mhannay3 6:1c4dd5e24b8d 86 case bK:
mhannay3 6:1c4dd5e24b8d 87 isWhite = movingPiece == wK;
mhannay3 6:1c4dd5e24b8d 88 for (int i = -1; i <= 1; i++) {
mhannay3 6:1c4dd5e24b8d 89 for (int j = -1; j <= 1; j++) {
mhannay3 6:1c4dd5e24b8d 90 if (validMove(isWhite, row + i, column + j)) {
mhannay3 6:1c4dd5e24b8d 91 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 92 row + i, column + j
mhannay3 6:1c4dd5e24b8d 93 });
mhannay3 6:1c4dd5e24b8d 94 }
mhannay3 6:1c4dd5e24b8d 95 }
mhannay3 6:1c4dd5e24b8d 96 }
mhannay3 6:1c4dd5e24b8d 97 break;
mhannay3 6:1c4dd5e24b8d 98 case wQ:
mhannay3 6:1c4dd5e24b8d 99 case bQ:
mhannay3 6:1c4dd5e24b8d 100 isWhite = movingPiece == wQ;
mhannay3 8:928d5d33258f 101 lineMoves = movesInLine(isWhite, row, column, 1, 0);
mhannay3 8:928d5d33258f 102 moves.insert(moves.end(), lineMoves.begin(), moves.end());
mhannay3 8:928d5d33258f 103 lineMoves = movesInLine(isWhite, row, column, -1, 0);
mhannay3 8:928d5d33258f 104 moves.insert(moves.end(), lineMoves.begin(), moves.end());
mhannay3 8:928d5d33258f 105 lineMoves = movesInLine(isWhite, row, column, 0, 1);
mhannay3 8:928d5d33258f 106 moves.insert(moves.end(), lineMoves.begin(), moves.end());
mhannay3 8:928d5d33258f 107 lineMoves = movesInLine(isWhite, row, column, 0, -1);
mhannay3 8:928d5d33258f 108 moves.insert(moves.end(), lineMoves.begin(), moves.end());
mhannay3 8:928d5d33258f 109 lineMoves = movesInLine(isWhite, row, column, 1, 1);
mhannay3 8:928d5d33258f 110 moves.insert(moves.end(), lineMoves.begin(), moves.end());
mhannay3 8:928d5d33258f 111 lineMoves = movesInLine(isWhite, row, column, -1, 1);
mhannay3 8:928d5d33258f 112 moves.insert(moves.end(), lineMoves.begin(), moves.end());
mhannay3 8:928d5d33258f 113 lineMoves = movesInLine(isWhite, row, column, 1, -1);
mhannay3 8:928d5d33258f 114 moves.insert(moves.end(), lineMoves.begin(), moves.end());
mhannay3 8:928d5d33258f 115 lineMoves = movesInLine(isWhite, row, column, -1, -1);
mhannay3 8:928d5d33258f 116 moves.insert(moves.end(), lineMoves.begin(), moves.end());
mhannay3 6:1c4dd5e24b8d 117 break;
mhannay3 6:1c4dd5e24b8d 118 case wB:
mhannay3 6:1c4dd5e24b8d 119 case bB:
mhannay3 6:1c4dd5e24b8d 120 isWhite = movingPiece == wB;
mhannay3 8:928d5d33258f 121 lineMoves = movesInLine(isWhite, row, column, 1, 1);
mhannay3 8:928d5d33258f 122 moves.insert(moves.end(), lineMoves.begin(), moves.end());
mhannay3 8:928d5d33258f 123 lineMoves = movesInLine(isWhite, row, column, -1, 1);
mhannay3 8:928d5d33258f 124 moves.insert(moves.end(), lineMoves.begin(), moves.end());
mhannay3 8:928d5d33258f 125 lineMoves = movesInLine(isWhite, row, column, 1, -1);
mhannay3 8:928d5d33258f 126 moves.insert(moves.end(), lineMoves.begin(), moves.end());
mhannay3 8:928d5d33258f 127 lineMoves = movesInLine(isWhite, row, column, -1, -1);
mhannay3 8:928d5d33258f 128 moves.insert(moves.end(), lineMoves.begin(), moves.end());
mhannay3 6:1c4dd5e24b8d 129 break;
mhannay3 6:1c4dd5e24b8d 130 case wN:
mhannay3 6:1c4dd5e24b8d 131 case bN:
mhannay3 6:1c4dd5e24b8d 132 isWhite = movingPiece == wN;
mhannay3 6:1c4dd5e24b8d 133 for (int i = -1; i <= 1; i+=2) {
mhannay3 6:1c4dd5e24b8d 134 for (int j = -1; j <= 1; j+=2) {
mhannay3 6:1c4dd5e24b8d 135 if (validMove(isWhite, row + 2*i, column + j)) {
mhannay3 6:1c4dd5e24b8d 136 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 137 row + 2*i, column + j
mhannay3 6:1c4dd5e24b8d 138 });
mhannay3 6:1c4dd5e24b8d 139 }
mhannay3 6:1c4dd5e24b8d 140 if (validMove(isWhite, row + i, column + 2*j)) {
mhannay3 6:1c4dd5e24b8d 141 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 142 row + i, column + 2*j
mhannay3 6:1c4dd5e24b8d 143 });
mhannay3 6:1c4dd5e24b8d 144 }
mhannay3 6:1c4dd5e24b8d 145 }
mhannay3 6:1c4dd5e24b8d 146 }
mhannay3 6:1c4dd5e24b8d 147 break;
mhannay3 6:1c4dd5e24b8d 148 case wR:
mhannay3 6:1c4dd5e24b8d 149 case bR:
mhannay3 6:1c4dd5e24b8d 150 isWhite = movingPiece == wR;
mhannay3 8:928d5d33258f 151 lineMoves = movesInLine(isWhite, row, column, 1, 0);
mhannay3 8:928d5d33258f 152 moves.insert(moves.end(), lineMoves.begin(), moves.end());
mhannay3 8:928d5d33258f 153 lineMoves = movesInLine(isWhite, row, column, -1, 0);
mhannay3 8:928d5d33258f 154 moves.insert(moves.end(), lineMoves.begin(), moves.end());
mhannay3 8:928d5d33258f 155 lineMoves = movesInLine(isWhite, row, column, 0, 1);
mhannay3 8:928d5d33258f 156 moves.insert(moves.end(), lineMoves.begin(), moves.end());
mhannay3 8:928d5d33258f 157 lineMoves = movesInLine(isWhite, row, column, 0, -1);
mhannay3 8:928d5d33258f 158 moves.insert(moves.end(), lineMoves.begin(), moves.end());
mhannay3 6:1c4dd5e24b8d 159 break;
mhannay3 6:1c4dd5e24b8d 160 case w:
mhannay3 6:1c4dd5e24b8d 161 case b:
mhannay3 6:1c4dd5e24b8d 162 isWhite = movingPiece == w;
mhannay3 6:1c4dd5e24b8d 163 int rowChange = isWhite ? 1 : -1;
mhannay3 6:1c4dd5e24b8d 164 if (validMove(isWhite, row + rowChange, column)) {
mhannay3 6:1c4dd5e24b8d 165 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 166 row + rowChange, column
mhannay3 6:1c4dd5e24b8d 167 });
mhannay3 6:1c4dd5e24b8d 168 }
mhannay3 6:1c4dd5e24b8d 169 if (validMove(isWhite, row + rowChange, column + 1) && getPiece(row + rowChange, column + 1) != e) {
mhannay3 6:1c4dd5e24b8d 170 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 171 row + rowChange, column + 1
mhannay3 6:1c4dd5e24b8d 172 });
mhannay3 6:1c4dd5e24b8d 173 }
mhannay3 6:1c4dd5e24b8d 174 if (validMove(isWhite, row + rowChange, column - 1) && getPiece(row + rowChange, column - 1) != e) {
mhannay3 6:1c4dd5e24b8d 175 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 176 row + rowChange, column - 1
mhannay3 6:1c4dd5e24b8d 177 });
mhannay3 6:1c4dd5e24b8d 178 }
mhannay3 6:1c4dd5e24b8d 179 break;
mhannay3 6:1c4dd5e24b8d 180 }
mhannay3 6:1c4dd5e24b8d 181 return moves;
mhannay3 6:1c4dd5e24b8d 182 }
mhannay3 9:1f33c0f299ae 183
mhannay3 8:928d5d33258f 184 // returns a vector of board positions that a piece can move in a line
mhannay3 8:928d5d33258f 185 std::vector<boardPos> movesInLine(bool isMovingPieceWhite, int row, int column, int rowChange, int columnChange)
mhannay3 8:928d5d33258f 186 {
mhannay3 8:928d5d33258f 187 std::vector<boardPos> moves;
mhannay3 8:928d5d33258f 188 for (int i = 1; ; i++) {
mhannay3 8:928d5d33258f 189 // check if piece can move to location
mhannay3 8:928d5d33258f 190 if (validMove(isMovingPieceWhite, row + i * rowChange, column + i * columnChange)) {
mhannay3 8:928d5d33258f 191 moves.push_back((boardPos) {
mhannay3 8:928d5d33258f 192 row + i * rowChange, column + i * columnChange
mhannay3 8:928d5d33258f 193 });
mhannay3 8:928d5d33258f 194 // if piece is capturable, stop moving beyond it
mhannay3 9:1f33c0f299ae 195 if (getPiece(row + i * rowChange, column + i * columnChange) != e) {
mhannay3 8:928d5d33258f 196 break;
mhannay3 8:928d5d33258f 197 }
mhannay3 9:1f33c0f299ae 198 // if unable to move, break out
mhannay3 8:928d5d33258f 199 } else {
mhannay3 8:928d5d33258f 200 break;
mhannay3 8:928d5d33258f 201 }
mhannay3 8:928d5d33258f 202 }
mhannay3 8:928d5d33258f 203 return moves;
mhannay3 8:928d5d33258f 204 }
mhannay3 6:1c4dd5e24b8d 205
mhannay3 8:928d5d33258f 206 // returns if a piece can move to a given location
mhannay3 6:1c4dd5e24b8d 207 bool validMove(bool isMovingPieceWhite, int row, int column)
mhannay3 6:1c4dd5e24b8d 208 {
mhannay3 6:1c4dd5e24b8d 209 if (row < 0 || row > 7 || column < 0 || column > 7) {
mhannay3 6:1c4dd5e24b8d 210 return false;
mhannay3 6:1c4dd5e24b8d 211 }
mhannay3 6:1c4dd5e24b8d 212 Piece capturedPiece = getPiece(row, column);
mhannay3 6:1c4dd5e24b8d 213 switch(capturedPiece) {
mhannay3 6:1c4dd5e24b8d 214 case wK:
mhannay3 6:1c4dd5e24b8d 215 case wQ:
mhannay3 6:1c4dd5e24b8d 216 case wR:
mhannay3 6:1c4dd5e24b8d 217 case wB:
mhannay3 6:1c4dd5e24b8d 218 case wN:
mhannay3 6:1c4dd5e24b8d 219 case w:
mhannay3 6:1c4dd5e24b8d 220 return !isMovingPieceWhite;
mhannay3 6:1c4dd5e24b8d 221 case bK:
mhannay3 6:1c4dd5e24b8d 222 case bQ:
mhannay3 6:1c4dd5e24b8d 223 case bR:
mhannay3 6:1c4dd5e24b8d 224 case bB:
mhannay3 6:1c4dd5e24b8d 225 case bN:
mhannay3 6:1c4dd5e24b8d 226 case b:
mhannay3 6:1c4dd5e24b8d 227 return isMovingPieceWhite;
mhannay3 6:1c4dd5e24b8d 228 case e:
mhannay3 6:1c4dd5e24b8d 229 return true;
mhannay3 6:1c4dd5e24b8d 230 }
mhannay3 6:1c4dd5e24b8d 231 return false;
mhannay3 6:1c4dd5e24b8d 232 }
mhannay3 5:b553c51b3b85 233 };
mhannay3 1:cd78922f70fa 234
mhannay3 5:b553c51b3b85 235 class GameBoard
mhannay3 5:b553c51b3b85 236 {
mhannay3 5:b553c51b3b85 237 private:
mhannay3 5:b553c51b3b85 238 BoardState boardState;
mhannay3 5:b553c51b3b85 239 static const int BOARD_DARK_COLOR = 0x769656;
mhannay3 5:b553c51b3b85 240 static const int BOARD_LIGHT_COLOR = 0xbaca44;
mhannay3 5:b553c51b3b85 241 static const int HOVER_COLOR = 0x0000ff;
mhannay3 5:b553c51b3b85 242 static const int SELECTED_COLOR = 0xff8800;
mhannay3 6:1c4dd5e24b8d 243 static const int MOVE_COLOR = 0xff00ff;
mhannay3 10:8f73a917b239 244
mhannay3 10:8f73a917b239 245 // gets the pixel coordinates of the top left of the square
mhannay3 10:8f73a917b239 246 static pixelCoord getTopLeftOfSquare(boardPos pos)
mhannay3 10:8f73a917b239 247 {
mhannay3 10:8f73a917b239 248 return getTopLeftOfSquare(pos.row, pos.column);
mhannay3 10:8f73a917b239 249 }
mhannay3 10:8f73a917b239 250 static pixelCoord getTopLeftOfSquare(int row, int column)
mhannay3 10:8f73a917b239 251 {
mhannay3 10:8f73a917b239 252 pixelCoord topLeft;
mhannay3 10:8f73a917b239 253 topLeft.x = 16 * column;
mhannay3 10:8f73a917b239 254 topLeft.y = 112 - 16 * row;
mhannay3 10:8f73a917b239 255 return topLeft;
mhannay3 10:8f73a917b239 256 }
mhannay3 5:b553c51b3b85 257
mhannay3 5:b553c51b3b85 258 // piece sprites (12 x 12)
mhannay3 10:8f73a917b239 259 static void drawPawn(int row, int column, bool white, bool light)
mhannay3 5:b553c51b3b85 260 {
mhannay3 5:b553c51b3b85 261 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 262 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 263 int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 5:b553c51b3b85 264 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 5:b553c51b3b85 265 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 5:b553c51b3b85 266 _, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 5:b553c51b3b85 267 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 268 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 5:b553c51b3b85 269 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 5:b553c51b3b85 270 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 271 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 272 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 5:b553c51b3b85 273 _, _, X, X, X, X, X, X, X, X, _, _,
mhannay3 5:b553c51b3b85 274 _, _, X, X, X, X, X, X, X, X, _, _
mhannay3 5:b553c51b3b85 275 };
mhannay3 10:8f73a917b239 276 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 10:8f73a917b239 277 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mhannay3 5:b553c51b3b85 278 }
mhannay3 5:b553c51b3b85 279
mhannay3 10:8f73a917b239 280 static void drawRook(int row, int column, bool white, bool light)
mhannay3 5:b553c51b3b85 281 {
mhannay3 5:b553c51b3b85 282 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 283 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 284 int sprite[144] = {X, X, _, X, X, _, _, X, X, _, X, X,
mhannay3 5:b553c51b3b85 285 X, X, _, X, X, _, _, X, X, _, X, X,
mhannay3 5:b553c51b3b85 286 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 287 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 288 _, X, X, X, _, X, X, _, X, X, X, _,
mhannay3 5:b553c51b3b85 289 _, X, X, X, _, X, X, _, X, X, X, _,
mhannay3 5:b553c51b3b85 290 _, _, X, X, _, X, X, _, X, X, _, _,
mhannay3 5:b553c51b3b85 291 _, _, X, X, _, X, X, _, X, X, _, _,
mhannay3 5:b553c51b3b85 292 _, _, X, X, _, X, X, _, X, X, _, _,
mhannay3 5:b553c51b3b85 293 _, X, X, X, X, X, X, X, X, X, X, _,
mhannay3 5:b553c51b3b85 294 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 295 X, X, X, X, X, X, X, X, X, X, X, X
mhannay3 5:b553c51b3b85 296 };
mhannay3 10:8f73a917b239 297 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 10:8f73a917b239 298 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mhannay3 5:b553c51b3b85 299 }
mhannay3 5:b553c51b3b85 300
mhannay3 10:8f73a917b239 301 static void drawKnight(int row, int column, bool white, bool light)
mhannay3 5:b553c51b3b85 302 {
mhannay3 5:b553c51b3b85 303 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 304 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 305 int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _,
mhannay3 5:b553c51b3b85 306 _, _, _, _, _, X, X, _, X, X, _, _,
mhannay3 5:b553c51b3b85 307 _, _, _, _, _, X, X, _, X, X, _, _,
mhannay3 5:b553c51b3b85 308 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 309 _, _, X, X, X, X, X, _, X, _, _, _,
mhannay3 5:b553c51b3b85 310 _, _, X, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 311 _, _, _, _, _, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 312 _, _, _, _, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 313 _, _, _, X, X, X, X, X, X, X, _, _,
mhannay3 5:b553c51b3b85 314 _, _, X, X, X, X, X, X, X, X, _, _,
mhannay3 5:b553c51b3b85 315 _, X, X, X, X, X, X, X, X, X, X, _,
mhannay3 5:b553c51b3b85 316 _, X, X, X, X, X, X, X, X, X, X, _
mhannay3 5:b553c51b3b85 317 };
mhannay3 10:8f73a917b239 318 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 10:8f73a917b239 319 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mhannay3 5:b553c51b3b85 320 }
mhannay3 5:b553c51b3b85 321
mhannay3 10:8f73a917b239 322 static void drawBishop(int row, int column, bool white, bool light)
mhannay3 5:b553c51b3b85 323 {
mhannay3 5:b553c51b3b85 324 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 325 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 326 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 327 _, _, _, _, X, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 328 _, _, _, X, X, X, _, _, X, _, _, _,
mhannay3 5:b553c51b3b85 329 _, _, _, X, X, _, _, X, X, _, _, _,
mhannay3 5:b553c51b3b85 330 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 331 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 5:b553c51b3b85 332 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 333 _, _, _, _, X, X, X, X, _, _, _, _,
mhannay3 5:b553c51b3b85 334 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 335 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 336 _, _, X, X, X, X, X, X, X, X, _, _,
mhannay3 5:b553c51b3b85 337 _, _, X, X, X, X, X, X, X, X, _, _
mhannay3 5:b553c51b3b85 338 };
mhannay3 10:8f73a917b239 339 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 10:8f73a917b239 340 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mhannay3 5:b553c51b3b85 341 }
mhannay3 5:b553c51b3b85 342
mhannay3 10:8f73a917b239 343 static void drawQueen(int row, int column, bool white, bool light)
mhannay3 5:b553c51b3b85 344 {
mhannay3 5:b553c51b3b85 345 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 346 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 347 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 348 _, _, X, _, _, X, X, _, _, X, _, _,
mhannay3 5:b553c51b3b85 349 X, _, X, X, _, X, X, _, X, X, _, X,
mhannay3 5:b553c51b3b85 350 X, _, X, X, _, X, X, _, X, X, _, X,
mhannay3 5:b553c51b3b85 351 X, _, X, X, _, X, X, _, X, X, _, X,
mhannay3 5:b553c51b3b85 352 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 353 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 354 X, X, _, X, X, X, X, X, X, _, X, X,
mhannay3 5:b553c51b3b85 355 X, X, X, X, _, X, X, _, X, X, X, X,
mhannay3 5:b553c51b3b85 356 _, X, X, X, X, X, X, X, X, X, X, _,
mhannay3 5:b553c51b3b85 357 _, _, X, X, X, X, X, X, X, X, _, _,
mhannay3 5:b553c51b3b85 358 _, X, X, X, X, X, X, X, X, X, X, _
mhannay3 5:b553c51b3b85 359 };
mhannay3 10:8f73a917b239 360 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 10:8f73a917b239 361 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mhannay3 5:b553c51b3b85 362 }
mhannay3 5:b553c51b3b85 363
mhannay3 10:8f73a917b239 364 static void drawKing(int row, int column, bool white, bool light)
mhannay3 5:b553c51b3b85 365 {
mhannay3 5:b553c51b3b85 366 int X = white ? 0xffffff : 0x000000;
mhannay3 5:b553c51b3b85 367 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 368 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 369 _, _, _, _, _, X, X, _, _, _, _, _,
mhannay3 5:b553c51b3b85 370 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 371 _, _, _, X, X, X, X, X, X, _, _, _,
mhannay3 5:b553c51b3b85 372 X, X, _, _, _, X, X, _, _, _, X, X,
mhannay3 5:b553c51b3b85 373 X, X, X, X, _, X, X, _, X, X, X, X,
mhannay3 5:b553c51b3b85 374 X, _, X, X, X, X, X, X, X, X, _, X,
mhannay3 5:b553c51b3b85 375 X, X, X, X, X, X, X, X, X, X, X, X,
mhannay3 5:b553c51b3b85 376 X, X, X, _, X, X, X, X, _, X, X, X,
mhannay3 5:b553c51b3b85 377 _, X, X, X, X, X, X, X, X, X, X, _,
mhannay3 5:b553c51b3b85 378 _, _, X, X, X, _, _, X, X, X, _, _,
mhannay3 5:b553c51b3b85 379 _, X, X, X, X, X, X, X, X, X, X, _
mhannay3 5:b553c51b3b85 380 };
mhannay3 10:8f73a917b239 381 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 10:8f73a917b239 382 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mhannay3 5:b553c51b3b85 383 }
mhannay3 5:b553c51b3b85 384
mhannay3 5:b553c51b3b85 385 public:
mhannay3 5:b553c51b3b85 386 BoardState getBoardState()
mhannay3 5:b553c51b3b85 387 {
mhannay3 5:b553c51b3b85 388 return boardState;
mhannay3 5:b553c51b3b85 389 }
mhannay3 5:b553c51b3b85 390
mhannay3 5:b553c51b3b85 391 void setBoardState(BoardState newBoardState)
mhannay3 5:b553c51b3b85 392 {
mhannay3 5:b553c51b3b85 393 boardState = newBoardState;
mhannay3 5:b553c51b3b85 394 }
mhannay3 5:b553c51b3b85 395
mhannay3 5:b553c51b3b85 396 // initializes the starting board state
mhannay3 5:b553c51b3b85 397 GameBoard()
mhannay3 5:b553c51b3b85 398 {
mhannay3 5:b553c51b3b85 399 // draw board
mhannay3 5:b553c51b3b85 400 for (int row = 0; row < 8; row++) {
mhannay3 5:b553c51b3b85 401 for (int column = 0; column < 8; column++) {
mhannay3 5:b553c51b3b85 402 uint64_t color;
mhannay3 5:b553c51b3b85 403 if ((row + column) % 2 == 0) {
mhannay3 5:b553c51b3b85 404 color = BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 405 } else {
mhannay3 5:b553c51b3b85 406 color = BOARD_LIGHT_COLOR;
mhannay3 5:b553c51b3b85 407 }
mhannay3 6:1c4dd5e24b8d 408 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 409 uLCD.filled_rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, color);
mhannay3 5:b553c51b3b85 410 }
mhannay3 5:b553c51b3b85 411 }
mhannay3 5:b553c51b3b85 412 // draw pieces
mhannay3 5:b553c51b3b85 413 placePieceAndDraw(wR, 0, 0);
mhannay3 5:b553c51b3b85 414 placePieceAndDraw(wN, 0, 1);
mhannay3 5:b553c51b3b85 415 placePieceAndDraw(wB, 0, 2);
mhannay3 5:b553c51b3b85 416 placePieceAndDraw(wQ, 0, 3);
mhannay3 5:b553c51b3b85 417 placePieceAndDraw(wK, 0, 4);
mhannay3 5:b553c51b3b85 418 placePieceAndDraw(wB, 0, 5);
mhannay3 5:b553c51b3b85 419 placePieceAndDraw(wN, 0, 6);
mhannay3 5:b553c51b3b85 420 placePieceAndDraw(wR, 0, 7);
mhannay3 5:b553c51b3b85 421 placePieceAndDraw(bR, 7, 0);
mhannay3 5:b553c51b3b85 422 placePieceAndDraw(bN, 7, 1);
mhannay3 5:b553c51b3b85 423 placePieceAndDraw(bB, 7, 2);
mhannay3 5:b553c51b3b85 424 placePieceAndDraw(bQ, 7, 3);
mhannay3 5:b553c51b3b85 425 placePieceAndDraw(bK, 7, 4);
mhannay3 5:b553c51b3b85 426 placePieceAndDraw(bB, 7, 5);
mhannay3 5:b553c51b3b85 427 placePieceAndDraw(bN, 7, 6);
mhannay3 5:b553c51b3b85 428 placePieceAndDraw(bR, 7, 7);
mhannay3 5:b553c51b3b85 429 for (int i = 0; i < 8; i++) {
mhannay3 5:b553c51b3b85 430 placePieceAndDraw(w, 1, i);
mhannay3 5:b553c51b3b85 431 placePieceAndDraw(b, 6, i);
mhannay3 5:b553c51b3b85 432 }
mhannay3 5:b553c51b3b85 433 }
mhannay3 5:b553c51b3b85 434
mhannay3 5:b553c51b3b85 435 // PIECE MOVEMENT AND GRAPHICS FUNCTIONS
mhannay3 5:b553c51b3b85 436
mhannay3 5:b553c51b3b85 437 // returns the piece at a given location
mhannay3 7:9e01c9a334dc 438 Piece getPiece(boardPos pos)
mhannay3 7:9e01c9a334dc 439 {
mhannay3 7:9e01c9a334dc 440 return getPiece(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 441 }
mhannay3 5:b553c51b3b85 442 Piece getPiece(int row, int column)
mhannay3 5:b553c51b3b85 443 {
mhannay3 5:b553c51b3b85 444 return boardState.getPiece(row, column);
mhannay3 5:b553c51b3b85 445 }
mhannay3 5:b553c51b3b85 446
mhannay3 5:b553c51b3b85 447 /* puts the bit representation of a piece at the set position of the board
mhannay3 5:b553c51b3b85 448 assumes that the position of the board is emptied beforehand
mhannay3 5:b553c51b3b85 449 */
mhannay3 7:9e01c9a334dc 450 void placePieceAndDraw(Piece piece, boardPos pos)
mhannay3 7:9e01c9a334dc 451 {
mhannay3 7:9e01c9a334dc 452 placePieceAndDraw(piece, pos.row, pos.column);
mhannay3 7:9e01c9a334dc 453 }
mhannay3 5:b553c51b3b85 454 void placePieceAndDraw(Piece piece, int row, int column)
mhannay3 5:b553c51b3b85 455 {
mhannay3 5:b553c51b3b85 456 boardState.placePiece(piece, row, column);
mhannay3 6:1c4dd5e24b8d 457 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 458 switch(piece) {
mhannay3 5:b553c51b3b85 459 case wK:
mhannay3 5:b553c51b3b85 460 case bK:
mhannay3 10:8f73a917b239 461 drawKing(row, column, piece==wK, (row+column)%2);
mhannay3 5:b553c51b3b85 462 break;
mhannay3 5:b553c51b3b85 463 case wQ:
mhannay3 5:b553c51b3b85 464 case bQ:
mhannay3 10:8f73a917b239 465 drawQueen(row, column, piece==wQ, (row+column)%2);
mhannay3 5:b553c51b3b85 466 break;
mhannay3 5:b553c51b3b85 467 case wB:
mhannay3 5:b553c51b3b85 468 case bB:
mhannay3 10:8f73a917b239 469 drawBishop(row, column, piece==wB, (row+column)%2);
mhannay3 5:b553c51b3b85 470 break;
mhannay3 5:b553c51b3b85 471 case wN:
mhannay3 5:b553c51b3b85 472 case bN:
mhannay3 10:8f73a917b239 473 drawKnight(row, column, piece==wN, (row+column)%2);
mhannay3 5:b553c51b3b85 474 break;
mhannay3 5:b553c51b3b85 475 case wR:
mhannay3 5:b553c51b3b85 476 case bR:
mhannay3 10:8f73a917b239 477 drawRook(row, column, piece==wR, (row+column)%2);
mhannay3 5:b553c51b3b85 478 break;
mhannay3 5:b553c51b3b85 479 case w:
mhannay3 5:b553c51b3b85 480 case b:
mhannay3 10:8f73a917b239 481 drawPawn(row, column, piece==w, (row+column)%2);
mhannay3 5:b553c51b3b85 482 break;
mhannay3 5:b553c51b3b85 483 }
mhannay3 5:b553c51b3b85 484 }
mhannay3 5:b553c51b3b85 485
mhannay3 5:b553c51b3b85 486 /* removes a piece from the set position of the board
mhannay3 5:b553c51b3b85 487 returns the bit representation of the piece
mhannay3 5:b553c51b3b85 488 */
mhannay3 7:9e01c9a334dc 489 Piece removePieceAndDraw(boardPos pos)
mhannay3 7:9e01c9a334dc 490 {
mhannay3 7:9e01c9a334dc 491 return removePieceAndDraw(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 492 }
mhannay3 5:b553c51b3b85 493 Piece removePieceAndDraw(int row, int column)
mhannay3 5:b553c51b3b85 494 {
mhannay3 5:b553c51b3b85 495 Piece removedPiece = boardState.removePiece(row, column);
mhannay3 6:1c4dd5e24b8d 496 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 1:cd78922f70fa 497 uint64_t color;
mhannay3 1:cd78922f70fa 498 if ((row + column) % 2 == 0) {
mhannay3 1:cd78922f70fa 499 color = BOARD_DARK_COLOR;
mhannay3 1:cd78922f70fa 500 } else {
mhannay3 1:cd78922f70fa 501 color = BOARD_LIGHT_COLOR;
mhannay3 1:cd78922f70fa 502 }
mhannay3 5:b553c51b3b85 503 uLCD.filled_rectangle(tl.x+2, tl.y+2, tl.x + 13, tl.y + 13, color);
mhannay3 5:b553c51b3b85 504 return removedPiece;
mhannay3 5:b553c51b3b85 505 }
mhannay3 5:b553c51b3b85 506
mhannay3 5:b553c51b3b85 507 /* moves a piece from one position to another
mhannay3 5:b553c51b3b85 508 returns the captured piece
mhannay3 5:b553c51b3b85 509 */
mhannay3 7:9e01c9a334dc 510 Piece movePieceAndDraw(boardPos startPos, boardPos endPos)
mhannay3 7:9e01c9a334dc 511 {
mhannay3 7:9e01c9a334dc 512 return movePieceAndDraw(startPos.row, startPos.column, endPos.row, endPos.column);
mhannay3 7:9e01c9a334dc 513 }
mhannay3 5:b553c51b3b85 514 Piece movePieceAndDraw(int startRow, int startColumn, int endRow, int endColumn)
mhannay3 5:b553c51b3b85 515 {
mhannay3 5:b553c51b3b85 516 Piece movingPiece = removePieceAndDraw(startRow, startColumn);
mhannay3 5:b553c51b3b85 517 Piece capturedPiece = boardState.removePiece(endRow, endColumn);
mhannay3 5:b553c51b3b85 518 placePieceAndDraw(movingPiece, endRow, endColumn);
mhannay3 5:b553c51b3b85 519 return capturedPiece;
mhannay3 1:cd78922f70fa 520 }
mhannay3 1:cd78922f70fa 521
mhannay3 5:b553c51b3b85 522 // SQUARE BORDER GRAPHICS FUNCTIONS
mhannay3 5:b553c51b3b85 523
mhannay3 5:b553c51b3b85 524 // removes selection border around square
mhannay3 7:9e01c9a334dc 525 void unselectSquare(boardPos pos)
mhannay3 7:9e01c9a334dc 526 {
mhannay3 7:9e01c9a334dc 527 unselectSquare(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 528 }
mhannay3 5:b553c51b3b85 529 void unselectSquare(int row, int column)
mhannay3 5:b553c51b3b85 530 {
mhannay3 6:1c4dd5e24b8d 531 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 532 uint64_t color;
mhannay3 5:b553c51b3b85 533 if ((row + column) % 2 == 0) {
mhannay3 5:b553c51b3b85 534 color = BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 535 } else {
mhannay3 5:b553c51b3b85 536 color = BOARD_LIGHT_COLOR;
mhannay3 5:b553c51b3b85 537 }
mhannay3 5:b553c51b3b85 538 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, color);
mhannay3 5:b553c51b3b85 539 }
mhannay3 5:b553c51b3b85 540
mhannay3 7:9e01c9a334dc 541 // draws the hover border around square
mhannay3 7:9e01c9a334dc 542 void hoverSquare(boardPos pos)
mhannay3 7:9e01c9a334dc 543 {
mhannay3 7:9e01c9a334dc 544 hoverSquare(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 545 }
mhannay3 5:b553c51b3b85 546 void hoverSquare(int row, int column)
mhannay3 5:b553c51b3b85 547 {
mhannay3 6:1c4dd5e24b8d 548 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 549 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, HOVER_COLOR);
mhannay3 1:cd78922f70fa 550 }
mhannay3 1:cd78922f70fa 551
mhannay3 1:cd78922f70fa 552 // draws selection border around square
mhannay3 7:9e01c9a334dc 553 void selectSquare(boardPos pos)
mhannay3 7:9e01c9a334dc 554 {
mhannay3 7:9e01c9a334dc 555 selectSquare(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 556 }
mhannay3 5:b553c51b3b85 557 void selectSquare(int row, int column)
mhannay3 5:b553c51b3b85 558 {
mhannay3 6:1c4dd5e24b8d 559 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 560 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, SELECTED_COLOR);
mhannay3 1:cd78922f70fa 561 }
mhannay3 9:1f33c0f299ae 562
mhannay3 8:928d5d33258f 563 // draws the movement border around square
mhannay3 9:1f33c0f299ae 564 void movementSquare(boardPos pos)
mhannay3 8:928d5d33258f 565 {
mhannay3 9:1f33c0f299ae 566 movementSquare(pos.row, pos.column);
mhannay3 8:928d5d33258f 567 }
mhannay3 9:1f33c0f299ae 568 void movementSquare(int row, int column)
mhannay3 8:928d5d33258f 569 {
mhannay3 8:928d5d33258f 570 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 8:928d5d33258f 571 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, MOVE_COLOR);
mhannay3 8:928d5d33258f 572 }
mhannay3 5:b553c51b3b85 573 };
mus3 0:d16cc59110a3 574
mhannay3 7:9e01c9a334dc 575 // game variables
mhannay3 7:9e01c9a334dc 576 GameBoard gameBoard;
mhannay3 7:9e01c9a334dc 577 GameState state = whiteSelecting;
mhannay3 7:9e01c9a334dc 578 boardPos cursorPos = (boardPos)
mhannay3 7:9e01c9a334dc 579 {
mhannay3 7:9e01c9a334dc 580 3, 4
mhannay3 7:9e01c9a334dc 581 };
mhannay3 8:928d5d33258f 582 boardPos selectedPos;
mhannay3 9:1f33c0f299ae 583 Piece selectedPiece;
mhannay3 9:1f33c0f299ae 584 std::vector<boardPos> possibleMoves;
mhannay3 7:9e01c9a334dc 585
mhannay3 7:9e01c9a334dc 586 // callbacks
mhannay3 9:1f33c0f299ae 587 void moveCursor(int rowChange, int columnChange)
mhannay3 9:1f33c0f299ae 588 {
mhannay3 9:1f33c0f299ae 589 // calculate new positoin that is within board bounds
mhannay3 9:1f33c0f299ae 590 int newRow = cursorPos.row + rowChange;
mhannay3 9:1f33c0f299ae 591 newRow = newRow <= 7 ? newRow : 7;
mhannay3 9:1f33c0f299ae 592 newRow = newRow >= 0 ? newRow : 0;
mhannay3 9:1f33c0f299ae 593 int newColumn = cursorPos.column + columnChange;
mhannay3 9:1f33c0f299ae 594 newColumn = newColumn <= 7 ? newColumn : 7;
mhannay3 9:1f33c0f299ae 595 newColumn = newColumn >= 0 ? newColumn : 0;
mhannay3 9:1f33c0f299ae 596 boardPos newPos = (boardPos) {
mhannay3 9:1f33c0f299ae 597 newRow, newColumn
mhannay3 9:1f33c0f299ae 598 };
mhannay3 9:1f33c0f299ae 599
mhannay3 9:1f33c0f299ae 600 // draw border around square that should be there after moving cursor off
mhannay3 9:1f33c0f299ae 601 if (cursorPos == selectedPos) {
mhannay3 9:1f33c0f299ae 602 gameBoard.selectSquare(cursorPos);
mhannay3 9:1f33c0f299ae 603 } else if (std::find(possibleMoves.begin(), possibleMoves.end(), cursorPos) != possibleMoves.end()) {
mhannay3 9:1f33c0f299ae 604 gameBoard.movementSquare(cursorPos);
mhannay3 9:1f33c0f299ae 605 } else {
mhannay3 9:1f33c0f299ae 606 gameBoard.unselectSquare(cursorPos);
mhannay3 9:1f33c0f299ae 607 }
mhannay3 9:1f33c0f299ae 608
mhannay3 9:1f33c0f299ae 609 // draw hover rectangle over new square
mhannay3 9:1f33c0f299ae 610 cursorPos = newPos;
mhannay3 9:1f33c0f299ae 611 gameBoard.hoverSquare(cursorPos);
mhannay3 9:1f33c0f299ae 612 }
mhannay3 9:1f33c0f299ae 613
mhannay3 7:9e01c9a334dc 614 void joyStickUp()
mhannay3 7:9e01c9a334dc 615 {
mhannay3 9:1f33c0f299ae 616 moveCursor(1, 0);
mhannay3 7:9e01c9a334dc 617 }
mhannay3 7:9e01c9a334dc 618
mhannay3 7:9e01c9a334dc 619 void joyStickDown()
mhannay3 7:9e01c9a334dc 620 {
mhannay3 9:1f33c0f299ae 621 moveCursor(-1, 0);
mhannay3 7:9e01c9a334dc 622 }
mhannay3 7:9e01c9a334dc 623
mhannay3 7:9e01c9a334dc 624 void joyStickLeft()
mhannay3 7:9e01c9a334dc 625 {
mhannay3 9:1f33c0f299ae 626 moveCursor(0, -1);
mhannay3 7:9e01c9a334dc 627 }
mhannay3 7:9e01c9a334dc 628
mhannay3 7:9e01c9a334dc 629 void joyStickRight()
mhannay3 7:9e01c9a334dc 630 {
mhannay3 9:1f33c0f299ae 631 moveCursor(0, 1);
mhannay3 7:9e01c9a334dc 632 }
mhannay3 7:9e01c9a334dc 633
mhannay3 7:9e01c9a334dc 634 void joyStickPressed()
mhannay3 7:9e01c9a334dc 635 {
mhannay3 8:928d5d33258f 636 switch(state) {
mhannay3 8:928d5d33258f 637 case whiteSelecting:
mhannay3 9:1f33c0f299ae 638 case blackSelecting: {
mhannay3 8:928d5d33258f 639 selectedPos = cursorPos;
mhannay3 9:1f33c0f299ae 640 Piece tempPiece = gameBoard.getPiece(cursorPos);
mhannay3 9:1f33c0f299ae 641 std::vector<Piece> pickablePieces = state == whiteSelecting ? whitePieces : blackPieces;
mhannay3 9:1f33c0f299ae 642 // check that piece is white and able to be picked up
mhannay3 9:1f33c0f299ae 643 if (std::find(pickablePieces.begin(), pickablePieces.end(), tempPiece) != pickablePieces.end()) {
mhannay3 9:1f33c0f299ae 644 selectedPiece = tempPiece;
mhannay3 9:1f33c0f299ae 645 possibleMoves = gameBoard.getBoardState().getMoves(cursorPos);
mhannay3 9:1f33c0f299ae 646 // draw movement squares
mhannay3 9:1f33c0f299ae 647 for (std::vector<boardPos>::iterator it = possibleMoves.begin(); it != possibleMoves.end(); ++it) {
mhannay3 9:1f33c0f299ae 648 gameBoard.movementSquare(*it);
mhannay3 9:1f33c0f299ae 649 }
mhannay3 9:1f33c0f299ae 650 gameBoard.selectSquare(selectedPos);
mhannay3 9:1f33c0f299ae 651 // transistion state
mhannay3 9:1f33c0f299ae 652 state = state == whiteSelecting ? whitePickedUp : blackPickedUp;
mhannay3 9:1f33c0f299ae 653 }
mhannay3 8:928d5d33258f 654 break;
mhannay3 9:1f33c0f299ae 655 }
mhannay3 9:1f33c0f299ae 656 case whitePickedUp:
mhannay3 9:1f33c0f299ae 657 case blackPickedUp: {
mhannay3 9:1f33c0f299ae 658 // check if move is valid
mhannay3 9:1f33c0f299ae 659 if (std::find(possibleMoves.begin(), possibleMoves.end(), cursorPos) != possibleMoves.end()) {
mhannay3 9:1f33c0f299ae 660 // move the piece
mhannay3 9:1f33c0f299ae 661 Piece capturedPiece = gameBoard.movePieceAndDraw(selectedPos, cursorPos);
mhannay3 9:1f33c0f299ae 662 // check for king capture
mhannay3 9:1f33c0f299ae 663 if (state == whitePickedUp && capturedPiece == bK || state == blackPickedUp && capturedPiece == wK) {
mhannay3 9:1f33c0f299ae 664 // game end
mhannay3 9:1f33c0f299ae 665 }
mhannay3 9:1f33c0f299ae 666 // transition state
mhannay3 9:1f33c0f299ae 667 state = state == whitePickedUp ? blackSelecting : whiteSelecting;
mhannay3 9:1f33c0f299ae 668 // check if placing piece back down
mhannay3 9:1f33c0f299ae 669 } else if (cursorPos == selectedPos) {
mhannay3 9:1f33c0f299ae 670 // transition state
mhannay3 9:1f33c0f299ae 671 state = state == whitePickedUp ? whiteSelecting : blackSelecting;
mhannay3 9:1f33c0f299ae 672 }
mhannay3 9:1f33c0f299ae 673 // unselect movement squares
mhannay3 9:1f33c0f299ae 674 for (std::vector<boardPos>::iterator it = possibleMoves.begin(); it != possibleMoves.end(); ++it) {
mhannay3 9:1f33c0f299ae 675 gameBoard.unselectSquare(*it);
mhannay3 9:1f33c0f299ae 676 }
mhannay3 9:1f33c0f299ae 677 gameBoard.unselectSquare(selectedPos);
mhannay3 9:1f33c0f299ae 678 gameBoard.hoverSquare(cursorPos);
mhannay3 8:928d5d33258f 679 break;
mhannay3 9:1f33c0f299ae 680 }
mhannay3 9:1f33c0f299ae 681 case whiteAI:
mhannay3 9:1f33c0f299ae 682 case blackAI: {
mhannay3 8:928d5d33258f 683 break;
mhannay3 9:1f33c0f299ae 684 }
mhannay3 8:928d5d33258f 685 }
mhannay3 7:9e01c9a334dc 686 }
mhannay3 7:9e01c9a334dc 687
mhannay3 5:b553c51b3b85 688 int main()
mhannay3 5:b553c51b3b85 689 {
mhannay3 10:8f73a917b239 690 //gameBoard = GameBoard();
mhannay3 9:1f33c0f299ae 691 whitePieces.push_back(wK);
mhannay3 9:1f33c0f299ae 692 whitePieces.push_back(wQ);
mhannay3 9:1f33c0f299ae 693 whitePieces.push_back(wB);
mhannay3 9:1f33c0f299ae 694 whitePieces.push_back(wN);
mhannay3 9:1f33c0f299ae 695 whitePieces.push_back(wR);
mhannay3 9:1f33c0f299ae 696 whitePieces.push_back(w);
mhannay3 9:1f33c0f299ae 697 blackPieces.push_back(bK);
mhannay3 9:1f33c0f299ae 698 blackPieces.push_back(bQ);
mhannay3 9:1f33c0f299ae 699 blackPieces.push_back(bB);
mhannay3 9:1f33c0f299ae 700 blackPieces.push_back(bN);
mhannay3 9:1f33c0f299ae 701 blackPieces.push_back(bR);
mhannay3 9:1f33c0f299ae 702 blackPieces.push_back(b);
mhannay3 1:cd78922f70fa 703 return 0;
mhannay3 1:cd78922f70fa 704 }