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: 4DGL-uLCD-SE EthernetInterface HTTPClient mbed-rtos mbed
Fork of Final_test by
Diff: main.cpp
- Revision:
- 3:b834ab4a53d1
- Parent:
- 1:4e629f9ac9e8
- Child:
- 4:427dbdf3ccd5
--- a/main.cpp Wed Apr 22 01:48:08 2015 +0000
+++ b/main.cpp Mon Apr 27 17:38:55 2015 +0000
@@ -53,10 +53,11 @@
} GAME_STATE;
HTTPClient http;
EthernetInterface eth;
-char str[4096];
+char str[1024];
int StartNewGame()
{
- HTTPResult ret = http.get("http://4180.azurewebsites.net/api/TicTacToeGames/New", str, 4096);
+ char* url = "http://4180.azurewebsites.net/api/TicTacToeGames/New\0";
+ HTTPResult ret = http.get(url, str, 1024);
if (ret > 0) {
pc.printf("HTTP Error return in StartNewGame\n");
return -1;
@@ -65,25 +66,23 @@
}
int JoinGame()
{
- HTTPResult ret = http.get("http://4180.azurewebsites.net/api/TicTacToeGames/Join", str, 4096);
+ char* url = "http://4180.azurewebsites.net/api/TicTacToeGames/Join\0";
+ HTTPResult ret = http.get(url, str, 1024);
if (ret > 0) {
pc.printf("HTTP Error return in JoinGame\n");
return -1;
}
return atoi(str);
}
-ostringstream host;
-string s;
-const char* cstr;
GAME_STATE GetGameState(int gameId)
{
- //ostringstream host;
+ string s;
+ stringstream host;
host.clear();
host << "http://4180.azurewebsites.net/odata/TicTacToeGames(" << gameId << ")?$select=GameState";
- s = host.str();
- cstr = s.c_str();
- printf("**** HOST IS %s\n", cstr);
- HTTPResult ret = http.get(cstr, str, 4096);
+ //printf("**** HOST IS %s\n", host.str().c_str());
+
+ HTTPResult ret = http.get(host.str().c_str(), str, 1024, 15000);
if (ret > 0) {
pc.printf("HTTP Error return in GetGameState\n");
return (GAME_STATE)-1;
@@ -98,63 +97,106 @@
else
return RETURN_ERROR;
- return (GAME_STATE)atoi(str);
+ // return (GAME_STATE)atoi(str);
}
void ParseBoard(string str, int** board)
{
-
- // printf("Entering ParseBoard, got %s\n", str.c_str());
- for (int i = 0; i < str.length(); i++){
+
+ // printf("Entering ParseBoard, got %s\n", str.c_str());
+ for (int i = 0; i < str.length(); i++) {
if (str[i] == '[' || str[i] == ']' || str[i] == ',' || str[i] == '"')
- str[i] = ' ';
+ str[i] = ' ';
}
string temp;
- for (int i = 0; i < str.length() - 1; i++){
- if (!(str[i] == ' ' && str[i+1] == ' ')){
+ for (int i = 0; i < str.length() - 1; i++) {
+ if (!(str[i] == ' ' && str[i+1] == ' ')) {
temp += str[i];
}
}
-
+
str = temp;
- // printf("With changed delimiters %s\n", str.c_str());
+ // printf("With changed delimiters %s\n", str.c_str());
stringstream stream(str);
int t = 0;
- for (int i = 0; i < 3; i++){
- for (int j = 0; j < 3; j++){
- while(t < str.length() && str[t] == ' ')
- t++;
- board[i][j] = (int)(str[t] - '0');
- //stream >> board[i][j];
- }
+ for (int i = 0; i < 3; i++) {
+ for (int j = 0; j < 3; j++) {
+ stream >> board[i][j];
+ //printf("%d\n", board[i][j]);
+ }
}
-
- /* char c;
- stream >> c; // eat [
- for (int i = 0; i < 3; i++) {
- stream >> c; // eat [
- for (int j = 0; j < 2; j++) {
- stream >> board[i][j];
- stream >> c ;// eat ,
- }
- stream >> board[i][2];
- stream >> c ;// eat ]
- if (i != 2)
- stream >> c; // eat ,
- }*/
- /* for (int i = 0; i < 3; i++) {
+
+ /* for (int i = 0; i < 3; i++) {
printf("\n[");
for (int j = 0; j < 3; j++)
printf("%d ", board[i][j]);
printf("]");
}*/
}
+int PostMove(int playerId,int gameId, int x, int y)
+{
+ stringstream host;
+ host << "http://4180.azurewebsites.net/api/TicTacToeGames?gameId=" << gameId << "&playerId=" << playerId << "&moveX=" << x << "&moveY=" << y;
+ string s = host.str();
+ HTTPText datain("");
+ HTTPMap dataout;
+ HTTPResult ret = http.post(s.c_str(), dataout, &datain, 10000);
+ if (ret > 0) {
+ pc.printf("HTTP Error in PostMove");
+ return -1;
+ }
+ return 0;
+
+}
+int GetCurrentPlayer(int gameId)
+{
+
+ stringstream host;
+ host << "http://4180.azurewebsites.net/odata/TicTacToeGames(" << gameId << ")?$select=CurrentPlayer";
+ string s = host.str();
+ int retry_count = 5;
+ HTTPResult ret = (HTTPResult)1;
+ while (ret > 0 & retry_count-- > 0)
+ ret = http.get(s.c_str(), str, 1024);
+ if (ret > 0) {
+ pc.printf("HTTP Error return in GetCurrentPlayer\n");
+ return -1;
+ }
+ s = string(str);
+ int pos = s.find("\"CurrentPlayer\":");
+ if (pos == string::npos)
+ return -1;
+ return s[pos+16] - '0';
+}
+int GetWinner(int gameId)
+{
+
+ stringstream host;
+ host << "http://4180.azurewebsites.net/odata/TicTacToeGames(" << gameId << ")?$select=Winner";
+ string s = host.str();
+ int retry_count = 5;
+ HTTPResult ret = (HTTPResult)1;
+ while (ret > 0 & retry_count-- > 0)
+ ret = http.get(s.c_str(), str, 1024);
+ if (ret > 0) {
+ pc.printf("HTTP Error return in GetWinner\n");
+ return -1;
+ }
+ s = string(str);
+ int pos = s.find("\"Winner\":");
+ if (pos == string::npos)
+ return -1;
+ return s[pos+9] - '0';
+}
int GetBoardState(int gameId, int** board)
{
- ostringstream host;
+ stringstream host;
host << "http://4180.azurewebsites.net/odata/TicTacToeGames(" << gameId << ")?$select=BoardState";
string s = host.str();
- HTTPResult ret = http.get(s.c_str(), str, 4096);
+ int retry_count = 5;
+ HTTPResult ret = (HTTPResult)1;
+ while (ret > 0 & retry_count-- > 0)
+ ret = http.get(s.c_str(), str, 1024);
if (ret > 0) {
pc.printf("HTTP Error return in GetBoardState\n");
return -1;
@@ -181,15 +223,244 @@
return "ERROR";
}
}
+int GetSerialInput()
+{
+ return pc.getc() - '0';
+}
+void ScanForMove(int* x, int* y, int** board)
+{
+ // the following commented section can be used to input moves over pc terminal by pressing 00 for move at location [0,0]
+ /*
+ pc.printf("Entering scan\n");
+ *x = GetSerialInput();
+ *y = GetSerialInput();
+ return;
+ */
+
+ while(1) {
+ if (!in0 && board[0][0]== -1) {
+ *x = 0;
+ *y = 0;
+ return;
+ } else if (!in1 && board[0][1] == -1) {
+ *x = 0;
+ *y = 1;
+ return;
+ } else if (!in2 && board[0][2] == -1) {
+ *x = 0;
+ *y = 2;
+ return;
+ } else if (!in3 && board[1][0] == -1) {
+ *x = 1;
+ *y = 0;
+ return;
+ } else if (!in4 && board[1][1] == -1) {
+ *x = 1;
+ *y = 1;
+ return;
+ } else if (!in5 && board[1][2] == -1) {
+ *x = 1;
+ *y = 2;
+ return;
+ } else if (!in6 && board[2][0] == -1) {
+ *x = 2;
+ *y = 0;
+ return;
+ } else if (!in7 && board[2][1] == -1) {
+ *x = 2;
+ *y = 1;
+ return;
+ } else if (!in8 && board[2][2] == -1) {
+ *x = 2;
+ *y = 2;
+ return;
+ }
+ wait(0.2);
+ }
+}
int main()
{
pc.printf("Ethernet connecting...\n");
eth.init(); //Use DHCP
eth.connect();
pc.printf("IP Address is %s\n", eth.getIPAddress());
+
+ in0.mode(PullUp);
+ in1.mode(PullUp);
+ in2.mode(PullUp);
+ in3.mode(PullUp);
+ in4.mode(PullUp);
+ in5.mode(PullUp);
+ in6.mode(PullUp);
+ in7.mode(PullUp);
+ in8.mode(PullUp);
+ lcd.baudrate(3000000);
+
+ // START GAME OR JOIN GAME
+ // FORMAT THIS TO LOOK PRETTY ON LCD
+ int gameId = -1;
+ int playerId = -1;
+ bool reprint = true;
+ do {
+ if (reprint) {
+ lcd.cls();
+ lcd.printf("Button 0: Start New Game\nButton 1: Join Existing Game");
+ reprint = false;
+ }
+ // I'm assuming by looking at the code that
+ // you guys made the buttons active low so inverting the read
+ if (!in0 /*|| true*/) {
+ // Start Game
+ gameId = StartNewGame();
+ if (gameId < 1) { // fail
+ lcd.printf("Failed to start a new game!\n");
+ wait(1.5);
+ reprint = true;
+ } else
+ playerId = 0;
+
+ } else if(!in1) {
+ gameId = JoinGame();
+ if (gameId < 1) { // fail
+ lcd.printf("Failed to join game, try making one!\n");
+ wait(1.5);
+ reprint = true;
+ } else
+ playerId = 1;
+ }
+ } while (gameId < 1);
+ lcd.cls();
+
+ if (playerId == 1){
+ pc.printf("Successfully joined game %d!\n", gameId);
+ lcd.printf("Successfully joined game %d!\n", gameId);
+ }
+ else{
+ pc.printf("Successfully started game %d!\n", gameId);
+ lcd.printf("Successfully started game %d!\n", gameId);
+ }
+
+ wait(1.5);
+
+ Board board;
+ lcd.cls();
+ lcd.printf("Waiting for game to start...\n");
+ // wait for game to start
+ while(GetGameState(gameId) != PLAYING) {
+ wait(2);
+ }
+
+ // Main Game Loop
+ lcd.cls();
+
+ while(1) {
+ GAME_STATE state = GetGameState(gameId);
+ if (state == OVER)
+ break; // game over
+ // wait for turn
+ while(GetCurrentPlayer(gameId) != playerId){ wait(1.5); }
+
+ GetBoardState(gameId, board.get_board());
+ printBoard(board.get_board());
+ int moveX, moveY;
+ ScanForMove(&moveX,&moveY, board.get_board());
+ PostMove(playerId, gameId, moveX, moveY);
+ GetBoardState(gameId, board.get_board());
+ printBoard(board.get_board());
+ wait(2);
+ }
+ int winner = GetWinner(gameId);
+ pc.printf("Player %d Won!\n", winner);
+ lcd.printf("Player %d Won!\n", winner);
+ /* lcd.line(0, 42 , 127, 42 , 0xFF0000);
+ lcd.line(0, 84 , 127, 84 , 0xFF0000);
+ lcd.line(42, 0 , 42, 127, 0xFF0000);
+ lcd.line(84, 0, 84, 127, 0xFF0000);*/
+/*
+ while (true) {
+ if(!in0)
+ printout = 1;
+ else if(!in1)
+ printout = 2;
+ else if(!in2)
+ printout = 3;
+ else if(!in3)
+ printout = 4;
+ else if(!in4)
+ printout = 5;
+ else if(!in5)
+ printout = 6;
+ else if(!in6)
+ printout = 7;
+ else if(!in7)
+ printout = 8;
+ else if(!in8)
+ printout = 9;
+ else
+ printout = 0;
+
+
+ if(printout>0) {
+ bool valid = board.updateBoard(turn, printout-1);
+
+ if(valid) {
+ output = board.get_board();
+ printBoard(output);
+ printout=0;
+ int victory = board.check_victory(turn);
+ if(victory ==1) {
+ lcd.cls();
+ lcd.printf("player 1 win!");
+ return 0;
+ } else if(victory == 2) {
+ lcd.cls();
+ lcd.printf("player 2 win!");
+ return 0;
+ }
+ if(turn ==1)
+ turn =2;
+ else
+ turn =1;
+ }
+ }
+ Thread::wait(400);
+ }
+ */
+
+}
+
+
+
+int TEST_FN()
+{
pc.printf("Trying Start New Game...\n");
int game = StartNewGame();
wait(1);
+ while (JoinGame() != game) {
+ wait(0.5);
+ }
+ int player = 0;
+ Board b;
+ printf("Joined Game %d\n", game);
+ bool sentinel = true;
+ while (sentinel) {
+ GetBoardState(game, b.get_board());
+ printBoard(b.get_board());
+ if (player == 0) {
+ PostMove(player, game, 0, 0);
+ pc.printf("Exited first post move\n");
+ player = 1;
+ } else {
+ PostMove(1, game, 1, 1);
+ sentinel = false;
+ }
+ wait(0.5);
+ }
+ pc.printf("Done with move\n");
+ wait(0.5);
+ GetBoardState(game, b.get_board());
+ printBoard(b.get_board());
+
pc.printf("Got: %d\n\n", game);
pc.printf("Trying GetGameState for Game %d...\n", game);
pc.printf("Got: %s\n\n", GameStateToString(GetGameState(game)).c_str());
@@ -200,7 +471,7 @@
//pc.printf("Trying GetGameState for Game %d...\n", game);
//pc.printf("Got: %s\n\n", GameStateToString(GetGameState(game)).c_str());
pc.printf("Trying GetBoardState for Game %d...\n", game);
- Board b;
+ // Board b;
wait(1);
pc.printf("Got: %d\n\n", GetBoardState(game, b.get_board()));
pc.printf("Testing over****\n");
@@ -236,84 +507,5 @@
pc.printf("Closing socket...\n");
socket.close();
eth.disconnect();
- /* int ret = http.get("http://ece4180mbedonlinegaming.azurewebsites.net/odata/TicTacToeGames(1)?$select=BoardState", str, 2048);
- printf("%d\n",ret);
- printf("Result: %s\n", str);*/
- /*
- in0.mode(PullUp);
- in1.mode(PullUp);
- in2.mode(PullUp);
- in3.mode(PullUp);
- in4.mode(PullUp);
- in5.mode(PullUp);
- in6.mode(PullUp);
- in7.mode(PullUp);
- in8.mode(PullUp);
-
- Board board;
- int ** output = board.get_board();
- printBoard(output);
- lcd.baudrate(3000000);
-
- lcd.line(0, 42 , 127, 42 , 0xFF0000);
- lcd.line(0, 84 , 127, 84 , 0xFF0000);
- lcd.line(42, 0 , 42, 127, 0xFF0000);
- lcd.line(84, 0, 84, 127, 0xFF0000);
-
- while (true) {
- if(!in0)
- printout = 1;
- else if(!in1)
- printout = 2;
- else if(!in2)
- printout = 3;
- else if(!in3)
- printout = 4;
- else if(!in4)
- printout = 5;
- else if(!in5)
- printout = 6;
- else if(!in6)
- printout = 7;
- else if(!in7)
- printout = 8;
- else if(!in8)
- printout = 9;
- else
- printout = 0;
-
-
- if(printout>0)
- {
- bool valid = board.updateBoard(turn, printout-1);
-
- if(valid)
- {
- output = board.get_board();
- printBoard(output);
- printout=0;
- int victory = board.check_victory(turn);
- if(victory ==1)
- {
- lcd.cls();
- lcd.printf("player 1 win!");
- return 0;
- }
- else if(victory == 2)
- {
- lcd.cls();
- lcd.printf("player 2 win!");
- return 0;
- }
- if(turn ==1)
- turn =2;
- else
- turn =1;
- }
- }
- Thread::wait(400);
- }
- */
-
}
