change to final_test
Dependencies: 4DGL-uLCD-SE EthernetInterface HTTPClient mbed-rtos mbed
Fork of Fianl_test by
main.cpp@1:4e629f9ac9e8, 2015-04-22 (annotated)
- Committer:
- jderiso2
- Date:
- Wed Apr 22 01:46:14 2015 +0000
- Revision:
- 1:4e629f9ac9e8
- Parent:
- 0:28f8ba171e86
- Child:
- 3:b834ab4a53d1
update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
honelight | 0:28f8ba171e86 | 1 | #include "mbed.h" |
honelight | 0:28f8ba171e86 | 2 | #include "uLCD_4DGL.h" |
honelight | 0:28f8ba171e86 | 3 | #include "board.h" |
honelight | 0:28f8ba171e86 | 4 | #include "rtos.h" |
honelight | 0:28f8ba171e86 | 5 | #include "EthernetInterface.h" |
honelight | 0:28f8ba171e86 | 6 | #include "HTTPClient.h" |
jderiso2 | 1:4e629f9ac9e8 | 7 | #include <sstream> |
jderiso2 | 1:4e629f9ac9e8 | 8 | #include <string> |
jderiso2 | 1:4e629f9ac9e8 | 9 | #include <iostream> |
honelight | 0:28f8ba171e86 | 10 | |
honelight | 0:28f8ba171e86 | 11 | uLCD_4DGL lcd(p28,p27,p30); // serial tx, serial rx, reset pin; |
jderiso2 | 1:4e629f9ac9e8 | 12 | Serial pc(USBTX, USBRX); |
honelight | 0:28f8ba171e86 | 13 | DigitalIn in0(p11); |
honelight | 0:28f8ba171e86 | 14 | DigitalIn in1(p12); |
honelight | 0:28f8ba171e86 | 15 | DigitalIn in2(p13); |
honelight | 0:28f8ba171e86 | 16 | DigitalIn in3(p14); |
honelight | 0:28f8ba171e86 | 17 | DigitalIn in4(p15); |
honelight | 0:28f8ba171e86 | 18 | DigitalIn in5(p16); |
honelight | 0:28f8ba171e86 | 19 | DigitalIn in6(p17); |
honelight | 0:28f8ba171e86 | 20 | DigitalIn in7(p18); |
honelight | 0:28f8ba171e86 | 21 | DigitalIn in8(p19); |
honelight | 0:28f8ba171e86 | 22 | |
honelight | 0:28f8ba171e86 | 23 | Mutex stdio_mutex; |
honelight | 0:28f8ba171e86 | 24 | |
honelight | 0:28f8ba171e86 | 25 | |
honelight | 0:28f8ba171e86 | 26 | |
honelight | 0:28f8ba171e86 | 27 | int player1=1; |
honelight | 0:28f8ba171e86 | 28 | int player2=2; |
honelight | 0:28f8ba171e86 | 29 | int turn =1; |
honelight | 0:28f8ba171e86 | 30 | int printout = 0; |
honelight | 0:28f8ba171e86 | 31 | |
honelight | 0:28f8ba171e86 | 32 | |
honelight | 0:28f8ba171e86 | 33 | void printBoard(int ** board) |
honelight | 0:28f8ba171e86 | 34 | { |
honelight | 0:28f8ba171e86 | 35 | lcd.cls(); |
honelight | 0:28f8ba171e86 | 36 | lcd.line(0, 42 , 127, 42 , 0xFF0000); |
honelight | 0:28f8ba171e86 | 37 | lcd.line(0, 84 , 127, 84 , 0xFF0000); |
honelight | 0:28f8ba171e86 | 38 | lcd.line(42, 0 , 42, 127, 0xFF0000); |
honelight | 0:28f8ba171e86 | 39 | lcd.line(84, 0, 84, 127, 0xFF0000); |
jderiso2 | 1:4e629f9ac9e8 | 40 | for(int i=0; i<3; i++) { |
jderiso2 | 1:4e629f9ac9e8 | 41 | for(int j=0; j<3; j++) { |
jderiso2 | 1:4e629f9ac9e8 | 42 | if(board[i][j] == 0) { |
honelight | 0:28f8ba171e86 | 43 | lcd.line( j*42+2 , i*42 + 2 ,(j+1)*42-2 , (i+1)*42-2 , GREEN); |
honelight | 0:28f8ba171e86 | 44 | lcd.line( j*42+2 , (i+1)*42 - 2 ,(j+1)*42-2 , (i)*42+2 , GREEN); |
jderiso2 | 1:4e629f9ac9e8 | 45 | } else if(board[i][j] == 1) { |
honelight | 0:28f8ba171e86 | 46 | lcd.circle(42*j+21, 42*i+21, 19, BLUE); |
honelight | 0:28f8ba171e86 | 47 | } |
honelight | 0:28f8ba171e86 | 48 | } |
honelight | 0:28f8ba171e86 | 49 | } |
honelight | 0:28f8ba171e86 | 50 | } |
jderiso2 | 1:4e629f9ac9e8 | 51 | typedef enum GAME_STATE { |
jderiso2 | 1:4e629f9ac9e8 | 52 | STARTING, PLAYING, OVER, RETURN_ERROR = -1 |
jderiso2 | 1:4e629f9ac9e8 | 53 | } GAME_STATE; |
jderiso2 | 1:4e629f9ac9e8 | 54 | HTTPClient http; |
jderiso2 | 1:4e629f9ac9e8 | 55 | EthernetInterface eth; |
jderiso2 | 1:4e629f9ac9e8 | 56 | char str[4096]; |
jderiso2 | 1:4e629f9ac9e8 | 57 | int StartNewGame() |
jderiso2 | 1:4e629f9ac9e8 | 58 | { |
jderiso2 | 1:4e629f9ac9e8 | 59 | HTTPResult ret = http.get("http://4180.azurewebsites.net/api/TicTacToeGames/New", str, 4096); |
jderiso2 | 1:4e629f9ac9e8 | 60 | if (ret > 0) { |
jderiso2 | 1:4e629f9ac9e8 | 61 | pc.printf("HTTP Error return in StartNewGame\n"); |
jderiso2 | 1:4e629f9ac9e8 | 62 | return -1; |
jderiso2 | 1:4e629f9ac9e8 | 63 | } |
jderiso2 | 1:4e629f9ac9e8 | 64 | return atoi(str); |
jderiso2 | 1:4e629f9ac9e8 | 65 | } |
jderiso2 | 1:4e629f9ac9e8 | 66 | int JoinGame() |
jderiso2 | 1:4e629f9ac9e8 | 67 | { |
jderiso2 | 1:4e629f9ac9e8 | 68 | HTTPResult ret = http.get("http://4180.azurewebsites.net/api/TicTacToeGames/Join", str, 4096); |
jderiso2 | 1:4e629f9ac9e8 | 69 | if (ret > 0) { |
jderiso2 | 1:4e629f9ac9e8 | 70 | pc.printf("HTTP Error return in JoinGame\n"); |
jderiso2 | 1:4e629f9ac9e8 | 71 | return -1; |
jderiso2 | 1:4e629f9ac9e8 | 72 | } |
jderiso2 | 1:4e629f9ac9e8 | 73 | return atoi(str); |
jderiso2 | 1:4e629f9ac9e8 | 74 | } |
jderiso2 | 1:4e629f9ac9e8 | 75 | ostringstream host; |
jderiso2 | 1:4e629f9ac9e8 | 76 | string s; |
jderiso2 | 1:4e629f9ac9e8 | 77 | const char* cstr; |
jderiso2 | 1:4e629f9ac9e8 | 78 | GAME_STATE GetGameState(int gameId) |
jderiso2 | 1:4e629f9ac9e8 | 79 | { |
jderiso2 | 1:4e629f9ac9e8 | 80 | //ostringstream host; |
jderiso2 | 1:4e629f9ac9e8 | 81 | host.clear(); |
jderiso2 | 1:4e629f9ac9e8 | 82 | host << "http://4180.azurewebsites.net/odata/TicTacToeGames(" << gameId << ")?$select=GameState"; |
jderiso2 | 1:4e629f9ac9e8 | 83 | s = host.str(); |
jderiso2 | 1:4e629f9ac9e8 | 84 | cstr = s.c_str(); |
jderiso2 | 1:4e629f9ac9e8 | 85 | printf("**** HOST IS %s\n", cstr); |
jderiso2 | 1:4e629f9ac9e8 | 86 | HTTPResult ret = http.get(cstr, str, 4096); |
jderiso2 | 1:4e629f9ac9e8 | 87 | if (ret > 0) { |
jderiso2 | 1:4e629f9ac9e8 | 88 | pc.printf("HTTP Error return in GetGameState\n"); |
jderiso2 | 1:4e629f9ac9e8 | 89 | return (GAME_STATE)-1; |
jderiso2 | 1:4e629f9ac9e8 | 90 | } |
jderiso2 | 1:4e629f9ac9e8 | 91 | s = string(str); |
jderiso2 | 1:4e629f9ac9e8 | 92 | if (s.find("\"GameState\":\"STARTING\"") != string::npos) |
jderiso2 | 1:4e629f9ac9e8 | 93 | return STARTING; |
jderiso2 | 1:4e629f9ac9e8 | 94 | else if(s.find("\"GameState\":\"PLAYING\"") != string::npos) |
jderiso2 | 1:4e629f9ac9e8 | 95 | return PLAYING; |
jderiso2 | 1:4e629f9ac9e8 | 96 | else if(s.find("\"GameState\":\"OVER\"") != string::npos) |
jderiso2 | 1:4e629f9ac9e8 | 97 | return OVER; |
jderiso2 | 1:4e629f9ac9e8 | 98 | else |
jderiso2 | 1:4e629f9ac9e8 | 99 | return RETURN_ERROR; |
honelight | 0:28f8ba171e86 | 100 | |
jderiso2 | 1:4e629f9ac9e8 | 101 | return (GAME_STATE)atoi(str); |
jderiso2 | 1:4e629f9ac9e8 | 102 | } |
jderiso2 | 1:4e629f9ac9e8 | 103 | void ParseBoard(string str, int** board) |
jderiso2 | 1:4e629f9ac9e8 | 104 | { |
jderiso2 | 1:4e629f9ac9e8 | 105 | |
jderiso2 | 1:4e629f9ac9e8 | 106 | // printf("Entering ParseBoard, got %s\n", str.c_str()); |
jderiso2 | 1:4e629f9ac9e8 | 107 | for (int i = 0; i < str.length(); i++){ |
jderiso2 | 1:4e629f9ac9e8 | 108 | if (str[i] == '[' || str[i] == ']' || str[i] == ',' || str[i] == '"') |
jderiso2 | 1:4e629f9ac9e8 | 109 | str[i] = ' '; |
jderiso2 | 1:4e629f9ac9e8 | 110 | } |
jderiso2 | 1:4e629f9ac9e8 | 111 | string temp; |
jderiso2 | 1:4e629f9ac9e8 | 112 | for (int i = 0; i < str.length() - 1; i++){ |
jderiso2 | 1:4e629f9ac9e8 | 113 | if (!(str[i] == ' ' && str[i+1] == ' ')){ |
jderiso2 | 1:4e629f9ac9e8 | 114 | temp += str[i]; |
jderiso2 | 1:4e629f9ac9e8 | 115 | } |
jderiso2 | 1:4e629f9ac9e8 | 116 | |
jderiso2 | 1:4e629f9ac9e8 | 117 | } |
jderiso2 | 1:4e629f9ac9e8 | 118 | |
jderiso2 | 1:4e629f9ac9e8 | 119 | str = temp; |
jderiso2 | 1:4e629f9ac9e8 | 120 | // printf("With changed delimiters %s\n", str.c_str()); |
jderiso2 | 1:4e629f9ac9e8 | 121 | stringstream stream(str); |
jderiso2 | 1:4e629f9ac9e8 | 122 | int t = 0; |
jderiso2 | 1:4e629f9ac9e8 | 123 | for (int i = 0; i < 3; i++){ |
jderiso2 | 1:4e629f9ac9e8 | 124 | for (int j = 0; j < 3; j++){ |
jderiso2 | 1:4e629f9ac9e8 | 125 | while(t < str.length() && str[t] == ' ') |
jderiso2 | 1:4e629f9ac9e8 | 126 | t++; |
jderiso2 | 1:4e629f9ac9e8 | 127 | board[i][j] = (int)(str[t] - '0'); |
jderiso2 | 1:4e629f9ac9e8 | 128 | //stream >> board[i][j]; |
jderiso2 | 1:4e629f9ac9e8 | 129 | } |
jderiso2 | 1:4e629f9ac9e8 | 130 | } |
honelight | 0:28f8ba171e86 | 131 | |
jderiso2 | 1:4e629f9ac9e8 | 132 | /* char c; |
jderiso2 | 1:4e629f9ac9e8 | 133 | stream >> c; // eat [ |
jderiso2 | 1:4e629f9ac9e8 | 134 | for (int i = 0; i < 3; i++) { |
jderiso2 | 1:4e629f9ac9e8 | 135 | stream >> c; // eat [ |
jderiso2 | 1:4e629f9ac9e8 | 136 | for (int j = 0; j < 2; j++) { |
jderiso2 | 1:4e629f9ac9e8 | 137 | stream >> board[i][j]; |
jderiso2 | 1:4e629f9ac9e8 | 138 | stream >> c ;// eat , |
jderiso2 | 1:4e629f9ac9e8 | 139 | } |
jderiso2 | 1:4e629f9ac9e8 | 140 | stream >> board[i][2]; |
jderiso2 | 1:4e629f9ac9e8 | 141 | stream >> c ;// eat ] |
jderiso2 | 1:4e629f9ac9e8 | 142 | if (i != 2) |
jderiso2 | 1:4e629f9ac9e8 | 143 | stream >> c; // eat , |
jderiso2 | 1:4e629f9ac9e8 | 144 | }*/ |
jderiso2 | 1:4e629f9ac9e8 | 145 | /* for (int i = 0; i < 3; i++) { |
jderiso2 | 1:4e629f9ac9e8 | 146 | printf("\n["); |
jderiso2 | 1:4e629f9ac9e8 | 147 | for (int j = 0; j < 3; j++) |
jderiso2 | 1:4e629f9ac9e8 | 148 | printf("%d ", board[i][j]); |
jderiso2 | 1:4e629f9ac9e8 | 149 | printf("]"); |
jderiso2 | 1:4e629f9ac9e8 | 150 | }*/ |
jderiso2 | 1:4e629f9ac9e8 | 151 | } |
jderiso2 | 1:4e629f9ac9e8 | 152 | int GetBoardState(int gameId, int** board) |
jderiso2 | 1:4e629f9ac9e8 | 153 | { |
jderiso2 | 1:4e629f9ac9e8 | 154 | ostringstream host; |
jderiso2 | 1:4e629f9ac9e8 | 155 | host << "http://4180.azurewebsites.net/odata/TicTacToeGames(" << gameId << ")?$select=BoardState"; |
jderiso2 | 1:4e629f9ac9e8 | 156 | string s = host.str(); |
jderiso2 | 1:4e629f9ac9e8 | 157 | HTTPResult ret = http.get(s.c_str(), str, 4096); |
jderiso2 | 1:4e629f9ac9e8 | 158 | if (ret > 0) { |
jderiso2 | 1:4e629f9ac9e8 | 159 | pc.printf("HTTP Error return in GetBoardState\n"); |
jderiso2 | 1:4e629f9ac9e8 | 160 | return -1; |
jderiso2 | 1:4e629f9ac9e8 | 161 | } |
jderiso2 | 1:4e629f9ac9e8 | 162 | s = string(str); |
jderiso2 | 1:4e629f9ac9e8 | 163 | int pos = s.find("\"BoardState\":"); |
jderiso2 | 1:4e629f9ac9e8 | 164 | if (pos == string::npos) |
jderiso2 | 1:4e629f9ac9e8 | 165 | return -1; |
jderiso2 | 1:4e629f9ac9e8 | 166 | |
jderiso2 | 1:4e629f9ac9e8 | 167 | ParseBoard(s.substr(pos +13, string::npos), board); |
jderiso2 | 1:4e629f9ac9e8 | 168 | return 0; |
jderiso2 | 1:4e629f9ac9e8 | 169 | } |
jderiso2 | 1:4e629f9ac9e8 | 170 | string GameStateToString(GAME_STATE state) |
jderiso2 | 1:4e629f9ac9e8 | 171 | { |
jderiso2 | 1:4e629f9ac9e8 | 172 | switch(state) { |
jderiso2 | 1:4e629f9ac9e8 | 173 | case STARTING: |
jderiso2 | 1:4e629f9ac9e8 | 174 | return "STARTING"; |
jderiso2 | 1:4e629f9ac9e8 | 175 | case PLAYING: |
jderiso2 | 1:4e629f9ac9e8 | 176 | return "PLAYING"; |
jderiso2 | 1:4e629f9ac9e8 | 177 | case OVER: |
jderiso2 | 1:4e629f9ac9e8 | 178 | return "OVER"; |
jderiso2 | 1:4e629f9ac9e8 | 179 | default: |
jderiso2 | 1:4e629f9ac9e8 | 180 | case RETURN_ERROR: |
jderiso2 | 1:4e629f9ac9e8 | 181 | return "ERROR"; |
jderiso2 | 1:4e629f9ac9e8 | 182 | } |
jderiso2 | 1:4e629f9ac9e8 | 183 | } |
jderiso2 | 1:4e629f9ac9e8 | 184 | int main() |
jderiso2 | 1:4e629f9ac9e8 | 185 | { |
jderiso2 | 1:4e629f9ac9e8 | 186 | pc.printf("Ethernet connecting...\n"); |
honelight | 0:28f8ba171e86 | 187 | eth.init(); //Use DHCP |
honelight | 0:28f8ba171e86 | 188 | eth.connect(); |
jderiso2 | 1:4e629f9ac9e8 | 189 | pc.printf("IP Address is %s\n", eth.getIPAddress()); |
jderiso2 | 1:4e629f9ac9e8 | 190 | pc.printf("Trying Start New Game...\n"); |
jderiso2 | 1:4e629f9ac9e8 | 191 | int game = StartNewGame(); |
jderiso2 | 1:4e629f9ac9e8 | 192 | wait(1); |
jderiso2 | 1:4e629f9ac9e8 | 193 | pc.printf("Got: %d\n\n", game); |
jderiso2 | 1:4e629f9ac9e8 | 194 | pc.printf("Trying GetGameState for Game %d...\n", game); |
jderiso2 | 1:4e629f9ac9e8 | 195 | pc.printf("Got: %s\n\n", GameStateToString(GetGameState(game)).c_str()); |
jderiso2 | 1:4e629f9ac9e8 | 196 | pc.printf("Trying Join Game...\n"); |
jderiso2 | 1:4e629f9ac9e8 | 197 | game = JoinGame(); |
jderiso2 | 1:4e629f9ac9e8 | 198 | wait(1); |
jderiso2 | 1:4e629f9ac9e8 | 199 | pc.printf("Got: %d\n\n", game); |
jderiso2 | 1:4e629f9ac9e8 | 200 | //pc.printf("Trying GetGameState for Game %d...\n", game); |
jderiso2 | 1:4e629f9ac9e8 | 201 | //pc.printf("Got: %s\n\n", GameStateToString(GetGameState(game)).c_str()); |
jderiso2 | 1:4e629f9ac9e8 | 202 | pc.printf("Trying GetBoardState for Game %d...\n", game); |
jderiso2 | 1:4e629f9ac9e8 | 203 | Board b; |
jderiso2 | 1:4e629f9ac9e8 | 204 | wait(1); |
jderiso2 | 1:4e629f9ac9e8 | 205 | pc.printf("Got: %d\n\n", GetBoardState(game, b.get_board())); |
jderiso2 | 1:4e629f9ac9e8 | 206 | pc.printf("Testing over****\n"); |
jderiso2 | 1:4e629f9ac9e8 | 207 | /*pc.printf("%d\n", sizeof(str)); |
jderiso2 | 1:4e629f9ac9e8 | 208 | HTTPResult ret = http.get("http://4180.azurewebsites.net/api/TicTacToeGames/New", str, 4096);//http.get("http://4180.azurewebsites.net/", str, 4096); |
jderiso2 | 1:4e629f9ac9e8 | 209 | if (ret > 0) |
jderiso2 | 1:4e629f9ac9e8 | 210 | pc.printf("Return error\n"); |
jderiso2 | 1:4e629f9ac9e8 | 211 | for (int i = 0; i < 4096;i++){ |
jderiso2 | 1:4e629f9ac9e8 | 212 | if (str[i] == '1'|| str[i] == '2' || str[i] == '3' || str[i] == '4') |
jderiso2 | 1:4e629f9ac9e8 | 213 | pc.printf("HERE %d\n", i); |
jderiso2 | 1:4e629f9ac9e8 | 214 | pc.putc(str[i]); |
jderiso2 | 1:4e629f9ac9e8 | 215 | pc.printf("\n"); |
jderiso2 | 1:4e629f9ac9e8 | 216 | }*/ |
jderiso2 | 1:4e629f9ac9e8 | 217 | return 0; |
jderiso2 | 1:4e629f9ac9e8 | 218 | TCPSocketConnection socket; |
jderiso2 | 1:4e629f9ac9e8 | 219 | |
jderiso2 | 1:4e629f9ac9e8 | 220 | |
jderiso2 | 1:4e629f9ac9e8 | 221 | int r = socket.connect("4180.azurewebsites.net", 80); |
jderiso2 | 1:4e629f9ac9e8 | 222 | if (r != 0) |
jderiso2 | 1:4e629f9ac9e8 | 223 | pc.printf("Failed to connect to 4180.azurewebsites.net:80!\r\n"); |
jderiso2 | 1:4e629f9ac9e8 | 224 | char http_cmd[] = "GET / HTTP/1.0\n\n"; |
jderiso2 | 1:4e629f9ac9e8 | 225 | socket.send_all(http_cmd, sizeof(http_cmd)-1); |
jderiso2 | 1:4e629f9ac9e8 | 226 | |
jderiso2 | 1:4e629f9ac9e8 | 227 | char buff[300]; |
jderiso2 | 1:4e629f9ac9e8 | 228 | int rtn; |
jderiso2 | 1:4e629f9ac9e8 | 229 | while (true) { |
jderiso2 | 1:4e629f9ac9e8 | 230 | rtn = socket.receive(buff, sizeof(buff)-1); |
jderiso2 | 1:4e629f9ac9e8 | 231 | if (rtn <= 0) |
jderiso2 | 1:4e629f9ac9e8 | 232 | break; |
jderiso2 | 1:4e629f9ac9e8 | 233 | buff[rtn] = '\0'; |
jderiso2 | 1:4e629f9ac9e8 | 234 | pc.printf("Received %d chars from server:\n%s\n", rtn, buff); |
jderiso2 | 1:4e629f9ac9e8 | 235 | } |
jderiso2 | 1:4e629f9ac9e8 | 236 | pc.printf("Closing socket...\n"); |
jderiso2 | 1:4e629f9ac9e8 | 237 | socket.close(); |
jderiso2 | 1:4e629f9ac9e8 | 238 | eth.disconnect(); |
jderiso2 | 1:4e629f9ac9e8 | 239 | /* int ret = http.get("http://ece4180mbedonlinegaming.azurewebsites.net/odata/TicTacToeGames(1)?$select=BoardState", str, 2048); |
jderiso2 | 1:4e629f9ac9e8 | 240 | printf("%d\n",ret); |
jderiso2 | 1:4e629f9ac9e8 | 241 | printf("Result: %s\n", str);*/ |
honelight | 0:28f8ba171e86 | 242 | /* |
honelight | 0:28f8ba171e86 | 243 | in0.mode(PullUp); |
honelight | 0:28f8ba171e86 | 244 | in1.mode(PullUp); |
honelight | 0:28f8ba171e86 | 245 | in2.mode(PullUp); |
honelight | 0:28f8ba171e86 | 246 | in3.mode(PullUp); |
honelight | 0:28f8ba171e86 | 247 | in4.mode(PullUp); |
honelight | 0:28f8ba171e86 | 248 | in5.mode(PullUp); |
honelight | 0:28f8ba171e86 | 249 | in6.mode(PullUp); |
honelight | 0:28f8ba171e86 | 250 | in7.mode(PullUp); |
honelight | 0:28f8ba171e86 | 251 | in8.mode(PullUp); |
jderiso2 | 1:4e629f9ac9e8 | 252 | |
honelight | 0:28f8ba171e86 | 253 | Board board; |
honelight | 0:28f8ba171e86 | 254 | int ** output = board.get_board(); |
honelight | 0:28f8ba171e86 | 255 | printBoard(output); |
honelight | 0:28f8ba171e86 | 256 | lcd.baudrate(3000000); |
jderiso2 | 1:4e629f9ac9e8 | 257 | |
honelight | 0:28f8ba171e86 | 258 | lcd.line(0, 42 , 127, 42 , 0xFF0000); |
honelight | 0:28f8ba171e86 | 259 | lcd.line(0, 84 , 127, 84 , 0xFF0000); |
honelight | 0:28f8ba171e86 | 260 | lcd.line(42, 0 , 42, 127, 0xFF0000); |
honelight | 0:28f8ba171e86 | 261 | lcd.line(84, 0, 84, 127, 0xFF0000); |
jderiso2 | 1:4e629f9ac9e8 | 262 | |
honelight | 0:28f8ba171e86 | 263 | while (true) { |
honelight | 0:28f8ba171e86 | 264 | if(!in0) |
honelight | 0:28f8ba171e86 | 265 | printout = 1; |
honelight | 0:28f8ba171e86 | 266 | else if(!in1) |
honelight | 0:28f8ba171e86 | 267 | printout = 2; |
honelight | 0:28f8ba171e86 | 268 | else if(!in2) |
honelight | 0:28f8ba171e86 | 269 | printout = 3; |
honelight | 0:28f8ba171e86 | 270 | else if(!in3) |
honelight | 0:28f8ba171e86 | 271 | printout = 4; |
honelight | 0:28f8ba171e86 | 272 | else if(!in4) |
honelight | 0:28f8ba171e86 | 273 | printout = 5; |
honelight | 0:28f8ba171e86 | 274 | else if(!in5) |
honelight | 0:28f8ba171e86 | 275 | printout = 6; |
honelight | 0:28f8ba171e86 | 276 | else if(!in6) |
honelight | 0:28f8ba171e86 | 277 | printout = 7; |
honelight | 0:28f8ba171e86 | 278 | else if(!in7) |
honelight | 0:28f8ba171e86 | 279 | printout = 8; |
honelight | 0:28f8ba171e86 | 280 | else if(!in8) |
honelight | 0:28f8ba171e86 | 281 | printout = 9; |
honelight | 0:28f8ba171e86 | 282 | else |
honelight | 0:28f8ba171e86 | 283 | printout = 0; |
jderiso2 | 1:4e629f9ac9e8 | 284 | |
jderiso2 | 1:4e629f9ac9e8 | 285 | |
honelight | 0:28f8ba171e86 | 286 | if(printout>0) |
honelight | 0:28f8ba171e86 | 287 | { |
honelight | 0:28f8ba171e86 | 288 | bool valid = board.updateBoard(turn, printout-1); |
honelight | 0:28f8ba171e86 | 289 | |
honelight | 0:28f8ba171e86 | 290 | if(valid) |
honelight | 0:28f8ba171e86 | 291 | { |
honelight | 0:28f8ba171e86 | 292 | output = board.get_board(); |
honelight | 0:28f8ba171e86 | 293 | printBoard(output); |
honelight | 0:28f8ba171e86 | 294 | printout=0; |
honelight | 0:28f8ba171e86 | 295 | int victory = board.check_victory(turn); |
honelight | 0:28f8ba171e86 | 296 | if(victory ==1) |
honelight | 0:28f8ba171e86 | 297 | { |
honelight | 0:28f8ba171e86 | 298 | lcd.cls(); |
honelight | 0:28f8ba171e86 | 299 | lcd.printf("player 1 win!"); |
honelight | 0:28f8ba171e86 | 300 | return 0; |
honelight | 0:28f8ba171e86 | 301 | } |
honelight | 0:28f8ba171e86 | 302 | else if(victory == 2) |
honelight | 0:28f8ba171e86 | 303 | { |
honelight | 0:28f8ba171e86 | 304 | lcd.cls(); |
honelight | 0:28f8ba171e86 | 305 | lcd.printf("player 2 win!"); |
honelight | 0:28f8ba171e86 | 306 | return 0; |
honelight | 0:28f8ba171e86 | 307 | } |
honelight | 0:28f8ba171e86 | 308 | if(turn ==1) |
honelight | 0:28f8ba171e86 | 309 | turn =2; |
honelight | 0:28f8ba171e86 | 310 | else |
honelight | 0:28f8ba171e86 | 311 | turn =1; |
honelight | 0:28f8ba171e86 | 312 | } |
honelight | 0:28f8ba171e86 | 313 | } |
honelight | 0:28f8ba171e86 | 314 | Thread::wait(400); |
jderiso2 | 1:4e629f9ac9e8 | 315 | } |
honelight | 0:28f8ba171e86 | 316 | */ |
jderiso2 | 1:4e629f9ac9e8 | 317 | |
honelight | 0:28f8ba171e86 | 318 | } |
honelight | 0:28f8ba171e86 | 319 |