// // Othecko 1.1 // // Steve Matuszek // Jack Freelander // University of North Carolina at Chapel Hill // // // GameState.java // Steve Matuszek 1.1 // // // The GameState class encapsulates all the information about the state // of the game. In our proof-of-concept, it is a simple tic-tac-toe game. // // A very simple tic-tac-toe game! It does not even end when three in a // row is completed. It does report that the game is over when all the // spots are filled, but the GameServer and OtheckoApplet currently never // ask whether the game is over. // // This should really be an interface that specific games could implement. // For what it's worth, GameServer only makes the following calls: // // GameState() // computerGoFirst() // applyUserMove(Vote v) // chooseAMove() // // OtheckoApplet makes some other calls, but any applet would have to be // overhauled anyway. // // GameState implements Serializable so that it can be sent whole cloth // to the GameServer's subscribers. // import java.io.*; import java.lang.*; import java.util.Random; public class GameState implements Serializable { private char values[][]; private boolean someoneWon; private boolean itsADraw; private int winner; private String message; private int timestamp; public GameState() { values = new char[3][3]; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { values[i][j] = '.'; } } someoneWon = false; itsADraw = false; winner = 0; message = "Nothing has happened yet. "; timestamp = 0; } public String getStringValueAt(int i, int j) { return (new String(" " + values[i][j] + " ")); } public char getValueAt(int i, int j) { return values[i][j]; } public String getMessage() { return message; } public String toString() { String result; result = "\nGameState " + timestamp + ": \n"; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { result += " " + values[i][j]; } result += "\n"; } return result; } public void computerGoFirst() throws GameException { // setValue('x', 1, 1); message = "Computer going first. "; chooseAMove(); } public boolean gameIsOver() { return noMovesLeft(); } public void setValue(char c, int i, int j) throws GameException { c = Character.toLowerCase(c); if (c != 'x' && c != 'o' && c != '.' && c != 'c') { throw new GameException("Bad character " + c); } else if (i<0 || i>2 || j<0 || j>2) { throw new GameException("Bad array location: " + i + ", " + j); } else if (values[i][j] == 'o' || values[i][j] == 'x') { throw new GameException( "That spot is taken! [" + i + ", " + j + "]"); } else values[i][j] = c; } public void applyUserMove(Vote v) throws GameException { int i = v.x; int j = v.y; setValue('o', i, j); message = "User went at (" + i + ", " + j + "). "; } public void applyCompMove(Vote v) throws GameException { int i = v.x; int j = v.y; setValue('x', i, j); message = message + "Computer went at (" + i + ", " + j + "). "; } public void chooseAMove() throws GameException { Random random = new Random(); int i = Math.abs(random.nextInt()) % 3; int j = Math.abs(random.nextInt()) % 3; if (noMovesLeft()) { throw new GameException("No moves left!"); } for (int foo=0; foo<9; foo++) { System.out.println("------ " + i + " " + j + " ------"); if (values[i][j] == '.') { setValue('x', i, j); message = message + "Computer went at (" + i + ", " + j + "). "; System.out.println("Found spot, moving there."); return; } else { i = i+1; if (i>2) { i=0; j = (j+1) % 3; } } } throw new GameException("No moves left.....?"); } public boolean noMovesLeft() { for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { if (values[i][j] == '.') { return false; } } } return true; } public static GameState randomState() { Random random = new Random(); GameState g = new GameState(); int r = Math.abs(random.nextInt()) % 3; try { for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { switch(r) { case 0: g.setValue('x', i, j); break; case 1: g.setValue('o', i, j); break; default: g.setValue('.', i, j); break; } r = Math.abs(random.nextInt()) % 3; } } } catch (GameException ge) { System.out.println("GameException occurred: " + ge); } return g; } public static void testChooseAMove() { try { GameState g = new GameState(); for (int foo=0; foo<9; foo++) { g.chooseAMove(); System.out.println(g); } } catch (GameException ge) { System.out.println("GameException occurred: " + ge); } } }