Question: this is tic tac toe game within telnet. The game is not allowing user input, it asks for your move and when Move 5 is
this is tic tac toe game within telnet. The game is not allowing user input, it asks for your move and when Move 5 is typed in, nothing occurs.
Client
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket;
/** * * @author idagr */ public class client { static final int PORT = 8080; private Socket socket; private BufferedReader in; private PrintWriter out; String serverAddress = "host";
public client(String serverAddress) throws Exception { socket = new Socket(serverAddress, PORT); in = new BufferedReader(new InputStreamReader( socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); } public void play() throws Exception { String response; try { response = in.readLine(); if (response.startsWith("WELCOME")) { char mark = response.charAt(8); } while (true) { response = in.readLine(); if (response.startsWith("VALID_MOVE")) { System.out.println("Valid move, please wait"); } else if (response.startsWith("OPPONENT_MOVED")) { System.out.println("Opponent moved, your turn"); } else if (response.startsWith("VICTORY")) { System.out.println("You win"); break; } else if (response.startsWith("DEFEAT")) { System.out.println("You lose"); break; } else if (response.startsWith("TIE")) { System.out.println("You tied"); break; } else if (response.startsWith("MESSAGE")) { System.out.println(response.substring(8)); } } out.println("QUIT"); } finally { socket.close(); } }
/** * Graphical square in the client window. Each square is * a white panel containing. A client calls setIcon() to fill * it with an Icon, presumably an X or O. */
/** * Runs the client as an application. */ public static void main(String[] args) throws Exception { while (true) { String serverAddress = (args.length == 0) ? "localhost" : args[1]; client Client = new client(serverAddress); Client.play(); } } }
TictoeServer
package tictoe; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.logging.Level; import java.util.logging.Logger;
public class TicToe {
/** * @param args the command line arguments */ public static void main(String[] args) throws IOException { int portNum = 8080; ServerSocket listener = new ServerSocket (portNum); System.out.println("Server is Running"); try{ while(true){ Socket connectionSocket = listener.accept(); System.out.println("Connection socket" + connectionSocket.toString()); if(connectionSocket.isConnected() == true) { // connectionSocket.close(); } Game game = new Game(); Game.Player playerX = game.new Player (listener.accept(), 'X'); Game.Player playerO = game.new Player (listener.accept(), 'O'); playerX.setOpponent(playerO); playerO.setOpponent(playerX); game.currentPlayer = playerX; playerX.start(); playerO.start(); connectionSocket.close(); } } finally { // listener.close(); // listener.close(); // } try { listener.close(); } catch (IOException ex) { Logger.getLogger(TicToe.class.getName()).log(Level.SEVERE, null, ex); } } // TODO code application logic here } } class Game { private Player[] board = { null, null, null, null, null, null, null, null, null}; Player currentPlayer; public boolean Winner() { return (board[0] != null && board[0] == board[1] && board[0] == board[2]) ||(board[3] != null && board[3] == board[4] && board[3] == board[5]) ||(board[6] != null && board[6] == board[7] && board[6] == board[8]) ||(board[0] != null && board[0] == board[3] && board[0] == board[6]) ||(board[1] != null && board[1] == board[4] && board[1] == board[7]) ||(board[2] != null && board[2] == board[5] && board[2] == board[8]) ||(board[0] != null && board[0] == board[4] && board[0] == board[8]) ||(board[2] != null && board[2] == board[4] && board[2] == board[6]); } public boolean filled() { for (int i = 0; i < board.length; i++) { if (board[i] == null) { return false; } } return true; } public synchronized boolean legalMove(int location, Player player) { if (player == currentPlayer && board[location] == null) { board[location] = currentPlayer; currentPlayer = currentPlayer.opponent; currentPlayer.otherPlayerMoved(location); return true; } return false; } class Player extends Thread { char mark; Player opponent; Socket socket; BufferedReader input; PrintWriter output; public Player(Socket socket, char mark) { this.socket = socket; this.mark = mark; try { input = new BufferedReader( new InputStreamReader(socket.getInputStream())); output = new PrintWriter(socket.getOutputStream(), true); output.println("MESSAGE Waiting for opponent to connect" + " and mark = " + mark); output.println("WELCOME " + mark);
} catch (IOException e) { System.out.println("Player died: " + e); } } public void setOpponent(Player opponent) { this.opponent = opponent; // mark='O'; // output.println("mark in setopponent = " + mark); } public void otherPlayerMoved(int location) { output.println("OPPONENT_MOVED " + location); output.println( Winner() ? "DEFEAT" : filled() ? "TIE" : ""); }
public void run() { try { output.println("MESSAGE All players connected"); if (mark == 'X') { output.println("MESSAGE Your move" + "mark = " + mark); }
while (true){ String command = input.readLine(); if(command.startsWith("Move")){ int location = Integer.parseInt(command.substring(5)); if (legalMove(location,this)){ output.println("VALID MOVE"); output.println(Winner() ? "VICTORY" :filled()? "TIE" :""); }else { output.println("MESSAGE?"); } } else if (command.startsWith("QUIT")){ return; } } } catch (IOException e){ System.out.println("Player left:" + e); } finally { try{socket.close();} catch (IOException e){} } }
} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
