Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed mbed-rtos 4DGL-uLCD-SE
main.cpp@0:d9dcc0c3cd64, 2020-04-28 (annotated)
- Committer:
- jsmith687
- Date:
- Tue Apr 28 01:18:23 2020 +0000
- Revision:
- 0:d9dcc0c3cd64
- Child:
- 1:4af874b42e33
Public Version
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jsmith687 | 0:d9dcc0c3cd64 | 1 | |
| jsmith687 | 0:d9dcc0c3cd64 | 2 | /* |
| jsmith687 | 0:d9dcc0c3cd64 | 3 | |
| jsmith687 | 0:d9dcc0c3cd64 | 4 | desired functionality (not priority) |
| jsmith687 | 0:d9dcc0c3cd64 | 5 | ----------------------------------- |
| jsmith687 | 0:d9dcc0c3cd64 | 6 | - 2 player |
| jsmith687 | 0:d9dcc0c3cd64 | 7 | - check for moves so that if you try to move into check it displays an error |
| jsmith687 | 0:d9dcc0c3cd64 | 8 | - highlight previous move and new position in movePiece() function |
| jsmith687 | 0:d9dcc0c3cd64 | 9 | - display possible moves as yellow |
| jsmith687 | 0:d9dcc0c3cd64 | 10 | - audio needs done in separate thread or with short clips so that it doesn't freeze program |
| jsmith687 | 0:d9dcc0c3cd64 | 11 | - alternating color tiles |
| jsmith687 | 0:d9dcc0c3cd64 | 12 | |
| jsmith687 | 0:d9dcc0c3cd64 | 13 | program flow |
| jsmith687 | 0:d9dcc0c3cd64 | 14 | ----------------------------------- |
| jsmith687 | 0:d9dcc0c3cd64 | 15 | - game initializes |
| jsmith687 | 0:d9dcc0c3cd64 | 16 | - a startup screen displays (or start on menu?) |
| jsmith687 | 0:d9dcc0c3cd64 | 17 | - timer begins on first move |
| jsmith687 | 0:d9dcc0c3cd64 | 18 | - timer changes every time a move is made |
| jsmith687 | 0:d9dcc0c3cd64 | 19 | - loop for new button inputs |
| jsmith687 | 0:d9dcc0c3cd64 | 20 | - when select button is hit is when the real checks run |
| jsmith687 | 0:d9dcc0c3cd64 | 21 | - if selecting a piece, simply update x and y coordinates of selected piece |
| jsmith687 | 0:d9dcc0c3cd64 | 22 | - if moving a selected piece, check if the move is valid and then update the board, switch current player |
| jsmith687 | 0:d9dcc0c3cd64 | 23 | - if menu button is hit, it takes priority |
| jsmith687 | 0:d9dcc0c3cd64 | 24 | - menu contains settings |
| jsmith687 | 0:d9dcc0c3cd64 | 25 | - if timer runs out or your king is taken you lose (display game end screen) |
| jsmith687 | 0:d9dcc0c3cd64 | 26 | - make sure gameend resets all variables to original values (or just use the initialize() function) |
| jsmith687 | 0:d9dcc0c3cd64 | 27 | |
| jsmith687 | 0:d9dcc0c3cd64 | 28 | TODO |
| jsmith687 | 0:d9dcc0c3cd64 | 29 | ---------------------------------- |
| jsmith687 | 0:d9dcc0c3cd64 | 30 | - game settings menu |
| jsmith687 | 0:d9dcc0c3cd64 | 31 | - en passant |
| jsmith687 | 0:d9dcc0c3cd64 | 32 | - castling |
| jsmith687 | 0:d9dcc0c3cd64 | 33 | - timer |
| jsmith687 | 0:d9dcc0c3cd64 | 34 | - images for pieces |
| jsmith687 | 0:d9dcc0c3cd64 | 35 | - need to handle input better, with debounced timer or on deasserted or something |
| jsmith687 | 0:d9dcc0c3cd64 | 36 | - restart button |
| jsmith687 | 0:d9dcc0c3cd64 | 37 | */ |
| jsmith687 | 0:d9dcc0c3cd64 | 38 | |
| jsmith687 | 0:d9dcc0c3cd64 | 39 | #include "mbed.h" |
| jsmith687 | 0:d9dcc0c3cd64 | 40 | #include "rtos.h" |
| jsmith687 | 0:d9dcc0c3cd64 | 41 | #include "uLCD_4DGL.h" |
| jsmith687 | 0:d9dcc0c3cd64 | 42 | #include "Speaker.h" |
| jsmith687 | 0:d9dcc0c3cd64 | 43 | |
| jsmith687 | 0:d9dcc0c3cd64 | 44 | //piece colors |
| jsmith687 | 0:d9dcc0c3cd64 | 45 | #define white true |
| jsmith687 | 0:d9dcc0c3cd64 | 46 | #define black false |
| jsmith687 | 0:d9dcc0c3cd64 | 47 | |
| jsmith687 | 0:d9dcc0c3cd64 | 48 | //button inputs |
| jsmith687 | 0:d9dcc0c3cd64 | 49 | #define SELECT 0 |
| jsmith687 | 0:d9dcc0c3cd64 | 50 | #define FORWARD 1 |
| jsmith687 | 0:d9dcc0c3cd64 | 51 | #define BACKWARD 2 |
| jsmith687 | 0:d9dcc0c3cd64 | 52 | #define RIGHT 3 |
| jsmith687 | 0:d9dcc0c3cd64 | 53 | #define LEFT 4 |
| jsmith687 | 0:d9dcc0c3cd64 | 54 | #define MENU 5 |
| jsmith687 | 0:d9dcc0c3cd64 | 55 | |
| jsmith687 | 0:d9dcc0c3cd64 | 56 | //piece types |
| jsmith687 | 0:d9dcc0c3cd64 | 57 | #define NONE 0 |
| jsmith687 | 0:d9dcc0c3cd64 | 58 | #define KING 1 |
| jsmith687 | 0:d9dcc0c3cd64 | 59 | #define QUEEN 2 |
| jsmith687 | 0:d9dcc0c3cd64 | 60 | #define BISHOP 3 |
| jsmith687 | 0:d9dcc0c3cd64 | 61 | #define KNIGHT 4 |
| jsmith687 | 0:d9dcc0c3cd64 | 62 | #define ROOK 5 |
| jsmith687 | 0:d9dcc0c3cd64 | 63 | #define PAWN 6 |
| jsmith687 | 0:d9dcc0c3cd64 | 64 | |
| jsmith687 | 0:d9dcc0c3cd64 | 65 | //seven-segment display conversions |
| jsmith687 | 0:d9dcc0c3cd64 | 66 | #define ZERO 126 |
| jsmith687 | 0:d9dcc0c3cd64 | 67 | #define ONE 48 |
| jsmith687 | 0:d9dcc0c3cd64 | 68 | #define TWO 109 |
| jsmith687 | 0:d9dcc0c3cd64 | 69 | #define THREE 121 |
| jsmith687 | 0:d9dcc0c3cd64 | 70 | #define FOUR 51 |
| jsmith687 | 0:d9dcc0c3cd64 | 71 | #define FIVE 91 |
| jsmith687 | 0:d9dcc0c3cd64 | 72 | #define SIX 95 |
| jsmith687 | 0:d9dcc0c3cd64 | 73 | #define SEVEN 112 |
| jsmith687 | 0:d9dcc0c3cd64 | 74 | #define EIGHT 127 |
| jsmith687 | 0:d9dcc0c3cd64 | 75 | #define NINE 123 |
| jsmith687 | 0:d9dcc0c3cd64 | 76 | |
| jsmith687 | 0:d9dcc0c3cd64 | 77 | #define D1 7 |
| jsmith687 | 0:d9dcc0c3cd64 | 78 | #define D2 11 |
| jsmith687 | 0:d9dcc0c3cd64 | 79 | #define D3 13 |
| jsmith687 | 0:d9dcc0c3cd64 | 80 | #define D4 14 |
| jsmith687 | 0:d9dcc0c3cd64 | 81 | |
| jsmith687 | 0:d9dcc0c3cd64 | 82 | uLCD_4DGL uLCD(p28, p27, p29); // serial tx, serial rx, reset pin; |
| jsmith687 | 0:d9dcc0c3cd64 | 83 | |
| jsmith687 | 0:d9dcc0c3cd64 | 84 | Serial pc(USBTX, USBRX); |
| jsmith687 | 0:d9dcc0c3cd64 | 85 | |
| jsmith687 | 0:d9dcc0c3cd64 | 86 | //button control (directions optional) |
| jsmith687 | 0:d9dcc0c3cd64 | 87 | DigitalIn Select(p14); |
| jsmith687 | 0:d9dcc0c3cd64 | 88 | DigitalIn Menu(p15); |
| jsmith687 | 0:d9dcc0c3cd64 | 89 | DigitalOut led3(LED3); |
| jsmith687 | 0:d9dcc0c3cd64 | 90 | DigitalOut whiteLED(p12); |
| jsmith687 | 0:d9dcc0c3cd64 | 91 | DigitalOut blackLED(p13); |
| jsmith687 | 0:d9dcc0c3cd64 | 92 | Speaker speakerOut(p26); |
| jsmith687 | 0:d9dcc0c3cd64 | 93 | |
| jsmith687 | 0:d9dcc0c3cd64 | 94 | //Joystick variables |
| jsmith687 | 0:d9dcc0c3cd64 | 95 | AnalogIn JoyX(p19); |
| jsmith687 | 0:d9dcc0c3cd64 | 96 | AnalogIn JoyY(p20); |
| jsmith687 | 0:d9dcc0c3cd64 | 97 | |
| jsmith687 | 0:d9dcc0c3cd64 | 98 | |
| jsmith687 | 0:d9dcc0c3cd64 | 99 | volatile bool currentPlayer = white; |
| jsmith687 | 0:d9dcc0c3cd64 | 100 | volatile bool gameActive = false; |
| jsmith687 | 0:d9dcc0c3cd64 | 101 | |
| jsmith687 | 0:d9dcc0c3cd64 | 102 | bool menuOpen = false; |
| jsmith687 | 0:d9dcc0c3cd64 | 103 | int menuSelectionIndex = 0; |
| jsmith687 | 0:d9dcc0c3cd64 | 104 | |
| jsmith687 | 0:d9dcc0c3cd64 | 105 | bool playSound = true; |
| jsmith687 | 0:d9dcc0c3cd64 | 106 | |
| jsmith687 | 0:d9dcc0c3cd64 | 107 | int boxWidth = 16; |
| jsmith687 | 0:d9dcc0c3cd64 | 108 | |
| jsmith687 | 0:d9dcc0c3cd64 | 109 | int programTimeSetting = 600; |
| jsmith687 | 0:d9dcc0c3cd64 | 110 | |
| jsmith687 | 0:d9dcc0c3cd64 | 111 | bool pieceSelected = false; |
| jsmith687 | 0:d9dcc0c3cd64 | 112 | int currentPiece = NONE; |
| jsmith687 | 0:d9dcc0c3cd64 | 113 | int positionX = 0; |
| jsmith687 | 0:d9dcc0c3cd64 | 114 | int positionY = 0; |
| jsmith687 | 0:d9dcc0c3cd64 | 115 | int selectedX = 0; |
| jsmith687 | 0:d9dcc0c3cd64 | 116 | int selectedY = 0; |
| jsmith687 | 0:d9dcc0c3cd64 | 117 | int board[8][8] = { |
| jsmith687 | 0:d9dcc0c3cd64 | 118 | {ROOK, PAWN, NONE, NONE, NONE, NONE, PAWN, ROOK}, |
| jsmith687 | 0:d9dcc0c3cd64 | 119 | {KNIGHT, PAWN, NONE, NONE, NONE, NONE, PAWN, KNIGHT}, |
| jsmith687 | 0:d9dcc0c3cd64 | 120 | {BISHOP, PAWN, NONE, NONE, NONE, NONE, PAWN, BISHOP}, |
| jsmith687 | 0:d9dcc0c3cd64 | 121 | {KING, PAWN, NONE, NONE, NONE, NONE, PAWN, KING}, |
| jsmith687 | 0:d9dcc0c3cd64 | 122 | {QUEEN, PAWN, NONE, NONE, NONE, NONE, PAWN, QUEEN}, |
| jsmith687 | 0:d9dcc0c3cd64 | 123 | {BISHOP, PAWN, NONE, NONE, NONE, NONE, PAWN, BISHOP}, |
| jsmith687 | 0:d9dcc0c3cd64 | 124 | {KNIGHT, PAWN, NONE, NONE, NONE, NONE, PAWN, KNIGHT}, |
| jsmith687 | 0:d9dcc0c3cd64 | 125 | {ROOK, PAWN, NONE, NONE, NONE, NONE, PAWN, ROOK} |
| jsmith687 | 0:d9dcc0c3cd64 | 126 | };//not sure if i did this right, also check x and y |
| jsmith687 | 0:d9dcc0c3cd64 | 127 | |
| jsmith687 | 0:d9dcc0c3cd64 | 128 | bool color[8][8] = { |
| jsmith687 | 0:d9dcc0c3cd64 | 129 | {white, white, white, white, white, white, black, black}, |
| jsmith687 | 0:d9dcc0c3cd64 | 130 | {white, white, white, white, white, white, black, black}, |
| jsmith687 | 0:d9dcc0c3cd64 | 131 | {white, white, white, white, white, white, black, black}, |
| jsmith687 | 0:d9dcc0c3cd64 | 132 | {white, white, white, white, white, white, black, black}, |
| jsmith687 | 0:d9dcc0c3cd64 | 133 | {white, white, white, white, white, white, black, black}, |
| jsmith687 | 0:d9dcc0c3cd64 | 134 | {white, white, white, white, white, white, black, black}, |
| jsmith687 | 0:d9dcc0c3cd64 | 135 | {white, white, white, white, white, white, black, black}, |
| jsmith687 | 0:d9dcc0c3cd64 | 136 | {white, white, white, white, white, white, black, black} |
| jsmith687 | 0:d9dcc0c3cd64 | 137 | }; |
| jsmith687 | 0:d9dcc0c3cd64 | 138 | |
| jsmith687 | 0:d9dcc0c3cd64 | 139 | Timer timerWhite; |
| jsmith687 | 0:d9dcc0c3cd64 | 140 | Timer timerBlack; |
| jsmith687 | 0:d9dcc0c3cd64 | 141 | |
| jsmith687 | 0:d9dcc0c3cd64 | 142 | void drawGameBoard(); |
| jsmith687 | 0:d9dcc0c3cd64 | 143 | int waitForInput(); |
| jsmith687 | 0:d9dcc0c3cd64 | 144 | |
| jsmith687 | 0:d9dcc0c3cd64 | 145 | //TIMER FUNCTIONS--------------------------------------------------------------------------------------------------------------------------------- |
| jsmith687 | 0:d9dcc0c3cd64 | 146 | |
| jsmith687 | 0:d9dcc0c3cd64 | 147 | void timer(void const *args) { |
| jsmith687 | 0:d9dcc0c3cd64 | 148 | //updates the small clock |
| jsmith687 | 0:d9dcc0c3cd64 | 149 | int time_left = programTimeSetting; // remaining time for either player |
| jsmith687 | 0:d9dcc0c3cd64 | 150 | int min2, min1, sec2, sec1; // decimal value for each digit |
| jsmith687 | 0:d9dcc0c3cd64 | 151 | int digits[10] = {ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE}; //contains bus words for each symbol |
| jsmith687 | 0:d9dcc0c3cd64 | 152 | |
| jsmith687 | 0:d9dcc0c3cd64 | 153 | BusOut digit_value(p18, p17, p9, p8, p7, p6, p5); |
| jsmith687 | 0:d9dcc0c3cd64 | 154 | BusOut digit_select(p21, p22, p23, p24); |
| jsmith687 | 0:d9dcc0c3cd64 | 155 | DigitalOut decimal_point(p25); |
| jsmith687 | 0:d9dcc0c3cd64 | 156 | |
| jsmith687 | 0:d9dcc0c3cd64 | 157 | while (true) { |
| jsmith687 | 0:d9dcc0c3cd64 | 158 | led3 = currentPlayer; |
| jsmith687 | 0:d9dcc0c3cd64 | 159 | whiteLED = currentPlayer; |
| jsmith687 | 0:d9dcc0c3cd64 | 160 | blackLED = !currentPlayer; |
| jsmith687 | 0:d9dcc0c3cd64 | 161 | if (!gameActive) { |
| jsmith687 | 0:d9dcc0c3cd64 | 162 | time_left = programTimeSetting; |
| jsmith687 | 0:d9dcc0c3cd64 | 163 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 164 | else if (currentPlayer == black) { |
| jsmith687 | 0:d9dcc0c3cd64 | 165 | //output: programTimeSetting - whiteTime.read() |
| jsmith687 | 0:d9dcc0c3cd64 | 166 | time_left = programTimeSetting - timerBlack.read(); |
| jsmith687 | 0:d9dcc0c3cd64 | 167 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 168 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 169 | //output: programTimeSetting - blackTime.read() |
| jsmith687 | 0:d9dcc0c3cd64 | 170 | time_left = programTimeSetting - timerWhite.read(); |
| jsmith687 | 0:d9dcc0c3cd64 | 171 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 172 | |
| jsmith687 | 0:d9dcc0c3cd64 | 173 | // calculate digit values for display |
| jsmith687 | 0:d9dcc0c3cd64 | 174 | min2 = time_left / 600; |
| jsmith687 | 0:d9dcc0c3cd64 | 175 | min1 = (time_left - 600*min2) / 60; |
| jsmith687 | 0:d9dcc0c3cd64 | 176 | sec2 = (time_left - 600*min2 - 60*min1) / 10; |
| jsmith687 | 0:d9dcc0c3cd64 | 177 | sec1 = (time_left - 600*min2 - 60*min1 - 10*sec2) / 1; |
| jsmith687 | 0:d9dcc0c3cd64 | 178 | |
| jsmith687 | 0:d9dcc0c3cd64 | 179 | // write 7seg display digits |
| jsmith687 | 0:d9dcc0c3cd64 | 180 | digit_select = D4; |
| jsmith687 | 0:d9dcc0c3cd64 | 181 | digit_value = digits[min2]; |
| jsmith687 | 0:d9dcc0c3cd64 | 182 | decimal_point = 0; |
| jsmith687 | 0:d9dcc0c3cd64 | 183 | Thread::wait(1); |
| jsmith687 | 0:d9dcc0c3cd64 | 184 | digit_select = D3; |
| jsmith687 | 0:d9dcc0c3cd64 | 185 | digit_value = digits[min1]; |
| jsmith687 | 0:d9dcc0c3cd64 | 186 | decimal_point = 1; |
| jsmith687 | 0:d9dcc0c3cd64 | 187 | Thread::wait(1); |
| jsmith687 | 0:d9dcc0c3cd64 | 188 | digit_select = D2; |
| jsmith687 | 0:d9dcc0c3cd64 | 189 | digit_value = digits[sec2]; |
| jsmith687 | 0:d9dcc0c3cd64 | 190 | decimal_point = 0; |
| jsmith687 | 0:d9dcc0c3cd64 | 191 | Thread::wait(1); |
| jsmith687 | 0:d9dcc0c3cd64 | 192 | digit_select = D1; |
| jsmith687 | 0:d9dcc0c3cd64 | 193 | digit_value = digits[sec1]; |
| jsmith687 | 0:d9dcc0c3cd64 | 194 | decimal_point = 0; |
| jsmith687 | 0:d9dcc0c3cd64 | 195 | Thread::wait(1); |
| jsmith687 | 0:d9dcc0c3cd64 | 196 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 197 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 198 | |
| jsmith687 | 0:d9dcc0c3cd64 | 199 | //INITIALIZING FUNCTIONS-------------------------------------------------------------------------------------------------------------------------- |
| jsmith687 | 0:d9dcc0c3cd64 | 200 | |
| jsmith687 | 0:d9dcc0c3cd64 | 201 | void initializeButtons() { |
| jsmith687 | 0:d9dcc0c3cd64 | 202 | blackLED = 0; |
| jsmith687 | 0:d9dcc0c3cd64 | 203 | whiteLED = 0; |
| jsmith687 | 0:d9dcc0c3cd64 | 204 | Select.mode(PullUp); |
| jsmith687 | 0:d9dcc0c3cd64 | 205 | Menu.mode(PullUp); |
| jsmith687 | 0:d9dcc0c3cd64 | 206 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 207 | |
| jsmith687 | 0:d9dcc0c3cd64 | 208 | void initializeVariables() { |
| jsmith687 | 0:d9dcc0c3cd64 | 209 | currentPlayer = white; |
| jsmith687 | 0:d9dcc0c3cd64 | 210 | positionX = 0; |
| jsmith687 | 0:d9dcc0c3cd64 | 211 | positionY = 0; |
| jsmith687 | 0:d9dcc0c3cd64 | 212 | pieceSelected = false; |
| jsmith687 | 0:d9dcc0c3cd64 | 213 | timerWhite.stop(); |
| jsmith687 | 0:d9dcc0c3cd64 | 214 | timerWhite.reset(); |
| jsmith687 | 0:d9dcc0c3cd64 | 215 | timerBlack.stop(); |
| jsmith687 | 0:d9dcc0c3cd64 | 216 | timerBlack.reset(); |
| jsmith687 | 0:d9dcc0c3cd64 | 217 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 218 | |
| jsmith687 | 0:d9dcc0c3cd64 | 219 | void initializeLCD() { |
| jsmith687 | 0:d9dcc0c3cd64 | 220 | uLCD.baudrate(BAUD_3000000); //jack up baud rate to max for fast display |
| jsmith687 | 0:d9dcc0c3cd64 | 221 | uLCD.display_control(PORTRAIT); |
| jsmith687 | 0:d9dcc0c3cd64 | 222 | uLCD.text_mode(TRANSPARENT); |
| jsmith687 | 0:d9dcc0c3cd64 | 223 | uLCD.cls(); |
| jsmith687 | 0:d9dcc0c3cd64 | 224 | uLCD.printf("ChessDude"); |
| jsmith687 | 0:d9dcc0c3cd64 | 225 | wait(1.0); |
| jsmith687 | 0:d9dcc0c3cd64 | 226 | uLCD.cls(); |
| jsmith687 | 0:d9dcc0c3cd64 | 227 | drawGameBoard(); |
| jsmith687 | 0:d9dcc0c3cd64 | 228 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 229 | |
| jsmith687 | 0:d9dcc0c3cd64 | 230 | void initialize() { |
| jsmith687 | 0:d9dcc0c3cd64 | 231 | initializeButtons(); |
| jsmith687 | 0:d9dcc0c3cd64 | 232 | initializeVariables(); |
| jsmith687 | 0:d9dcc0c3cd64 | 233 | initializeLCD(); |
| jsmith687 | 0:d9dcc0c3cd64 | 234 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 235 | |
| jsmith687 | 0:d9dcc0c3cd64 | 236 | |
| jsmith687 | 0:d9dcc0c3cd64 | 237 | //DRAW FUNCTIONS---------------------------------------------------------------------------------------------------------------------------------- |
| jsmith687 | 0:d9dcc0c3cd64 | 238 | |
| jsmith687 | 0:d9dcc0c3cd64 | 239 | void drawMenuLine(int line) { |
| jsmith687 | 0:d9dcc0c3cd64 | 240 | uLCD.filled_rectangle(1, 32*line+1, 127, 32*line+31, WHITE); |
| jsmith687 | 0:d9dcc0c3cd64 | 241 | uLCD.text_mode(TRANSPARENT); |
| jsmith687 | 0:d9dcc0c3cd64 | 242 | switch (line) { |
| jsmith687 | 0:d9dcc0c3cd64 | 243 | case 0: { |
| jsmith687 | 0:d9dcc0c3cd64 | 244 | if (playSound) |
| jsmith687 | 0:d9dcc0c3cd64 | 245 | uLCD.text_string("SOUND: ON", 4, 2, FONT_7X8, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 246 | else |
| jsmith687 | 0:d9dcc0c3cd64 | 247 | uLCD.text_string("SOUND: OFF", 4, 2, FONT_7X8, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 248 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 249 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 250 | case 1: { |
| jsmith687 | 0:d9dcc0c3cd64 | 251 | char text[13] = {"TIME: XX MIN"}; |
| jsmith687 | 0:d9dcc0c3cd64 | 252 | text[6] = (char)(programTimeSetting/600); |
| jsmith687 | 0:d9dcc0c3cd64 | 253 | text[7] = (char)((programTimeSetting/60)%10); |
| jsmith687 | 0:d9dcc0c3cd64 | 254 | uLCD.text_string(text, 4, 6, FONT_7X8, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 255 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 256 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 257 | case 2: { |
| jsmith687 | 0:d9dcc0c3cd64 | 258 | //unclear |
| jsmith687 | 0:d9dcc0c3cd64 | 259 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 260 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 261 | case 3: { |
| jsmith687 | 0:d9dcc0c3cd64 | 262 | uLCD.text_string("RESTART", 4, 14, FONT_7X8, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 263 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 264 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 265 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 266 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 267 | |
| jsmith687 | 0:d9dcc0c3cd64 | 268 | void drawMenuSelection(int color) { |
| jsmith687 | 0:d9dcc0c3cd64 | 269 | uLCD.line(0,32*menuSelectionIndex,128,32*menuSelectionIndex, color); |
| jsmith687 | 0:d9dcc0c3cd64 | 270 | uLCD.line(0,32*menuSelectionIndex,0,32*menuSelectionIndex+32, color); |
| jsmith687 | 0:d9dcc0c3cd64 | 271 | uLCD.line(0,32*menuSelectionIndex+32,128,32*menuSelectionIndex+32, color); |
| jsmith687 | 0:d9dcc0c3cd64 | 272 | uLCD.line(128,32*menuSelectionIndex+32,128,32*menuSelectionIndex, color); |
| jsmith687 | 0:d9dcc0c3cd64 | 273 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 274 | |
| jsmith687 | 0:d9dcc0c3cd64 | 275 | |
| jsmith687 | 0:d9dcc0c3cd64 | 276 | void drawSelectionBox(int x, int y, int color) { |
| jsmith687 | 0:d9dcc0c3cd64 | 277 | //draw 4 lines around the box in the color selected |
| jsmith687 | 0:d9dcc0c3cd64 | 278 | if (x < 0 || x >= 8 || y < 0 || y >= 8) return; |
| jsmith687 | 0:d9dcc0c3cd64 | 279 | int startX = x*boxWidth + 1; |
| jsmith687 | 0:d9dcc0c3cd64 | 280 | int endX = (x+1)*boxWidth - 1; |
| jsmith687 | 0:d9dcc0c3cd64 | 281 | int startY = y*boxWidth + 1; |
| jsmith687 | 0:d9dcc0c3cd64 | 282 | int endY = (y+1)*boxWidth - 1; |
| jsmith687 | 0:d9dcc0c3cd64 | 283 | uLCD.line(startX, startY, startX, endY-y/7, color); |
| jsmith687 | 0:d9dcc0c3cd64 | 284 | uLCD.line(startX, endY-y/7, endX-x/7, endY-y/7, color); |
| jsmith687 | 0:d9dcc0c3cd64 | 285 | uLCD.line(endX-x/7, endY-y/7, endX-x/7, startY, color); |
| jsmith687 | 0:d9dcc0c3cd64 | 286 | uLCD.line(endX-x/7, startY, startX, startY, color); |
| jsmith687 | 0:d9dcc0c3cd64 | 287 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 288 | |
| jsmith687 | 0:d9dcc0c3cd64 | 289 | void drawPiece(int x, int y, int piece) { |
| jsmith687 | 0:d9dcc0c3cd64 | 290 | //draws a piece in a specific box (including NONE, which draws a black rectangle to erase) |
| jsmith687 | 0:d9dcc0c3cd64 | 291 | uLCD.text_mode(TRANSPARENT); |
| jsmith687 | 0:d9dcc0c3cd64 | 292 | int textColor = WHITE; |
| jsmith687 | 0:d9dcc0c3cd64 | 293 | if (color[x][y] == black) textColor = RED; |
| jsmith687 | 0:d9dcc0c3cd64 | 294 | switch (piece) { |
| jsmith687 | 0:d9dcc0c3cd64 | 295 | case NONE: { |
| jsmith687 | 0:d9dcc0c3cd64 | 296 | uLCD.filled_rectangle(x*boxWidth+2, y*boxWidth+2, x*boxWidth+boxWidth-2, y*boxWidth+boxWidth-2, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 297 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 298 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 299 | case KING: { |
| jsmith687 | 0:d9dcc0c3cd64 | 300 | //uLCD.text_char('K', x*2.3+1, y*2+1, textColor); |
| jsmith687 | 0:d9dcc0c3cd64 | 301 | int king[14*14] = { |
| jsmith687 | 0:d9dcc0c3cd64 | 302 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 303 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 304 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 305 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 306 | BLACK, BLACK, textColor, textColor, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 307 | BLACK, BLACK, textColor, textColor, BLACK, BLACK, textColor, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 308 | textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 309 | textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 310 | BLACK, BLACK, textColor, textColor, BLACK, BLACK, textColor, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 311 | BLACK, BLACK, textColor, textColor, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 312 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 313 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 314 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 315 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK |
| jsmith687 | 0:d9dcc0c3cd64 | 316 | }; |
| jsmith687 | 0:d9dcc0c3cd64 | 317 | uLCD.BLIT(x*boxWidth+1, y*boxWidth+1, boxWidth-2-x/8, boxWidth-2-y/8, king); |
| jsmith687 | 0:d9dcc0c3cd64 | 318 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 319 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 320 | case QUEEN: { |
| jsmith687 | 0:d9dcc0c3cd64 | 321 | //uLCD.text_char('Q', x*2.3+1, y*2+1, textColor); |
| jsmith687 | 0:d9dcc0c3cd64 | 322 | int queen[14*14] = { |
| jsmith687 | 0:d9dcc0c3cd64 | 323 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 324 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 325 | BLACK, BLACK, textColor, textColor, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 326 | BLACK, BLACK, BLACK, textColor, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 327 | BLACK, BLACK, BLACK, textColor, textColor, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 328 | BLACK, BLACK, BLACK, textColor, textColor, BLACK, textColor, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 329 | BLACK, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 330 | BLACK, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 331 | BLACK, BLACK, BLACK, textColor, textColor, BLACK, textColor, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 332 | BLACK, BLACK, BLACK, textColor, textColor, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 333 | BLACK, BLACK, BLACK, textColor, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 334 | BLACK, BLACK, textColor, textColor, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 335 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 336 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK |
| jsmith687 | 0:d9dcc0c3cd64 | 337 | }; |
| jsmith687 | 0:d9dcc0c3cd64 | 338 | uLCD.BLIT(x*boxWidth+1, y*boxWidth+1, boxWidth-2-x/8, boxWidth-2-y/8, queen); |
| jsmith687 | 0:d9dcc0c3cd64 | 339 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 340 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 341 | case BISHOP: { |
| jsmith687 | 0:d9dcc0c3cd64 | 342 | //uLCD.text_char('B', x*2.3+1, y*2+1, textColor); |
| jsmith687 | 0:d9dcc0c3cd64 | 343 | int bishop[14*14] = { |
| jsmith687 | 0:d9dcc0c3cd64 | 344 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 345 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 346 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 347 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 348 | BLACK, BLACK, BLACK, BLACK, BLACK, textColor, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 349 | BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 350 | BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 351 | BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 352 | BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 353 | BLACK, BLACK, BLACK, BLACK, BLACK, textColor, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 354 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 355 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 356 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 357 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK |
| jsmith687 | 0:d9dcc0c3cd64 | 358 | }; |
| jsmith687 | 0:d9dcc0c3cd64 | 359 | uLCD.BLIT(x*boxWidth+1, y*boxWidth+1, boxWidth-2-x/8, boxWidth-2-y/8, bishop); |
| jsmith687 | 0:d9dcc0c3cd64 | 360 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 361 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 362 | case KNIGHT: { |
| jsmith687 | 0:d9dcc0c3cd64 | 363 | //uLCD.text_char('N', x*2.3+1, y*2+1, textColor); |
| jsmith687 | 0:d9dcc0c3cd64 | 364 | int knight[14*14] = { |
| jsmith687 | 0:d9dcc0c3cd64 | 365 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 366 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 367 | BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 368 | BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 369 | BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 370 | BLACK, BLACK, BLACK, BLACK, textColor, BLACK, textColor, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 371 | BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, BLACK, BLACK, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 372 | BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 373 | BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 374 | BLACK, BLACK, BLACK, textColor, BLACK, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 375 | BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, textColor, textColor, BLACK, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 376 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, BLACK, BLACK, BLACK, BLACK, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 377 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 378 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK |
| jsmith687 | 0:d9dcc0c3cd64 | 379 | }; |
| jsmith687 | 0:d9dcc0c3cd64 | 380 | uLCD.BLIT(x*boxWidth+1, y*boxWidth+1, boxWidth-2-x/8, boxWidth-2-y/8, knight); |
| jsmith687 | 0:d9dcc0c3cd64 | 381 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 382 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 383 | case ROOK: { |
| jsmith687 | 0:d9dcc0c3cd64 | 384 | //uLCD.text_char('R', x*2.3+1, y*2+1, textColor); |
| jsmith687 | 0:d9dcc0c3cd64 | 385 | int rook[14*14] = { |
| jsmith687 | 0:d9dcc0c3cd64 | 386 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 387 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 388 | BLACK, BLACK, BLACK, textColor, textColor, textColor, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 389 | BLACK, BLACK, BLACK, textColor, textColor, textColor, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 390 | BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 391 | BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 392 | BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 393 | BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 394 | BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 395 | BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 396 | BLACK, BLACK, BLACK, textColor, textColor, textColor, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 397 | BLACK, BLACK, BLACK, textColor, textColor, textColor, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 398 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 399 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK |
| jsmith687 | 0:d9dcc0c3cd64 | 400 | }; |
| jsmith687 | 0:d9dcc0c3cd64 | 401 | uLCD.BLIT(x*boxWidth+1, y*boxWidth+1, boxWidth-2-x/8, boxWidth-2-y/8, rook); |
| jsmith687 | 0:d9dcc0c3cd64 | 402 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 403 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 404 | case PAWN: { |
| jsmith687 | 0:d9dcc0c3cd64 | 405 | //uLCD.text_char('P', x*2.3+1, y*2+1, textColor); |
| jsmith687 | 0:d9dcc0c3cd64 | 406 | int pawn[14*14] = { |
| jsmith687 | 0:d9dcc0c3cd64 | 407 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 408 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 409 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 410 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 411 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, BLACK, BLACK, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 412 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 413 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 414 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 415 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, textColor, textColor, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 416 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, BLACK, BLACK, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 417 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, textColor, textColor, |
| jsmith687 | 0:d9dcc0c3cd64 | 418 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 419 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, |
| jsmith687 | 0:d9dcc0c3cd64 | 420 | BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK |
| jsmith687 | 0:d9dcc0c3cd64 | 421 | }; |
| jsmith687 | 0:d9dcc0c3cd64 | 422 | uLCD.BLIT(x*boxWidth+1, y*boxWidth+1, boxWidth-2-x/8, boxWidth-2-y/8, pawn); |
| jsmith687 | 0:d9dcc0c3cd64 | 423 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 424 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 425 | default: |
| jsmith687 | 0:d9dcc0c3cd64 | 426 | return; |
| jsmith687 | 0:d9dcc0c3cd64 | 427 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 428 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 429 | |
| jsmith687 | 0:d9dcc0c3cd64 | 430 | void drawGameBoard() { |
| jsmith687 | 0:d9dcc0c3cd64 | 431 | uLCD.cls(); |
| jsmith687 | 0:d9dcc0c3cd64 | 432 | for (int i = 0; i < 9; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 433 | uLCD.line(i*boxWidth-i/8, 0, i*boxWidth-i/8, 128, 0xFFFFFF); |
| jsmith687 | 0:d9dcc0c3cd64 | 434 | uLCD.line(0, i*boxWidth-i/8, 128, i*boxWidth-i/8, 0xFFFFFF); |
| jsmith687 | 0:d9dcc0c3cd64 | 435 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 436 | for (int i = 0; i < 8; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 437 | for (int j = 0; j < 8; j++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 438 | drawPiece(i, j, board[i][j]); |
| jsmith687 | 0:d9dcc0c3cd64 | 439 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 440 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 441 | if (pieceSelected) { |
| jsmith687 | 0:d9dcc0c3cd64 | 442 | drawSelectionBox(selectedX, selectedY, RED); |
| jsmith687 | 0:d9dcc0c3cd64 | 443 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 444 | drawSelectionBox(positionX, positionY, GREEN); |
| jsmith687 | 0:d9dcc0c3cd64 | 445 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 446 | |
| jsmith687 | 0:d9dcc0c3cd64 | 447 | |
| jsmith687 | 0:d9dcc0c3cd64 | 448 | //MENU FUNCTIONS---------------------------------------------------------------------------------------------------------------------------------- |
| jsmith687 | 0:d9dcc0c3cd64 | 449 | |
| jsmith687 | 0:d9dcc0c3cd64 | 450 | void closeMenu() { |
| jsmith687 | 0:d9dcc0c3cd64 | 451 | uLCD.cls(); |
| jsmith687 | 0:d9dcc0c3cd64 | 452 | drawGameBoard(); |
| jsmith687 | 0:d9dcc0c3cd64 | 453 | menuOpen = false; |
| jsmith687 | 0:d9dcc0c3cd64 | 454 | menuSelectionIndex = 0; |
| jsmith687 | 0:d9dcc0c3cd64 | 455 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 456 | |
| jsmith687 | 0:d9dcc0c3cd64 | 457 | void openMenu() { |
| jsmith687 | 0:d9dcc0c3cd64 | 458 | uLCD.cls(); |
| jsmith687 | 0:d9dcc0c3cd64 | 459 | for (int i = 0; i < 4; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 460 | drawMenuLine(i); |
| jsmith687 | 0:d9dcc0c3cd64 | 461 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 462 | drawMenuSelection(GREEN); |
| jsmith687 | 0:d9dcc0c3cd64 | 463 | menuOpen = true; |
| jsmith687 | 0:d9dcc0c3cd64 | 464 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 465 | |
| jsmith687 | 0:d9dcc0c3cd64 | 466 | void menuSelectHandler() { |
| jsmith687 | 0:d9dcc0c3cd64 | 467 | switch (menuSelectionIndex) { |
| jsmith687 | 0:d9dcc0c3cd64 | 468 | case 0: |
| jsmith687 | 0:d9dcc0c3cd64 | 469 | playSound = !playSound; |
| jsmith687 | 0:d9dcc0c3cd64 | 470 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 471 | case 1: |
| jsmith687 | 0:d9dcc0c3cd64 | 472 | switch (programTimeSetting) { |
| jsmith687 | 0:d9dcc0c3cd64 | 473 | case 300: |
| jsmith687 | 0:d9dcc0c3cd64 | 474 | programTimeSetting = 600; |
| jsmith687 | 0:d9dcc0c3cd64 | 475 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 476 | case 600: |
| jsmith687 | 0:d9dcc0c3cd64 | 477 | programTimeSetting = 900; |
| jsmith687 | 0:d9dcc0c3cd64 | 478 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 479 | case 900: |
| jsmith687 | 0:d9dcc0c3cd64 | 480 | programTimeSetting = 1200; |
| jsmith687 | 0:d9dcc0c3cd64 | 481 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 482 | case 1200: |
| jsmith687 | 0:d9dcc0c3cd64 | 483 | programTimeSetting = 1800; |
| jsmith687 | 0:d9dcc0c3cd64 | 484 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 485 | case 1800: |
| jsmith687 | 0:d9dcc0c3cd64 | 486 | programTimeSetting = 3600; |
| jsmith687 | 0:d9dcc0c3cd64 | 487 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 488 | case 3600: |
| jsmith687 | 0:d9dcc0c3cd64 | 489 | programTimeSetting = 300; |
| jsmith687 | 0:d9dcc0c3cd64 | 490 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 491 | default: |
| jsmith687 | 0:d9dcc0c3cd64 | 492 | programTimeSetting = 600; |
| jsmith687 | 0:d9dcc0c3cd64 | 493 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 494 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 495 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 496 | case 2: |
| jsmith687 | 0:d9dcc0c3cd64 | 497 | //unclear |
| jsmith687 | 0:d9dcc0c3cd64 | 498 | case 3: |
| jsmith687 | 0:d9dcc0c3cd64 | 499 | initialize(); |
| jsmith687 | 0:d9dcc0c3cd64 | 500 | return; |
| jsmith687 | 0:d9dcc0c3cd64 | 501 | default: |
| jsmith687 | 0:d9dcc0c3cd64 | 502 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 503 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 504 | drawMenuLine(menuSelectionIndex); |
| jsmith687 | 0:d9dcc0c3cd64 | 505 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 506 | |
| jsmith687 | 0:d9dcc0c3cd64 | 507 | void moveMenuSelection(int direction) { |
| jsmith687 | 0:d9dcc0c3cd64 | 508 | if (direction == RIGHT || direction == LEFT) return; |
| jsmith687 | 0:d9dcc0c3cd64 | 509 | drawMenuSelection(BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 510 | if (direction == FORWARD) { |
| jsmith687 | 0:d9dcc0c3cd64 | 511 | menuSelectionIndex++; |
| jsmith687 | 0:d9dcc0c3cd64 | 512 | if (menuSelectionIndex > 3) menuSelectionIndex = 0; |
| jsmith687 | 0:d9dcc0c3cd64 | 513 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 514 | if (direction == BACKWARD) { |
| jsmith687 | 0:d9dcc0c3cd64 | 515 | menuSelectionIndex--; |
| jsmith687 | 0:d9dcc0c3cd64 | 516 | if (menuSelectionIndex < 0) menuSelectionIndex = 3; |
| jsmith687 | 0:d9dcc0c3cd64 | 517 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 518 | drawMenuSelection(GREEN); |
| jsmith687 | 0:d9dcc0c3cd64 | 519 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 520 | |
| jsmith687 | 0:d9dcc0c3cd64 | 521 | void menuHandler() { |
| jsmith687 | 0:d9dcc0c3cd64 | 522 | int move = waitForInput(); |
| jsmith687 | 0:d9dcc0c3cd64 | 523 | if (move == MENU) { |
| jsmith687 | 0:d9dcc0c3cd64 | 524 | closeMenu(); |
| jsmith687 | 0:d9dcc0c3cd64 | 525 | return; |
| jsmith687 | 0:d9dcc0c3cd64 | 526 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 527 | if (move == SELECT) { |
| jsmith687 | 0:d9dcc0c3cd64 | 528 | menuSelectHandler(); |
| jsmith687 | 0:d9dcc0c3cd64 | 529 | return; |
| jsmith687 | 0:d9dcc0c3cd64 | 530 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 531 | moveMenuSelection(move); |
| jsmith687 | 0:d9dcc0c3cd64 | 532 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 533 | |
| jsmith687 | 0:d9dcc0c3cd64 | 534 | //GAME FUNCTIONS---------------------------------------------------------------------------------------------------------------------------------- |
| jsmith687 | 0:d9dcc0c3cd64 | 535 | |
| jsmith687 | 0:d9dcc0c3cd64 | 536 | void removeSelectionBox() { |
| jsmith687 | 0:d9dcc0c3cd64 | 537 | switch (currentPiece) { |
| jsmith687 | 0:d9dcc0c3cd64 | 538 | case KING: |
| jsmith687 | 0:d9dcc0c3cd64 | 539 | for (int i = selectedX - 1; i <= selectedX + 1; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 540 | for (int j = selectedY - 1; j <= selectedY + 1; j++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 541 | drawSelectionBox(i, j, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 542 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 543 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 544 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 545 | case QUEEN: |
| jsmith687 | 0:d9dcc0c3cd64 | 546 | for (int i = 0; i < 8; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 547 | drawSelectionBox(selectedX, i, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 548 | drawSelectionBox(i, selectedY, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 549 | drawSelectionBox(selectedX + i, selectedY + i, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 550 | drawSelectionBox(selectedX - i, selectedY - i, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 551 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 552 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 553 | case BISHOP: |
| jsmith687 | 0:d9dcc0c3cd64 | 554 | for (int i = 0; i < 8; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 555 | drawSelectionBox(selectedX + i, selectedY + i, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 556 | drawSelectionBox(selectedX - i, selectedY - i, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 557 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 558 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 559 | case KNIGHT: |
| jsmith687 | 0:d9dcc0c3cd64 | 560 | drawSelectionBox(selectedX, selectedY, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 561 | drawSelectionBox(selectedX + 2, selectedY + 1, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 562 | drawSelectionBox(selectedX + 1, selectedY + 2, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 563 | drawSelectionBox(selectedX - 2, selectedY + 1, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 564 | drawSelectionBox(selectedX - 1, selectedY + 2, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 565 | drawSelectionBox(selectedX + 2, selectedY - 1, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 566 | drawSelectionBox(selectedX + 1, selectedY - 2, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 567 | drawSelectionBox(selectedX - 2, selectedY - 1, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 568 | drawSelectionBox(selectedX - 1, selectedY - 2, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 569 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 570 | case ROOK: |
| jsmith687 | 0:d9dcc0c3cd64 | 571 | for (int i = 0; i < 8; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 572 | drawSelectionBox(selectedX, i, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 573 | drawSelectionBox(i, selectedY, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 574 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 575 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 576 | case PAWN: |
| jsmith687 | 0:d9dcc0c3cd64 | 577 | drawSelectionBox(selectedX, selectedY, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 578 | drawSelectionBox(selectedX, selectedY + 1, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 579 | drawSelectionBox(selectedX, selectedY + 2, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 580 | drawSelectionBox(selectedX + 1, selectedY + 1, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 581 | drawSelectionBox(selectedX - 1, selectedY + 1, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 582 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 583 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 584 | drawSelectionBox(positionX, positionY, GREEN); |
| jsmith687 | 0:d9dcc0c3cd64 | 585 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 586 | |
| jsmith687 | 0:d9dcc0c3cd64 | 587 | int waitForInput() { |
| jsmith687 | 0:d9dcc0c3cd64 | 588 | //main loop which waits for input while in game |
| jsmith687 | 0:d9dcc0c3cd64 | 589 | float calx = 0.0; // joystick hysteresis calibration (optional) |
| jsmith687 | 0:d9dcc0c3cd64 | 590 | float caly = 0.0; |
| jsmith687 | 0:d9dcc0c3cd64 | 591 | while (true) { |
| jsmith687 | 0:d9dcc0c3cd64 | 592 | if (JoyY > (0.6+caly)) { |
| jsmith687 | 0:d9dcc0c3cd64 | 593 | while (JoyY > (0.6+caly)) { Thread::wait(10); } |
| jsmith687 | 0:d9dcc0c3cd64 | 594 | return FORWARD; |
| jsmith687 | 0:d9dcc0c3cd64 | 595 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 596 | if (JoyY < (0.4+caly)) { |
| jsmith687 | 0:d9dcc0c3cd64 | 597 | while (JoyY < (0.4+caly)) { Thread::wait(10); } |
| jsmith687 | 0:d9dcc0c3cd64 | 598 | return BACKWARD; |
| jsmith687 | 0:d9dcc0c3cd64 | 599 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 600 | if (JoyX > (0.6+calx)) { |
| jsmith687 | 0:d9dcc0c3cd64 | 601 | while (JoyX > (0.6+calx)) { Thread::wait(10); } |
| jsmith687 | 0:d9dcc0c3cd64 | 602 | return RIGHT; |
| jsmith687 | 0:d9dcc0c3cd64 | 603 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 604 | if (JoyX < (0.4+calx)) { |
| jsmith687 | 0:d9dcc0c3cd64 | 605 | while (JoyX < (0.4+calx)) { Thread::wait(10); } |
| jsmith687 | 0:d9dcc0c3cd64 | 606 | return LEFT; |
| jsmith687 | 0:d9dcc0c3cd64 | 607 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 608 | if (!Select) { |
| jsmith687 | 0:d9dcc0c3cd64 | 609 | while (!Select) { Thread::wait(10); } |
| jsmith687 | 0:d9dcc0c3cd64 | 610 | return SELECT; |
| jsmith687 | 0:d9dcc0c3cd64 | 611 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 612 | if (!Menu) { |
| jsmith687 | 0:d9dcc0c3cd64 | 613 | while (!Menu) { Thread::wait(10); } |
| jsmith687 | 0:d9dcc0c3cd64 | 614 | return MENU; |
| jsmith687 | 0:d9dcc0c3cd64 | 615 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 616 | Thread::wait(10); |
| jsmith687 | 0:d9dcc0c3cd64 | 617 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 618 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 619 | |
| jsmith687 | 0:d9dcc0c3cd64 | 620 | void displayPossibleMoves() { |
| jsmith687 | 0:d9dcc0c3cd64 | 621 | //highlights squares where a selected piece can move |
| jsmith687 | 0:d9dcc0c3cd64 | 622 | //use a for loop to iterate over all possible positions (diagonals, lines, etc.) |
| jsmith687 | 0:d9dcc0c3cd64 | 623 | //if piece is already and not opposite team don't highlight |
| jsmith687 | 0:d9dcc0c3cd64 | 624 | //make sure not to highlight edge conditions (off board) |
| jsmith687 | 0:d9dcc0c3cd64 | 625 | /*switch (currentPiece) { |
| jsmith687 | 0:d9dcc0c3cd64 | 626 | case NONE: |
| jsmith687 | 0:d9dcc0c3cd64 | 627 | //return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 628 | case KING: |
| jsmith687 | 0:d9dcc0c3cd64 | 629 | //make sure it's opposite player's team or none |
| jsmith687 | 0:d9dcc0c3cd64 | 630 | //check for castling (queenside and short) |
| jsmith687 | 0:d9dcc0c3cd64 | 631 | for (int i = positionX - 1; i <= positionX + 1; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 632 | for (int j = positionY - 1; j <= positionY + 1; j++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 633 | //if (checkValidMove(KING, i, j)) { |
| jsmith687 | 0:d9dcc0c3cd64 | 634 | // drawSelectionBox(i, j, YELLOW); |
| jsmith687 | 0:d9dcc0c3cd64 | 635 | //} |
| jsmith687 | 0:d9dcc0c3cd64 | 636 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 637 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 638 | if (board[positionX][positionY] == NONE) |
| jsmith687 | 0:d9dcc0c3cd64 | 639 | //return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 640 | if (color[positionX][positionY] != currentPlayer) |
| jsmith687 | 0:d9dcc0c3cd64 | 641 | //return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 642 | //return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 643 | case QUEEN: |
| jsmith687 | 0:d9dcc0c3cd64 | 644 | //check it's on the lines |
| jsmith687 | 0:d9dcc0c3cd64 | 645 | //check it's on diagonal |
| jsmith687 | 0:d9dcc0c3cd64 | 646 | //make sure no pieces are in the way |
| jsmith687 | 0:d9dcc0c3cd64 | 647 | //return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 648 | case BISHOP: |
| jsmith687 | 0:d9dcc0c3cd64 | 649 | //check it's on diagonals |
| jsmith687 | 0:d9dcc0c3cd64 | 650 | //make sure no pieces are in the way |
| jsmith687 | 0:d9dcc0c3cd64 | 651 | //return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 652 | case KNIGHT: |
| jsmith687 | 0:d9dcc0c3cd64 | 653 | //check that it's knight position |
| jsmith687 | 0:d9dcc0c3cd64 | 654 | //make sure it's opposite player's team or none |
| jsmith687 | 0:d9dcc0c3cd64 | 655 | //return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 656 | case ROOK: |
| jsmith687 | 0:d9dcc0c3cd64 | 657 | //check it's on the lines |
| jsmith687 | 0:d9dcc0c3cd64 | 658 | //make sure no pieces are in the way |
| jsmith687 | 0:d9dcc0c3cd64 | 659 | //return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 660 | case PAWN: |
| jsmith687 | 0:d9dcc0c3cd64 | 661 | //check en passant |
| jsmith687 | 0:d9dcc0c3cd64 | 662 | //check no pieces in way |
| jsmith687 | 0:d9dcc0c3cd64 | 663 | //check for take on diagonal |
| jsmith687 | 0:d9dcc0c3cd64 | 664 | //return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 665 | default: |
| jsmith687 | 0:d9dcc0c3cd64 | 666 | //return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 667 | }*/ |
| jsmith687 | 0:d9dcc0c3cd64 | 668 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 669 | |
| jsmith687 | 0:d9dcc0c3cd64 | 670 | void showGameEnd() { |
| jsmith687 | 0:d9dcc0c3cd64 | 671 | //shows the end of the game |
| jsmith687 | 0:d9dcc0c3cd64 | 672 | uLCD.cls(); |
| jsmith687 | 0:d9dcc0c3cd64 | 673 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 674 | |
| jsmith687 | 0:d9dcc0c3cd64 | 675 | bool checkValidMove(int x, int y) { |
| jsmith687 | 0:d9dcc0c3cd64 | 676 | if (board[x][y] != NONE && color[x][y] == currentPlayer) return false; //handles moving onto own piece (and same space) |
| jsmith687 | 0:d9dcc0c3cd64 | 677 | switch (currentPiece) { |
| jsmith687 | 0:d9dcc0c3cd64 | 678 | case NONE: |
| jsmith687 | 0:d9dcc0c3cd64 | 679 | return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 680 | case KING: |
| jsmith687 | 0:d9dcc0c3cd64 | 681 | //make sure it's opposite player's team or none |
| jsmith687 | 0:d9dcc0c3cd64 | 682 | //check for castling (queenside and short) |
| jsmith687 | 0:d9dcc0c3cd64 | 683 | if (abs(x-selectedX) <= 1 && abs(y-selectedY) <= 1) { |
| jsmith687 | 0:d9dcc0c3cd64 | 684 | if (board[x][y] == NONE || color[x][y] != currentPlayer) { |
| jsmith687 | 0:d9dcc0c3cd64 | 685 | return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 686 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 687 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 688 | return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 689 | case QUEEN: |
| jsmith687 | 0:d9dcc0c3cd64 | 690 | //check it's on the lines |
| jsmith687 | 0:d9dcc0c3cd64 | 691 | //check it's on diagonal |
| jsmith687 | 0:d9dcc0c3cd64 | 692 | //make sure no pieces are in the way |
| jsmith687 | 0:d9dcc0c3cd64 | 693 | if (board[x][y] != NONE) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 694 | if (x == selectedX) { |
| jsmith687 | 0:d9dcc0c3cd64 | 695 | //check y direction in a line |
| jsmith687 | 0:d9dcc0c3cd64 | 696 | if (selectedY < y) { |
| jsmith687 | 0:d9dcc0c3cd64 | 697 | //if position is forward |
| jsmith687 | 0:d9dcc0c3cd64 | 698 | for (int i = selectedY; i < y; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 699 | if (board[selectedX][i] != NONE) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 700 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 701 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 702 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 703 | //if position is backward |
| jsmith687 | 0:d9dcc0c3cd64 | 704 | for (int i = selectedY; i > y; i--) { |
| jsmith687 | 0:d9dcc0c3cd64 | 705 | if (board[selectedX][i] != NONE) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 706 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 707 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 708 | return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 709 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 710 | else if (y == selectedY) { |
| jsmith687 | 0:d9dcc0c3cd64 | 711 | //check x direction in a line |
| jsmith687 | 0:d9dcc0c3cd64 | 712 | if (selectedX < x) { |
| jsmith687 | 0:d9dcc0c3cd64 | 713 | //if position is forward |
| jsmith687 | 0:d9dcc0c3cd64 | 714 | for (int i = selectedX; i < x; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 715 | if (board[i][selectedY] != NONE) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 716 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 717 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 718 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 719 | //if position is backward |
| jsmith687 | 0:d9dcc0c3cd64 | 720 | for (int i = selectedX; i > x; i--) { |
| jsmith687 | 0:d9dcc0c3cd64 | 721 | if (board[i][selectedY] != NONE) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 722 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 723 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 724 | return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 725 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 726 | else if (abs(x - selectedX) == abs(y - selectedY)) { |
| jsmith687 | 0:d9dcc0c3cd64 | 727 | //check diagonals |
| jsmith687 | 0:d9dcc0c3cd64 | 728 | if (selectedX < x) { |
| jsmith687 | 0:d9dcc0c3cd64 | 729 | if (selectedY < y) { |
| jsmith687 | 0:d9dcc0c3cd64 | 730 | for (int i = 1; i < x - selectedX; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 731 | if (board[selectedX + i][selectedY + i] != NONE) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 732 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 733 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 734 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 735 | for (int i = 1; i < x - selectedX; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 736 | if (board[selectedX + i][selectedY - i] != NONE) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 737 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 738 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 739 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 740 | if (selectedX > x) { |
| jsmith687 | 0:d9dcc0c3cd64 | 741 | if (selectedY < y) { |
| jsmith687 | 0:d9dcc0c3cd64 | 742 | for (int i = 1; i < selectedX - x; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 743 | if (board[selectedX - i][selectedY + i] != NONE) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 744 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 745 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 746 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 747 | for (int i = 1; i < selectedX - x; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 748 | if (board[selectedX - i][selectedY - i] != NONE) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 749 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 750 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 751 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 752 | return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 753 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 754 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 755 | return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 756 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 757 | case BISHOP: |
| jsmith687 | 0:d9dcc0c3cd64 | 758 | //check it's on diagonals |
| jsmith687 | 0:d9dcc0c3cd64 | 759 | //make sure no pieces are in the way |
| jsmith687 | 0:d9dcc0c3cd64 | 760 | if (abs(x - selectedX) == abs(y - selectedY)) { |
| jsmith687 | 0:d9dcc0c3cd64 | 761 | //check diagonals |
| jsmith687 | 0:d9dcc0c3cd64 | 762 | if (selectedX < x) { |
| jsmith687 | 0:d9dcc0c3cd64 | 763 | if (selectedY < y) { |
| jsmith687 | 0:d9dcc0c3cd64 | 764 | for (int i = 1; i < x - selectedX; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 765 | if (board[selectedX + i][selectedY + i] != NONE) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 766 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 767 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 768 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 769 | for (int i = 1; i < x - selectedX; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 770 | if (board[selectedX + i][selectedY - i] != NONE) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 771 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 772 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 773 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 774 | if (selectedX > x) { |
| jsmith687 | 0:d9dcc0c3cd64 | 775 | if (selectedY < y) { |
| jsmith687 | 0:d9dcc0c3cd64 | 776 | for (int i = 1; i < selectedX - x; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 777 | if (board[selectedX - i][selectedY + i] != NONE) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 778 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 779 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 780 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 781 | for (int i = 1; i < selectedX - x; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 782 | if (board[selectedX - i][selectedY - i] != NONE) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 783 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 784 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 785 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 786 | return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 787 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 788 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 789 | return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 790 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 791 | case KNIGHT: |
| jsmith687 | 0:d9dcc0c3cd64 | 792 | //check that it's knight position |
| jsmith687 | 0:d9dcc0c3cd64 | 793 | //make sure it's opposite player's team or none |
| jsmith687 | 0:d9dcc0c3cd64 | 794 | if (abs(y - selectedY) == 2) { |
| jsmith687 | 0:d9dcc0c3cd64 | 795 | if (abs(x - selectedX) == 1) { |
| jsmith687 | 0:d9dcc0c3cd64 | 796 | return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 797 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 798 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 799 | else if (abs(y - selectedY) == 1) { |
| jsmith687 | 0:d9dcc0c3cd64 | 800 | if (abs(x - selectedX) == 2) { |
| jsmith687 | 0:d9dcc0c3cd64 | 801 | return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 802 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 803 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 804 | return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 805 | case ROOK: |
| jsmith687 | 0:d9dcc0c3cd64 | 806 | //check it's on the lines |
| jsmith687 | 0:d9dcc0c3cd64 | 807 | //make sure no pieces are in the way |
| jsmith687 | 0:d9dcc0c3cd64 | 808 | if (x == selectedX) { |
| jsmith687 | 0:d9dcc0c3cd64 | 809 | //check y direction in a line |
| jsmith687 | 0:d9dcc0c3cd64 | 810 | if (selectedY < y) { |
| jsmith687 | 0:d9dcc0c3cd64 | 811 | //if position is forward |
| jsmith687 | 0:d9dcc0c3cd64 | 812 | for (int i = selectedY; i < y; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 813 | if (board[selectedX][i] != NONE) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 814 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 815 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 816 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 817 | //if position is backward |
| jsmith687 | 0:d9dcc0c3cd64 | 818 | for (int i = selectedY; i > y; i--) { |
| jsmith687 | 0:d9dcc0c3cd64 | 819 | if (board[selectedX][i] != NONE) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 820 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 821 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 822 | return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 823 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 824 | else if (y == selectedY) { |
| jsmith687 | 0:d9dcc0c3cd64 | 825 | //check x direction in a line |
| jsmith687 | 0:d9dcc0c3cd64 | 826 | if (selectedX < x) { |
| jsmith687 | 0:d9dcc0c3cd64 | 827 | //if position is forward |
| jsmith687 | 0:d9dcc0c3cd64 | 828 | for (int i = selectedX; i < x; i++) { |
| jsmith687 | 0:d9dcc0c3cd64 | 829 | if (board[i][selectedY] != NONE) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 830 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 831 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 832 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 833 | //if position is backward |
| jsmith687 | 0:d9dcc0c3cd64 | 834 | for (int i = selectedX; i > x; i--) { |
| jsmith687 | 0:d9dcc0c3cd64 | 835 | if (board[i][selectedY] != NONE) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 836 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 837 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 838 | return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 839 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 840 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 841 | return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 842 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 843 | case PAWN: |
| jsmith687 | 0:d9dcc0c3cd64 | 844 | //check en passant |
| jsmith687 | 0:d9dcc0c3cd64 | 845 | //check no pieces in way |
| jsmith687 | 0:d9dcc0c3cd64 | 846 | //check for take on diagonal |
| jsmith687 | 0:d9dcc0c3cd64 | 847 | //check for 2 moves on first row |
| jsmith687 | 0:d9dcc0c3cd64 | 848 | if (currentPlayer == white && y <= selectedY) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 849 | if (currentPlayer == black && y >= selectedY) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 850 | if (x != selectedX) { |
| jsmith687 | 0:d9dcc0c3cd64 | 851 | if (abs(x - selectedX) != 1) return false; //must be one diagonal space away |
| jsmith687 | 0:d9dcc0c3cd64 | 852 | if (abs(y - selectedY) != 1) return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 853 | if (board[x][y] == NONE) return false; //must be taking a piece |
| jsmith687 | 0:d9dcc0c3cd64 | 854 | return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 855 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 856 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 857 | if (currentPlayer == white) { |
| jsmith687 | 0:d9dcc0c3cd64 | 858 | if (selectedY == 1) { |
| jsmith687 | 0:d9dcc0c3cd64 | 859 | if (y == 2 && board[x][y] == NONE) return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 860 | else if (y == 3 && board[x][y] == NONE && board[x][y - 1] == NONE) return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 861 | else return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 862 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 863 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 864 | if (y == selectedY + 1) { |
| jsmith687 | 0:d9dcc0c3cd64 | 865 | if (board[x][y] == NONE) { |
| jsmith687 | 0:d9dcc0c3cd64 | 866 | return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 867 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 868 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 869 | return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 870 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 871 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 872 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 873 | if (selectedY == 6) { |
| jsmith687 | 0:d9dcc0c3cd64 | 874 | if (y == 5 && board[x][y] == NONE) return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 875 | else if (y == 4 && board[x][y] == NONE && board[x][y + 1] == NONE) return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 876 | else return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 877 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 878 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 879 | if (y == selectedY - 1) { |
| jsmith687 | 0:d9dcc0c3cd64 | 880 | if (board[x][y] == NONE) { |
| jsmith687 | 0:d9dcc0c3cd64 | 881 | return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 882 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 883 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 884 | return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 885 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 886 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 887 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 888 | default: |
| jsmith687 | 0:d9dcc0c3cd64 | 889 | return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 890 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 891 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 892 | |
| jsmith687 | 0:d9dcc0c3cd64 | 893 | void movePiece() { |
| jsmith687 | 0:d9dcc0c3cd64 | 894 | //new position is viable, update location and change current player |
| jsmith687 | 0:d9dcc0c3cd64 | 895 | if (!gameActive) gameActive = true; |
| jsmith687 | 0:d9dcc0c3cd64 | 896 | if (board[positionX][positionY] == KING) { |
| jsmith687 | 0:d9dcc0c3cd64 | 897 | showGameEnd(); |
| jsmith687 | 0:d9dcc0c3cd64 | 898 | return; |
| jsmith687 | 0:d9dcc0c3cd64 | 899 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 900 | removeSelectionBox(); |
| jsmith687 | 0:d9dcc0c3cd64 | 901 | drawPiece(selectedX, selectedY, NONE); |
| jsmith687 | 0:d9dcc0c3cd64 | 902 | board[positionX][positionY] = board[selectedX][selectedY]; |
| jsmith687 | 0:d9dcc0c3cd64 | 903 | color[positionX][positionY] = currentPlayer; |
| jsmith687 | 0:d9dcc0c3cd64 | 904 | board[selectedX][selectedY] = NONE; |
| jsmith687 | 0:d9dcc0c3cd64 | 905 | drawPiece(positionX, positionY, board[positionX][positionY]); |
| jsmith687 | 0:d9dcc0c3cd64 | 906 | pieceSelected = false; |
| jsmith687 | 0:d9dcc0c3cd64 | 907 | if (currentPlayer == white) { |
| jsmith687 | 0:d9dcc0c3cd64 | 908 | timerWhite.stop(); |
| jsmith687 | 0:d9dcc0c3cd64 | 909 | timerBlack.start(); |
| jsmith687 | 0:d9dcc0c3cd64 | 910 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 911 | if (currentPlayer == black) { |
| jsmith687 | 0:d9dcc0c3cd64 | 912 | timerBlack.stop(); |
| jsmith687 | 0:d9dcc0c3cd64 | 913 | timerWhite.start(); |
| jsmith687 | 0:d9dcc0c3cd64 | 914 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 915 | speakerOut.PlayNote(2000, .1, .25); |
| jsmith687 | 0:d9dcc0c3cd64 | 916 | currentPlayer = !currentPlayer; |
| jsmith687 | 0:d9dcc0c3cd64 | 917 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 918 | |
| jsmith687 | 0:d9dcc0c3cd64 | 919 | void moveSelectionBox(int direction) { |
| jsmith687 | 0:d9dcc0c3cd64 | 920 | if (direction == NONE) { |
| jsmith687 | 0:d9dcc0c3cd64 | 921 | drawSelectionBox(positionX, positionY, GREEN); |
| jsmith687 | 0:d9dcc0c3cd64 | 922 | return; |
| jsmith687 | 0:d9dcc0c3cd64 | 923 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 924 | if (direction == FORWARD && positionY == 7) return; |
| jsmith687 | 0:d9dcc0c3cd64 | 925 | if (direction == BACKWARD && positionY == 0) return; |
| jsmith687 | 0:d9dcc0c3cd64 | 926 | if (direction == RIGHT && positionX == 7) return; |
| jsmith687 | 0:d9dcc0c3cd64 | 927 | if (direction == LEFT && positionX == 0) return; |
| jsmith687 | 0:d9dcc0c3cd64 | 928 | if (pieceSelected && positionX == selectedX && positionY == selectedY) { |
| jsmith687 | 0:d9dcc0c3cd64 | 929 | drawSelectionBox(positionX, positionY, RED); |
| jsmith687 | 0:d9dcc0c3cd64 | 930 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 931 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 932 | drawSelectionBox(positionX, positionY, BLACK); |
| jsmith687 | 0:d9dcc0c3cd64 | 933 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 934 | if (direction == FORWARD) { |
| jsmith687 | 0:d9dcc0c3cd64 | 935 | positionY++; |
| jsmith687 | 0:d9dcc0c3cd64 | 936 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 937 | if (direction == BACKWARD) { |
| jsmith687 | 0:d9dcc0c3cd64 | 938 | positionY--; |
| jsmith687 | 0:d9dcc0c3cd64 | 939 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 940 | if (direction == RIGHT) { |
| jsmith687 | 0:d9dcc0c3cd64 | 941 | positionX++; |
| jsmith687 | 0:d9dcc0c3cd64 | 942 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 943 | if (direction == LEFT) { |
| jsmith687 | 0:d9dcc0c3cd64 | 944 | positionX--; |
| jsmith687 | 0:d9dcc0c3cd64 | 945 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 946 | speakerOut.PlayNote(500, .1, .25); |
| jsmith687 | 0:d9dcc0c3cd64 | 947 | drawSelectionBox(positionX, positionY, GREEN); |
| jsmith687 | 0:d9dcc0c3cd64 | 948 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 949 | |
| jsmith687 | 0:d9dcc0c3cd64 | 950 | bool checkValidSelection() { |
| jsmith687 | 0:d9dcc0c3cd64 | 951 | if (board[positionX][positionY] != NONE) { |
| jsmith687 | 0:d9dcc0c3cd64 | 952 | if (color[positionX][positionY] == currentPlayer) { |
| jsmith687 | 0:d9dcc0c3cd64 | 953 | return true; |
| jsmith687 | 0:d9dcc0c3cd64 | 954 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 955 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 956 | return false; |
| jsmith687 | 0:d9dcc0c3cd64 | 957 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 958 | |
| jsmith687 | 0:d9dcc0c3cd64 | 959 | void selectPiece() { |
| jsmith687 | 0:d9dcc0c3cd64 | 960 | selectedX = positionX; |
| jsmith687 | 0:d9dcc0c3cd64 | 961 | selectedY = positionY; |
| jsmith687 | 0:d9dcc0c3cd64 | 962 | pieceSelected = true; |
| jsmith687 | 0:d9dcc0c3cd64 | 963 | currentPiece = board[positionX][positionY]; |
| jsmith687 | 0:d9dcc0c3cd64 | 964 | displayPossibleMoves(); |
| jsmith687 | 0:d9dcc0c3cd64 | 965 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 966 | |
| jsmith687 | 0:d9dcc0c3cd64 | 967 | void deselectPiece() { |
| jsmith687 | 0:d9dcc0c3cd64 | 968 | pieceSelected = false; |
| jsmith687 | 0:d9dcc0c3cd64 | 969 | currentPiece = NONE; |
| jsmith687 | 0:d9dcc0c3cd64 | 970 | removeSelectionBox(); |
| jsmith687 | 0:d9dcc0c3cd64 | 971 | moveSelectionBox(NONE); |
| jsmith687 | 0:d9dcc0c3cd64 | 972 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 973 | |
| jsmith687 | 0:d9dcc0c3cd64 | 974 | void selectHandler() { |
| jsmith687 | 0:d9dcc0c3cd64 | 975 | if (pieceSelected) { |
| jsmith687 | 0:d9dcc0c3cd64 | 976 | if (positionX == selectedX && positionY == selectedY) { |
| jsmith687 | 0:d9dcc0c3cd64 | 977 | deselectPiece(); |
| jsmith687 | 0:d9dcc0c3cd64 | 978 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 979 | if (checkValidMove(positionX, positionY)) { |
| jsmith687 | 0:d9dcc0c3cd64 | 980 | movePiece(); |
| jsmith687 | 0:d9dcc0c3cd64 | 981 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 982 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 983 | else { |
| jsmith687 | 0:d9dcc0c3cd64 | 984 | if (checkValidSelection()) { |
| jsmith687 | 0:d9dcc0c3cd64 | 985 | selectPiece(); |
| jsmith687 | 0:d9dcc0c3cd64 | 986 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 987 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 988 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 989 | |
| jsmith687 | 0:d9dcc0c3cd64 | 990 | void gameHandler() { |
| jsmith687 | 0:d9dcc0c3cd64 | 991 | if (menuOpen) return; |
| jsmith687 | 0:d9dcc0c3cd64 | 992 | int move = waitForInput(); |
| jsmith687 | 0:d9dcc0c3cd64 | 993 | switch (move) { |
| jsmith687 | 0:d9dcc0c3cd64 | 994 | case MENU: |
| jsmith687 | 0:d9dcc0c3cd64 | 995 | openMenu(); |
| jsmith687 | 0:d9dcc0c3cd64 | 996 | return; |
| jsmith687 | 0:d9dcc0c3cd64 | 997 | case SELECT: |
| jsmith687 | 0:d9dcc0c3cd64 | 998 | selectHandler(); |
| jsmith687 | 0:d9dcc0c3cd64 | 999 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 1000 | default: |
| jsmith687 | 0:d9dcc0c3cd64 | 1001 | moveSelectionBox(move); |
| jsmith687 | 0:d9dcc0c3cd64 | 1002 | break; |
| jsmith687 | 0:d9dcc0c3cd64 | 1003 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 1004 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 1005 | |
| jsmith687 | 0:d9dcc0c3cd64 | 1006 | //MAIN---------------------------------------------------------------------------------------------------------------------------------- |
| jsmith687 | 0:d9dcc0c3cd64 | 1007 | |
| jsmith687 | 0:d9dcc0c3cd64 | 1008 | int main() { |
| jsmith687 | 0:d9dcc0c3cd64 | 1009 | |
| jsmith687 | 0:d9dcc0c3cd64 | 1010 | initialize(); |
| jsmith687 | 0:d9dcc0c3cd64 | 1011 | |
| jsmith687 | 0:d9dcc0c3cd64 | 1012 | Thread timerThread(timer); |
| jsmith687 | 0:d9dcc0c3cd64 | 1013 | |
| jsmith687 | 0:d9dcc0c3cd64 | 1014 | while (true) { |
| jsmith687 | 0:d9dcc0c3cd64 | 1015 | if (menuOpen) menuHandler(); |
| jsmith687 | 0:d9dcc0c3cd64 | 1016 | else gameHandler(); |
| jsmith687 | 0:d9dcc0c3cd64 | 1017 | } |
| jsmith687 | 0:d9dcc0c3cd64 | 1018 | |
| jsmith687 | 0:d9dcc0c3cd64 | 1019 | } |