Skip to content

Instantly share code, notes, and snippets.

@sixhat
Created May 14, 2018 12:35
Show Gist options
  • Save sixhat/4d9d0cff597a2ee079c456d7373835a4 to your computer and use it in GitHub Desktop.
Save sixhat/4d9d0cff597a2ee079c456d7373835a4 to your computer and use it in GitHub Desktop.
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Player {
private final static String FMT_PRINT = "%d %d %d";
private Scanner input;
private Registry reg;
private Tabuleiro jogo;
private String playerName;
public Player() {
input = new Scanner(System.in);
try {
reg = LocateRegistry.getRegistry(null);
jogo = (Tabuleiro) reg.lookup("Galitos");
} catch (RemoteException | NotBoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Player p = new Player();
p.identifyPlayer();
try {
p.playGameLoop();
} catch (RemoteException e) {
e.printStackTrace();
}
}
private void identifyPlayer() {
System.out.print("Como te chamas? >");
playerName = input.nextLine();
}
private void playGameLoop() throws RemoteException {
int[] board;
while (jogo.jogoEstaValido()) {
board = jogo.getBoard(playerName);
printDesteBoard(board);
int pos = -1;
while (pos<0 || pos>8) {
System.out.print("Faz a tua Jogada [0-8] >");
try {
pos = input.nextInt();
} catch (InputMismatchException e) {
input.nextLine();
System.out.println("O Galo, números pá! 0 a 8!!!!");
}
}
board = jogo.play(pos, playerName);
printDesteBoard(board);
}
}
private void printDesteBoard(int[] board) {
System.out.println(String.format(FMT_PRINT, board[0], board[1], board[2]));
System.out.println(String.format(FMT_PRINT, board[3], board[4], board[5]));
System.out.println(String.format(FMT_PRINT, board[6], board[7], board[8]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment