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