Skip to content

Instantly share code, notes, and snippets.

@sixhat
Created May 14, 2018 08:53
Show Gist options
  • Save sixhat/1d50caef95f76ae45782401eabb94c80 to your computer and use it in GitHub Desktop.
Save sixhat/1d50caef95f76ae45782401eabb94c80 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 Scanner input;
private Registry reg;
private Board jogo;
private String playerName;
public final static String FMT_PRINT = "%d %d %d";
public Player() {
input = new Scanner(System.in);
try {
reg = LocateRegistry.getRegistry(null);
jogo = (Board) reg.lookup("Galitos");
} catch (RemoteException | NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("-- Setup Player Complete. ");
}
public static void main(String[] args) {
Player p = new Player();
p.getPlayerName();
try {
p.playGame();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void getPlayerName() {
System.out.print("Como te chamas? >");
playerName = input.nextLine();
}
private void playGame() throws RemoteException {
int[] board;
while (true) {
board = jogo.getBoard(playerName);
printMyGalitos(board);
int pos = -1;
while (pos < 0 || pos > 8) {
try {
System.out.print("Qual a jogada? > ");
pos = input.nextInt();
} catch (InputMismatchException e) {
System.out.println("és parvo pá! entre 0 e 8");
input.nextLine();
}
}
board = jogo.play(pos, playerName);
printMyGalitos(board);
}
}
private void printMyGalitos(int[] board) {
System.out.println(String.format(FMT_PRINT + "\t 0 1 2", board[0], board[1], board[2]));
System.out.println(String.format(FMT_PRINT+ "\t 3 4 5", board[3], board[4], board[5]));
System.out.println(String.format(FMT_PRINT+ "\t 6 7 8", board[6], board[7], board[8]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment