Created
May 14, 2018 12:36
-
-
Save sixhat/14511c4e03dc992b72a0c1cac56ea918 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.rmi.RemoteException; | |
import java.rmi.registry.LocateRegistry; | |
import java.rmi.registry.Registry; | |
import java.rmi.server.UnicastRemoteObject; | |
import java.util.ArrayList; | |
public class JogoDoGalo implements Tabuleiro { | |
private ArrayList<String> jogadores; | |
private int[] board; | |
private String turno; | |
public JogoDoGalo() { | |
jogadores = new ArrayList<>(); | |
board = new int[9]; | |
} | |
@Override | |
public synchronized int[] play(int pos, String nome) throws RemoteException { | |
if (turno.equals(nome)) { | |
if (board[pos] == 0) { | |
int indice = jogadores.indexOf(nome); | |
board[pos] = indice + 1; | |
turno = jogadores.get(1 - indice); | |
} | |
} | |
notifyAll(); | |
return board; | |
} | |
@Override | |
public synchronized boolean jogoEstaValido() { | |
for (int i = 0; i < board.length; i++) { | |
if (board[i]==0) return true; | |
} | |
return false; | |
} | |
@Override | |
public synchronized int[] getBoard(String nome) throws RemoteException { | |
if (jogadores.size() < 2) { | |
if (!jogadores.contains(nome)) { | |
jogadores.add(nome); | |
} | |
} | |
if (jogadores.size() == 1) { | |
turno = nome; | |
} | |
if (jogadores.size() == 2) { | |
notifyAll(); | |
} | |
while (!turno.equals(nome) || jogadores.size() < 2) { | |
try { | |
wait(); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
return board; | |
} | |
public static void main(String[] args) { | |
JogoDoGalo server = new JogoDoGalo(); | |
try { | |
Registry reg = LocateRegistry.getRegistry(null); | |
Tabuleiro stub = (Tabuleiro) UnicastRemoteObject.exportObject(server, 0); | |
reg.rebind("Galitos", stub); | |
System.out.println("-- Server Ready, Fight!!!"); | |
} catch (RemoteException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment