2 Overview of the Chapter PART IOverview of the Chapter
3 OBJECTHow to use the binary search algorithm in word search puzzle problem to solve large instances of a word search in under 1 secHow to use the alpha-beta pruning algorithm to speed up the recursive algorithm presented in Section7.7How to use maps to increase the speed of the Tic-Tac-Toe algorithm
4 10.1 WORD SEARCH PUZZLESThe input to the word search puzzle problem is a two-dimensional array of characters and a list of words.The object is to find the words in the grid.Words may be oriented in one of eight directions (horizontal, vertical or diagonal).
5 THEORYThe most direct algorithm is brute-force approach as follows:Algorithmfor each word W in the word listfor each row Rfor each column Cfor each direction Dcheck if W exists at row R, columnC in direction DThe algorithm requires 8WRC checks
6 THEORY (cont)An alternative algorithm searches from each point in the grid in each direction for each word length and looks for the word in the word list. The lookups can be done by a binary search if the word list is sorted.Algorithmfor each row Rfor each column Cfor each direction Dfor each word length Lcheck if L chars starting at row Rcolumn C in direction D form a wordThe algorithm requires 8LRC checks
7 THEORY (cont)An further improved algorithm is to add an assertion. If a character sequence is not a prefix of any word, we can stop that search and look in another direction.Algorithmfor each row Rfor each column Cfor each direction Dfor each word length Lcheck if L chars starting at row Rcolumn C in direction D form a wordif they do not form a prefixbreak; //the innermost loopThe prefix testing can also be done by binary search
8 JAVA IMPLEMENTATIONWordSearch class: to store the grid and word list, as well as the corresponding input streams.A constructor. It merely opens and reads the two files corresponding to the grid and the word list.Private openFile method. It repeatly prompts for a file until an open is successful.Private readWords method. It reads the word list.Private readPuzzle method reads the grid and is also concerned with error handling.Public solvePuzzle method nests the row, column and direction loops and then calls the private method solveDirection for each possibility.
9 10.1.2 JAVA IMPLEMENTATION (cont) Private solveDirection method constructs a string by starting at the base row and colunm and extending inthe appropriate direction.Private prefixSearch method performs the binary search for word search.Main class is a simple routine for the word search puzzle problem
10 10.2 THE GAME OF TIC-TAC-TOE A simple algorithm known as mininax strategy allows the computer to select an optimal move in a game of Tic-Tac-Toe. It involves the following decisions.A terminal position can immediately be evaluated, so if the position is terminal, return its value.Otherwise, if it is the computer’s turn to move, return the maximum value of all positions reachable by making one move. The reachable values are calculated recursively.Otherwise, it is the human player’s turn to move. Return the minimum value of all positions reachable by making one move. The reachable values are calculated recursively.
11 ALPHA-BETA PRUNINGThe minimax strategy gives an optimal Tic-Tac-Toe move, it performs more searching than necessary.Alpha-beta pruning is the strategy of reducing the number of positions evaluated in a minimax search using refutation.A refutation is a countermove that proves that a proposed move is not an improvement over moves previously considered. If a refutation found, we do not have to examine any more moves and the recursive call can return.Alpha is the value that the human player has to
12 10.2.1 ALPHA-BETA PRUNING (cont) refute and beta is the value that the computer has to refute.Alpha-beta pruning requires only a few changes to chooseMove.Both alpha and beta are passed as additional parameters.Add an assertion which provides for an immediate return when a refutation is found.
13 TRANSPOSITION TABLESA transpostion table stores previously evaluated positions.A map is used to implement the transposition table. Often the underlying implementation is a hash table.The chooseMove method has additional parameters, all of which have defaults.The use of the transposition table in this tic-tac-toe algorithm can almost double the program’s speed by removing about half the positons from consideration.
14 COMPUTER CHESSTerminal positions cannot be searched in computer chess. So we have to stop the search after a certain depth of recursion is reached.In the best programs, considerable knowledge is built into the evaluation function.
15 Overview of TicTacToe Program PART IIOverview of TicTacToe Program
16 WHAT HAVE BEEN REVISEDUse a static final int SIZE to replace literal size of the board in class TicTacToeReplace 3*3 board with 4*4 boardChange the law of winning. If one side has occupied 3 square in the diagonal or back-diagonal, the side wins the game
17 CLASSES OF THE PROGRAM Best class Position class TicTacToe class TicTacMain classTicTacPanel class
18 Best CLASSThe class is to store the value, the row and column corresponding to a position.Has two constructorspublic Best( int v )public Best( int v, int r, int c )
19 Position CLASSThe class define a postion in the board and define the equals of position and the hashcodeHas three methodsConstructor: initialize the boardequals: defines if the position is equal or nothashCode: defines the hash value of the board position in the hash table
20 TicTacToe CLASS The class has eleven methods. Constructor: clear the board of gamegetBoard: get the board of the gamechooseMove with one parameter: choose the optimal movechooseMove with four parameter: choose the optimal moveplayMove: Play move, including checking legalityclearBoard: clear the board of game
21 TicTacToe CLASS (cont) boardIsFull: check if the board is fullisAWin: check if the side wins the gameplace: place the mark at a position in the boardsquareIsEmpty: checks if the square is emptypositionValue: computes static value of current position (win, draw, etc.)
22 TicTacMain CLASSThe class is to add TicTacPanel object and windowAdapter show all actions.Has two methods and class TicTacPanelConstructor: add TicTacPanel object and windowAdaptermain: a very simple main method
23 TicTacPanel CLASSThe class is to set a GridLayout and make every square listen to the action of button. Compute the value of the computer side and make a move. Calculate and show the game result.Has five methodsConstructor: set a GridLayout and make every square listen to the action of button. After each game, reset the board.resetBoard: reset the board and make each square of the board enable
24 TicTacPanel CLASS (cont) doCompMove: the computer side to choose best position and make a moveresetIfDone: gives information of the game at the end of the gameactionPerformed: performs a move corresponding to human's or compter's move and gives the result of the game
25 JAVA CODE AND RUNNING RESULTS See the attached fileExecute the program