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