From 7db1fb5f8bb61d072233f952b90cef397a254e26 Mon Sep 17 00:00:00 2001 From: willgonzz Date: Thu, 28 Oct 2021 10:34:09 -0300 Subject: [PATCH] Initial commit --- Tablero.java | 46 ++++++++++++++++++++++++++++++++++++++++++++++ Tateti.java | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 Tablero.java create mode 100644 Tateti.java diff --git a/Tablero.java b/Tablero.java new file mode 100644 index 0000000..9d1e7b8 --- /dev/null +++ b/Tablero.java @@ -0,0 +1,46 @@ +import java.util.Scanner; + +public class Tablero{ + 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=Tateti.convertirChar(jugada.charAt(0)); + // System.out.println(I1); + I2= Tateti.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; + } + } + } + +} \ No newline at end of file diff --git a/Tateti.java b/Tateti.java new file mode 100644 index 0000000..5b312e4 --- /dev/null +++ b/Tateti.java @@ -0,0 +1,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; + } + } + }*/ + + +} \ No newline at end of file -- libgit2 0.26.0