//hexIIRandom.java 11/14 //Should be O(n^2), but... import java.util.*; import java.io.*; //import java.awt.*; //For graphics (later) //import java.applet.*; class MoveandScore extends Move{ int score; MoveandScore(){ this(0, 0); } MoveandScore(coords c){ super(c); score = 0; } MoveandScore(int a, int b){ super(a, b); score = 0; } int getScore(){ return score; } void putScore(int a){ score = a; } } class RandomIIAnalysis{ hexBoard theBoard; private int SIZE; static int MAX_TEST = 10000; private Vector available; private Vector choices; private occupantType side; RandomIIAnalysis(){ this(new hexBoard(), occupantType.VERT); } RandomIIAnalysis(hexBoard theBoard) { this(theBoard, occupantType.VERT); } RandomIIAnalysis(hexBoard theBoard, occupantType side) { this.theBoard = theBoard; this.side = side; SIZE = theBoard.SIZE; } int growthNeeded(int a){ int result; result = 400; if (a>10) result += 100 * Math.log(a); else result += 10 * a; System.out.println(result); return result; } public void setuplists() { int i,j; MoveandScore here; available = new Vector(); choices = new Vector(); for(i=0; i0) { randomPercent = Math.random(); index = (int) (total*randomPercent); here = (MoveandScore) available.elementAt(index); theBoard.doIt(here, occupantType.VERT); available.removeElementAt(index); choices.addElement(here); total--; moves--; } } void refresh(int moves, int total) { int i,j; Move here; //Here we unplay all of the random moves from fillup. //We have to remove the v's from indices total-moves to total-1. for(i=0;i best) { best = eval; result = here; } } return result; } int isRandomWin(int moves, int total) { int result = 1; fillup(moves, total); //Play random moves. //Test for a vertical win. //Return 1 if a win and -1 for a loss. if (theBoard.isWon(side)) result = 1; else result = -1; return result; } } class hexIIRandom { public static void main(String args[]) { hexBoard theBoard; RandomIIAnalysis guessing; int i,j; byte [] inputBytes = new byte[6]; Move compMove; Move here; int left; int remaining; boolean gameOver = false; int firstval, secondval; int size; System.out.println( "Input board size " ); try{ System.in.read( inputBytes); size = inputBytes[0] - 48; if (inputBytes[1] != 10) size = (10 * size) + (inputBytes[1] -48); inputBytes = new byte[6]; } catch (IOException e) { size = 15; } theBoard = new hexBoard(size); System.out.println("Size " + size); theBoard.displayHexBoard(); while(gameOver == false) { System.out.println( "Input x-value: " ); try{ System.in.read( inputBytes); secondval = inputBytes[0] - 48; if (inputBytes[1] != 10) secondval = (10 * secondval) + (inputBytes[1] -48 ); secondval -= 1; inputBytes = new byte[6]; } catch (IOException e) { secondval = 0; } System.out.println( "Input y-value: " ); try{ System.in.read( inputBytes); firstval = inputBytes[0] - 48; if (inputBytes[1] != 10) firstval = (10 * firstval) + (inputBytes[1] -48); firstval -= 1; inputBytes = new byte[6]; } catch (IOException e) { firstval = 0; } if (firstval < 0 || secondval < 0 ) break; if (firstval >= size || secondval >= size ) break; if (!theBoard.board[firstval][secondval].contains.isEmpty() ) continue; here = new Move(firstval, secondval); theBoard.doIt(here, occupantType.HORZ); theBoard.displayHexBoard(); if (theBoard.isWon(occupantType.HORZ)) { gameOver = true; System.out.println( "You won" ); break; } guessing = new RandomIIAnalysis(theBoard, occupantType.VERT); left = theBoard.numberOfEmpties(); remaining = (int) ((left + 1)/2); compMove = guessing.choose_move(remaining); theBoard.doIt(compMove, occupantType.VERT); theBoard.displayHexBoard(); System.out.println(compMove); if (theBoard.isWon(occupantType.VERT)) { gameOver = true; System.out.println( "You lost" ); } if (theBoard.numberOfEmpties()==0) break; } } }