0% found this document useful (0 votes)
4 views

Game

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Game

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Pratical:- Game(Stone,Paper,scissors)

//Interface

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface RPSInterface extends Remote {


int PLAYERS_NUM = 2;
int ROCK = 1;
int PAPER = 2;
int SCISSORS = 3;

int register() throws RemoteException;


boolean play(int playerId, int move) throws RemoteException;
boolean allPlayersReady() throws RemoteException;
boolean isallPlayersPlayed() throws RemoteException;
int getResult(int playerId) throws RemoteException;
}

//Implementation

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class RPSImpl extends UnicastRemoteObject implements RPSInterface {


private int numPlayers;
private boolean ready;
private int[] moves;
private boolean flag;

public RPSImpl() throws RemoteException {


super();
numPlayers = 0;
ready = false;
flag=false;
moves = new int[2];
}

public int register() throws RemoteException {


System.out.println("Player " + (numPlayers + 1) + " joined the game.");
numPlayers++;
if (numPlayers == 2) {
ready = true;
System.out.println("Both players are ready.");
}
return numPlayers;
}

public boolean allPlayersReady() throws RemoteException {


System.out.println("Checking if both players are ready.");
return ready;
}
public boolean play(int playerId, int move) throws RemoteException {
if (moves[playerId - 1] == 0) {
moves[playerId - 1] = move;
System.out.println("Player " + playerId + " played " + move + ".");
return true;
} else {
System.out.println("Player " + playerId + " already played.");
return false;
}
}
public boolean isallPlayersPlayed() throws RemoteException {
if(moves[0]!=0 && moves[1]!=0){
flag=true;
}
System.out.println("Checking if both players are played.");
return flag;
}
public int getResult(int playerId) throws RemoteException {
//int otherPlayerId = 3 - playerId;
int result;
if (moves[0]==moves[1]) {
System.out.println("It's a tie!");
return 0;
}
else if((moves[0]==3 &&moves[1]==2)||(moves[0]==2 &&moves[1]==1)||
(moves[0]==1 &&moves[1]==2)){
System.out.println("Player " + 1 + " wins!");
return 1;
}
else{
System.out.println("Player " + 2 + " wins!");
return 2;
}
}
}

//Server

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;

public class RPSServer {


public static void main(String[] args) {
try {
LocateRegistry.createRegistry(1099);
RPSImpl game = new RPSImpl();
Naming.rebind("rmi://localhost/RPSGame", game);
System.out.println("Server started.");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}

//Client

import java.rmi.Naming;
import java.util.Scanner;

public class RPSClient {


public static void main(String[] args) {
try {
RPSInterface game = (RPSInterface)
Naming.lookup("rmi://localhost/RPSGame");
int playerId = game.register();
System.out.println("You are player " + playerId + ".");
Scanner scanner = new Scanner(System.in);
while (!game.allPlayersReady()) {
System.out.println("Waiting for other player...");
Thread.sleep(1000);
}
System.out.println("Both players are ready. Enter your move (1=rock,
2=paper, 3=scissors):");
int move = scanner.nextInt();
boolean success = game.play(playerId, move);
while (!success) {
System.out.println("You already played. Enter your move again:");
move = scanner.nextInt();
success = game.play(playerId, move);
}
while (!game.isallPlayersPlayed()) {
System.out.println("Waiting for other player to play...");
Thread.sleep(1000);
}
int result = game.getResult(playerId);
if (result == 0) {
System.out.println("It's a tie!");
} else {
System.out.println("Player " + result + " wins!");
}
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}

Output:

it@it-HP-EliteDesk-800-G2-SFF:~/Desktop/Kapil/RSPGame$ java RPSServer


Server started.
Player 1 joined the game.
Checking if both players are ready.
Checking if both players are ready.
Checking if both players are ready.
Checking if both players are ready.
Checking if both players are ready.
Player 2 joined the game.
Both players are ready.
Checking if both players are ready.
Checking if both players are ready.
Player 1 played 2.
Checking if both players are played.
Checking if both players are played.
Checking if both players are played.
Checking if both players are played.
Player 2 played 3.
Checking if both players are played.
Player 2 wins!
Checking if both players are played.
Player 2 wins!

it@it-HP-EliteDesk-800-G2-SFF:~/Desktop/Kapil/RSPGame$ java RPSClient


You are player 1.
Waiting for other player...
Waiting for other player...
Waiting for other player...
Waiting for other player...
Waiting for other player...
Both players are ready. Enter your move (1=rock, 2=paper, 3=scissors):
2
Waiting for other player to play...
Waiting for other player to play...
Waiting for other player to play...
Waiting for other player to play...
Player 2 wins!
it@it-HP-EliteDesk-800-G2-SFF:~/Desktop/Kapil/RSPGame$

it@it-HP-EliteDesk-800-G2-SFF:~/Desktop/Kapil/RSPGame$ java RPSClient


You are player 2.
Both players are ready. Enter your move (1=rock, 2=paper, 3=scissors):
3
Player 2 wins!
it@it-HP-EliteDesk-800-G2-SFF:~/Desktop/Kapil/RSPGame$

You might also like