Skip to content

Instantly share code, notes, and snippets.

@danlucraft
Created May 15, 2010 15:56
Show Gist options
  • Save danlucraft/402260 to your computer and use it in GitHub Desktop.
Save danlucraft/402260 to your computer and use it in GitHub Desktop.
import java.util.*;
public class TicTacToe {
public static boolean DEBUG = true;
public static int EMPTY = 0;
public static int O = 1;
public static int X = 2;
public String boardString;
public TicTacToe(String boardString) {
this.boardString = boardString;
if (DEBUG) System.out.printf("board: %s\n", this.boardString);
}
public int firstEmptySpace() {
for (int i = 0; i < 10; i++) {
if (DEBUG) System.out.printf("board: %d\n", i);
if (DEBUG) System.out.printf("board: %s\n", boardString.substring(i, i+1));
if (DEBUG) System.out.printf("foo\n");
if (boardString.substring(i, i+1).equals("-")) {
if (DEBUG) System.out.printf("returning: %d\n", i);
return i;
}
}
return 0;
}
public int randomEmptySpace() {
ArrayList<Integer> emptySpaces = emptySpaces();
Random generator = new Random();
int r = generator.nextInt(emptySpaces.size());
return (int) emptySpaces.get(r);
}
public ArrayList<Integer> emptySpaces() {
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 9; i++) {
if(isEmptySpace(i))
list.add((Integer) i);
}
return list;
}
public int strategy() {
// rows
for(int row = 0; row < 3; row++) {
if (DEBUG) System.out.printf("row: %d\n", row);
if (DEBUG) System.out.printf("mytokensinrow: %d\n", myTokensInRow(row));
if (myTokensInRow(row) == 2 && firstEmptySpaceInRow(row) != -1)
return firstEmptySpaceInRow(row);
}
// columns
for(int col = 0; col < 3; col++) {
if (DEBUG) System.out.printf("col: %d\n", col);
if (DEBUG) System.out.printf("mytokensincol: %d\n", myTokensInCol(col));
if (DEBUG) System.out.printf("firstemptyincol(%d): %d\n", col, firstEmptySpaceInCol(col));
if (myTokensInCol(col) == 2 && firstEmptySpaceInCol(col) != -1)
return firstEmptySpaceInCol(col);
}
return firstEmptySpace();
}
public int whoAmI() {
int numO = count(X);
int numX = count(O);
if (numO > numX)
return X;
else {
if (numO == numX)
return O;
else
return X;
}
}
public int count(int piece) {
int c = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (tokenAt(i, j) == piece)
c++;
return c;
}
public boolean isEmptySpace(int i) {
return boardString.substring(i, i+1).equals("-");
}
public int myTokensInRow(int row) {
int count = 0;
for(int col = 0; col < 3; col++) {
if (tokenAt(row, col) == whoAmI())
count++;
}
return count;
}
public int firstEmptySpaceInRow(int row) {
for(int col = 0; col < 3; col++) {
if (tokenAt(row, col) == EMPTY)
return row*3 + col;
}
return -1;
}
public int myTokensInCol(int row) {
int count = 0;
for(int col = 0; col < 3; col++) {
if (tokenAt(col, row) == whoAmI())
count++;
}
return count;
}
public int firstEmptySpaceInCol(int col) {
for(int row = 0; row < 3; row++) {
if (tokenAt(row, col) == EMPTY)
return row*3 + col;
}
return -1;
}
public int tokenAt(int row, int col) {
String tokenString = boardString.substring(row*3 + col, row*3 + col + 1);
if (tokenString.equals("o"))
return O;
else if (tokenString.equals("x"))
return X;
else if (tokenString.equals("-"))
return EMPTY;
System.out.printf("fail 1\n");
return -1;
}
public static void main (String[] args) {
String b = args[0];
TicTacToe ttt = new TicTacToe(b);
if (DEBUG) System.out.printf("whoami: %d\n", ttt.whoAmI());
System.out.printf("%d\n", ttt.strategy());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment