Skip to content

Instantly share code, notes, and snippets.

@theiostream
Created December 11, 2013 02:14
Show Gist options
  • Save theiostream/7904128 to your computer and use it in GitHub Desktop.
Save theiostream/7904128 to your computer and use it in GitHub Desktop.
Not so smart Tic Tac Toe implementation in Java. Has a pretty flawed AI (which also happens to rely on bruteforce.)
import java.util.Scanner;
public class SmartTTT {
private static int[][] t;
private static int lx, ly;
public static final int INFINITY = 2; // this is sad.
private static char ti(int i) {
char ret;
switch (i) {
case 1:
ret = 'x';
break;
case -1:
ret = 'o';
break;
default:
ret = ' ';
break;
}
return ret;
}
public static void print() {
System.out.println(String.format(" %c | %c | %c ", ti(t[0][0]), ti(t[1][0]), ti(t[2][0])));
System.out.println("---+---+---");
System.out.println(String.format(" %c | %c | %c ", ti(t[0][1]), ti(t[1][1]), ti(t[2][1])));
System.out.println("---+---+---");
System.out.println(String.format(" %c | %c | %c ", ti(t[0][2]), ti(t[1][2]), ti(t[2][2])));
}
private static void printerror(int error) {
switch (error) {
case 1:
System.out.println("Erro: Formato de entrada errado.");
break;
case 2:
System.out.println("Erro: Casa já ocupada.");
break;
case 3:
System.out.println("Erro: Casa inválida.");
break;
default:
break;
}
}
public static int winner(int mx, int my) {
int i;
for (i=0; i<3; i++) {
if (t[mx][i] == 0) break;
if (t[mx][i] != t[mx][my]) break;
if (i == 2) return t[mx][my];
}
for (i=0; i<3; i++) {
if (t[i][my] == 0) break;
if (t[i][my] != t[mx][my]) break;
if (i == 2) return t[mx][my];
}
if (mx == my) {
for (i=0; i<3; i++) {
if (t[i][i] == 0) break;
if (t[i][i] != t[mx][my]) break;
if (i == 2) return t[mx][my];
}
}
else if (mx + my == 2) {
for (i=0; i<3; i++) {
if (t[i][2-i] == 0) break;
if (t[i][2-i] != t[mx][my]) break;
if (i == 2) return t[mx][my];
}
}
return 0;
}
public static void FindBestMove() {
int i, j, k, l;
for (i=0; i<3; i++) {
for (j=0; j<3; j++) {
if (t[i][j] == 0) {
t[i][j] = -1;
if (winner(i, j) == -1) { lx = i; ly = j; return; }
t[i][j] = 0;
}
}
}
for (i=0; i<3; i++) {
for (j=0; j<3; j++) {
if (t[i][j] == 0) {
t[i][j] = 1;
if (winner(i, j) == 1) { t[i][j] = -1; return; }
t[i][j] = 0;
}
}
}
for (i=0; i<3; i++) {
for (j=0; j<3; j++) {
if (t[i][j] == 0) {
t[i][j] = -1;
int c = 0;
int fk = 0;
for (k=0; k<3; k++) {
for (l=0; l<3; l++) {
if (t[k][l] == -1) c++;
}
if (c == 2) fk++;
c = 0;
}
if (fk >= 2) return;
for (k=0; k<3; k++) {
for (l=0; l<3; l++) {
if (t[l][k] == -1) c++;
}
if (c == 2) fk++;
c = 0;
}
if (fk >= 2) return;
for (k=0; k<3; k++) {
if (t[k][k] == -1) c++;
if (c == 2) fk++;
c = 0;
}
if (fk >= 2) return;
for (k=0; k<3; k++) {
if (t[k][2-k] == -1) c++;
if (c == 2) fk++;
c = 0;
}
if (fk >= 2) return;
t[i][j] = 0;
}
}
}
// This is most likely not perfect, but it's worth the cost.
int c = 0;
for (i=0; i<2; i++) {
for (j=0; j<2; j++) {
if (t[i==1?2:0][j==1?2:0] == 1) c++;
}
}
if (c >= 2) {
if (t[1][1] == 0) { t[1][1] = -1; return; }
else {
for (i=0; i<2; i++) {
for (j=0; j<2; j++) {
if (t[i==1?2:0][j==1?2:0] == 0) {
t[i==1?2:0][j==1?2:0] = -1;
return;
}
}
}
}
}
if (t[1][1] == 0) {
t[1][1] = -1;
return;
}
for (i=0; i<2; i++) {
for (j=0; j<2; j++) {
if (t[i==1?2:0][j==1?2:0] == 1) {
if (t[i==1?0:2][j==1?0:2] == 0) {
t[i==1?0:2][j==1?0:2] = -1;
return;
}
}
}
}
for (i=0; i<2; i++) {
for (j=0; j<2; j++) {
if (t[i==1?2:0][j==1?2:0] == 0) {
t[i==1?2:0][j==1?2:0] = -1;
return;
}
}
}
for (i=0; i<3; i++) {
for (j=0; j<3; j++) {
if (t[i][j] == 0) {
t[i][j] = -1;
return;
}
}
}
}
public static void main(String args[]) {
int i, j;
t = new int[3][3];
for (i=0; i<3; i++) {
for (j=0; j<3; j++) {
t[i][j] = 0;
}
}
print();
int jg = 1;
for (i=0; i<9; i++)
System.out.println("Vez do jogador " + jg);
if (/*args[1].equals("-m") || */jg == 1) {
Scanner sc_ = new Scanner(System.in);
String line = sc_.nextLine();
int x, y;
Scanner sc = new Scanner(line);
if (!sc.hasNextInt()) { printerror(1); continue; }
x = sc.nextInt();
if (!sc.hasNextInt()) { printerror(1); continue; }
y = sc.nextInt();
if (sc.hasNext()) { printerror(1); continue; }
if (x < 0 || x > 2 || y < 0 || y > 2) { printerror(3); continue; }
if (t[x][y] != 0) { printerror(2); continue; }
t[x][y] = jg==1 ? 1 : -1;
lx = x; ly = y;
}
else {
FindBestMove();
}
if (winner(lx, ly) != 0) {
System.out.println("Vencedor: " + jg + "!");
print();
break;
}
jg = jg==1 ? 2 : 1;
print();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment