Last active
May 30, 2019 13:06
-
-
Save boogie666/830b43570545256b88ee2bc9c5bcc4f9 to your computer and use it in GitHub Desktop.
X si Zero in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.boogie666.tictactoe; | |
import java.util.Scanner; | |
public class Main { | |
private static char X = 'X'; | |
private static char ZERO = 'O'; | |
private static char NIMIC = '-'; | |
private static char[][] tabla = { | |
{NIMIC, NIMIC, NIMIC}, | |
{NIMIC, NIMIC, NIMIC}, | |
{NIMIC, NIMIC, NIMIC} | |
}; | |
public static void main(String[] args) { | |
boolean sfarsitDeJoc = false; | |
char jucatorCurent = ZERO; | |
while (!sfarsitDeJoc) { | |
afiseazaTabla(tabla); | |
jucatorCurent = jucatorulUrmator(jucatorCurent); | |
mutare(tabla, jucatorCurent); | |
sfarsitDeJoc = verificaSfarsit(tabla, jucatorCurent); | |
} | |
afiseazaRezultat(tabla, jucatorCurent); | |
} | |
public static boolean verificaSfarsit(char[][] tabla, char jucatorCurent) { | |
return victorie(tabla, jucatorCurent) || egalitate(tabla); | |
} | |
public static boolean victorie(char[][] tabla, char jucatorCurent){ | |
return castigPeLinie(tabla, jucatorCurent) || | |
castigPeColoana(tabla, jucatorCurent) || | |
castigPeDiagonala(tabla, jucatorCurent); | |
} | |
public static boolean egalitate(char[][] tabla){ | |
// daca nu exista nici un spatiu gol, atunci este egalitate | |
for(int i = 0; i < tabla.length; i++){ | |
for(int j = 0; j < tabla[i].length; j++){ | |
if(tabla[i][j] == NIMIC){ | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
public static boolean castigPeDiagonala(char[][] tabla, char jucatorCurent){ | |
if(tabla[0][0] == jucatorCurent && tabla[1][1] == jucatorCurent && tabla[2][2] == jucatorCurent){ | |
return true; // digonala principala | |
} | |
if(tabla[0][2] == jucatorCurent && tabla[1][1] == jucatorCurent && tabla[2][0] == jucatorCurent){ | |
return true; // digonala secundara | |
} | |
return false; // nu o castigat pe diagonala. | |
} | |
public static boolean castigPeColoana(char[][] tabla, char jucatorCurent){ | |
for(int j = 0; j < tabla.length; j++){ | |
if(tabla[0][j] == jucatorCurent && tabla[1][j] == jucatorCurent && tabla[2][j] == jucatorCurent) { | |
return true; | |
} | |
} | |
return false; // nu o castigat pe coloana | |
} | |
public static boolean castigPeLinie(char[][] tabla, char jucatorCurent){ | |
for(int i = 0; i < tabla.length; i++){ | |
if(tabla[i][0] == jucatorCurent && tabla[i][1] == jucatorCurent && tabla[i][2] == jucatorCurent) { | |
return true; | |
} | |
} | |
return false; // nu o castigat pe linie | |
} | |
public static void mutare(char[][] tabla, char jucatorCurent) { | |
int[] iSiJ = citesteMutare(jucatorCurent); | |
int i = iSiJ[0]; | |
int j = iSiJ[1]; | |
if (mutareValida(tabla, i, j)) { | |
tabla[i][j] = jucatorCurent; // pune piesa pe tabla. | |
} else { | |
mutare(tabla, jucatorCurent); // mai face odata | |
} | |
} | |
private static boolean mutareValida(char[][] tabla, int i, int j) { | |
return tabla[i][j] == NIMIC; | |
} | |
public static int[] citesteMutare(char jucatorCurent) { | |
System.out.println("Jucatorul " + jucatorCurent + " muta:"); | |
Scanner s = new Scanner(System.in); | |
String text = s.nextLine(); | |
String[] iSiJ = text.split(" "); | |
return new int[]{Integer.valueOf(iSiJ[0]), Integer.valueOf(iSiJ[1])}; | |
} | |
private static char jucatorulUrmator(char jucatorCurent) { | |
if (jucatorCurent == X) { // dupa X ii ZERO | |
return ZERO; | |
} else { // dupa ZERO ii X | |
return X; | |
} | |
} | |
private static void afiseazaTabla(char[][] tabla) { | |
for (int i = 0; i < tabla.length; i++) { | |
for (int j = 0; j < tabla.length; j++) { | |
System.out.print(tabla[i][j] + " "); | |
} | |
System.out.println(); | |
} | |
} | |
private static void afiseazaRezultat(char[][] tabla, char jucatorCurent) { | |
afiseazaTabla(tabla); | |
if(victorie(tabla, jucatorCurent)){ | |
System.out.println("Jucatorul " + jucatorCurent + " a castigat!"); | |
}else { | |
System.out.println("Egaliate!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment