/** Load a log file and feed the moves to the GUI, as if the server were running. */ private void loadLogFile(String file) throws Exception { try { BufferedReader in = new BufferedReader(new FileReader(file)); this.clearData(); // Find the board class line String line; while (!(line = in.readLine()).startsWith("# Game ID:")) if (!line.startsWith("#")) throw new IllegalArgumentException("No 'Game ID:' line found in header."); int gameID = Integer.parseInt(line.substring(10).trim()); while (!(line = in.readLine()).startsWith("# Board class:")) if (!line.startsWith("#")) throw new IllegalArgumentException("No 'Board class:' line found in header."); String cls = line.substring(14).trim(); // Create a board instance Class cl = Class.forName(cls); java.lang.reflect.Constructor co = cl.getConstructor(new Class[0]); Board b = (Board) co.newInstance(new Object[0]); // Add the moves as if receiving them from the server String[] players = new String[b.getNumberOfPlayers()]; boolean gameOver = false; int pcount = 0; while ((line = in.readLine()) != null) { if (line.startsWith("#")) { // Skip rest of header } else if (line.startsWith("START")) { String name = line.substring(line.indexOf(' ') + 1).trim(); name = name.substring(name.indexOf(' ') + 1).trim(); players[pcount++] = name; if (pcount >= players.length) this.gameStarted((Board) b.clone(), gameID, players); } else if (line.startsWith("GAMEOVER")) { gameOver = true; } else if (line.startsWith("WINNER") || line.startsWith("DRAW") || line.startsWith("UNDECIDED")) { if (!gameOver) System.err.println("Warning: 'GAMEOVER' line missing"); this.gameEnded(line); break; } else { if (pcount < players.length) throw new IllegalArgumentException("Missing 'START' message(s)."); Move m = b.parseMove(line); b.move(m); boardUpdated((Board) b.clone(), m); } } in.close(); this.setCurrentBoard(0); } catch (Exception e) { this.clearData(); System.err.println("Exception loading file:"); e.printStackTrace(); throw e; } }
public GameState( Map<Character, Plane> frontBuffer, Map<Character, Plane> backBuffer, int successfulExits, int unsucessfulExits, int epoch, Board board, Map<Gate, Integer> prevAdds, Map<Gate, Integer> actAdds) { this.frontBuffer = new HashMap<Character, Plane>(); for (Map.Entry<Character, Plane> e : frontBuffer.entrySet()) this.frontBuffer.put(e.getKey(), e.getValue()); this.backBuffer = new HashMap<Character, Plane>(); for (Map.Entry<Character, Plane> e : backBuffer.entrySet()) this.backBuffer.put(e.getKey(), e.getValue()); this.successfulExits = successfulExits; this.unsuccessfulExits = unsucessfulExits; this.epoch = epoch; this.board = board.clone(); previousAdds = new HashMap<Gate, Integer>(); for (Map.Entry<Gate, Integer> e : prevAdds.entrySet()) previousAdds.put(e.getKey().clone(), e.getValue().intValue()); actualAdds = new HashMap<Gate, Integer>(); for (Map.Entry<Gate, Integer> e : actAdds.entrySet()) actualAdds.put(e.getKey().clone(), e.getValue().intValue()); }
public void select2(Board b) { for (int i = 0; i < raiz.b.getValidMoves().length; i++) { Board bClone = b.clone(); bClone.makeMove(raiz.b.getValidMoves()[i]); this.lista.add(new NodoMCTS(raiz, bClone)); this.lista.get(i).mov = raiz.b.getValidMoves()[i]; // System.out.println(raiz.b.getValidMoves()[i]); } NodoMCTS evaluado = null; boolean seleccion = true; int aux = 0; while (seleccion && aux < 1000) { profundidad = 0; evaluado = this.expand2(); if (this.movimiento == null) { this.movimiento = evaluado; this.bestoverallmove = this.movimiento.mov; } else if (this.movimiento.mov != null && evaluado.mov != null) { // System.out.println("intentos: "+evaluado.intentos); // System.out.println("aciertos: "+evaluado.aciertos); // System.out.println("Fallos: "+evaluado.fallos); // System.out.println("intentos: "+movimiento.intentos); // System.out.println("aciertos: "+movimiento.aciertos); // System.out.println("Fallos: "+movimiento.fallos); // System.out.println("----"); if (evaluado.aciertos / evaluado.intentos > this.movimiento.aciertos / this.movimiento.intentos) { this.movimiento = evaluado; this.bestoverallmove = this.movimiento.mov; } } aux += 1; } }
public synchronized void setBoard(Board board) { this.board = board.clone(); }
public synchronized Board getBoard() { return board.clone(); }
// MF Re-enable all this once you have the AJAX working // LV That would be now. Took some time to get all the errors out. Sure it could be implemented // better and more Java-like. public static Integer[] minMax(int p, Board b, int d) { // p: player designation // b: current board state // d: iteration depth Integer score = getScore(b, p); // some game states do not require any thinking (e.g. already lost or won) if (d == 0) return new Integer[] {null, score}; // max depth reached. just return the score. if (score == -WIN_SCORE) return new Integer[] {null, score}; // we lose, not good. if (score == WIN_SCORE) return new Integer[] {null, score}; // we win, good. if (b.isFull()) return new Integer[] {null, 8888}; // board is full, pretty good, but not as good as winning. if (moveCache == null) { System.out.println("moveCache was null."); moveCache = new Hashtable<String, Integer[]>(); } // LV See if we can get the next move from the cache. // System.out.println(d+": "+b.getHash()); if (moveCache.containsKey(b.getHash())) { Integer[] c = moveCache.get(b.getHash()); // System.out.println("cache possible for hash = " + b.getHash()); if (c[0] >= (Integer) d) { // System.out.println("cache hit!"); return new Integer[] {c[1], c[2]}; } } // LV No cache. /* // simple optimization attempt. look ahead two moves to see if win or lose possible. // this prevents the algorithm from exploring parts of the state space that are unrealistic to occur. if (d > 2) { for (int q = 0; q < b.getSize(); ++q) { // for each possible move. Board n = b.clone(); // copy current state. if (n.playColumn(q)) { // make move. Integer[] qs = minMax(p,n,2); // look ahead one move. if (qs[1] == WIN_SCORE || qs[1] == -WIN_SCORE) { return new Integer[] {q,qs[1]}; // if I win or lose, stop exploring. }}}} */ // algorithm considers best and worst possible moves in one loop to save lines of code. Integer maxv = 0; // best score. Integer maxc = -1; // column where best score occurs. Integer minv = 999999; // worst score. Integer minc = -1; // colum where worst score occurs. for (int q = 0; q < b.getSize(); ++q) { // for each possible move. Board n = b.clone(); // copy current state. if (n.playColumn(q)) { // make move. Integer[] next = minMax(p, n, d - 1); // look ahead d-1 moves. if (maxc == -1 || next[1] > maxv) { maxc = q; maxv = next[1]; } // compare to previous best. if (minc == -1 || next[1] < minv) { minc = q; minv = next[1]; } // compare to previous worst. } } if (b.getTurn() == p) { // if it is our turn. moveCache.put(b.getHash(), new Integer[] {d, maxc, maxv / 2 + score / 2}); return new Integer[] {maxc, maxv / 2 + score / 2}; // make best possible move. } else { // otherwise. moveCache.put(b.getHash(), new Integer[] {d, minc, minv / 2 + score / 2}); return new Integer[] {minc, minv / 2 + score / 2}; // make worst possible move. } }
public void play() { Scanner scanner = new Scanner(System.in); if (playAsBlack == false) { System.out.println("Playing as X (white)"); } else { System.out.println("Playing as O (black)"); } while (!curGame.TerminalState(curBoard)) { curBoard.display(); if (curBoard.turn == playAsBlack) { boolean found = false; System.out.print("X - coordinate of piece to move: "); int fromX = scanner.nextInt(); System.out.print("Y - coordinate of piece to move: "); int fromY = scanner.nextInt(); System.out.print("X - coordinate to move to: "); int toX = scanner.nextInt(); System.out.print("Y - coordinate to move to: "); int toY = scanner.nextInt(); Board cloned = curBoard.clone(); if (fromX < 0 || fromX > 5 || fromY < 0 || fromY > 5 || toX < 0 || toX > 5 || toY < 0 || toY > 5) { found = false; } else { int previousVal = cloned.gameBoard[fromX][fromY]; cloned.gameBoard[fromX][fromY] = 0; cloned.gameBoard[toX][toY] = previousVal; ArrayList<GameState> successors = curGame.Successors(curBoard); found = false; for (int i = 0; i < successors.size(); i++) { if (cloned.equals(successors.get(i))) { found = true; break; } } } if (found) { curBoard = cloned; System.out.println("Valid move"); } else { System.out.println("Not a valid move"); } } else { AlphaBeta alphaBeta = new AlphaBeta(curGame, maxDepth); curBoard = (Board) alphaBeta.search(curBoard, false); } } curBoard.display(); if (curBoard.winner < 0) { System.out.println("Black wins"); } else if (curBoard.winner > 0) { System.out.println("White wins"); } else { System.out.println("Its a tie"); } }