/***********************************************************************/ // Play against the automatec tic-tac-toe player. This let's you read // in an arbitrary starting configuration for the board, so that you can // experiment with configurations that allow you to force a win against // the automated player. It also allows you to test other basic // functions out. /***********************************************************************/ import java.io.*; class TTTDemo { public static void main (String[] args) { String filename = null; //name of maze data file String line; //holds a line of input try { BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter name of Tic-Tac-Toe file: "); filename = console.readLine(); System.out.print ("\n" + filename + "\n\n\n"); TTT board = new TTT(filename); System.out.println(board); System.out.println ("\nEvaluation: " + board.eval()); System.out.print ("\nisDraw: " + board.isDraw() + "\n\n\n"); System.out.print ("Would you like to move first? (y/n) "); line = console.readLine(); line = line.trim(); int [] BestMove = new int [2]; if (line.charAt(0) == 'y') { while (board.eval() == 0 && ! board.isDraw()) { System.out.println(board); int movei, movej; System.out.print ("Enter row of move: "); line = console.readLine(); line = line.trim(); movei = Integer.parseInt (line); System.out.print ("Enter column of move: "); line = console.readLine(); line = line.trim(); movej = Integer.parseInt (line); board.setEntry(movei,movej,'W'); if (board.eval() == 0 && ! board.isDraw()) { System.out.println (board.stateScore (false, BestMove)); board.setEntry(BestMove[0],BestMove[1],'B'); System.out.println (board.stateScore (true, BestMove)); } } } else { while (board.eval() == 0 && ! board.isDraw()) { board.stateScore (true, BestMove); board.setEntry(BestMove[0],BestMove[1],'W'); if (board.eval() == 0 && ! board.isDraw()) { System.out.println(board); int movei, movej; System.out.print ("Enter row of move: "); line = console.readLine(); line = line.trim(); movei = Integer.parseInt (line); System.out.print ("Enter column of move: "); line = console.readLine(); line = line.trim(); movej = Integer.parseInt (line); board.setEntry(movei,movej,'B'); } } } System.out.println(board); if (board.eval() == -1 ) System.out.println("Black wins"); else if (board.eval() == 1) System.out.println("White wins"); else if (board.isDraw()) System.out.println("It's a draw."); } catch (FileNotFoundException e) { System.out.print ("\n\n"); System.out.println (e.getMessage()); System.out.print ("\n\n"); System.exit(0); } catch (IOException e) { System.out.print ("\n\n"); System.out.println (e.getMessage()); System.out.print ("\n\n"); System.exit(0); } } }