1 / 26

CHAPTER 10 FUN AND GAMES Group 1: Xiangling Liu

CHAPTER 10 FUN AND GAMES Group 1: Xiangling Liu. PART I Overview of the Chapter. OBJECT. How to use the binary search algorithm in word search puzzle problem to solve large instances of a word search in under 1 sec

paiva
Download Presentation

CHAPTER 10 FUN AND GAMES Group 1: Xiangling Liu

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 10 FUN AND GAMES Group 1: Xiangling Liu

  2. PART I Overview of the Chapter

  3. OBJECT • How to use the binary search algorithm in word search puzzle problem to solve large instances of a word search in under 1 sec • How to use the alpha-beta pruning algorithm to speed up the recursive algorithm presented in Section7.7 • How to use maps to increase the speed of the Tic-Tac-Toe algorithm

  4. 10.1 WORD SEARCH PUZZLES • The 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. 10.1.1 THEORY • The most direct algorithm is brute-force approach as follows: • Algorithm for each word W in the word list for each row R for each column C for each direction D check if W exists at row R, column C in direction D • The algorithm requires 8WRC checks

  6. 10.1.1 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. • Algorithm for each row R for each column C for each direction D for each word length L check if L chars starting at row R column C in direction D form a word • The algorithm requires 8LRC checks

  7. 10.1.1 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. • Algorithm for each row R for each column C for each direction D for each word length L check if L chars starting at row R column C in direction D form a word if they do not form a prefix break; //the innermost loop • The prefix testing can also be done by binary search

  8. 10.1.2 JAVA IMPLEMENTATION • WordSearch 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 in the 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. 10.2.1 ALPHA-BETA PRUNING • The 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. 10.2.2 TRANSPOSITION TABLES • A 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. 10.2.3 COMPUTER CHESS • Terminal 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. PART II Overview of TicTacToe Program

  16. WHAT HAVE BEEN REVISED • Use a static final int SIZE to replace literal size of the board in class TicTacToe • Replace 3*3 board with 4*4 board • Change 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 class • TicTacPanel class

  18. Best CLASS • The class is to store the value, the row and column corresponding to a position. • Has two constructors • public Best( int v ) • public Best( int v, int r, int c )

  19. Position CLASS • The class define a postion in the board and define the equals of position and the hashcode • Has three methods • Constructor: initialize the board • equals: defines if the position is equal or not • hashCode: 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 game • getBoard: get the board of the game • chooseMove with one parameter: choose the optimal move • chooseMove with four parameter: choose the optimal move • playMove: Play move, including checking legality • clearBoard: clear the board of game

  21. TicTacToe CLASS (cont) • boardIsFull: check if the board is full • isAWin: check if the side wins the game • place: place the mark at a position in the board • squareIsEmpty: checks if the square is empty • positionValue: computes static value of current position (win, draw, etc.)

  22. TicTacMain CLASS • The class is to add TicTacPanel object and windowAdapter show all actions. • Has two methods and class TicTacPanel • Constructor: add TicTacPanel object and windowAdapter • main: a very simple main method

  23. TicTacPanel CLASS • The 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 methods • Constructor: 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 move • resetIfDone: gives information of the game at the end of the game • actionPerformed: 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 file • Execute the program

  26. Thank you all!

More Related