4180 fianl project
Dependencies: 4DGL-uLCD-SE EthernetInterface HTTPClient mbed-rtos mbed
Revision 0:28f8ba171e86, committed 2015-04-20
- Comitter:
- honelight
- Date:
- Mon Apr 20 23:05:36 2015 +0000
- Commit message:
- test program, still doesn't work;
Changed in this revision
diff -r 000000000000 -r 28f8ba171e86 4DGL-uLCD-SE.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4DGL-uLCD-SE.lib Mon Apr 20 23:05:36 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/4180_1/code/4DGL-uLCD-SE/#e39a44de229a
diff -r 000000000000 -r 28f8ba171e86 EthernetInterface.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Mon Apr 20 23:05:36 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/mbed_official/code/EthernetInterface/#2fc406e2553f
diff -r 000000000000 -r 28f8ba171e86 HTTPClient.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPClient.lib Mon Apr 20 23:05:36 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/HTTPClient/#be61104f4e91
diff -r 000000000000 -r 28f8ba171e86 board.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/board.cpp Mon Apr 20 23:05:36 2015 +0000 @@ -0,0 +1,69 @@ +#include "stdio.h" +#include "stdlib.h" +#include "board.h" + + + +Board::Board(void) +{ + board = new int*[3]; + for(int i=0; i<3; i++) + { + board[i] = new int[3]; + for(int j=0; j<3; j++) + board[i][j]=0; + } +} + +int ** Board::get_board() +{ + return board; +} + +void Board::set_board(int col, int row, int playerID) +{ + if(board[col][row]!=0 && turn == playerID) + {} + else + { + board[col][row] = playerID; + } +} + +bool Board::updateBoard(int turn, int printout) +{ + int location_x = printout/3; + int location_y = printout%3; + if(board[location_x][location_y] == 0) + { + board[location_x][location_y] = turn; + return true; + } + else + return false; +} + +int Board::updateBoard_output(int turn, int printout) +{ + int location_x = printout/3; + int location_y = printout%3; + return board[location_x][location_y]; +} + + +int Board::check_victory(int playerID) +{ + if(board[0][0] == playerID && board[0][1] == playerID && board[0][2] == playerID + ||board[0][0] == playerID && board[1][1] == playerID && board[2][2] == playerID + ||board[0][0] == playerID && board[1][0] == playerID && board[2][0] == playerID + ||board[1][0] == playerID && board[1][1] == playerID && board[1][2] == playerID + ||board[2][0] == playerID && board[2][1] == playerID && board[2][2] == playerID + ||board[0][1] == playerID && board[1][1] == playerID && board[1][2] == playerID + ||board[0][1] == playerID && board[2][1] == playerID && board[2][2] == playerID + ||board[0][2] == playerID && board[1][1] == playerID && board[2][0] == playerID) + return playerID; + else + return 0; +} + + \ No newline at end of file
diff -r 000000000000 -r 28f8ba171e86 board.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/board.h Mon Apr 20 23:05:36 2015 +0000 @@ -0,0 +1,18 @@ + + +class Board +{ + public: + int check_victory(int playerID); + void update_board(Board other_board); + int ** get_board(); + void set_board(int col, int row, int playerID); + bool updateBoard(int turn, int printout); + int updateBoard_output(int turn, int printout); + void printBoard(); + int turn; + Board(void); + + private: + int **board; +}; \ No newline at end of file
diff -r 000000000000 -r 28f8ba171e86 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Apr 20 23:05:36 2015 +0000 @@ -0,0 +1,143 @@ +#include "mbed.h" +#include "uLCD_4DGL.h" +#include "board.h" +#include "rtos.h" +#include "EthernetInterface.h" +#include "HTTPClient.h" + +uLCD_4DGL lcd(p28,p27,p30); // serial tx, serial rx, reset pin; + +DigitalIn in0(p11); +DigitalIn in1(p12); +DigitalIn in2(p13); +DigitalIn in3(p14); +DigitalIn in4(p15); +DigitalIn in5(p16); +DigitalIn in6(p17); +DigitalIn in7(p18); +DigitalIn in8(p19); + +Mutex stdio_mutex; + + + +int player1=1; +int player2=2; +int turn =1; +int printout = 0; + + +void printBoard(int ** board) +{ + lcd.cls(); + 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); + for(int i=0; i<3; i++) + { + for(int j=0; j<3; j++) + { + if(board[i][j] == 1) + { + lcd.line( j*42+2 , i*42 + 2 ,(j+1)*42-2 , (i+1)*42-2 , GREEN); + lcd.line( j*42+2 , (i+1)*42 - 2 ,(j+1)*42-2 , (i)*42+2 , GREEN); + } + else if(board[i][j] == 2) + { + lcd.circle(42*j+21, 42*i+21, 19, BLUE); + } + } + } +} + +HTTPClient http; +char str[2048]; +int main() { + + EthernetInterface eth; + eth.init(); //Use DHCP + eth.connect(); + printf("IP Address is %s\n", eth.getIPAddress()); + printf("%d\n", sizeof(str)); + 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); + } + */ + +} +
diff -r 000000000000 -r 28f8ba171e86 mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Mon Apr 20 23:05:36 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#631c0f1008c3
diff -r 000000000000 -r 28f8ba171e86 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Apr 20 23:05:36 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/487b796308b0 \ No newline at end of file