Mert Us Matthew Hannay Logan Starr

Dependencies:   mbed 4DGL-uLCD-SE

Committer:
mus3
Date:
Sun Dec 04 09:32:07 2022 +0000
Revision:
14:f390d08e5f92
Parent:
11:43c89579ac52
Child:
15:cae96e8b62a3
Child:
16:b66a720631dd
AI code and King illegal move check

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
mus3 14:f390d08e5f92 7 DigitalOut myLed(LED1);
mus3 14:f390d08e5f92 8 Serial Blue(p13, p14);
mus3 14:f390d08e5f92 9
mhannay3 5:b553c51b3b85 10 enum Piece {e, wK, bK, wQ, bQ, wR, bR, wB, bB, wN, bN, w, b};
mhannay3 9:1f33c0f299ae 11 std::vector<Piece> whitePieces;
mhannay3 9:1f33c0f299ae 12 std::vector<Piece> blackPieces;
mhannay3 1:cd78922f70fa 13
mhannay3 9:1f33c0f299ae 14 enum GameState {whiteSelecting, whitePickedUp, whiteAI, blackSelecting, blackPickedUp, blackAI};
mhannay3 7:9e01c9a334dc 15
mhannay3 6:1c4dd5e24b8d 16 struct pixelCoord {
mhannay3 1:cd78922f70fa 17 uint8_t x;
mhannay3 1:cd78922f70fa 18 uint8_t y;
mhannay3 5:b553c51b3b85 19 };
mhannay3 1:cd78922f70fa 20
mhannay3 6:1c4dd5e24b8d 21 struct boardPos {
mhannay3 6:1c4dd5e24b8d 22 uint8_t row;
mhannay3 6:1c4dd5e24b8d 23 uint8_t column;
mhannay3 9:1f33c0f299ae 24
mhannay3 9:1f33c0f299ae 25 bool operator==(const boardPos &other) const
mhannay3 9:1f33c0f299ae 26 {
mus3 11:43c89579ac52 27 return row == other.row && column == other.column;
mhannay3 9:1f33c0f299ae 28 }
mhannay3 6:1c4dd5e24b8d 29 };
mhannay3 6:1c4dd5e24b8d 30
mus3 11:43c89579ac52 31 class Nav_Switch
mus3 11:43c89579ac52 32 {
mus3 11:43c89579ac52 33 public:
mus3 11:43c89579ac52 34 Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire);
mus3 11:43c89579ac52 35 int read();
mus3 11:43c89579ac52 36 //boolean functions to test each switch
mus3 11:43c89579ac52 37 bool up();
mus3 11:43c89579ac52 38 bool down();
mus3 11:43c89579ac52 39 bool left();
mus3 11:43c89579ac52 40 bool right();
mus3 11:43c89579ac52 41 bool fire();
mus3 11:43c89579ac52 42 //automatic read on RHS
mus3 11:43c89579ac52 43 operator int ();
mus3 11:43c89579ac52 44 //index to any switch array style
mus3 11:43c89579ac52 45 bool operator[](int index) {
mus3 11:43c89579ac52 46 return _pins[index];
mus3 11:43c89579ac52 47 };
mus3 11:43c89579ac52 48 private:
mus3 11:43c89579ac52 49 BusIn _pins;
mus3 11:43c89579ac52 50
mus3 11:43c89579ac52 51 };
mus3 11:43c89579ac52 52 Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire):
mus3 11:43c89579ac52 53 _pins(up, down, left, right, fire)
mus3 11:43c89579ac52 54 {
mus3 11:43c89579ac52 55 _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise
mus3 11:43c89579ac52 56 wait(0.001); //delays just a bit for pullups to pull inputs high
mus3 11:43c89579ac52 57 }
mus3 11:43c89579ac52 58 inline bool Nav_Switch::up()
mus3 11:43c89579ac52 59 {
mus3 11:43c89579ac52 60 return !(_pins[0]);
mus3 11:43c89579ac52 61 }
mus3 11:43c89579ac52 62 inline bool Nav_Switch::down()
mus3 11:43c89579ac52 63 {
mus3 11:43c89579ac52 64 return !(_pins[1]);
mus3 11:43c89579ac52 65 }
mus3 11:43c89579ac52 66 inline bool Nav_Switch::left()
mus3 11:43c89579ac52 67 {
mus3 11:43c89579ac52 68 return !(_pins[2]);
mus3 11:43c89579ac52 69 }
mus3 11:43c89579ac52 70 inline bool Nav_Switch::right()
mus3 11:43c89579ac52 71 {
mus3 11:43c89579ac52 72 return !(_pins[3]);
mus3 11:43c89579ac52 73 }
mus3 11:43c89579ac52 74 inline bool Nav_Switch::fire()
mus3 11:43c89579ac52 75 {
mus3 11:43c89579ac52 76 return !(_pins[4]);
mus3 11:43c89579ac52 77 }
mus3 11:43c89579ac52 78 inline int Nav_Switch::read()
mus3 11:43c89579ac52 79 {
mus3 11:43c89579ac52 80 return _pins.read();
mus3 11:43c89579ac52 81 }
mus3 11:43c89579ac52 82 inline Nav_Switch::operator int ()
mus3 11:43c89579ac52 83 {
mus3 11:43c89579ac52 84 return _pins.read();
mus3 11:43c89579ac52 85 }
mus3 11:43c89579ac52 86
mus3 14:f390d08e5f92 87 const float KING_POSITION_VALUES[] = {2.0, 3.0, 1.0, 0.0, 0.0, 1.0, 3.0, 2,0,
mus3 14:f390d08e5f92 88 2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 2.0, 2.0,
mus3 14:f390d08e5f92 89 -1.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -1.0,
mus3 14:f390d08e5f92 90 -2.0, -3.0, -3.0, -4.0, -4.0, -3.0, -3.0, -2,0,
mus3 14:f390d08e5f92 91 -3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0,
mus3 14:f390d08e5f92 92 -3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0,
mus3 14:f390d08e5f92 93 -3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0,
mus3 14:f390d08e5f92 94 -3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0};
mus3 14:f390d08e5f92 95
mus3 14:f390d08e5f92 96 const float QUEEN_POSITION_VALUES[] = {-2.0, -1.0, -1.0, -0.5, -0.5, -1.0, -1.0, -2.0,
mus3 14:f390d08e5f92 97 -1.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, -1.0,
mus3 14:f390d08e5f92 98 -1.0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.0, -1.0,
mus3 14:f390d08e5f92 99 0.0, 0.0, 0.5, 0.5, 0.5, 0.5, 0.0, -0.5,
mus3 14:f390d08e5f92 100 -0.5, 0.0, 0.5, 0.5, 0.5, 0.5, 0.0, -0.5,
mus3 14:f390d08e5f92 101 -1.0, 0.0, 0.5, 0.5, 0.5, 0.5, 0.0, -1.0,
mus3 14:f390d08e5f92 102 -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0,
mus3 14:f390d08e5f92 103 -2.0, -1.0, -1.0, -0.5, -0.5, -1.0, -1.0, -2.0};
mus3 14:f390d08e5f92 104
mus3 14:f390d08e5f92 105 const float ROOK_POSITION_VALUES[] = {0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0,
mus3 14:f390d08e5f92 106 -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5,
mus3 14:f390d08e5f92 107 -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5,
mus3 14:f390d08e5f92 108 -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5,
mus3 14:f390d08e5f92 109 -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5,
mus3 14:f390d08e5f92 110 -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5,
mus3 14:f390d08e5f92 111 0.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.5,
mus3 14:f390d08e5f92 112 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
mus3 14:f390d08e5f92 113
mus3 14:f390d08e5f92 114 const float BISHOP_POSITION_VALUES[] = {-2.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -2.0,
mus3 14:f390d08e5f92 115 -1.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, -1.0,
mus3 14:f390d08e5f92 116 -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0,
mus3 14:f390d08e5f92 117 -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, -1.0,
mus3 14:f390d08e5f92 118 -1.0, 0.5, 0.5, 1.0, 1.0, 0.5, 0.5, -1.0,
mus3 14:f390d08e5f92 119 -1.0, 0.0, 0.5, 1.0, 1.0, 0.5, 0.0, -1.0,
mus3 14:f390d08e5f92 120 -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0,
mus3 14:f390d08e5f92 121 -2.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -2.0};
mus3 14:f390d08e5f92 122 const float KNIGHT_POSITION_VALUES[] = {-5.0, -4.0, -3.0, -3.0, -3.0, -3.0, -4.0, -5.0,
mus3 14:f390d08e5f92 123 -4.0, -2.0, 0.0, 0.5, 0.5, 0.0, -2.0, -4.0,
mus3 14:f390d08e5f92 124 -3.0, 0.5, 1.0, 1.5, 1.5, 1.0, 0.5, -3.0,
mus3 14:f390d08e5f92 125 -3.0, 0.0, 1.5, 2.0, 2.0, 1.5, 0.0, -3.0,
mus3 14:f390d08e5f92 126 -3.0, 0.5, 1.5, 2.0, 2.0, 1.5, 0.5, -3.0,
mus3 14:f390d08e5f92 127 -3.0, 0.0, 1.0, 1.5, 1.5, 1.0, 0.0, -3.0,
mus3 14:f390d08e5f92 128 -4.0, -2.0, 0.0, 0.0, 0.0, 0.0, -2.0, -4.0,
mus3 14:f390d08e5f92 129 -5.0, -4.0, -3.0, -3.0, -3.0, -3.0, -4.0, -5.0};
mus3 14:f390d08e5f92 130 const float PAWN_POSITION_VALUES[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
mus3 14:f390d08e5f92 131 0.5, 1.0, 1.0, -2.0, -2.0, 1.0, 1.0, 0.5,
mus3 14:f390d08e5f92 132 0.5, -0.5, -1.0, 0.0, 0.0, -1.0, -0.5, 0.5,
mus3 14:f390d08e5f92 133 0.0, 0.0, 0.0, 2.0, 2.0, 0.0, 0.0, 0.0,
mus3 14:f390d08e5f92 134 0.5, 0.5, 1.0, 2.5, 2.5, 1.0, 0.5, 0.5,
mus3 14:f390d08e5f92 135 1.0, 1.0, 2.0, 3.0, 3.0, 2.0, 1.0, 1.0,
mus3 14:f390d08e5f92 136 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0,
mus3 14:f390d08e5f92 137 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
mus3 14:f390d08e5f92 138
mhannay3 5:b553c51b3b85 139 class BoardState
mhannay3 5:b553c51b3b85 140 {
mhannay3 5:b553c51b3b85 141 private:
mhannay3 5:b553c51b3b85 142 Piece array[64];
mus3 14:f390d08e5f92 143 // These values are for white pieces, need to be inverted
mus3 14:f390d08e5f92 144 // when making calculations for black pieces.
mus3 14:f390d08e5f92 145 static const float PAWN_VALUE = 10.0;
mus3 14:f390d08e5f92 146 static const float KNIGHT_VALUE = 30.0;
mus3 14:f390d08e5f92 147 static const float BISHOP_VALUE = 30.0;
mus3 14:f390d08e5f92 148 static const float ROOK_VALUE = 50.0;
mus3 14:f390d08e5f92 149 static const float QUEEN_VALUE = 90.0;
mus3 14:f390d08e5f92 150 static const float KING_VALUE = 900.0;
mhannay3 5:b553c51b3b85 151 public:
mus3 14:f390d08e5f92 152 BoardState() {}
mus3 14:f390d08e5f92 153
mhannay3 4:5798e4062350 154 // calculates the advantage difference for the board state
mhannay3 5:b553c51b3b85 155 float calculateBoardState()
mhannay3 5:b553c51b3b85 156 {
mus3 14:f390d08e5f92 157 float sum = 0.0;
mus3 14:f390d08e5f92 158 for (int i = 0; i < 64; i++) {
mus3 14:f390d08e5f92 159 int row = i / 8;
mus3 14:f390d08e5f92 160 int column = i % 8;
mus3 14:f390d08e5f92 161 Piece curr = getPiece(row, column);
mus3 14:f390d08e5f92 162 switch(curr) {
mus3 14:f390d08e5f92 163 case wK:
mus3 14:f390d08e5f92 164 sum += KING_VALUE + KING_POSITION_VALUES[row*8 + column];
mus3 14:f390d08e5f92 165 break;
mus3 14:f390d08e5f92 166 case bK:
mus3 14:f390d08e5f92 167 sum -= (KING_VALUE + KING_POSITION_VALUES[(7-row)*8 + (7-column)]);
mus3 14:f390d08e5f92 168 break;
mus3 14:f390d08e5f92 169 case wQ:
mus3 14:f390d08e5f92 170 sum += QUEEN_VALUE + QUEEN_POSITION_VALUES[row*8 + column];
mus3 14:f390d08e5f92 171 break;
mus3 14:f390d08e5f92 172 case bQ:
mus3 14:f390d08e5f92 173 sum -= (QUEEN_VALUE - QUEEN_POSITION_VALUES[(7-row)*8 + (7-column)]);
mus3 14:f390d08e5f92 174 break;
mus3 14:f390d08e5f92 175 case wR:
mus3 14:f390d08e5f92 176 sum += ROOK_VALUE + ROOK_POSITION_VALUES[row*8 + column];
mus3 14:f390d08e5f92 177 break;
mus3 14:f390d08e5f92 178 case bR:
mus3 14:f390d08e5f92 179 sum -= (ROOK_VALUE - ROOK_POSITION_VALUES[(7-row)*8 + (7-column)]);
mus3 14:f390d08e5f92 180 break;
mus3 14:f390d08e5f92 181 case wB:
mus3 14:f390d08e5f92 182 sum += BISHOP_VALUE + BISHOP_POSITION_VALUES[row*8 + column];
mus3 14:f390d08e5f92 183 break;
mus3 14:f390d08e5f92 184 case bB:
mus3 14:f390d08e5f92 185 sum -= (BISHOP_VALUE - BISHOP_POSITION_VALUES[(7-row)*8 + (7-column)]);
mus3 14:f390d08e5f92 186 break;
mus3 14:f390d08e5f92 187 case wN:
mus3 14:f390d08e5f92 188 sum += KNIGHT_VALUE + KNIGHT_POSITION_VALUES[row*8 + column];
mus3 14:f390d08e5f92 189 break;
mus3 14:f390d08e5f92 190 case bN:
mus3 14:f390d08e5f92 191 sum -= (KNIGHT_VALUE - KNIGHT_POSITION_VALUES[(7-row)*8 + (7-column)]);
mus3 14:f390d08e5f92 192 break;
mus3 14:f390d08e5f92 193 case w:
mus3 14:f390d08e5f92 194 sum += PAWN_VALUE + PAWN_POSITION_VALUES[row*8 + column];
mus3 14:f390d08e5f92 195 break;
mus3 14:f390d08e5f92 196 case b:
mus3 14:f390d08e5f92 197 sum -= (PAWN_VALUE - PAWN_POSITION_VALUES[(7-row)*8 + (7-column)]);
mus3 14:f390d08e5f92 198 break;
mus3 14:f390d08e5f92 199 default:
mus3 14:f390d08e5f92 200 break;
mus3 14:f390d08e5f92 201 }
mus3 14:f390d08e5f92 202 }
mus3 14:f390d08e5f92 203 return sum;
mhannay3 4:5798e4062350 204 }
mhannay3 5:b553c51b3b85 205
mhannay3 4:5798e4062350 206 // returns the piece at a given location
mhannay3 5:b553c51b3b85 207 Piece getPiece(int row, int column)
mhannay3 5:b553c51b3b85 208 {
mhannay3 5:b553c51b3b85 209 return array[column + 8 * row];
mhannay3 4:5798e4062350 210 }
mhannay3 4:5798e4062350 211
mhannay3 5:b553c51b3b85 212 // puts a piece at a given location
mhannay3 5:b553c51b3b85 213 void placePiece(Piece piece, int row, int column)
mhannay3 5:b553c51b3b85 214 {
mhannay3 5:b553c51b3b85 215 array[column + 8 * row] = piece;
mhannay3 4:5798e4062350 216 }
mhannay3 4:5798e4062350 217
mhannay3 4:5798e4062350 218 /* removes a piece from the set position of the board
mhannay3 4:5798e4062350 219 returns the bit representation of the piece
mhannay3 4:5798e4062350 220 */
mhannay3 5:b553c51b3b85 221 Piece removePiece(int row, int column)
mhannay3 5:b553c51b3b85 222 {
mhannay3 5:b553c51b3b85 223 Piece removedPiece = array[column + 8 * row];
mhannay3 5:b553c51b3b85 224 array[column + 8 * row] = e;
mhannay3 4:5798e4062350 225 return removedPiece;
mhannay3 1:cd78922f70fa 226 }
mhannay3 1:cd78922f70fa 227
mhannay3 1:cd78922f70fa 228 /* moves a piece from one position to another
mhannay3 1:cd78922f70fa 229 returns the captured piece
mhannay3 1:cd78922f70fa 230 */
mhannay3 5:b553c51b3b85 231 Piece movePiece(int startRow, int startColumn, int endRow, int endColumn)
mhannay3 5:b553c51b3b85 232 {
mhannay3 5:b553c51b3b85 233 Piece movingPiece = removePiece(startRow, startColumn);
mhannay3 5:b553c51b3b85 234 Piece capturedPiece = removePiece(endRow, endColumn);
mhannay3 5:b553c51b3b85 235 placePiece(movingPiece, endRow, endColumn);
mhannay3 1:cd78922f70fa 236 return capturedPiece;
mhannay3 1:cd78922f70fa 237 }
mhannay3 6:1c4dd5e24b8d 238
mhannay3 6:1c4dd5e24b8d 239 // generates a list of possible moves for a piece
mhannay3 6:1c4dd5e24b8d 240 // returns moves
mhannay3 9:1f33c0f299ae 241 std::vector<boardPos> getMoves(boardPos pos)
mhannay3 9:1f33c0f299ae 242 {
mhannay3 9:1f33c0f299ae 243 return getMoves(pos.row, pos.column);
mhannay3 9:1f33c0f299ae 244 }
mhannay3 6:1c4dd5e24b8d 245 std::vector<boardPos> getMoves(int row, int column)
mhannay3 6:1c4dd5e24b8d 246 {
mhannay3 6:1c4dd5e24b8d 247 std::vector<boardPos> moves;
mhannay3 8:928d5d33258f 248 std::vector<boardPos> lineMoves;
mhannay3 6:1c4dd5e24b8d 249 Piece movingPiece = getPiece(row, column);
mus3 11:43c89579ac52 250 uint8_t rowIndex;
mus3 11:43c89579ac52 251 uint8_t columnIndex;
mhannay3 6:1c4dd5e24b8d 252 bool isWhite;
mhannay3 6:1c4dd5e24b8d 253 switch(movingPiece) {
mhannay3 6:1c4dd5e24b8d 254 case wK:
mhannay3 6:1c4dd5e24b8d 255 case bK:
mhannay3 6:1c4dd5e24b8d 256 isWhite = movingPiece == wK;
mus3 11:43c89579ac52 257 if (validMove(isWhite, row + 1, column)) {
mus3 14:f390d08e5f92 258 boardPos tPos = (boardPos) {
mus3 11:43c89579ac52 259 row + 1, column
mus3 14:f390d08e5f92 260 };
mus3 14:f390d08e5f92 261 bool possibleKingMove = true;
mus3 14:f390d08e5f92 262 for (int i = 0; i < 64; i++) {
mus3 14:f390d08e5f92 263 Piece possibleThreat = getPiece(i/8, i%8);
mus3 14:f390d08e5f92 264 if (isWhite && (possibleThreat == bK || possibleThreat == bQ || possibleThreat == bR || possibleThreat == bN || possibleThreat == bB || possibleThreat == b)) {
mus3 14:f390d08e5f92 265 std::vector<boardPos> possibleThreatMoves = getMoves(i/8, i%8);
mus3 14:f390d08e5f92 266 if (std::find(possibleThreatMoves.begin(), possibleThreatMoves.end(), tPos) != possibleThreatMoves.end()) {
mus3 14:f390d08e5f92 267 possibleKingMove = false;
mus3 14:f390d08e5f92 268 break;
mus3 14:f390d08e5f92 269 }
mus3 14:f390d08e5f92 270 } else if (!isWhite && (possibleThreat == wK || possibleThreat == wQ || possibleThreat == wR || possibleThreat == wN || possibleThreat == wB || possibleThreat == w)) {
mus3 14:f390d08e5f92 271 std::vector<boardPos> possibleThreatMoves = getMoves(i/8, i%8);
mus3 14:f390d08e5f92 272 if (std::find(possibleThreatMoves.begin(), possibleThreatMoves.end(), tPos) != possibleThreatMoves.end()) {
mus3 14:f390d08e5f92 273 possibleKingMove = false;
mus3 14:f390d08e5f92 274 break;
mus3 14:f390d08e5f92 275 }
mus3 14:f390d08e5f92 276 }
mus3 14:f390d08e5f92 277 }
mus3 14:f390d08e5f92 278 if (possibleKingMove) {
mus3 14:f390d08e5f92 279 moves.push_back(tPos);
mus3 14:f390d08e5f92 280 }
mus3 11:43c89579ac52 281 }
mus3 11:43c89579ac52 282 if (validMove(isWhite, row, column + 1)) {
mus3 14:f390d08e5f92 283 boardPos tPos = (boardPos) {
mus3 11:43c89579ac52 284 row, column + 1
mus3 14:f390d08e5f92 285 };
mus3 14:f390d08e5f92 286 bool possibleKingMove = true;
mus3 14:f390d08e5f92 287 for (int i = 0; i < 64; i++) {
mus3 14:f390d08e5f92 288 Piece possibleThreat = getPiece(i/8, i%8);
mus3 14:f390d08e5f92 289 if (isWhite && (possibleThreat == bK || possibleThreat == bQ || possibleThreat == bR || possibleThreat == bN || possibleThreat == bB || possibleThreat == b)) {
mus3 14:f390d08e5f92 290 std::vector<boardPos> possibleThreatMoves = getMoves(i/8, i%8);
mus3 14:f390d08e5f92 291 if (std::find(possibleThreatMoves.begin(), possibleThreatMoves.end(), tPos) != possibleThreatMoves.end()) {
mus3 14:f390d08e5f92 292 possibleKingMove = false;
mus3 14:f390d08e5f92 293 break;
mus3 14:f390d08e5f92 294 }
mus3 14:f390d08e5f92 295 } else if (!isWhite && (possibleThreat == wK || possibleThreat == wQ || possibleThreat == wR || possibleThreat == wN || possibleThreat == wB || possibleThreat == w)) {
mus3 14:f390d08e5f92 296 std::vector<boardPos> possibleThreatMoves = getMoves(i/8, i%8);
mus3 14:f390d08e5f92 297 if (std::find(possibleThreatMoves.begin(), possibleThreatMoves.end(), tPos) != possibleThreatMoves.end()) {
mus3 14:f390d08e5f92 298 possibleKingMove = false;
mus3 14:f390d08e5f92 299 break;
mus3 14:f390d08e5f92 300 }
mus3 14:f390d08e5f92 301 }
mus3 14:f390d08e5f92 302 }
mus3 14:f390d08e5f92 303 if (possibleKingMove) {
mus3 14:f390d08e5f92 304 moves.push_back(tPos);
mus3 14:f390d08e5f92 305 }
mus3 11:43c89579ac52 306 }
mus3 11:43c89579ac52 307 if (validMove(isWhite, row - 1, column)) {
mus3 14:f390d08e5f92 308 boardPos tPos = (boardPos) {
mus3 11:43c89579ac52 309 row - 1, column
mus3 14:f390d08e5f92 310 };
mus3 14:f390d08e5f92 311 bool possibleKingMove = true;
mus3 14:f390d08e5f92 312 for (int i = 0; i < 64; i++) {
mus3 14:f390d08e5f92 313 Piece possibleThreat = getPiece(i/8, i%8);
mus3 14:f390d08e5f92 314 if (isWhite && (possibleThreat == bK || possibleThreat == bQ || possibleThreat == bR || possibleThreat == bN || possibleThreat == bB || possibleThreat == b)) {
mus3 14:f390d08e5f92 315 std::vector<boardPos> possibleThreatMoves = getMoves(i/8, i%8);
mus3 14:f390d08e5f92 316 if (std::find(possibleThreatMoves.begin(), possibleThreatMoves.end(), tPos) != possibleThreatMoves.end()) {
mus3 14:f390d08e5f92 317 possibleKingMove = false;
mus3 14:f390d08e5f92 318 break;
mus3 14:f390d08e5f92 319 }
mus3 14:f390d08e5f92 320 } else if (!isWhite && (possibleThreat == wK || possibleThreat == wQ || possibleThreat == wR || possibleThreat == wN || possibleThreat == wB || possibleThreat == w)) {
mus3 14:f390d08e5f92 321 std::vector<boardPos> possibleThreatMoves = getMoves(i/8, i%8);
mus3 14:f390d08e5f92 322 if (std::find(possibleThreatMoves.begin(), possibleThreatMoves.end(), tPos) != possibleThreatMoves.end()) {
mus3 14:f390d08e5f92 323 possibleKingMove = false;
mus3 14:f390d08e5f92 324 break;
mus3 14:f390d08e5f92 325 }
mus3 14:f390d08e5f92 326 }
mus3 14:f390d08e5f92 327 }
mus3 14:f390d08e5f92 328 if (possibleKingMove) {
mus3 14:f390d08e5f92 329 moves.push_back(tPos);
mus3 14:f390d08e5f92 330 }
mus3 11:43c89579ac52 331 }
mus3 11:43c89579ac52 332 if (validMove(isWhite, row, column - 1)) {
mus3 14:f390d08e5f92 333 boardPos tPos = (boardPos) {
mus3 11:43c89579ac52 334 row, column - 1
mus3 14:f390d08e5f92 335 };
mus3 14:f390d08e5f92 336 bool possibleKingMove = true;
mus3 14:f390d08e5f92 337 for (int i = 0; i < 64; i++) {
mus3 14:f390d08e5f92 338 Piece possibleThreat = getPiece(i/8, i%8);
mus3 14:f390d08e5f92 339 if (isWhite && (possibleThreat == bK || possibleThreat == bQ || possibleThreat == bR || possibleThreat == bN || possibleThreat == bB || possibleThreat == b)) {
mus3 14:f390d08e5f92 340 std::vector<boardPos> possibleThreatMoves = getMoves(i/8, i%8);
mus3 14:f390d08e5f92 341 if (std::find(possibleThreatMoves.begin(), possibleThreatMoves.end(), tPos) != possibleThreatMoves.end()) {
mus3 14:f390d08e5f92 342 possibleKingMove = false;
mus3 14:f390d08e5f92 343 break;
mus3 14:f390d08e5f92 344 }
mus3 14:f390d08e5f92 345 } else if (!isWhite && (possibleThreat == wK || possibleThreat == wQ || possibleThreat == wR || possibleThreat == wN || possibleThreat == wB || possibleThreat == w)) {
mus3 14:f390d08e5f92 346 std::vector<boardPos> possibleThreatMoves = getMoves(i/8, i%8);
mus3 14:f390d08e5f92 347 if (std::find(possibleThreatMoves.begin(), possibleThreatMoves.end(), tPos) != possibleThreatMoves.end()) {
mus3 14:f390d08e5f92 348 possibleKingMove = false;
mus3 14:f390d08e5f92 349 break;
mus3 14:f390d08e5f92 350 }
mus3 14:f390d08e5f92 351 }
mus3 14:f390d08e5f92 352 }
mus3 14:f390d08e5f92 353 if (possibleKingMove) {
mus3 14:f390d08e5f92 354 moves.push_back(tPos);
mus3 14:f390d08e5f92 355 }
mus3 11:43c89579ac52 356 }
mus3 11:43c89579ac52 357 if (validMove(isWhite, row + 1, column + 1)) {
mus3 14:f390d08e5f92 358 boardPos tPos = (boardPos) {
mus3 11:43c89579ac52 359 row + 1, column + 1
mus3 14:f390d08e5f92 360 };
mus3 14:f390d08e5f92 361 bool possibleKingMove = true;
mus3 14:f390d08e5f92 362 for (int i = 0; i < 64; i++) {
mus3 14:f390d08e5f92 363 Piece possibleThreat = getPiece(i/8, i%8);
mus3 14:f390d08e5f92 364 if (isWhite && (possibleThreat == bK || possibleThreat == bQ || possibleThreat == bR || possibleThreat == bN || possibleThreat == bB || possibleThreat == b)) {
mus3 14:f390d08e5f92 365 std::vector<boardPos> possibleThreatMoves = getMoves(i/8, i%8);
mus3 14:f390d08e5f92 366 if (std::find(possibleThreatMoves.begin(), possibleThreatMoves.end(), tPos) != possibleThreatMoves.end()) {
mus3 14:f390d08e5f92 367 possibleKingMove = false;
mus3 14:f390d08e5f92 368 break;
mus3 14:f390d08e5f92 369 }
mus3 14:f390d08e5f92 370 } else if (!isWhite && (possibleThreat == wK || possibleThreat == wQ || possibleThreat == wR || possibleThreat == wN || possibleThreat == wB || possibleThreat == w)) {
mus3 14:f390d08e5f92 371 std::vector<boardPos> possibleThreatMoves = getMoves(i/8, i%8);
mus3 14:f390d08e5f92 372 if (std::find(possibleThreatMoves.begin(), possibleThreatMoves.end(), tPos) != possibleThreatMoves.end()) {
mus3 14:f390d08e5f92 373 possibleKingMove = false;
mus3 14:f390d08e5f92 374 break;
mus3 14:f390d08e5f92 375 }
mus3 14:f390d08e5f92 376 }
mus3 14:f390d08e5f92 377 }
mus3 14:f390d08e5f92 378 if (possibleKingMove) {
mus3 14:f390d08e5f92 379 moves.push_back(tPos);
mus3 14:f390d08e5f92 380 }
mus3 11:43c89579ac52 381 }
mus3 11:43c89579ac52 382 if (validMove(isWhite, row - 1, column + 1)) {
mus3 14:f390d08e5f92 383 boardPos tPos = (boardPos) {
mus3 11:43c89579ac52 384 row - 1, column + 1
mus3 14:f390d08e5f92 385 };
mus3 14:f390d08e5f92 386 bool possibleKingMove = true;
mus3 14:f390d08e5f92 387 for (int i = 0; i < 64; i++) {
mus3 14:f390d08e5f92 388 Piece possibleThreat = getPiece(i/8, i%8);
mus3 14:f390d08e5f92 389 if (isWhite && (possibleThreat == bK || possibleThreat == bQ || possibleThreat == bR || possibleThreat == bN || possibleThreat == bB || possibleThreat == b)) {
mus3 14:f390d08e5f92 390 std::vector<boardPos> possibleThreatMoves = getMoves(i/8, i%8);
mus3 14:f390d08e5f92 391 if (std::find(possibleThreatMoves.begin(), possibleThreatMoves.end(), tPos) != possibleThreatMoves.end()) {
mus3 14:f390d08e5f92 392 possibleKingMove = false;
mus3 14:f390d08e5f92 393 break;
mus3 14:f390d08e5f92 394 }
mus3 14:f390d08e5f92 395 } else if (!isWhite && (possibleThreat == wK || possibleThreat == wQ || possibleThreat == wR || possibleThreat == wN || possibleThreat == wB || possibleThreat == w)) {
mus3 14:f390d08e5f92 396 std::vector<boardPos> possibleThreatMoves = getMoves(i/8, i%8);
mus3 14:f390d08e5f92 397 if (std::find(possibleThreatMoves.begin(), possibleThreatMoves.end(), tPos) != possibleThreatMoves.end()) {
mus3 14:f390d08e5f92 398 possibleKingMove = false;
mus3 14:f390d08e5f92 399 break;
mus3 14:f390d08e5f92 400 }
mus3 14:f390d08e5f92 401 }
mus3 14:f390d08e5f92 402 }
mus3 14:f390d08e5f92 403 if (possibleKingMove) {
mus3 14:f390d08e5f92 404 moves.push_back(tPos);
mus3 14:f390d08e5f92 405 }
mus3 11:43c89579ac52 406 }
mus3 11:43c89579ac52 407 if (validMove(isWhite, row - 1, column - 1)) {
mus3 14:f390d08e5f92 408 boardPos tPos = (boardPos) {
mus3 11:43c89579ac52 409 row - 1, column - 1
mus3 14:f390d08e5f92 410 };
mus3 14:f390d08e5f92 411 bool possibleKingMove = true;
mus3 14:f390d08e5f92 412 for (int i = 0; i < 64; i++) {
mus3 14:f390d08e5f92 413 Piece possibleThreat = getPiece(i/8, i%8);
mus3 14:f390d08e5f92 414 if (isWhite && (possibleThreat == bK || possibleThreat == bQ || possibleThreat == bR || possibleThreat == bN || possibleThreat == bB || possibleThreat == b)) {
mus3 14:f390d08e5f92 415 std::vector<boardPos> possibleThreatMoves = getMoves(i/8, i%8);
mus3 14:f390d08e5f92 416 if (std::find(possibleThreatMoves.begin(), possibleThreatMoves.end(), tPos) != possibleThreatMoves.end()) {
mus3 14:f390d08e5f92 417 possibleKingMove = false;
mus3 14:f390d08e5f92 418 break;
mus3 14:f390d08e5f92 419 }
mus3 14:f390d08e5f92 420 } else if (!isWhite && (possibleThreat == wK || possibleThreat == wQ || possibleThreat == wR || possibleThreat == wN || possibleThreat == wB || possibleThreat == w)) {
mus3 14:f390d08e5f92 421 std::vector<boardPos> possibleThreatMoves = getMoves(i/8, i%8);
mus3 14:f390d08e5f92 422 if (std::find(possibleThreatMoves.begin(), possibleThreatMoves.end(), tPos) != possibleThreatMoves.end()) {
mus3 14:f390d08e5f92 423 possibleKingMove = false;
mus3 14:f390d08e5f92 424 break;
mus3 14:f390d08e5f92 425 }
mus3 14:f390d08e5f92 426 }
mus3 14:f390d08e5f92 427 }
mus3 14:f390d08e5f92 428 if (possibleKingMove) {
mus3 14:f390d08e5f92 429 moves.push_back(tPos);
mus3 14:f390d08e5f92 430 }
mus3 11:43c89579ac52 431 }
mus3 11:43c89579ac52 432 if (validMove(isWhite, row + 1, column - 1)) {
mus3 14:f390d08e5f92 433 boardPos tPos = (boardPos) {
mus3 11:43c89579ac52 434 row + 1, column - 1
mus3 14:f390d08e5f92 435 };
mus3 14:f390d08e5f92 436 bool possibleKingMove = true;
mus3 14:f390d08e5f92 437 for (int i = 0; i < 64; i++) {
mus3 14:f390d08e5f92 438 Piece possibleThreat = getPiece(i/8, i%8);
mus3 14:f390d08e5f92 439 if (isWhite && (possibleThreat == bK || possibleThreat == bQ || possibleThreat == bR || possibleThreat == bN || possibleThreat == bB || possibleThreat == b)) {
mus3 14:f390d08e5f92 440 std::vector<boardPos> possibleThreatMoves = getMoves(i/8, i%8);
mus3 14:f390d08e5f92 441 if (std::find(possibleThreatMoves.begin(), possibleThreatMoves.end(), tPos) != possibleThreatMoves.end()) {
mus3 14:f390d08e5f92 442 possibleKingMove = false;
mus3 14:f390d08e5f92 443 break;
mus3 14:f390d08e5f92 444 }
mus3 14:f390d08e5f92 445 } else if (!isWhite && (possibleThreat == wK || possibleThreat == wQ || possibleThreat == wR || possibleThreat == wN || possibleThreat == wB || possibleThreat == w)) {
mus3 14:f390d08e5f92 446 std::vector<boardPos> possibleThreatMoves = getMoves(i/8, i%8);
mus3 14:f390d08e5f92 447 if (std::find(possibleThreatMoves.begin(), possibleThreatMoves.end(), tPos) != possibleThreatMoves.end()) {
mus3 14:f390d08e5f92 448 possibleKingMove = false;
mus3 14:f390d08e5f92 449 break;
mus3 14:f390d08e5f92 450 }
mus3 14:f390d08e5f92 451 }
mus3 14:f390d08e5f92 452 }
mus3 14:f390d08e5f92 453 if (possibleKingMove) {
mus3 14:f390d08e5f92 454 moves.push_back(tPos);
mus3 14:f390d08e5f92 455 }
mhannay3 6:1c4dd5e24b8d 456 }
mhannay3 6:1c4dd5e24b8d 457 break;
mhannay3 6:1c4dd5e24b8d 458 case wQ:
mhannay3 6:1c4dd5e24b8d 459 case bQ:
mhannay3 6:1c4dd5e24b8d 460 isWhite = movingPiece == wQ;
mus3 11:43c89579ac52 461 rowIndex = row + 1;
mus3 11:43c89579ac52 462 columnIndex = column + 1;
mus3 11:43c89579ac52 463 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 464 moves.push_back((boardPos) {
mus3 11:43c89579ac52 465 rowIndex, columnIndex
mus3 11:43c89579ac52 466 });
mus3 11:43c89579ac52 467 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 468 break;
mus3 11:43c89579ac52 469 }
mus3 11:43c89579ac52 470 rowIndex++;
mus3 11:43c89579ac52 471 columnIndex++;
mus3 11:43c89579ac52 472 }
mus3 11:43c89579ac52 473 rowIndex = row - 1;
mus3 11:43c89579ac52 474 columnIndex = column + 1;
mus3 11:43c89579ac52 475 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 476 moves.push_back((boardPos) {
mus3 11:43c89579ac52 477 rowIndex, columnIndex
mus3 11:43c89579ac52 478 });
mus3 11:43c89579ac52 479 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 480 break;
mus3 11:43c89579ac52 481 }
mus3 11:43c89579ac52 482 rowIndex--;
mus3 11:43c89579ac52 483 columnIndex++;
mus3 11:43c89579ac52 484 }
mus3 11:43c89579ac52 485 rowIndex = row + 1;
mus3 11:43c89579ac52 486 columnIndex = column - 1;
mus3 11:43c89579ac52 487 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 488 moves.push_back((boardPos) {
mus3 11:43c89579ac52 489 rowIndex, columnIndex
mus3 11:43c89579ac52 490 });
mus3 11:43c89579ac52 491 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 492 break;
mus3 11:43c89579ac52 493 }
mus3 11:43c89579ac52 494 rowIndex++;
mus3 11:43c89579ac52 495 columnIndex--;
mus3 11:43c89579ac52 496 }
mus3 11:43c89579ac52 497 rowIndex = row - 1;
mus3 11:43c89579ac52 498 columnIndex = column - 1;
mus3 11:43c89579ac52 499 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 500 moves.push_back((boardPos) {
mus3 11:43c89579ac52 501 rowIndex, columnIndex
mus3 11:43c89579ac52 502 });
mus3 11:43c89579ac52 503 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 504 break;
mus3 11:43c89579ac52 505 }
mus3 11:43c89579ac52 506 rowIndex--;
mus3 11:43c89579ac52 507 columnIndex--;
mus3 11:43c89579ac52 508 }
mus3 11:43c89579ac52 509 rowIndex = row + 1;
mus3 11:43c89579ac52 510 columnIndex = column;
mus3 11:43c89579ac52 511 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 512 moves.push_back((boardPos) {
mus3 11:43c89579ac52 513 rowIndex, columnIndex
mus3 11:43c89579ac52 514 });
mus3 11:43c89579ac52 515 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 516 break;
mus3 11:43c89579ac52 517 }
mus3 11:43c89579ac52 518 rowIndex++;
mus3 11:43c89579ac52 519 }
mus3 11:43c89579ac52 520 rowIndex = row - 1;
mus3 11:43c89579ac52 521 columnIndex = column;
mus3 11:43c89579ac52 522 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 523 moves.push_back((boardPos) {
mus3 11:43c89579ac52 524 rowIndex, columnIndex
mus3 11:43c89579ac52 525 });
mus3 11:43c89579ac52 526 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 527 break;
mus3 11:43c89579ac52 528 }
mus3 11:43c89579ac52 529 rowIndex--;
mus3 11:43c89579ac52 530 }
mus3 11:43c89579ac52 531 rowIndex = row;
mus3 11:43c89579ac52 532 columnIndex = column + 1;
mus3 11:43c89579ac52 533 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 534 moves.push_back((boardPos) {
mus3 11:43c89579ac52 535 rowIndex, columnIndex
mus3 11:43c89579ac52 536 });
mus3 11:43c89579ac52 537 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 538 break;
mus3 11:43c89579ac52 539 }
mus3 11:43c89579ac52 540 columnIndex++;
mus3 11:43c89579ac52 541 }
mus3 11:43c89579ac52 542 rowIndex = row;
mus3 11:43c89579ac52 543 columnIndex = column - 1;
mus3 11:43c89579ac52 544 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 545 moves.push_back((boardPos) {
mus3 11:43c89579ac52 546 rowIndex, columnIndex
mus3 11:43c89579ac52 547 });
mus3 11:43c89579ac52 548 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 549 break;
mus3 11:43c89579ac52 550 }
mus3 11:43c89579ac52 551 columnIndex--;
mus3 11:43c89579ac52 552 }
mhannay3 6:1c4dd5e24b8d 553 break;
mhannay3 6:1c4dd5e24b8d 554 case wB:
mhannay3 6:1c4dd5e24b8d 555 case bB:
mhannay3 6:1c4dd5e24b8d 556 isWhite = movingPiece == wB;
mus3 11:43c89579ac52 557 rowIndex = row + 1;
mus3 11:43c89579ac52 558 columnIndex = column + 1;
mus3 11:43c89579ac52 559 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 560 moves.push_back((boardPos) {
mus3 11:43c89579ac52 561 rowIndex, columnIndex
mus3 11:43c89579ac52 562 });
mus3 11:43c89579ac52 563 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 564 break;
mus3 11:43c89579ac52 565 }
mus3 11:43c89579ac52 566 rowIndex++;
mus3 11:43c89579ac52 567 columnIndex++;
mus3 11:43c89579ac52 568 }
mus3 11:43c89579ac52 569 rowIndex = row - 1;
mus3 11:43c89579ac52 570 columnIndex = column + 1;
mus3 11:43c89579ac52 571 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 572 moves.push_back((boardPos) {
mus3 11:43c89579ac52 573 rowIndex, columnIndex
mus3 11:43c89579ac52 574 });
mus3 11:43c89579ac52 575 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 576 break;
mus3 11:43c89579ac52 577 }
mus3 11:43c89579ac52 578 rowIndex--;
mus3 11:43c89579ac52 579 columnIndex++;
mus3 11:43c89579ac52 580 }
mus3 11:43c89579ac52 581 rowIndex = row + 1;
mus3 11:43c89579ac52 582 columnIndex = column - 1;
mus3 11:43c89579ac52 583 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 584 moves.push_back((boardPos) {
mus3 11:43c89579ac52 585 rowIndex, columnIndex
mus3 11:43c89579ac52 586 });
mus3 11:43c89579ac52 587 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 588 break;
mus3 11:43c89579ac52 589 }
mus3 11:43c89579ac52 590 rowIndex++;
mus3 11:43c89579ac52 591 columnIndex--;
mus3 11:43c89579ac52 592 }
mus3 11:43c89579ac52 593 rowIndex = row - 1;
mus3 11:43c89579ac52 594 columnIndex = column - 1;
mus3 11:43c89579ac52 595 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 596 moves.push_back((boardPos) {
mus3 11:43c89579ac52 597 rowIndex, columnIndex
mus3 11:43c89579ac52 598 });
mus3 11:43c89579ac52 599 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 600 break;
mus3 11:43c89579ac52 601 }
mus3 11:43c89579ac52 602 rowIndex--;
mus3 11:43c89579ac52 603 columnIndex--;
mus3 11:43c89579ac52 604 }
mhannay3 6:1c4dd5e24b8d 605 break;
mhannay3 6:1c4dd5e24b8d 606 case wN:
mhannay3 6:1c4dd5e24b8d 607 case bN:
mhannay3 6:1c4dd5e24b8d 608 isWhite = movingPiece == wN;
mus3 11:43c89579ac52 609 if (validMove(isWhite, row + 2, column + 1)) {
mus3 11:43c89579ac52 610 moves.push_back((boardPos) {
mus3 11:43c89579ac52 611 row + 2, column + 1
mus3 11:43c89579ac52 612 });
mus3 11:43c89579ac52 613 }
mus3 11:43c89579ac52 614 if (validMove(isWhite, row + 2, column - 1)) {
mus3 11:43c89579ac52 615 moves.push_back((boardPos) {
mus3 11:43c89579ac52 616 row + 2, column - 1
mus3 11:43c89579ac52 617 });
mus3 11:43c89579ac52 618 }
mus3 11:43c89579ac52 619 if (validMove(isWhite, row - 2, column - 1)) {
mus3 11:43c89579ac52 620 moves.push_back((boardPos) {
mus3 11:43c89579ac52 621 row - 2, column - 1
mus3 11:43c89579ac52 622 });
mus3 11:43c89579ac52 623 }
mus3 11:43c89579ac52 624 if (validMove(isWhite, row - 2, column + 1)) {
mus3 11:43c89579ac52 625 moves.push_back((boardPos) {
mus3 11:43c89579ac52 626 row - 2, column + 1
mus3 11:43c89579ac52 627 });
mus3 11:43c89579ac52 628 }
mus3 11:43c89579ac52 629 if (validMove(isWhite, row + 1, column + 2)) {
mus3 11:43c89579ac52 630 moves.push_back((boardPos) {
mus3 11:43c89579ac52 631 row + 1, column + 2
mus3 11:43c89579ac52 632 });
mus3 11:43c89579ac52 633 }
mus3 11:43c89579ac52 634 if (validMove(isWhite, row - 1, column + 2)) {
mus3 11:43c89579ac52 635 moves.push_back((boardPos) {
mus3 11:43c89579ac52 636 row - 1, column + 2
mus3 11:43c89579ac52 637 });
mus3 11:43c89579ac52 638 }
mus3 11:43c89579ac52 639 if (validMove(isWhite, row - 1, column - 2)) {
mus3 11:43c89579ac52 640 moves.push_back((boardPos) {
mus3 11:43c89579ac52 641 row - 1, column - 2
mus3 11:43c89579ac52 642 });
mus3 11:43c89579ac52 643 }
mus3 11:43c89579ac52 644 if (validMove(isWhite, row + 1, column - 2)) {
mus3 11:43c89579ac52 645 moves.push_back((boardPos) {
mus3 11:43c89579ac52 646 row + 1, column - 2
mus3 11:43c89579ac52 647 });
mhannay3 6:1c4dd5e24b8d 648 }
mhannay3 6:1c4dd5e24b8d 649 break;
mhannay3 6:1c4dd5e24b8d 650 case wR:
mhannay3 6:1c4dd5e24b8d 651 case bR:
mhannay3 6:1c4dd5e24b8d 652 isWhite = movingPiece == wR;
mus3 11:43c89579ac52 653 rowIndex = row + 1;
mus3 11:43c89579ac52 654 columnIndex = column;
mus3 11:43c89579ac52 655 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 656 moves.push_back((boardPos) {
mus3 11:43c89579ac52 657 rowIndex, columnIndex
mus3 11:43c89579ac52 658 });
mus3 11:43c89579ac52 659 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 660 break;
mus3 11:43c89579ac52 661 }
mus3 11:43c89579ac52 662 rowIndex++;
mus3 11:43c89579ac52 663 }
mus3 11:43c89579ac52 664 rowIndex = row - 1;
mus3 11:43c89579ac52 665 columnIndex = column;
mus3 11:43c89579ac52 666 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 667 moves.push_back((boardPos) {
mus3 11:43c89579ac52 668 rowIndex, columnIndex
mus3 11:43c89579ac52 669 });
mus3 11:43c89579ac52 670 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 671 break;
mus3 11:43c89579ac52 672 }
mus3 11:43c89579ac52 673 rowIndex--;
mus3 11:43c89579ac52 674 }
mus3 11:43c89579ac52 675 rowIndex = row;
mus3 11:43c89579ac52 676 columnIndex = column + 1;
mus3 11:43c89579ac52 677 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 678 moves.push_back((boardPos) {
mus3 11:43c89579ac52 679 rowIndex, columnIndex
mus3 11:43c89579ac52 680 });
mus3 11:43c89579ac52 681 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 682 break;
mus3 11:43c89579ac52 683 }
mus3 11:43c89579ac52 684 columnIndex++;
mus3 11:43c89579ac52 685 }
mus3 11:43c89579ac52 686 rowIndex = row;
mus3 11:43c89579ac52 687 columnIndex = column - 1;
mus3 11:43c89579ac52 688 while (validMove(isWhite, rowIndex, columnIndex)) {
mus3 11:43c89579ac52 689 moves.push_back((boardPos) {
mus3 11:43c89579ac52 690 rowIndex, columnIndex
mus3 11:43c89579ac52 691 });
mus3 11:43c89579ac52 692 if (getPiece(rowIndex, columnIndex) != e) {
mus3 11:43c89579ac52 693 break;
mus3 11:43c89579ac52 694 }
mus3 11:43c89579ac52 695 columnIndex--;
mus3 11:43c89579ac52 696 }
mhannay3 6:1c4dd5e24b8d 697 break;
mhannay3 6:1c4dd5e24b8d 698 case w:
mhannay3 6:1c4dd5e24b8d 699 case b:
mhannay3 6:1c4dd5e24b8d 700 isWhite = movingPiece == w;
mhannay3 6:1c4dd5e24b8d 701 int rowChange = isWhite ? 1 : -1;
mus3 11:43c89579ac52 702 if (validMove(isWhite, row + rowChange, column) && getPiece(row + rowChange, column) == e) {
mhannay3 6:1c4dd5e24b8d 703 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 704 row + rowChange, column
mhannay3 6:1c4dd5e24b8d 705 });
mus3 11:43c89579ac52 706 // The case for pawns moving two squares at the start
mus3 11:43c89579ac52 707 if (((isWhite && row == 1) || (!isWhite && row == 6)) && validMove(isWhite, row + rowChange + rowChange, column) && getPiece(row + rowChange + rowChange, column) == e) {
mus3 11:43c89579ac52 708 moves.push_back((boardPos) {
mus3 11:43c89579ac52 709 row + rowChange + rowChange, column
mus3 11:43c89579ac52 710 });
mus3 11:43c89579ac52 711 }
mhannay3 6:1c4dd5e24b8d 712 }
mhannay3 6:1c4dd5e24b8d 713 if (validMove(isWhite, row + rowChange, column + 1) && getPiece(row + rowChange, column + 1) != e) {
mhannay3 6:1c4dd5e24b8d 714 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 715 row + rowChange, column + 1
mhannay3 6:1c4dd5e24b8d 716 });
mhannay3 6:1c4dd5e24b8d 717 }
mhannay3 6:1c4dd5e24b8d 718 if (validMove(isWhite, row + rowChange, column - 1) && getPiece(row + rowChange, column - 1) != e) {
mhannay3 6:1c4dd5e24b8d 719 moves.push_back((boardPos) {
mhannay3 6:1c4dd5e24b8d 720 row + rowChange, column - 1
mhannay3 6:1c4dd5e24b8d 721 });
mhannay3 6:1c4dd5e24b8d 722 }
mhannay3 6:1c4dd5e24b8d 723 break;
mus3 11:43c89579ac52 724 default:
mus3 11:43c89579ac52 725 break;
mhannay3 6:1c4dd5e24b8d 726 }
mhannay3 6:1c4dd5e24b8d 727 return moves;
mhannay3 6:1c4dd5e24b8d 728 }
mhannay3 9:1f33c0f299ae 729
mhannay3 8:928d5d33258f 730 // returns a vector of board positions that a piece can move in a line
mhannay3 8:928d5d33258f 731 std::vector<boardPos> movesInLine(bool isMovingPieceWhite, int row, int column, int rowChange, int columnChange)
mhannay3 8:928d5d33258f 732 {
mhannay3 8:928d5d33258f 733 std::vector<boardPos> moves;
mhannay3 8:928d5d33258f 734 for (int i = 1; ; i++) {
mhannay3 8:928d5d33258f 735 // check if piece can move to location
mhannay3 8:928d5d33258f 736 if (validMove(isMovingPieceWhite, row + i * rowChange, column + i * columnChange)) {
mhannay3 8:928d5d33258f 737 moves.push_back((boardPos) {
mhannay3 8:928d5d33258f 738 row + i * rowChange, column + i * columnChange
mhannay3 8:928d5d33258f 739 });
mhannay3 8:928d5d33258f 740 // if piece is capturable, stop moving beyond it
mhannay3 9:1f33c0f299ae 741 if (getPiece(row + i * rowChange, column + i * columnChange) != e) {
mhannay3 8:928d5d33258f 742 break;
mhannay3 8:928d5d33258f 743 }
mhannay3 9:1f33c0f299ae 744 // if unable to move, break out
mhannay3 8:928d5d33258f 745 } else {
mhannay3 8:928d5d33258f 746 break;
mhannay3 8:928d5d33258f 747 }
mhannay3 8:928d5d33258f 748 }
mhannay3 8:928d5d33258f 749 return moves;
mhannay3 8:928d5d33258f 750 }
mhannay3 6:1c4dd5e24b8d 751
mhannay3 8:928d5d33258f 752 // returns if a piece can move to a given location
mhannay3 6:1c4dd5e24b8d 753 bool validMove(bool isMovingPieceWhite, int row, int column)
mhannay3 6:1c4dd5e24b8d 754 {
mhannay3 6:1c4dd5e24b8d 755 if (row < 0 || row > 7 || column < 0 || column > 7) {
mhannay3 6:1c4dd5e24b8d 756 return false;
mhannay3 6:1c4dd5e24b8d 757 }
mhannay3 6:1c4dd5e24b8d 758 Piece capturedPiece = getPiece(row, column);
mhannay3 6:1c4dd5e24b8d 759 switch(capturedPiece) {
mhannay3 6:1c4dd5e24b8d 760 case wK:
mhannay3 6:1c4dd5e24b8d 761 case wQ:
mhannay3 6:1c4dd5e24b8d 762 case wR:
mhannay3 6:1c4dd5e24b8d 763 case wB:
mhannay3 6:1c4dd5e24b8d 764 case wN:
mhannay3 6:1c4dd5e24b8d 765 case w:
mhannay3 6:1c4dd5e24b8d 766 return !isMovingPieceWhite;
mhannay3 6:1c4dd5e24b8d 767 case bK:
mhannay3 6:1c4dd5e24b8d 768 case bQ:
mhannay3 6:1c4dd5e24b8d 769 case bR:
mhannay3 6:1c4dd5e24b8d 770 case bB:
mhannay3 6:1c4dd5e24b8d 771 case bN:
mhannay3 6:1c4dd5e24b8d 772 case b:
mhannay3 6:1c4dd5e24b8d 773 return isMovingPieceWhite;
mhannay3 6:1c4dd5e24b8d 774 case e:
mhannay3 6:1c4dd5e24b8d 775 return true;
mhannay3 6:1c4dd5e24b8d 776 }
mhannay3 6:1c4dd5e24b8d 777 return false;
mhannay3 6:1c4dd5e24b8d 778 }
mhannay3 5:b553c51b3b85 779 };
mhannay3 1:cd78922f70fa 780
mhannay3 5:b553c51b3b85 781 class GameBoard
mhannay3 5:b553c51b3b85 782 {
mhannay3 5:b553c51b3b85 783 private:
mhannay3 5:b553c51b3b85 784 BoardState boardState;
mhannay3 5:b553c51b3b85 785 static const int BOARD_DARK_COLOR = 0x769656;
mhannay3 5:b553c51b3b85 786 static const int BOARD_LIGHT_COLOR = 0xbaca44;
mhannay3 5:b553c51b3b85 787 static const int HOVER_COLOR = 0x0000ff;
mhannay3 5:b553c51b3b85 788 static const int SELECTED_COLOR = 0xff8800;
mhannay3 6:1c4dd5e24b8d 789 static const int MOVE_COLOR = 0xff00ff;
mus3 11:43c89579ac52 790
mus3 11:43c89579ac52 791 // gets the pixel coordinates of the top left of the square
mus3 11:43c89579ac52 792 static pixelCoord getTopLeftOfSquare(boardPos pos)
mus3 11:43c89579ac52 793 {
mus3 11:43c89579ac52 794 return getTopLeftOfSquare(pos.row, pos.column);
mus3 11:43c89579ac52 795 }
mus3 11:43c89579ac52 796 static pixelCoord getTopLeftOfSquare(int row, int column)
mus3 11:43c89579ac52 797 {
mus3 11:43c89579ac52 798 pixelCoord topLeft;
mus3 11:43c89579ac52 799 topLeft.x = 16 * column;
mus3 11:43c89579ac52 800 topLeft.y = 112 - 16 * row;
mus3 11:43c89579ac52 801 return topLeft;
mhannay3 5:b553c51b3b85 802 }
mhannay3 5:b553c51b3b85 803
mus3 11:43c89579ac52 804 // piece sprites (12 x 12)
mus3 11:43c89579ac52 805 static void drawPawn(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 806 {
mus3 11:43c89579ac52 807 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 808 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 809 int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _,
mus3 11:43c89579ac52 810 _, _, _, _, _, _, _, _, _, _, _, _,
mus3 11:43c89579ac52 811 _, _, _, _, _, _, _, _, _, _, _, _,
mus3 11:43c89579ac52 812 _, _, _, _, _, _, _, _, _, _, _, _,
mus3 11:43c89579ac52 813 _, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 814 _, _, _, _, X, X, X, X, _, _, _, _,
mus3 11:43c89579ac52 815 _, _, _, _, X, X, X, X, _, _, _, _,
mus3 11:43c89579ac52 816 _, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 817 _, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 818 _, _, _, _, X, X, X, X, _, _, _, _,
mus3 11:43c89579ac52 819 _, _, X, X, X, X, X, X, X, X, _, _,
mus3 11:43c89579ac52 820 _, _, X, X, X, X, X, X, X, X, _, _
mus3 11:43c89579ac52 821 };
mus3 11:43c89579ac52 822 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 823 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mus3 11:43c89579ac52 824 }
mus3 11:43c89579ac52 825 static void drawRook(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 826 {
mus3 11:43c89579ac52 827 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 828 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 829 int sprite[144] = {X, X, _, X, X, _, _, X, X, _, X, X,
mus3 11:43c89579ac52 830 X, X, _, X, X, _, _, X, X, _, X, X,
mus3 11:43c89579ac52 831 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 832 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 833 _, X, X, X, _, X, X, _, X, X, X, _,
mus3 11:43c89579ac52 834 _, X, X, X, _, X, X, _, X, X, X, _,
mus3 11:43c89579ac52 835 _, _, X, X, _, X, X, _, X, X, _, _,
mus3 11:43c89579ac52 836 _, _, X, X, _, X, X, _, X, X, _, _,
mus3 11:43c89579ac52 837 _, _, X, X, _, X, X, _, X, X, _, _,
mus3 11:43c89579ac52 838 _, X, X, X, X, X, X, X, X, X, X, _,
mus3 11:43c89579ac52 839 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 840 X, X, X, X, X, X, X, X, X, X, X, X
mus3 11:43c89579ac52 841 };
mus3 11:43c89579ac52 842 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 843 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mus3 11:43c89579ac52 844 }
mus3 11:43c89579ac52 845 static void drawKnight(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 846 {
mus3 11:43c89579ac52 847 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 848 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 849 int sprite[144] = {_, _, _, _, _, _, _, _, _, _, _, _,
mus3 11:43c89579ac52 850 _, _, _, _, _, X, X, _, X, X, _, _,
mus3 11:43c89579ac52 851 _, _, _, _, _, X, X, _, X, X, _, _,
mus3 11:43c89579ac52 852 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 853 _, _, X, X, X, X, X, _, X, _, _, _,
mus3 11:43c89579ac52 854 _, _, X, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 855 _, _, _, _, _, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 856 _, _, _, _, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 857 _, _, _, X, X, X, X, X, X, X, _, _,
mus3 11:43c89579ac52 858 _, _, X, X, X, X, X, X, X, X, _, _,
mus3 11:43c89579ac52 859 _, X, X, X, X, X, X, X, X, X, X, _,
mus3 11:43c89579ac52 860 _, X, X, X, X, X, X, X, X, X, X, _
mus3 11:43c89579ac52 861 };
mus3 11:43c89579ac52 862 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 863 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mus3 11:43c89579ac52 864 }
mus3 11:43c89579ac52 865 static void drawBishop(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 866 {
mus3 11:43c89579ac52 867 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 868 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 869 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 870 _, _, _, _, X, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 871 _, _, _, X, X, X, _, _, X, _, _, _,
mus3 11:43c89579ac52 872 _, _, _, X, X, _, _, X, X, _, _, _,
mus3 11:43c89579ac52 873 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 874 _, _, _, _, X, X, X, X, _, _, _, _,
mus3 11:43c89579ac52 875 _, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 876 _, _, _, _, X, X, X, X, _, _, _, _,
mus3 11:43c89579ac52 877 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 878 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 879 _, _, X, X, X, X, X, X, X, X, _, _,
mus3 11:43c89579ac52 880 _, _, X, X, X, X, X, X, X, X, _, _
mus3 11:43c89579ac52 881 };
mus3 11:43c89579ac52 882 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 883 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mus3 11:43c89579ac52 884 }
mus3 11:43c89579ac52 885 static void drawQueen(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 886 {
mus3 11:43c89579ac52 887 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 888 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 889 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 890 _, _, X, _, _, X, X, _, _, X, _, _,
mus3 11:43c89579ac52 891 X, _, X, X, _, X, X, _, X, X, _, X,
mus3 11:43c89579ac52 892 X, _, X, X, _, X, X, _, X, X, _, X,
mus3 11:43c89579ac52 893 X, _, X, X, _, X, X, _, X, X, _, X,
mus3 11:43c89579ac52 894 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 895 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 896 X, X, _, X, X, X, X, X, X, _, X, X,
mus3 11:43c89579ac52 897 X, X, X, X, _, X, X, _, X, X, X, X,
mus3 11:43c89579ac52 898 _, X, X, X, X, X, X, X, X, X, X, _,
mus3 11:43c89579ac52 899 _, _, X, X, X, X, X, X, X, X, _, _,
mus3 11:43c89579ac52 900 _, X, X, X, X, X, X, X, X, X, X, _
mus3 11:43c89579ac52 901 };
mus3 11:43c89579ac52 902 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 903 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mus3 11:43c89579ac52 904 }
mus3 11:43c89579ac52 905 static void drawKing(int row, int column, bool white, bool light)
mus3 11:43c89579ac52 906 {
mus3 11:43c89579ac52 907 int X = white ? 0xffffff : 0x000000;
mus3 11:43c89579ac52 908 int _ = light ? BOARD_LIGHT_COLOR : BOARD_DARK_COLOR;
mus3 11:43c89579ac52 909 int sprite[144] = {_, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 910 _, _, _, _, _, X, X, _, _, _, _, _,
mus3 11:43c89579ac52 911 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 912 _, _, _, X, X, X, X, X, X, _, _, _,
mus3 11:43c89579ac52 913 X, X, _, _, _, X, X, _, _, _, X, X,
mus3 11:43c89579ac52 914 X, X, X, X, _, X, X, _, X, X, X, X,
mus3 11:43c89579ac52 915 X, _, X, X, X, X, X, X, X, X, _, X,
mus3 11:43c89579ac52 916 X, X, X, X, X, X, X, X, X, X, X, X,
mus3 11:43c89579ac52 917 X, X, X, _, X, X, X, X, _, X, X, X,
mus3 11:43c89579ac52 918 _, X, X, X, X, X, X, X, X, X, X, _,
mus3 11:43c89579ac52 919 _, _, X, X, X, _, _, X, X, X, _, _,
mus3 11:43c89579ac52 920 _, X, X, X, X, X, X, X, X, X, X, _
mus3 11:43c89579ac52 921 };
mus3 11:43c89579ac52 922 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 923 uLCD.BLIT(tl.x + 2, tl.y + 2, 12, 12, sprite);
mhannay3 5:b553c51b3b85 924 }
mhannay3 5:b553c51b3b85 925
mhannay3 5:b553c51b3b85 926 public:
mhannay3 5:b553c51b3b85 927 BoardState getBoardState()
mhannay3 5:b553c51b3b85 928 {
mhannay3 5:b553c51b3b85 929 return boardState;
mhannay3 5:b553c51b3b85 930 }
mhannay3 5:b553c51b3b85 931
mhannay3 5:b553c51b3b85 932 void setBoardState(BoardState newBoardState)
mhannay3 5:b553c51b3b85 933 {
mhannay3 5:b553c51b3b85 934 boardState = newBoardState;
mhannay3 5:b553c51b3b85 935 }
mhannay3 5:b553c51b3b85 936
mhannay3 5:b553c51b3b85 937 // initializes the starting board state
mhannay3 5:b553c51b3b85 938 GameBoard()
mhannay3 5:b553c51b3b85 939 {
mhannay3 5:b553c51b3b85 940 // draw board
mhannay3 5:b553c51b3b85 941 for (int row = 0; row < 8; row++) {
mhannay3 5:b553c51b3b85 942 for (int column = 0; column < 8; column++) {
mhannay3 5:b553c51b3b85 943 uint64_t color;
mhannay3 5:b553c51b3b85 944 if ((row + column) % 2 == 0) {
mhannay3 5:b553c51b3b85 945 color = BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 946 } else {
mhannay3 5:b553c51b3b85 947 color = BOARD_LIGHT_COLOR;
mhannay3 5:b553c51b3b85 948 }
mhannay3 6:1c4dd5e24b8d 949 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 950 uLCD.filled_rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, color);
mhannay3 5:b553c51b3b85 951 }
mhannay3 5:b553c51b3b85 952 }
mhannay3 5:b553c51b3b85 953 // draw pieces
mhannay3 5:b553c51b3b85 954 placePieceAndDraw(wR, 0, 0);
mhannay3 5:b553c51b3b85 955 placePieceAndDraw(wN, 0, 1);
mhannay3 5:b553c51b3b85 956 placePieceAndDraw(wB, 0, 2);
mhannay3 5:b553c51b3b85 957 placePieceAndDraw(wQ, 0, 3);
mhannay3 5:b553c51b3b85 958 placePieceAndDraw(wK, 0, 4);
mhannay3 5:b553c51b3b85 959 placePieceAndDraw(wB, 0, 5);
mhannay3 5:b553c51b3b85 960 placePieceAndDraw(wN, 0, 6);
mhannay3 5:b553c51b3b85 961 placePieceAndDraw(wR, 0, 7);
mhannay3 5:b553c51b3b85 962 placePieceAndDraw(bR, 7, 0);
mhannay3 5:b553c51b3b85 963 placePieceAndDraw(bN, 7, 1);
mhannay3 5:b553c51b3b85 964 placePieceAndDraw(bB, 7, 2);
mhannay3 5:b553c51b3b85 965 placePieceAndDraw(bQ, 7, 3);
mhannay3 5:b553c51b3b85 966 placePieceAndDraw(bK, 7, 4);
mhannay3 5:b553c51b3b85 967 placePieceAndDraw(bB, 7, 5);
mhannay3 5:b553c51b3b85 968 placePieceAndDraw(bN, 7, 6);
mhannay3 5:b553c51b3b85 969 placePieceAndDraw(bR, 7, 7);
mhannay3 5:b553c51b3b85 970 for (int i = 0; i < 8; i++) {
mhannay3 5:b553c51b3b85 971 placePieceAndDraw(w, 1, i);
mhannay3 5:b553c51b3b85 972 placePieceAndDraw(b, 6, i);
mhannay3 5:b553c51b3b85 973 }
mhannay3 5:b553c51b3b85 974 }
mhannay3 5:b553c51b3b85 975
mhannay3 5:b553c51b3b85 976 // PIECE MOVEMENT AND GRAPHICS FUNCTIONS
mhannay3 5:b553c51b3b85 977
mhannay3 5:b553c51b3b85 978 // returns the piece at a given location
mhannay3 7:9e01c9a334dc 979 Piece getPiece(boardPos pos)
mhannay3 7:9e01c9a334dc 980 {
mhannay3 7:9e01c9a334dc 981 return getPiece(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 982 }
mhannay3 5:b553c51b3b85 983 Piece getPiece(int row, int column)
mhannay3 5:b553c51b3b85 984 {
mhannay3 5:b553c51b3b85 985 return boardState.getPiece(row, column);
mhannay3 5:b553c51b3b85 986 }
mhannay3 5:b553c51b3b85 987
mhannay3 5:b553c51b3b85 988 /* puts the bit representation of a piece at the set position of the board
mhannay3 5:b553c51b3b85 989 assumes that the position of the board is emptied beforehand
mhannay3 5:b553c51b3b85 990 */
mhannay3 7:9e01c9a334dc 991 void placePieceAndDraw(Piece piece, boardPos pos)
mhannay3 7:9e01c9a334dc 992 {
mhannay3 7:9e01c9a334dc 993 placePieceAndDraw(piece, pos.row, pos.column);
mhannay3 7:9e01c9a334dc 994 }
mhannay3 5:b553c51b3b85 995 void placePieceAndDraw(Piece piece, int row, int column)
mhannay3 5:b553c51b3b85 996 {
mhannay3 5:b553c51b3b85 997 boardState.placePiece(piece, row, column);
mhannay3 6:1c4dd5e24b8d 998 pixelCoord tl = getTopLeftOfSquare(row, column);
mus3 11:43c89579ac52 999 switch(piece) {
mus3 11:43c89579ac52 1000 case wK:
mus3 11:43c89579ac52 1001 case bK:
mus3 11:43c89579ac52 1002 drawKing(row, column, piece==wK, (row+column)%2);
mus3 11:43c89579ac52 1003 break;
mus3 11:43c89579ac52 1004 case wQ:
mus3 11:43c89579ac52 1005 case bQ:
mus3 11:43c89579ac52 1006 drawQueen(row, column, piece==wQ, (row+column)%2);
mus3 11:43c89579ac52 1007 break;
mus3 11:43c89579ac52 1008 case wB:
mus3 11:43c89579ac52 1009 case bB:
mus3 11:43c89579ac52 1010 drawBishop(row, column, piece==wB, (row+column)%2);
mus3 11:43c89579ac52 1011 break;
mus3 11:43c89579ac52 1012 case wN:
mus3 11:43c89579ac52 1013 case bN:
mus3 11:43c89579ac52 1014 drawKnight(row, column, piece==wN, (row+column)%2);
mus3 11:43c89579ac52 1015 break;
mus3 11:43c89579ac52 1016 case wR:
mus3 11:43c89579ac52 1017 case bR:
mus3 11:43c89579ac52 1018 drawRook(row, column, piece==wR, (row+column)%2);
mus3 11:43c89579ac52 1019 break;
mus3 11:43c89579ac52 1020 case w:
mus3 11:43c89579ac52 1021 case b:
mus3 11:43c89579ac52 1022 drawPawn(row, column, piece==w, (row+column)%2);
mus3 11:43c89579ac52 1023 break;
mhannay3 5:b553c51b3b85 1024 }
mhannay3 5:b553c51b3b85 1025 }
mhannay3 5:b553c51b3b85 1026
mhannay3 5:b553c51b3b85 1027 /* removes a piece from the set position of the board
mhannay3 5:b553c51b3b85 1028 returns the bit representation of the piece
mhannay3 5:b553c51b3b85 1029 */
mhannay3 7:9e01c9a334dc 1030 Piece removePieceAndDraw(boardPos pos)
mhannay3 7:9e01c9a334dc 1031 {
mhannay3 7:9e01c9a334dc 1032 return removePieceAndDraw(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 1033 }
mhannay3 5:b553c51b3b85 1034 Piece removePieceAndDraw(int row, int column)
mhannay3 5:b553c51b3b85 1035 {
mhannay3 5:b553c51b3b85 1036 Piece removedPiece = boardState.removePiece(row, column);
mhannay3 6:1c4dd5e24b8d 1037 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 1:cd78922f70fa 1038 uint64_t color;
mhannay3 1:cd78922f70fa 1039 if ((row + column) % 2 == 0) {
mhannay3 1:cd78922f70fa 1040 color = BOARD_DARK_COLOR;
mhannay3 1:cd78922f70fa 1041 } else {
mhannay3 1:cd78922f70fa 1042 color = BOARD_LIGHT_COLOR;
mhannay3 1:cd78922f70fa 1043 }
mhannay3 5:b553c51b3b85 1044 uLCD.filled_rectangle(tl.x+2, tl.y+2, tl.x + 13, tl.y + 13, color);
mhannay3 5:b553c51b3b85 1045 return removedPiece;
mhannay3 5:b553c51b3b85 1046 }
mhannay3 5:b553c51b3b85 1047
mhannay3 5:b553c51b3b85 1048 /* moves a piece from one position to another
mhannay3 5:b553c51b3b85 1049 returns the captured piece
mhannay3 5:b553c51b3b85 1050 */
mhannay3 7:9e01c9a334dc 1051 Piece movePieceAndDraw(boardPos startPos, boardPos endPos)
mhannay3 7:9e01c9a334dc 1052 {
mhannay3 7:9e01c9a334dc 1053 return movePieceAndDraw(startPos.row, startPos.column, endPos.row, endPos.column);
mhannay3 7:9e01c9a334dc 1054 }
mhannay3 5:b553c51b3b85 1055 Piece movePieceAndDraw(int startRow, int startColumn, int endRow, int endColumn)
mhannay3 5:b553c51b3b85 1056 {
mhannay3 5:b553c51b3b85 1057 Piece movingPiece = removePieceAndDraw(startRow, startColumn);
mhannay3 5:b553c51b3b85 1058 Piece capturedPiece = boardState.removePiece(endRow, endColumn);
mhannay3 5:b553c51b3b85 1059 placePieceAndDraw(movingPiece, endRow, endColumn);
mhannay3 5:b553c51b3b85 1060 return capturedPiece;
mhannay3 1:cd78922f70fa 1061 }
mhannay3 1:cd78922f70fa 1062
mhannay3 5:b553c51b3b85 1063 // SQUARE BORDER GRAPHICS FUNCTIONS
mhannay3 5:b553c51b3b85 1064
mhannay3 5:b553c51b3b85 1065 // removes selection border around square
mhannay3 7:9e01c9a334dc 1066 void unselectSquare(boardPos pos)
mhannay3 7:9e01c9a334dc 1067 {
mhannay3 7:9e01c9a334dc 1068 unselectSquare(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 1069 }
mhannay3 5:b553c51b3b85 1070 void unselectSquare(int row, int column)
mhannay3 5:b553c51b3b85 1071 {
mus3 11:43c89579ac52 1072 if (row < 0 || row > 7 || column < 0 || column > 7) {
mus3 11:43c89579ac52 1073 return;
mus3 11:43c89579ac52 1074 }
mhannay3 6:1c4dd5e24b8d 1075 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 1076 uint64_t color;
mhannay3 5:b553c51b3b85 1077 if ((row + column) % 2 == 0) {
mhannay3 5:b553c51b3b85 1078 color = BOARD_DARK_COLOR;
mhannay3 5:b553c51b3b85 1079 } else {
mhannay3 5:b553c51b3b85 1080 color = BOARD_LIGHT_COLOR;
mhannay3 5:b553c51b3b85 1081 }
mhannay3 5:b553c51b3b85 1082 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, color);
mhannay3 5:b553c51b3b85 1083 }
mhannay3 5:b553c51b3b85 1084
mhannay3 7:9e01c9a334dc 1085 // draws the hover border around square
mhannay3 7:9e01c9a334dc 1086 void hoverSquare(boardPos pos)
mhannay3 7:9e01c9a334dc 1087 {
mhannay3 7:9e01c9a334dc 1088 hoverSquare(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 1089 }
mhannay3 5:b553c51b3b85 1090 void hoverSquare(int row, int column)
mhannay3 5:b553c51b3b85 1091 {
mhannay3 6:1c4dd5e24b8d 1092 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 1093 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, HOVER_COLOR);
mhannay3 1:cd78922f70fa 1094 }
mhannay3 1:cd78922f70fa 1095
mhannay3 1:cd78922f70fa 1096 // draws selection border around square
mhannay3 7:9e01c9a334dc 1097 void selectSquare(boardPos pos)
mhannay3 7:9e01c9a334dc 1098 {
mhannay3 7:9e01c9a334dc 1099 selectSquare(pos.row, pos.column);
mhannay3 7:9e01c9a334dc 1100 }
mhannay3 5:b553c51b3b85 1101 void selectSquare(int row, int column)
mhannay3 5:b553c51b3b85 1102 {
mus3 11:43c89579ac52 1103 if (row < 0 || row > 7 || column < 0 || column > 7) {
mus3 11:43c89579ac52 1104 return;
mus3 11:43c89579ac52 1105 }
mhannay3 6:1c4dd5e24b8d 1106 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 5:b553c51b3b85 1107 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, SELECTED_COLOR);
mhannay3 1:cd78922f70fa 1108 }
mhannay3 9:1f33c0f299ae 1109
mhannay3 8:928d5d33258f 1110 // draws the movement border around square
mhannay3 9:1f33c0f299ae 1111 void movementSquare(boardPos pos)
mhannay3 8:928d5d33258f 1112 {
mhannay3 9:1f33c0f299ae 1113 movementSquare(pos.row, pos.column);
mhannay3 8:928d5d33258f 1114 }
mhannay3 9:1f33c0f299ae 1115 void movementSquare(int row, int column)
mhannay3 8:928d5d33258f 1116 {
mhannay3 8:928d5d33258f 1117 pixelCoord tl = getTopLeftOfSquare(row, column);
mhannay3 8:928d5d33258f 1118 uLCD.rectangle(tl.x, tl.y, tl.x + 15, tl.y + 15, MOVE_COLOR);
mhannay3 8:928d5d33258f 1119 }
mhannay3 5:b553c51b3b85 1120 };
mus3 0:d16cc59110a3 1121
mhannay3 7:9e01c9a334dc 1122 // game variables
mhannay3 7:9e01c9a334dc 1123 GameBoard gameBoard;
mhannay3 7:9e01c9a334dc 1124 GameState state = whiteSelecting;
mhannay3 7:9e01c9a334dc 1125 boardPos cursorPos = (boardPos)
mhannay3 7:9e01c9a334dc 1126 {
mhannay3 7:9e01c9a334dc 1127 3, 4
mhannay3 7:9e01c9a334dc 1128 };
mus3 14:f390d08e5f92 1129 bool OnePlayer = true;
mhannay3 8:928d5d33258f 1130 boardPos selectedPos;
mhannay3 9:1f33c0f299ae 1131 Piece selectedPiece;
mhannay3 9:1f33c0f299ae 1132 std::vector<boardPos> possibleMoves;
mhannay3 7:9e01c9a334dc 1133
mhannay3 7:9e01c9a334dc 1134 // callbacks
mhannay3 9:1f33c0f299ae 1135 void moveCursor(int rowChange, int columnChange)
mhannay3 9:1f33c0f299ae 1136 {
mhannay3 9:1f33c0f299ae 1137 // calculate new positoin that is within board bounds
mhannay3 9:1f33c0f299ae 1138 int newRow = cursorPos.row + rowChange;
mhannay3 9:1f33c0f299ae 1139 newRow = newRow <= 7 ? newRow : 7;
mhannay3 9:1f33c0f299ae 1140 newRow = newRow >= 0 ? newRow : 0;
mhannay3 9:1f33c0f299ae 1141 int newColumn = cursorPos.column + columnChange;
mhannay3 9:1f33c0f299ae 1142 newColumn = newColumn <= 7 ? newColumn : 7;
mhannay3 9:1f33c0f299ae 1143 newColumn = newColumn >= 0 ? newColumn : 0;
mhannay3 9:1f33c0f299ae 1144 boardPos newPos = (boardPos) {
mhannay3 9:1f33c0f299ae 1145 newRow, newColumn
mhannay3 9:1f33c0f299ae 1146 };
mhannay3 9:1f33c0f299ae 1147
mhannay3 9:1f33c0f299ae 1148 // draw border around square that should be there after moving cursor off
mhannay3 9:1f33c0f299ae 1149 if (cursorPos == selectedPos) {
mhannay3 9:1f33c0f299ae 1150 gameBoard.selectSquare(cursorPos);
mhannay3 9:1f33c0f299ae 1151 } else if (std::find(possibleMoves.begin(), possibleMoves.end(), cursorPos) != possibleMoves.end()) {
mhannay3 9:1f33c0f299ae 1152 gameBoard.movementSquare(cursorPos);
mhannay3 9:1f33c0f299ae 1153 } else {
mhannay3 9:1f33c0f299ae 1154 gameBoard.unselectSquare(cursorPos);
mhannay3 9:1f33c0f299ae 1155 }
mhannay3 9:1f33c0f299ae 1156
mhannay3 9:1f33c0f299ae 1157 // draw hover rectangle over new square
mhannay3 9:1f33c0f299ae 1158 cursorPos = newPos;
mhannay3 9:1f33c0f299ae 1159 gameBoard.hoverSquare(cursorPos);
mhannay3 9:1f33c0f299ae 1160 }
mhannay3 9:1f33c0f299ae 1161
mhannay3 7:9e01c9a334dc 1162 void joyStickUp()
mhannay3 7:9e01c9a334dc 1163 {
mhannay3 9:1f33c0f299ae 1164 moveCursor(1, 0);
mhannay3 7:9e01c9a334dc 1165 }
mhannay3 7:9e01c9a334dc 1166
mhannay3 7:9e01c9a334dc 1167 void joyStickDown()
mhannay3 7:9e01c9a334dc 1168 {
mhannay3 9:1f33c0f299ae 1169 moveCursor(-1, 0);
mhannay3 7:9e01c9a334dc 1170 }
mhannay3 7:9e01c9a334dc 1171
mhannay3 7:9e01c9a334dc 1172 void joyStickLeft()
mhannay3 7:9e01c9a334dc 1173 {
mhannay3 9:1f33c0f299ae 1174 moveCursor(0, -1);
mhannay3 7:9e01c9a334dc 1175 }
mhannay3 7:9e01c9a334dc 1176
mhannay3 7:9e01c9a334dc 1177 void joyStickRight()
mhannay3 7:9e01c9a334dc 1178 {
mhannay3 9:1f33c0f299ae 1179 moveCursor(0, 1);
mhannay3 7:9e01c9a334dc 1180 }
mhannay3 7:9e01c9a334dc 1181
mhannay3 7:9e01c9a334dc 1182 void joyStickPressed()
mhannay3 7:9e01c9a334dc 1183 {
mhannay3 8:928d5d33258f 1184 switch(state) {
mhannay3 8:928d5d33258f 1185 case whiteSelecting:
mhannay3 9:1f33c0f299ae 1186 case blackSelecting: {
mus3 11:43c89579ac52 1187 possibleMoves.clear();
mhannay3 8:928d5d33258f 1188 selectedPos = cursorPos;
mhannay3 9:1f33c0f299ae 1189 Piece tempPiece = gameBoard.getPiece(cursorPos);
mhannay3 9:1f33c0f299ae 1190 std::vector<Piece> pickablePieces = state == whiteSelecting ? whitePieces : blackPieces;
mhannay3 9:1f33c0f299ae 1191 // check that piece is white and able to be picked up
mhannay3 9:1f33c0f299ae 1192 if (std::find(pickablePieces.begin(), pickablePieces.end(), tempPiece) != pickablePieces.end()) {
mhannay3 9:1f33c0f299ae 1193 selectedPiece = tempPiece;
mhannay3 9:1f33c0f299ae 1194 possibleMoves = gameBoard.getBoardState().getMoves(cursorPos);
mhannay3 9:1f33c0f299ae 1195 // draw movement squares
mhannay3 9:1f33c0f299ae 1196 for (std::vector<boardPos>::iterator it = possibleMoves.begin(); it != possibleMoves.end(); ++it) {
mhannay3 9:1f33c0f299ae 1197 gameBoard.movementSquare(*it);
mhannay3 9:1f33c0f299ae 1198 }
mhannay3 9:1f33c0f299ae 1199 gameBoard.selectSquare(selectedPos);
mus3 11:43c89579ac52 1200 // transition state
mus3 14:f390d08e5f92 1201 if (OnePlayer) {
mus3 14:f390d08e5f92 1202 state = whitePickedUp;
mus3 14:f390d08e5f92 1203 } else {
mus3 14:f390d08e5f92 1204 state = state == whiteSelecting ? whitePickedUp : blackPickedUp;
mus3 14:f390d08e5f92 1205 }
mus3 11:43c89579ac52 1206 } else {
mus3 11:43c89579ac52 1207 selectedPos = (boardPos)
mus3 11:43c89579ac52 1208 {
mus3 11:43c89579ac52 1209 10, 10
mus3 11:43c89579ac52 1210 };
mhannay3 9:1f33c0f299ae 1211 }
mhannay3 8:928d5d33258f 1212 break;
mhannay3 9:1f33c0f299ae 1213 }
mhannay3 9:1f33c0f299ae 1214 case whitePickedUp:
mhannay3 9:1f33c0f299ae 1215 case blackPickedUp: {
mhannay3 9:1f33c0f299ae 1216 // check if move is valid
mhannay3 9:1f33c0f299ae 1217 if (std::find(possibleMoves.begin(), possibleMoves.end(), cursorPos) != possibleMoves.end()) {
mhannay3 9:1f33c0f299ae 1218 // move the piece
mhannay3 9:1f33c0f299ae 1219 Piece capturedPiece = gameBoard.movePieceAndDraw(selectedPos, cursorPos);
mhannay3 9:1f33c0f299ae 1220 // check for king capture
mhannay3 9:1f33c0f299ae 1221 if (state == whitePickedUp && capturedPiece == bK || state == blackPickedUp && capturedPiece == wK) {
mhannay3 9:1f33c0f299ae 1222 // game end
mhannay3 9:1f33c0f299ae 1223 }
mhannay3 9:1f33c0f299ae 1224 // transition state
mus3 14:f390d08e5f92 1225 if (OnePlayer) {
mus3 14:f390d08e5f92 1226 state = blackAI;
mus3 14:f390d08e5f92 1227 } else {
mus3 14:f390d08e5f92 1228 state = state == whitePickedUp ? blackSelecting : whiteSelecting;
mus3 14:f390d08e5f92 1229 }
mhannay3 9:1f33c0f299ae 1230 // check if placing piece back down
mus3 11:43c89579ac52 1231 } else {
mhannay3 9:1f33c0f299ae 1232 // transition state
mhannay3 9:1f33c0f299ae 1233 state = state == whitePickedUp ? whiteSelecting : blackSelecting;
mhannay3 9:1f33c0f299ae 1234 }
mhannay3 9:1f33c0f299ae 1235 // unselect movement squares
mhannay3 9:1f33c0f299ae 1236 for (std::vector<boardPos>::iterator it = possibleMoves.begin(); it != possibleMoves.end(); ++it) {
mhannay3 9:1f33c0f299ae 1237 gameBoard.unselectSquare(*it);
mhannay3 9:1f33c0f299ae 1238 }
mhannay3 9:1f33c0f299ae 1239 gameBoard.unselectSquare(selectedPos);
mhannay3 9:1f33c0f299ae 1240 gameBoard.hoverSquare(cursorPos);
mus3 11:43c89579ac52 1241 possibleMoves.clear();
mus3 11:43c89579ac52 1242 selectedPos = (boardPos)
mus3 11:43c89579ac52 1243 {
mus3 11:43c89579ac52 1244 10, 10
mus3 11:43c89579ac52 1245 };
mhannay3 8:928d5d33258f 1246 break;
mhannay3 9:1f33c0f299ae 1247 }
mhannay3 9:1f33c0f299ae 1248 case whiteAI:
mus3 14:f390d08e5f92 1249 break;
mhannay3 9:1f33c0f299ae 1250 case blackAI: {
mus3 14:f390d08e5f92 1251 boardPos bestMoveSourceDepth0 = (boardPos) {
mus3 14:f390d08e5f92 1252 0, 0
mus3 14:f390d08e5f92 1253 };
mus3 14:f390d08e5f92 1254 boardPos bestMoveDestDepth0 = (boardPos) {
mus3 14:f390d08e5f92 1255 0, 0
mus3 14:f390d08e5f92 1256 };
mus3 14:f390d08e5f92 1257 float bestMoveValueDepth0 = 100000.0;
mus3 14:f390d08e5f92 1258 for (int i = 0; i < 64; i++) {
mus3 14:f390d08e5f92 1259 boardPos currSourceDepth0 = (boardPos) {
mus3 14:f390d08e5f92 1260 i/8, i%8
mus3 14:f390d08e5f92 1261 };
mus3 14:f390d08e5f92 1262 Piece currPieceDepth0 = gameBoard.getBoardState().getPiece(i/8, i%8);
mus3 14:f390d08e5f92 1263 if (currPieceDepth0 == bK || currPieceDepth0 == bQ || currPieceDepth0 == bR || currPieceDepth0 == bB || currPieceDepth0 == bN || currPieceDepth0 == b) {
mus3 14:f390d08e5f92 1264 std::vector<boardPos> possibleMovesDepth0 = gameBoard.getBoardState().getMoves(currSourceDepth0);
mus3 14:f390d08e5f92 1265 for (std::vector<boardPos>::iterator it = possibleMovesDepth0.begin(); it != possibleMovesDepth0.end(); ++it) {
mus3 14:f390d08e5f92 1266 BoardState bsDepth0;
mus3 14:f390d08e5f92 1267 for (int j = 0; j < 64; j++) {
mus3 14:f390d08e5f92 1268 bsDepth0.placePiece(gameBoard.getBoardState().getPiece(j/8, j%8), j/8, j%8);
mus3 14:f390d08e5f92 1269 }
mus3 14:f390d08e5f92 1270 bsDepth0.movePiece(currSourceDepth0.row, currSourceDepth0.column, (*it).row, (*it).column);
mus3 14:f390d08e5f92 1271 float bestMoveValueDepth1 = -100000.0;
mus3 14:f390d08e5f92 1272 for (int i2 = 0; i2 < 64; i2++) {
mus3 14:f390d08e5f92 1273 boardPos currSourceDepth1 = (boardPos) {
mus3 14:f390d08e5f92 1274 i2/8, i2%8
mus3 14:f390d08e5f92 1275 };
mus3 14:f390d08e5f92 1276 Piece currPieceDepth1 = bsDepth0.getPiece(i2/8, i2%8);
mus3 14:f390d08e5f92 1277 if (currPieceDepth1 == wK || currPieceDepth1 == wQ || currPieceDepth1 == wR || currPieceDepth1 == wB || currPieceDepth1 == wN || currPieceDepth1 == w) {
mus3 14:f390d08e5f92 1278 std::vector<boardPos> possibleMovesDepth1 = bsDepth0.getMoves(currSourceDepth1);
mus3 14:f390d08e5f92 1279 for (std::vector<boardPos>::iterator it2 = possibleMovesDepth1.begin(); it2 != possibleMovesDepth1.end(); ++it2) {
mus3 14:f390d08e5f92 1280 BoardState bsDepth1;
mus3 14:f390d08e5f92 1281 for (int j2 = 0; j2 < 64; j2++) {
mus3 14:f390d08e5f92 1282 bsDepth1.placePiece(bsDepth0.getPiece(j2/8, j2%8), j2/8, j2%8);
mus3 14:f390d08e5f92 1283 }
mus3 14:f390d08e5f92 1284 bsDepth1.movePiece(currSourceDepth1.row, currSourceDepth1.column, (*it2).row, (*it2).column);
mus3 14:f390d08e5f92 1285 float bestMoveValueDepth2 = 100000.0;
mus3 14:f390d08e5f92 1286 for (int i3 = 0; i3 < 64; i3++) {
mus3 14:f390d08e5f92 1287 boardPos currSourceDepth2 = (boardPos) {
mus3 14:f390d08e5f92 1288 i3/8, i3%8
mus3 14:f390d08e5f92 1289 };
mus3 14:f390d08e5f92 1290 Piece currPieceDepth2 = bsDepth1.getPiece(i3/8, i3%8);
mus3 14:f390d08e5f92 1291 if (currPieceDepth2 == bK || currPieceDepth2 == bQ || currPieceDepth2 == bR || currPieceDepth2 == bB || currPieceDepth2 == bN || currPieceDepth2 == b) {
mus3 14:f390d08e5f92 1292 std::vector<boardPos> possibleMovesDepth2 = bsDepth1.getMoves(currSourceDepth2);
mus3 14:f390d08e5f92 1293 for (std::vector<boardPos>::iterator it3 = possibleMovesDepth2.begin(); it3 != possibleMovesDepth2.end(); ++it3) {
mus3 14:f390d08e5f92 1294 BoardState bsDepth2;
mus3 14:f390d08e5f92 1295 for (int j3 = 0; j3 < 64; j3++) {
mus3 14:f390d08e5f92 1296 bsDepth2.placePiece(bsDepth1.getPiece(j3/8, j3%8), j3/8, j3%8);
mus3 14:f390d08e5f92 1297 }
mus3 14:f390d08e5f92 1298 bsDepth2.movePiece(currSourceDepth2.row, currSourceDepth2.column, (*it3).row, (*it3).column);
mus3 14:f390d08e5f92 1299 float thisMoveValueDepth2 = bsDepth2.calculateBoardState();
mus3 14:f390d08e5f92 1300 if (thisMoveValueDepth2 < bestMoveValueDepth2) {
mus3 14:f390d08e5f92 1301 bestMoveValueDepth2 = thisMoveValueDepth2;
mus3 14:f390d08e5f92 1302 }
mus3 14:f390d08e5f92 1303 }
mus3 14:f390d08e5f92 1304 }
mus3 14:f390d08e5f92 1305 }
mus3 14:f390d08e5f92 1306 if (bestMoveValueDepth2 > bestMoveValueDepth1) {
mus3 14:f390d08e5f92 1307 bestMoveValueDepth1 = bestMoveValueDepth2;
mus3 14:f390d08e5f92 1308 }
mus3 14:f390d08e5f92 1309 }
mus3 14:f390d08e5f92 1310 }
mus3 14:f390d08e5f92 1311 }
mus3 14:f390d08e5f92 1312
mus3 14:f390d08e5f92 1313 if (bestMoveValueDepth1 < bestMoveValueDepth0) {
mus3 14:f390d08e5f92 1314 bestMoveSourceDepth0 = currSourceDepth0;
mus3 14:f390d08e5f92 1315 bestMoveDestDepth0 = *it;
mus3 14:f390d08e5f92 1316 bestMoveValueDepth0 = bestMoveValueDepth1;
mus3 14:f390d08e5f92 1317 }
mus3 14:f390d08e5f92 1318 }
mus3 14:f390d08e5f92 1319 }
mus3 14:f390d08e5f92 1320 }
mus3 14:f390d08e5f92 1321 Piece capturedPiece = gameBoard.movePieceAndDraw(bestMoveSourceDepth0, bestMoveDestDepth0);
mus3 14:f390d08e5f92 1322 if (capturedPiece == bK) {
mus3 14:f390d08e5f92 1323 // game end
mus3 14:f390d08e5f92 1324 }
mus3 14:f390d08e5f92 1325 state = whiteSelecting;
mhannay3 8:928d5d33258f 1326 break;
mhannay3 9:1f33c0f299ae 1327 }
mus3 14:f390d08e5f92 1328 default: {
mus3 14:f390d08e5f92 1329 break;
mus3 14:f390d08e5f92 1330 }
mus3 14:f390d08e5f92 1331 }
mus3 14:f390d08e5f92 1332 }
mus3 14:f390d08e5f92 1333
mus3 14:f390d08e5f92 1334 // bluetooth
mus3 14:f390d08e5f92 1335 volatile bool button_ready = 0;
mus3 14:f390d08e5f92 1336 volatile int bnum = 0;
mus3 14:f390d08e5f92 1337 volatile int bhit = 0;
mus3 14:f390d08e5f92 1338 enum statetype {start = 0, got_exclm, got_B, got_num, got_hit};
mus3 14:f390d08e5f92 1339 statetype bluetooth_state = start;
mus3 14:f390d08e5f92 1340
mus3 14:f390d08e5f92 1341 void parse_message()
mus3 14:f390d08e5f92 1342 {
mus3 14:f390d08e5f92 1343 switch (bluetooth_state) {
mus3 14:f390d08e5f92 1344 case start:
mus3 14:f390d08e5f92 1345 if (Blue.getc() == '!') bluetooth_state = got_exclm;
mus3 14:f390d08e5f92 1346 else bluetooth_state = start;
mus3 14:f390d08e5f92 1347 break;
mus3 14:f390d08e5f92 1348 case got_exclm:
mus3 14:f390d08e5f92 1349 if (Blue.getc() == 'B') bluetooth_state = got_B;
mus3 14:f390d08e5f92 1350 else bluetooth_state = start;
mus3 14:f390d08e5f92 1351 break;
mus3 14:f390d08e5f92 1352 case got_B:
mus3 14:f390d08e5f92 1353 bnum = Blue.getc();
mus3 14:f390d08e5f92 1354 bluetooth_state = got_num;
mus3 14:f390d08e5f92 1355 break;
mus3 14:f390d08e5f92 1356 case got_num:
mus3 14:f390d08e5f92 1357 bhit = Blue.getc();
mus3 14:f390d08e5f92 1358 bluetooth_state = got_hit;
mus3 14:f390d08e5f92 1359 break;
mus3 14:f390d08e5f92 1360 case got_hit:
mus3 14:f390d08e5f92 1361 if (Blue.getc() == char(~('!' + ' B' + bnum + bhit))) button_ready = 1;
mus3 14:f390d08e5f92 1362 bluetooth_state = start;
mus3 14:f390d08e5f92 1363 break;
mus3 14:f390d08e5f92 1364 default:
mus3 14:f390d08e5f92 1365 Blue.getc();
mus3 14:f390d08e5f92 1366 bluetooth_state = start;
mhannay3 8:928d5d33258f 1367 }
mhannay3 7:9e01c9a334dc 1368 }
mhannay3 7:9e01c9a334dc 1369
mus3 11:43c89579ac52 1370 Nav_Switch myNav(p9, p6, p7, p5, p8); //pin order on Sparkfun breakout
mus3 11:43c89579ac52 1371
mhannay3 5:b553c51b3b85 1372 int main()
mhannay3 5:b553c51b3b85 1373 {
mus3 11:43c89579ac52 1374 //gameBoard = GameBoard();
mhannay3 9:1f33c0f299ae 1375 whitePieces.push_back(wK);
mhannay3 9:1f33c0f299ae 1376 whitePieces.push_back(wQ);
mhannay3 9:1f33c0f299ae 1377 whitePieces.push_back(wB);
mhannay3 9:1f33c0f299ae 1378 whitePieces.push_back(wN);
mhannay3 9:1f33c0f299ae 1379 whitePieces.push_back(wR);
mhannay3 9:1f33c0f299ae 1380 whitePieces.push_back(w);
mhannay3 9:1f33c0f299ae 1381 blackPieces.push_back(bK);
mhannay3 9:1f33c0f299ae 1382 blackPieces.push_back(bQ);
mhannay3 9:1f33c0f299ae 1383 blackPieces.push_back(bB);
mhannay3 9:1f33c0f299ae 1384 blackPieces.push_back(bN);
mhannay3 9:1f33c0f299ae 1385 blackPieces.push_back(bR);
mhannay3 9:1f33c0f299ae 1386 blackPieces.push_back(b);
mus3 11:43c89579ac52 1387 moveCursor(0, 0);
mus3 14:f390d08e5f92 1388 Blue.attach(&parse_message,Serial::RxIrq);
mus3 11:43c89579ac52 1389 while (1) {
mus3 14:f390d08e5f92 1390 if (state == blackAI) {
mus3 14:f390d08e5f92 1391 joyStickPressed();
mus3 14:f390d08e5f92 1392 }
mus3 11:43c89579ac52 1393 if (myNav.up()) {
mus3 11:43c89579ac52 1394 joyStickUp();
mus3 11:43c89579ac52 1395 } else if (myNav.down()) {
mus3 11:43c89579ac52 1396 joyStickDown();
mus3 11:43c89579ac52 1397 } else if (myNav.left()) {
mus3 11:43c89579ac52 1398 joyStickLeft();
mus3 11:43c89579ac52 1399 } else if (myNav.right()) {
mus3 11:43c89579ac52 1400 joyStickRight();
mus3 11:43c89579ac52 1401 } else if (myNav.fire()) {
mus3 11:43c89579ac52 1402 joyStickPressed();
mus3 14:f390d08e5f92 1403 } else if (button_ready && bhit == '1') {
mus3 14:f390d08e5f92 1404 switch(bnum) {
mus3 14:f390d08e5f92 1405 case '1':
mus3 14:f390d08e5f92 1406 joyStickPressed();
mus3 14:f390d08e5f92 1407 break;
mus3 14:f390d08e5f92 1408 case '5':
mus3 14:f390d08e5f92 1409 joyStickUp();
mus3 14:f390d08e5f92 1410 break;
mus3 14:f390d08e5f92 1411 case '6':
mus3 14:f390d08e5f92 1412 joyStickDown();
mus3 14:f390d08e5f92 1413 break;
mus3 14:f390d08e5f92 1414 case '7':
mus3 14:f390d08e5f92 1415 joyStickLeft();
mus3 14:f390d08e5f92 1416 break;
mus3 14:f390d08e5f92 1417 case '8':
mus3 14:f390d08e5f92 1418 joyStickRight();
mus3 14:f390d08e5f92 1419 break;
mus3 14:f390d08e5f92 1420 }
mus3 14:f390d08e5f92 1421 button_ready = false;
mus3 11:43c89579ac52 1422 }
mus3 14:f390d08e5f92 1423 wait(0.2);
mus3 11:43c89579ac52 1424 }
mhannay3 1:cd78922f70fa 1425 }