public GameState getMove(GameState state, int x, int y) { GameState retVal = null; XYLocation loc = new XYLocation(x, y); List moves = getMoves(state); List newMoves = (List)moves.clone(); if (moves.contains(loc)) { int index = newMoves.indexOf(loc); newMoves.remove(index); retVal = new GameState(); retVal.put("moves", newMoves); TicTacToeBoard newBoard = getBoard(state).cloneBoard(); if (getPlayerToMove(state) == "X") { newBoard.markX(x, y); retVal.put("player", "O"); } else { newBoard.markO(x, y); retVal.put("player", "X"); } retVal.put("board", newBoard); retVal.put("utility", new int(computeUtility(newBoard, getPlayerToMove(getState())))); retVal.put("level", new int(getLevel(state) + 1)); // presentState = retVal; } return(retVal); }
public override bool terminalTest(GameState state) { TicTacToeBoard board = (TicTacToeBoard)state.get("board"); bool line = board.lineThroughBoard(); bool filled = board.getNumberOfMarkedPositions() == 9; return(line || filled); }
public override Object clone() { TicTacToeBoard newBoard = new TicTacToeBoard(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { String s = getValue(i, j); newBoard.setValue(i, j, s); } } return newBoard; }
public override bool Equals(Object anObj) { TicTacToeBoard anotherBoard = (TicTacToeBoard)anObj; for (int i = 0; i < 9; i++) { if (state[i] != anotherBoard.state[i]) { return(false); } } return(true); }
public override Object clone() { TicTacToeBoard newBoard = new TicTacToeBoard(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { String s = getValue(i, j); newBoard.setValue(i, j, s); } } return(newBoard); }
public void printPossibleMoves() { System.Console.WriteLine("Possible moves"); List moves = getMoves(presentState); for (int i = 0; i < moves.Count; i++) { XYLocation moveLoc = (XYLocation)moves.get(i); GameState newState = getMove(presentState, moveLoc.getXCoOrdinate(), moveLoc.getYCoOrdinate()); TicTacToeBoard board = (TicTacToeBoard)newState.get("board"); System.Console.WriteLine("utility = " + computeUtility(newState)); System.Console.WriteLine(""); } }
// // PRIVATE METHODS // private int computeUtility(TicTacToeBoard aBoard, String playerToMove) { int retVal = 0; if (aBoard.lineThroughBoard()) { if (playerToMove.Equals("X")) { retVal = -1; } else { retVal = 1; } } return(retVal); }
// // PRIVATE METHODS // private int computeUtility(TicTacToeBoard aBoard, String playerToMove) { int retVal = 0; if (aBoard.lineThroughBoard()) { if (playerToMove.Equals("X")) { retVal = -1; } else { retVal = 1; } } return retVal; }