Home / Expert Answers / Computer Science / please-code-and-follow-instructions-and-code-in-java-client-server-communication-actually-the-detail-pa949

(Solved): Please code and follow instructions and code in java Client-Server Communication Actually the detail ...



Please code and follow instructions and code in javastudent submitted image, transcription available belowstudent submitted image, transcription available belowstudent submitted image, transcription available belowstudent submitted image, transcription available below

Client-Server Communication Actually the details of the client-server communication are entirely up to you. My version uses a pretty simple "protocol": Connecting to host on port 5000 . sending: play server: symbol 0 symbols: sending: status server: move sending: board server: board | | | | | I II II I I II II | Enter move: 1 sending: move 1 server: server 2 sending: status server: move sending: board server: board . I II | ? I I| || | Enter move: 5 sending: move 5 server: server 3 sending: status server: move sending: board server: board 0100.1... | Enter move: 6 sending: move 6 server: server 4 sending: status server: move sending: board server: board 0100011.. Tricky stuff (Hints, recommendations, ...) After programming the whole thing (including a lot of dead ends, trust me!) here are some observations: 1. Factor out the game logic into a game controller, that sets up the board, makes moves, determines game state, etc. It must completely GUI agnostic, i.e. no JavaFX or anything should be in this Controller. My Controller (public view) looks somehow like shown on the right --> 2. The main communication sits in the GameRunnable class (just a run method). It contains the response generation for all requests in the protocol. It starts out like this: public class GameRunnable implements Runnable \{ private Socket socket; private GameController controller; ... public GameRunnable(Socket socket, GameController controller) \{ this. socket = socket; this. controller = controller; \} Of course, the socket is the one generated by the server via server.accept(). The GameRunnable just talks to its GameController about moves, states, etc. It's launched as a Thread by the server after the connection has been established. 3. The server itself is almost trivial: Wait for a connection, get the socket, create a GameRunnable and a GameController, link them all up and launch the GameRunnable in a Thread. 3. The server itself is almost trivial: Wait for a connection, get the socket, create a GameRunnable and a GameController, link them all up and launch the GameRunnable in a Thread. 4. Clear up the confusion about Client/Server identification in the GameController. It's not the same as the vs. issue. is the first mover, the second. The server can be or the client can be . The GameController is just worried about Client and Server, the vs. O business is up to the client. That's why the server in my protocol tells the client what symbol to use. 5. I wrote a simple text client first to validate the server responses and see if a full game can be conducted. This is where the logs came from. It's almost impossible (at least for me...) to debug client/server issues inside a JavaFX application. After that worked out, moving the code into the graphical GUI took about an hour, not more! 6. Note that due to the nature of the GUI a lot of stuff happens in the event listener for mouse clicks! On each such event, the client checks connection status, legality (better do it on the client, it's one or two lines of code), server response, game state. etc. The listener for the Connect button just does some minor bookkeeping and terminates - hoping the click listener gets the rest done! This is where your text client can't help you a lot. Even if you don't manage to get the GUI to run, your text client will be good for some points. If you provide no client whatsoever, you will lose almost all points, since no one can play and evaluate your submission. 7. I used a Canvas to draw the board.You can attach a MouseEvent listener to it which lets you retrieve the coordinates relative to the Canvas origin - that's a lifesaver! 8. Right! The "Game Al". You can make this as smart as you want, but do this last when everything else works. I use one that blindly looks for the first free square on the board. Don't spend too much time on that in the beginning! Yes, Tic-Tac-Toe is a theoretical draw, but to enforce it takes some effort. As usual, knows how...


We have an Answer from Expert

View Expert Answer

Expert Answer



We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe