playTicTacToe.java playTicTacToe.java // version XXXXXXXXXX public class playTicTacToe { static boolean DEBUG = false; // not final to allow local debug toggles, ie use debug in 1 method only...

1 answer below »
no references


playTicTacToe.java playTicTacToe.java // version 201935.000 public class playTicTacToe {     static boolean DEBUG = false; // not final to allow local debug toggles, ie use debug in 1 method only     public static int[] buildBoards( TicTacToeNode root, char symbol ) {         // A straight forward way to generate at most 9 children per level.         // If the spot is already filled we skip that child, generating the         // expected tree.  Note we create the child after the if check to         // avoid throwing our count of nodes created off.         for ( int i = 0; i < 3; i++ ) {             for ( int j ="">< 3; j++ ) {                char[][] newboard =" root.copyBoard();"                if ( newboard[i][j] ="= Character.MIN_VALUE ) {"                   newboard[i][j] =" symbol;"                    tictactoenode child =" new TicTacToeNode( );"                    child.board =" newBoard;"                    root.children.add( child );=""                    if ( debug )=""                      system.out.println( child );=""                }=""             }=""         }=""         for ( tictactoenode child : root.children ) {=""             if ( child.isgamewon() ) {=""                 // stop creating branches, we are at a leaf node=""                 // do not return from here so that the other siblings=""                 // will be correctly generated=""                 if ( child.isgamewon('x') ) {root.xwins++; child.xwins++;}=""                 if ( child.isgamewon('o') ) {root.owins++; child.owins++;}=""             } else if ( !child.isfull() ) {=""                 // if the board is full there's nothing to do, simply iterate=""                 // to the next sibling.  if the board is not a win, nor a tie=""                 // then there are more children to create, thus the recursive=""                 // step appears here=""                 int[] scores;=""                 scores =" buildBoards( child, symbol=='O'? 'X' : 'O' );"                 root.xwins +=" scores[0];"                 root.owins +=" scores[1];"             }=""         }=""         =""         // once all children are processed we can return the results of the parent=""         // node.  to reach this return statement we will have already have created=""         // all sub-branches and counted their scores.=""         int[] scores =" {root.xWins, root.oWins};"         return scores;=""     }=""     public static void main( string[] args ) {=""         tictactoenode ttttree =" new TicTacToeNode();">< 1 + 9 + 9*8 + 9*8*7 + ... + 9*8*7*6*5*4*3*2*1 = 986 410         // 9! =" 362,880"         // 3^9 =" 19,683"         // choose formula with "one too many moves" error =" 6,046"         // minimal without rotation =" 5,478"         // minimal with rotation/reflections =" 765"         buildboards( ttttree, 'x' );=""         system.out.println(tictactoenode.nodes + " nodes created.");=""         system.out.printf("x: %d\t\to: %d\n", ttttree.xwins, ttttree.owins);=""         =""         // demonstrate symmetry by presenting opening moves=""         for( tictactoenode child : ttttree.children ) {=""             system.out.println( child );=""             system.out.printf("x: %d\t\to: %d\n", child.xwins, child.owins);            =""         }=""         =""         system.out.println("");=""     }="" }="" tictactoenode.java="" tictactoenode.java=""  version 201935.000="" import java.util.arraylist;="" import java.util.arrays;="" public class tictactoenode {=""     static int nodes =" 0;"     char[][] board;=""> children;     int xWins = 0;     int oWins = 0;     int minimaxScore = 0;     public TicTacToeNode() {         nodes++;         board = new char[3][3];         children = new ArrayList<>();     }          @Override     public String toString() {         String boardStr;                  boardStr = "---\n";         boardStr += Arrays.toString( board[0] ) + "\n";         boardStr += Arrays.toString( board[1] ) + "\n";         boardStr += Arrays.toString( board[2] ) + "\n";         boardStr += "---\n";                           return boardStr;     }          public boolean isGameWon( ) {         return isGameWon( 'X' ) || isGameWon( 'O' );     }          public boolean isGameWon( char symbol ) {         boolean won;                  // 2 diagonals         won = (board[1][1]==symbol) &&                  (( board[0][0]==board[1][1] && board[1][1]==board[2][2] ) ||                   ( board[0][2]==board[1][1] && board[1][1]==board[2][0]) );                  for ( int i = 0; i < 3; i++ ) {             // 3 rows=""             won =" won || (board[i][0]==symbol && board[i][0] == board[i][1] && board[i][1] == board[i][2]);"             // 3 columns=""             won =" won || (board[0][i]==symbol && board[0][i] == board[1][i] && board[1][i] == board[2][i]);"         }=""         =""         return won;=""     }=""     =""     public boolean isfull() {=""         boolean full =" true;"         =""         for ( char[] row : board )=""             for ( char pos : row )=""                 full &=" ( pos != Character.MIN_VALUE );"         =""         return full;=""     }=""     =""     public char[][] copyboard() {=""         // java's .clone() does not handle 2d arrays=""         // by copying values, rather we get a clone of refs=""         char[][] boardcopy =" new char[3][3];"         for ( int i ="">< 3; i ++ )             system.arraycopy(board[i], 0, boardcopy[i], 0, 3);=""         =""         return boardcopy;=""     }=""     ="" }="" package="" wolfsheep;="" import="" java.util.deque;="" import="" java.util.linkedlist;="" public="" class="" wolfsheep="" {="" **="" *="" solves="" the="" sheep-cabbage-wolf="" riddle="" and="" prints="" out="" its="" solution="" */="" public="" static="" void="" solvesheepcabbagewolf(){="" sheepcabbagewolf(false,="" false,="" false,="" false,="" new="">()); } /** * Solves the sheep-cabbage-wolf riddle and prints out its solution (the actual worker method) * @param sheep true if the sheep is on the right bank, false otherwise * @param cabbage true if the cabbage is on the right bank, false otherwise * @param wolf true if the wolf is on the right bank, false otherwise * @param farmer true if the farmer (boat) is on the right bank, false otherwise * @param solution partial solution * @return false if the partial solution is invalid */ private static boolean sheepCabbageWolf(boolean sheep, boolean cabbage, boolean wolf, boolean farmer, Deque solution) { if (sheep && cabbage && wolf && farmer) { printSolution(solution); return true; } if (!checkConsistency(sheep, cabbage, wolf, farmer)) { return false; } if (solution.isEmpty() || !solution.peek().equals("boatman")) { solution.addFirst("boatman"); if (sheepCabbageWolf(sheep, cabbage, wolf, !farmer, solution)) { return true; } solution.pop(); //backtrack } if (sheep == farmer && (solution.isEmpty() || !solution.peek().equals("sheep"))) { solution.addFirst("sheep"); if (sheepCabbageWolf(!sheep, cabbage, wolf, !farmer, solution)) { return true; } solution.pop(); //backtrack } if (cabbage == farmer && (solution.isEmpty() || !solution.peek().equals("cabbage"))) { solution.addFirst("cabbage"); if (sheepCabbageWolf(sheep, !cabbage, wolf, !farmer, solution)) { return true; } solution.pop(); //backtrack } if (wolf == farmer && (solution.isEmpty() || !solution.peek().equals("wolf"))) { solution.addFirst("wolf"); if (sheepCabbageWolf(sheep, cabbage, !wolf, !farmer, solution)) { return true; } solution.pop(); //backtrack } return false; } /** * Check consistency of the partial solution * @param sheep if the sheep is on the right bank, false otherwise * @param cabbage if the cabbage is on the right bank, false otherwise * @param wolf if the wolf is on the right bank, false otherwise * @param farmer if the farmer is on the right bank, false otherwise * @return true if the solution is consistent, false otherwise */ private static boolean checkConsistency(boolean sheep, boolean cabbage, boolean wolf, boolean farmer) { if (sheep == cabbage && sheep != farmer) { return false; } else if (sheep == wolf && sheep != farmer) { return false; } return true; } /** * Prints out the solution of the sheep-cabbage-wolf problem * @param solution */ private static void printSolution(Deque solution) { while (!solution.isEmpty()) { System.out.print(solution.pollLast() + " "); } System.out.println(); } public static void main(String[] args) { solveSheepCabbageWolf(); } }
Answered 101 days AfterOct 31, 2021

Answer To: playTicTacToe.java playTicTacToe.java // version XXXXXXXXXX public class playTicTacToe {...

Karthi answered on Feb 09 2022
113 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here