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

Codigo GatoGame

The document contains code for a Tic-Tac-Toe game written in Java. It defines classes for the board, players (human and computer), and a game class to run the logic. Methods are included to place marks on the board, check for wins, and allow players to take turns. Celebration behaviors are also defined to represent how players celebrate wins.

Uploaded by

Lalo Villagomez
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
180 views

Codigo GatoGame

The document contains code for a Tic-Tac-Toe game written in Java. It defines classes for the board, players (human and computer), and a game class to run the logic. Methods are included to place marks on the board, check for wins, and allow players to take turns. Celebration behaviors are also defined to represent how players celebrate wins.

Uploaded by

Lalo Villagomez
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

package gatogame; /** * Clase BOARD(tablero) * @author porfiriovillagomez */ public class Board { private int[][] Board= new int[3][3];

public Board(){ clearBoard(); } /*Limpia el tablero*/ public void clearBoard(){ for(int line=0 ; line<3 ; line++) for(int column=0 ; column<3 ; column++) Board[line][column]=0; } /*Muestra el tablero*/ public void showBoard(){ System.out.println(); for(int line=0 ; line<3 ; line++){ for(int column=0 ; column<3 ; column++){ if(Board[line][column]==-1){ System.out.print(" X "); } if(Board[line][column]==1){ System.out.print(" O "); } if(Board[line][column]==0){ System.out.print(" "); } if(column==0 || column==1) System.out.print("|");

} System.out.println(); } }

public int getPosition(int[] attempt){ return Board[attempt[0]][attempt[1]]; } public void setPosition(int[] attempt, int player){ if(player == 1) Board[attempt[0]][attempt[1]] = -1; else Board[attempt[0]][attempt[1]] = 1; } public int checkLines(){ for(int line=0 ; line<3 ; line++){ if( (Board[line][0] + Board[line][1] + Board[line][2]) == -3) return -1; if( (Board[line][0] + Board[line][1] + Board[line][2]) == 3) return 1;

return 0; } public int checkColumns(){ for(int column=0 ; column<3 ; column++){ if( (Board[0][column] + Board[1][column] + Board[2][column]) == -3) return -1; if( (Board[0][column] + Board[1][column] + Board[2][column]) == 3) return 1;

return 0; } public int checkDiagonals(){ if( (Board[0][0] + Board[1][1] + Board[2][2]) == -3) return -1; if( (Board[0][0] + Board[1][1] + Board[2][2]) == 3) return 1; if( (Board[0][2] + Board[1][1] + Board[2][0]) == -3) return -1; if( (Board[0][2] + Board[1][1] + Board[2][0]) == 3)

return 1; } return 0;

public boolean fullBoard(){ for(int line=0 ; line<3 ; line++) for(int column=0 ; column<3 ; column++) if( Board[line][column]==0 ) return false; return true; }

package gatogame; /** * Clase celebracion fisica, la cual implementa de la interface comportamiento celebracion * @author porfiriovillagomez */ public class Celebracionfisica implements comportamientocelebracion { public void celebrar() { /* Metodo celebrar */ System.out.println("Yo celebro bailando cuando gano una partida"); } } package gatogame; /** * Clase celebracion virtual, implementa a la interface comportamiento celebracion * @author porfiriovillagomez */ public class Celebracionvirtual implements comportamientocelebracion {

public void celebrar() { /* Metodo celebrar */ System.out.println("Yo celebro bailando virtualmente cuando gano una partida"); } }

package gatogame; /** * Clase del jugador computer(Computadora) * @author porfiriovillagomez */ public class Computer extends Player{ public Computer(int player){ super(player); System.out.println("Player 'Computer' created"); celebrar = new Celebracionvirtual(); } @Override /*Metodo play*/ public void play(Board board){ } @Override /*Metodo try*/ public void Try(Board board){ } public void mostrardescripcion() { System.out.println("soy una Computadora"); /* Descripcion (Metodo) de la Computadora*/

package gatogame; /** * Clase Game(Juego), la cual contiene el cuerpo de la aplicacion, se eligen los jugadores y se realizan los movimientos deseados. * @author porfiriovillagomez */ import java.util.Scanner; public class Game { private Board board; private int turn=1, who=1; private Player player1; private Player player2; public Scanner input = new Scanner(System.in); public Game(){ board = new Board(); startPlayers(); } while( Play() );

public void startPlayers(){ System.out.println("Who will be player1 ?"); if(choosePlayer() == 1) this.player1 = new Human(1); else this.player1 = new Computer(1); System.out.println("----------------------"); System.out.println("Who will be Player 2 ?"); if(choosePlayer() == 1) this.player2 = new Human(2); else this.player2 = new Computer(2); } /*Se elige al jugador deseado*/

public int choosePlayer(){ int option=0; do{ System.out.println("1. Human"); System.out.println("2. Computer\n"); System.out.print("Option: "); option = input.nextInt(); if(option != 1 && option != 2) System.out.println("Invalid Option! Try again"); }while(option != 1 && option != 2); return option; } public boolean Play(){ board.showBoard(); if(won() == 0 ){ System.out.println("----------------------"); System.out.println("\nTurn "+turn); System.out.println("It's turn of Player " + who() ); if(who()==1) player1.play(board); else player2.play(board); if(board.fullBoard()){ System.out.println("Full Board. Draw!"); return false; } who++; turn++; return true; } else{ if(won() == -1 ) System.out.println("Player 1 won!"); else System.out.println("Player 2 won!"); } return false;

} public int who(){ if(who%2 == 1) return 1; else return 2; } public int won(){ if(board.checkLines() == 1) return 1; if(board.checkColumns() == 1) return 1; if(board.checkDiagonals() == 1) return 1; if(board.checkLines() == -1) return -1; if(board.checkColumns() == -1) return -1; if(board.checkDiagonals() == -1) return -1; } } package gatogame; /** * Clase principal de la aplicacion * @author porfiriovillagomez */ public class GatoGame { public static void main(String[] args) { Game game = new Game(); Player Human = new Human(); return 0;

Player Computer = new Computer(); Human.hazcelebracion(); Computer.hazcelebracion();

package gatogame; /** * Clase del jugador Human(Humano) * @author porfiriovillagomez */ import java.util.Scanner; public class Human extends Player{ public Scanner input = new Scanner(System.in); public Human(int player){ super(player); this.player = player; System.out.println("Player 'Human' created!"); celebrar = new Celebracionfisica(); } @Override /*Metodo play*/ public void play(Board board){ Try(board); board.setPosition(attempt, player); } @Override /*Metodo try*/ public void Try(Board board){ do{ do{ System.out.print("Line: "); attempt[0] = input.nextInt();

if( attempt[0] > 3 ||attempt[0] < 1) System.out.println("Invalid line. It's 1, 2 or 3"); }while( attempt[0] > 3 ||attempt[0] < 1); do{ System.out.print("Column: "); attempt[1] = input.nextInt(); if(attempt[1] > 3 ||attempt[1] < 1) System.out.println("Invalid column. 1, 2 or 3"); }while(attempt[1] > 3 ||attempt[1] < 1); attempt[0]--; attempt[1]--; if(!checkTry(attempt, board)) System.out.println("Placed already marked. Try other."); }while( !checkTry(attempt, board) );

public void mostrardescripcion() { System.out.println("soy un Humano"); /* Descripcion (Metodo) del Humano*/ } }

package gatogame; /** * Clase abstracta Player, de la cual emanan los 2 diferentes jugadores (Human) y (Computer). * @author porfiriovillagomez */ public abstract class Player { protected int[] attempt = new int[2]; protected int player;

public Player(int player){ this.player = player; } /*Metodo abstracto play*/ public abstract void play(Board board); /*Metodo abstracto try*/ public abstract void Try(Board board); public boolean checkTry(int[] attempt, Board board){ if(board.getPosition(attempt) == 0) return true; else return false; } public void setCelebrar(comportamientocelebracion celebrar) { this.celebrar = celebrar; } comportamientocelebracion celebrar; /*Metodo mostrar descripcion*/ public abstract void mostrardescripcion(); public void hazcelebracion(){ celebrar.celebrar(); } }

package gatogame; /** * Interface Comportamiento Celebracion, se realizo debido a que cada jugador tiene una forma esclusiva de celebrar. * @author porfiriovillagomez */ public interface comportamientocelebracion {

/*Metodo celebrar*/ public void celebrar(); }

package gatogame; /** * Funciona para asegurar que la clase solo tiene una instancia y provee un mecanismo de acceso global a dicha instancia * @author porfiriovillagomez */ public class Singleton { private static Singleton instanciaUnica; private Singleton(){ } public static Singleton obtenerInstancia(){ if(instanciaUnica == null){ instanciaUnica = new Singleton(); } return instanciaUnica; }

package gatogame; import java.util.Observable; /** * Obtiene el promedio de las estadisticas de los usuarios y lo regresa * @author Porfirio Villagomez */ public class Estadisticas extends Observable{ private double promedio; public double getPromedio() { return promedio; }

public void setPromedio(double promedio) { this.promedio = promedio; setChanged(); } } package gatogame; import java.util.Observable; import java.util.Observer; public class Consola implements Observer{ /** * @author Porfirio Villagomez * @param o es el observador, busca la actualizacion de los promedios * @param arg es un objeto del tipo argumento esta reservada en java y me sirve para obtener el promedio */ public void update(Observable o, Object arg) { System.out.println("Nuevo Promedio Semanal "+((Estadisticas)o).getPromedio()); } }

package gatogame; import java.rmi.registry.*; public class Cliente { public static void main(String[] args) { try { Registry registro=LocateRegistry.getRegistry("localhost", 1099); InterfaceRemota Stub=(InterfaceRemota) registro.lookup("HolaRemoto"); System.out.println("La respuesta es: "+Stub.HolaMundo()); }

} }

catch (Exception e) { e.printStackTrace(); }

package gatogame; import java.rmi.*; public interface InterfaceRemota extends Remote { public String HolaMundo() throws RemoteException; }

package gatogame; import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.*; /** * Esta clase es la cual se hace pasar por un Servidor. * @author Porfirio Villagomez * @version 1.0 * */ public class Servidor implements InterfaceRemota { /** * Es un constructor sin argumentos que sirve para la comunicacion en RMI. OJO se tiene que implementar un constructor vacio. * @throws RemoteException Esta excepcion ocurre cuando hay un error de comunicacion de red. */ public Servidor() throws RemoteException { } /** * Este metodo nos regresa un String que representa la respuesta del servidor. * @return La respuesta del servidor.

*/ public String HolaMundo() { return "El servidor dice hola"; } public static void main(String[] args) { try { java.rmi.registry.LocateRegistry.createRegistry(1099); System.out.println("El registro de RMI listo "); } catch(Exception e){ System.out.println("Arrancando el registro de RMI"); e.printStackTrace(); } try { Servidor obj=new Servidor(); InterfaceRemota esqueleton=(InterfaceRemota)UnicastRemoteObject.exportObject(obj,1099); Registry registro=LocateRegistry.getRegistry(1099); registro.bind("HolaRemoto", esqueleton); } catch(Exception e){ } } } e.printStackTrace();

You might also like