From 4b420fa31f1a4e8bc981f94f1654019d9c4bd717 Mon Sep 17 00:00:00 2001 From: Alexander Huddleston Date: Wed, 28 Oct 2015 15:03:49 -0500 Subject: [PATCH] Java Client fully functional through ascii characters. --- Board.cpp | 2 +- Client.class | Bin 0 -> 1753 bytes Client.java | 46 +++++++++++++++++++ Engine.cpp | 10 ++--- Server.cpp | 5 ++- Serverbackup.txt | 114 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 169 insertions(+), 8 deletions(-) create mode 100644 Client.class create mode 100755 Client.java create mode 100755 Serverbackup.txt diff --git a/Board.cpp b/Board.cpp index a7c7f8e..05adb8c 100755 --- a/Board.cpp +++ b/Board.cpp @@ -261,7 +261,7 @@ void Board::move(moves m){ int row = 8 - (m.row); int column = m.column; - cout << "INSIDE MOVE: " << row << " " << column << " " << m.moveType << "\n\n"; + //cout << "INSIDE MOVE: " << row << " " << column << " " << m.moveType << "\n\n"; if (row > 8 || row < 0 || column > 8 || column < 0) { cout<<"ERROR: index out of bound."<T~ixX7=BK&$=NJR`6_{JOG~RLP#_gWZGxzzRHINTsigt6F3BNXNH!tat*!Uo zd#4xPdC^O!Tx=ZE(V5OzXZ#)dH^les5?Dq?hB+Va$FuMAyl>8*|NQbRfLVO4;U&y* zyqUmTcw0jR*EOV(;h5JD$2%Mga(`EYiuX9)=U9~YtOi|1cHxFxmSpx#jt?~SV@}0~ za?i=-Be{I6VHqnj@Rr1(lZsVoyRG6Ajy0K`S8+$fMSRL}mt&nFK5yBUv%nA?pSa7Q zWNRhC(6emW;+9+85{*0NR)s7|)wFDei7(w%YZL9?(6sxCY9(1+QLcYYsFpRNcR@QfN!>HsoPYVv)-^A(a=#uN&4xN$&P=~ z-QE@rQCbsbNi-OYjw5H+U5A(nvl{qxY@;mGALy{Kqhl8ph9N&bB*UU>ReU6EO*HE@ zyD8F|jw)=q)O6Ib$3e9;LAAQr)A0~v^7<8n>WHR8QqF`3|f-d^v~a zUFVMLI^o%nNM{HsM1%Sm0tz_@XW$qo7zBUN6&j2Z10Uba=5n&=Xw8+Bj;iqoFPI+! ziJSGhuqk2s6mEwULNXi<5hb384;q^Jzj)QDb-H=BLn#C-`E({cR3Q56DJ&9cB{T%o5<{zbs(H#0`-D|8#7ozWhf zY=J$c1H-Gd#(iTq5*Wt>ohg)kj~KmSz~5~|3j2t*pe#>DXICbb*`E4XtC5>YyP=w07OUmN{} z76$sydKfek9+Do0JPaG$gHcGUFED4s4AsLqBj(|JfT0nF7Q!$z4;Kile}poh9K3jf zD`{mPmpa&^5}OLXOnU(Tlt?G>9KX|}9PuUDpgYh*67sZ!wDRDB=fF{zuaOCsMsdy8lLEKF1UxPvQ?;#b21F6L1~M0VQf2S2?CQ jrWwZ!M~dSb{e$B*-~jVbjst*2j@gS;>gxm$!5jYq93i9@ literal 0 HcmV?d00001 diff --git a/Client.java b/Client.java new file mode 100755 index 0000000..417a4b6 --- /dev/null +++ b/Client.java @@ -0,0 +1,46 @@ +// Alex Huddleston +// Breakthrough Client in Java + +import java.io.IOException; +import java.io.PrintWriter; +import java.net.ServerSocket; +import java.net.Socket; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.Scanner; +import java.io.DataInputStream; + +public class Client { + public static void main (String[] args) { + Scanner keyboard = new Scanner(System.in); + String hostname = args[0]; + int portnum = Integer.parseInt(args[1]); + //keyboard.nextLine(); // used to buffer out extra space. + + try { + Socket echoSocket = new Socket(hostname, portnum); + PrintWriter output = new PrintWriter(echoSocket.getOutputStream(), true); + BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); + BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); + + String userInput; + char[] b = new char[256]; + in.read(b, 0, 256); + System.out.println("Response:\n" + b); + + while((userInput = stdIn.readLine()) != null) { + output.println(userInput); + output.flush(); + char[] buffer = new char[256]; + in.read(buffer, 0, 256); + System.out.println(buffer); + System.out.println("testing."); + } + + } + + catch (IOException e){ + System.err.println("IOException: " + e.getMessage()); + } + } +} \ No newline at end of file diff --git a/Engine.cpp b/Engine.cpp index 18efac8..856f0b0 100755 --- a/Engine.cpp +++ b/Engine.cpp @@ -75,7 +75,7 @@ void Engine::easyAI() } void Engine::AI(){ - cout << "----------------------BEGIN AI FUNCTION----------------------\n"; + //cout << "----------------------BEGIN AI FUNCTION----------------------\n"; vector listOfMoves = b->viewPossibleMoves(); Board* temp = new Board(*b); @@ -101,12 +101,11 @@ void Engine::AI(){ } */ b->displayBoard(); - cout << "----------------------END AI FUNCTION----------------------\n"; + //cout << "----------------------END AI FUNCTION----------------------\n"; } moves Engine::minMax(Board* temp, moves m, int c, int r){ //testing purposes only, c = finite depth - //cout << "c: " << c << "\n\n"; //cout << "current turn: " << temp->getTurn() << "\n"; vector listOfMoves = temp->viewPossibleMoves(); @@ -117,7 +116,7 @@ moves Engine::minMax(Board* temp, moves m, int c, int r){ } */ if (temp->isGameOver() == true){ - cout << "END OF PATH REACHED\n"; + //cout << "END OF PATH REACHED\n"; return m; } @@ -125,8 +124,9 @@ moves Engine::minMax(Board* temp, moves m, int c, int r){ if(temp->getPiece(8 - m.row, m.column)->getType() == temp->getTurn() && temp->isThisMovePossible(8 - m.row, m.column, m.moveType)){ //cout << "piece has been moved in minMax\n"; temp->move(m); + temp->evaluate('X', 'O'); temp->changeTurns(); - temp->displayBoard(); + //temp->displayBoard(); } else { //cout << m.row << " " << m.column << "\n\n"; diff --git a/Server.cpp b/Server.cpp index 7f49679..c5653ce 100644 --- a/Server.cpp +++ b/Server.cpp @@ -119,12 +119,13 @@ int main(int argc, char *argv[]) gameOver = e.getBoard()->isGameOver(); if(gameOver == true) { string endGame = "Game_Over"; - write(newsockfd, endGame.c_str(), endGame.length()); //Display the board to the client (line by line) + + //write(newsockfd, endGame.c_str(), endGame.length()); //Display the board to the client (line by line) break; } else { string continueGame = "Continue_Game"; - write(newsockfd, continueGame.c_str(), continueGame.length()); //Display the board to the client (line by line) + //write(newsockfd, continueGame.c_str(), continueGame.length()); //Display the board to the client (line by line) e.getBoard()->changeTurns(); } } diff --git a/Serverbackup.txt b/Serverbackup.txt new file mode 100755 index 0000000..de9af91 --- /dev/null +++ b/Serverbackup.txt @@ -0,0 +1,114 @@ + + + cout << "Launching server..." << endl; + int sockfd, newsockfd, portno; + socklen_t clilen; + struct sockaddr_in serv_addr, cli_addr; + int n; + + if (port < 2) { + cout << "ERROR, no port provided\n"; + exit(1); + } + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + + if (sockfd < 0) + cout << "ERROR opening socket"; + + bzero((char *) &serv_addr, sizeof(serv_addr)); + portno = port; + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = INADDR_ANY; + serv_addr.sin_port = htons(portno); + + if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) + cout << "ERROR on binding"; + + listen(sockfd,5); + clilen = sizeof(cli_addr); + newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); + + if (newsockfd < 0) + cout << "ERROR on accept"; + + string move; + bool gameOver = false; + vector record; + b->snapshot(record, *b); + char buffer[256]; + char info[256]; + int choice = -1; + int choice_difficulty = -1; + string final_move; + + //Ask client about game type + string introduction = "WELCOME\n1. Play against AI?\n2. Watch AI vs. AI?\nEnter choice: \n"; + write(newsockfd, introduction.c_str(), introduction.length()); + n = read(newsockfd,info,255); //Reads choice as a string + istringstream convert(info); //Converts the read string to an integer + convert >> choice; //Sets value equal to the converted value + //Later in the project, we need to check for AI and Human modes + cout << "OK" << endl; + bzero(info,256); //Resets info back to normal + + /*//Becca- once your AI code is working, use this code: + + string difficulty_select = "Choose Difficulty\n1. Easy\n2. Medium\n4. Hard\nEnter choice: \n"; + write(newsockfd, difficulty_select.c_str(), difficulty_select.length()); + n = read(newsockfd,info,255); + istringstream convert(info); + convert >> choice_difficulty; + cout << "OK" << endl; + bzero(info,256); + + */ + + while (gameOver != true) + { + gameOver = b->isGameOver(); + + while(b->getTurn() == 'O' && !b->isValid()) + { + b->displayBoard(); + string boardState = b->boardToString(); + final_move = b->boardToString(); + write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line) + cout<<"\nStanding by for client... \n"; + n = read(newsockfd,buffer,255);//Read the client's input + move = buffer; + bzero(buffer,256); + b->interpret(move, *b); + if(b->isValid()) { + b->changeTurns(); + b->setValidFalse(); + } + } + + if(b->isValid()) cout << b->getTurn(); + + while(b->getTurn() == 'X' ) + { + easyAI(); + /*Becca- once you finish your AI, uncomment this out and remove the line directly above this + if(choice_difficulty == 1) + easyAI(); + else if(choice_difficulty == 2) + mediumAI(); + else + hardAI(); + */ + } + + gameOver = b->isGameOver(); + b->setValidFalse(); + b->snapshot(record, *b); + } + final_move = b->boardToString(); + string game_over = "\n\nGAME OVER!!!\n"; + write(newsockfd, final_move.c_str(), final_move.length()); //Display the board to the client (line by line) + write(newsockfd, game_over.c_str(), game_over.length()); //Tell the client the game is over + cout << game_over; + usleep(1); + close(newsockfd); + close(sockfd); \ No newline at end of file