Tateti.java 3.87 KB
Newer Older
willgonzz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
import javax.lang.model.util.ElementScanner6;
import java.util.Scanner;
public class Tateti{
    char[][] tateti={{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};

    public Tateti(){
    }
    public static int convertirInt(char a){ 
        if(a=='1'||a=='2'||a=='3'){ 
            return ((Character.getNumericValue(a))-1);
        }else{
            return -1;
        }
    }
    public static int convertirChar(char a){
        if(a=='A'){
            return 0;
        }else if(a=='B'){
            return 1;
        }else if(a=='C'){
            return 2;
        }else{
            return -1;
        }
    }
    public int setJugada(int a,int b,char j){
        if(tateti[a][b]==' '){
            tateti[a][b]=j;
            return 1;
        }else{
            return 0;
        }
    }
    public int verificaJugada(){
        int CF[]=new int[3];
        int CC[]=new int[3];
        int CDI=0,CDD=0,gano=0;
        for(int i=0;i<3;i++){
            for(int j=0;j<2;j++){
                if(tateti[i][j]==tateti[i][j+1] && Character.compare(tateti[i][j],' ')!=0 && Character.compare(tateti[i][j+1],' ')!=0){          
                    CF[i]++;
                }  if(tateti[j+1][i]==tateti[j][i] && Character.compare(tateti[j+1][i],' ')!=0 && Character.compare(tateti[j][i],' ')!=0){          
                    CC[i]++;
                }
            }
        }
        if(CF[0]==2){
            System.out.println("A1-A2-A3");
            gano=1;
        }else if(CF[1]==2){
            System.out.println("B1-B2-B3");
            gano=1;
        }else if(CF[2]==2){
            System.out.println("C1-A2-A3");
            gano=1;
        }else if(CC[0]==2){
            System.out.println("A1-B1-C1");
            gano=1;
        }else if(CC[1]==2){
            System.out.println("A2-B2-C2");
            gano=1;
        }else if(CC[2]==2){
            System.out.println("A3-B3-C3");
            gano=1;
        }else if(tateti[0][0]==tateti[1][1] &&tateti[0][0]==tateti[2][2]&& Character.compare(tateti[0][0],' ')!=0 && Character.compare(tateti[1][1],' ')!=0){    
            System.out.println("A1-B2-C3");
            gano=1;
        }else if(tateti[0][2]==tateti[1][1] && tateti[2][0]==tateti[1][1]&& Character.compare(tateti[0][2],' ')!=0 && Character.compare(tateti[1][1],' ')!=0){
            System.out.println("A3-B2-C1");
            gano=1;
        }
        return gano;
    }
    public void imprimirTablero(){
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                System.out.print("| "+tateti[i][j]+" |");
            }
            System.out.println();
        }
    }
    /*public static void main(String[] args) {
        Tateti t=new Tateti();
        int gano=0;
        int jugadaValida=0;
        Scanner scanner=new Scanner(System.in);
        String jugada;
        int I1;
        int I2;
        char caracter;
        for(int i=0;i<9;i++){
            jugadaValida=0;
            if(i%2==0){
                caracter='X';
            }else{
                caracter='O';
            }
            while(jugadaValida==0){
                System.out.println("Ingrese la jugada:");
                jugada=scanner.nextLine();
                I1=convertirChar(jugada.charAt(0));
               // System.out.println(I1);
                I2= convertirInt(jugada.charAt(1));
               // System.out.println(I2);
                if(I1==-1||I2==-1){
                    System.out.println("Jugada Invalida");
                }else{
                    if(t.setJugada(I1,I2,caracter)==0){
                        System.out.println("Jugada Invalida");
                    }else{
                        t.imprimirTablero();
                        gano=t.verificaJugada();
                        jugadaValida=1;
                    }
                }
            }
            if(gano==1){
                System.out.println("Gano");
                break;
            }
        }
    }*/


}