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