Question: Please help write a javafx client program for Rock Paper Scissors Lizard Spock that go with my Server program and Constants program given below. Thank
Please help write a javafx client program for Rock Paper Scissors Lizard Spock that go with my Server program and Constants program given below. Thank you for your help.
SERVER PROGRAM ====================================
package study3;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Date;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class Study3Server extends Application
implements Study3Constants {
private int session = 1; // number a session
private ServerSocket serverSocket; // set up a serverSocket
TextArea taLog;
@Override
public void start(Stage primaryStage){
taLog = new TextArea();
//create a scene
Scene scene = new Scene (new ScrollPane(taLog), 450, 200);
primaryStage.setTitle("RPSLS Server");
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setOnCloseRequest(new EventHandler
@Override
public void handle(WindowEvent event) {
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stop();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
new Thread(() -> {
try{
serverSocket = new ServerSocket(8000);
Platform.runLater(() -> taLog.appendText(new Date() +
": Server started at socket 8000 "));
//ready to create a session for every two players
while (true) {
Platform.runLater(() -> taLog.appendText(new Date() +
": Wait for players to join session "+ session +
" "));
//connect to player 1
Socket player1 = serverSocket.accept();
Platform.runLater(() -> {
taLog.appendText(new Date() +
": Player 1 joined session "+ session +
" ");
taLog.appendText("Player 1's IP address is " +
player1.getInetAddress().getHostAddress()+
" ");
});
//Send the session number to the player
new DataOutputStream(
player1.getOutputStream()).writeInt(session);
//Notify that the player is Player1
new DataOutputStream(
player1.getOutputStream()).writeInt(PLAYER1);
//connect to player 2
Socket player2 = serverSocket.accept();
Platform.runLater(() -> {
taLog.appendText(new Date() +
": Player 2 joined session "+ session +
" ");
taLog.appendText("Player 2's IP address is " +
player2.getInetAddress().getHostAddress()+
" ");
});
//Send the session number to the player
new DataOutputStream(
player2.getOutputStream()).writeInt(session);
//Notify that the player is Player2
new DataOutputStream(
player2.getOutputStream()).writeInt(PLAYER2);
//inner class to handle a game for two players
new Thread(
new HandleASession(player1, player2, session)).start();
// serverSocket.close();
++session; // increment session for the next two players
} // while true block ends here
}catch(SocketException ex){ }
catch(IOException ex) { System.out.println(ex); }
}).start(); // end of the new Thread block
}
// create a thread class for handling a new session for two players
class HandleASession implements Runnable, RPSLSConstants {
// create sockets for two players
private Socket player1;
private Socket player2;
private int session;
//set up data streams for both players
private DataInputStream fromPlayer1;
private DataOutputStream toPlayer1;
private DataInputStream fromPlayer2;
private DataOutputStream toPlayer2;
// boolean for continuing play
//private boolean continueToPlay = true;
// Construct a thread in the constructor
public HandleASession(Socket p1, Socket player2, int session){
this.player1 = p1;
this.player2 = player2;
this.session = session;
}
// write the run method
public void run(){
try{
fromPlayer1 =
new DataInputStream(player1.getInputStream());
toPlayer1 =
new DataOutputStream(player1.getOutputStream());
fromPlayer2 =
new DataInputStream(player2.getInputStream());
toPlayer2 =
new DataOutputStream(player2.getOutputStream());
//write to notify player 1 to start
toPlayer1.writeInt(1);
while (true) {
// get the move from Player1
int player1 = fromPlayer1.readInt();
// get the move from Player2
int player2 = fromPlayer2.readInt();
//check to see who won
//int wins = 0;
if (player1 == 1)//rock
{
if (player2 == 1)
{
toPlayer2.writeInt(DRAW);
toPlayer1.writeInt(DRAW);
break; //tie
}
else if (player2 == 2 || player2 == 5)
{
toPlayer2.writeInt(PLAYER2_WON);
toPlayer1.writeInt(PLAYER2_WON);
break; //player2 wins //paper spock
}
else
{
toPlayer1.writeInt(PLAYER1_WON);
toPlayer2.writeInt(PLAYER1_WON);
break; //player1 win //Scissors Lizard
}
}
else if (player1 == 2)//Paper
{
if (player2 == 2)
{
toPlayer2.writeInt(DRAW);
toPlayer1.writeInt(DRAW);
break; //tie
}
else if (player2 == 3 || player2 == 4) //Scissors Lizard
{
toPlayer2.writeInt(PLAYER2_WON);
toPlayer1.writeInt(PLAYER2_WON);
break; //player2 wins
}
else
{
toPlayer1.writeInt(PLAYER1_WON);
toPlayer2.writeInt(PLAYER1_WON);
break; //player1 win //Rock Spock
}
}
else if (player1 == 3)//Scissors
{
if (player2 == 3)
{
toPlayer2.writeInt(DRAW);
toPlayer1.writeInt(DRAW);
break; //tie
}
else if (player2 == 2 || player2 == 5)
{
toPlayer2.writeInt(PLAYER2_WON);
toPlayer1.writeInt(PLAYER2_WON);
break; //player2 wins //Paper Spock
}
else
{
toPlayer1.writeInt(PLAYER1_WON);
toPlayer2.writeInt(PLAYER1_WON);
break; //player1 win //Rock Lizard
}
}
else if (player1 == 4)//Lizard
{
if (player2 == 4)
{
toPlayer2.writeInt(DRAW);
toPlayer1.writeInt(DRAW);
break; //tie
}
else if (player2 == 1 || player2 == 3)
{
toPlayer2.writeInt(PLAYER2_WON);
toPlayer1.writeInt(PLAYER2_WON);
break; //player2 wins //Rock Scissors
}
else
{
toPlayer1.writeInt(PLAYER1_WON);
toPlayer2.writeInt(PLAYER1_WON);
break; //player1 win //Paper Spock
}
}
else if (player1 == 5)//Spock
{
if (player2 == 5)
{
toPlayer2.writeInt(DRAW);
toPlayer1.writeInt(DRAW);
break; //tie
}
else if (player2 == 2 || player2 == 4)
{
toPlayer2.writeInt(PLAYER2_WON);
toPlayer1.writeInt(PLAYER2_WON);
break; //player2 wins //Paper Lizard
}
else
{
toPlayer1.writeInt(PLAYER1_WON);
toPlayer2.writeInt(PLAYER1_WON);
break; //player1 win //Rock Scissors
}
}
}
} catch(IOException ex) { System.out.println(ex); }
} // end run method
private void sendMove(DataOutputStream out, int s,) //change this spot
throws IOException {
out.writeInt(s);
}
}
public static void main(String[] args) {
launch(args);
}
}
CONSTANTS PROGRAM ======================================
package study3;
public interface Study3Constants {
public static int PLAYER1 = 1; // tells us we have player1
public static int PLAYER2 = 2; // Player2
public static int PLAYER1_WON = 1; //see if p1 wins
public static int PLAYER2_WON = 2; //see if p2 wins
public static int DRAW = 3; // see if we have a tie
public static int CONTINUE = 4; // wish to continue
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
