1 / 13

Data Structures CSCI 132, Spring 2019 Lecture 18 Recursion and Look-Ahead

Learn how to use recursion and look-ahead techniques to solve the Eight Queens Problem, where 8 queens need to be placed on a chessboard without being able to take each other. We will explore the implementation of the solution using data structures and the MiniMax method.

pkirby
Download Presentation

Data Structures CSCI 132, Spring 2019 Lecture 18 Recursion and Look-Ahead

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. Data StructuresCSCI 132, Spring 2019Lecture 18Recursion and Look-Ahead

  2. Recall The Eight Queens Problem Can we place 8 queens on a chessboard so that none can take another? (I.e. none share a row, column or diagonal)? Q

  3. Recall 8 Queens solution void solve_from(Queens &configuration) { if (configuration.is_solved( )) { configuration.print( ); } else { for (int col = 0; col < configuration.board_size; col++) { if (configuration.unguarded(col)) { configuration.insert(col); solve_from(configuration); // Recursively continue to add queens. configuration.remove(col); } } } }

  4. Print only one solution bool solve_from(Queens &configuration) //return true if solved { if (configuration.is_solved( )) { configuration.print( ); return true; } else { bool solved = false; for (int col = 0; !solved && col < configuration.board_size; col++) { if (configuration.unguarded(col)) { configuration.insert(col); solved = solve_from(configuration); configuration.remove(col); } } return solved; } }

  5. Recursion tree for 4 queens

  6. Game Tree for "Game of Six"

  7. The MiniMax method

  8. Finding the Best Move

  9. Looking ahead recursively LookAhead() for each possible move make move answer = lookAhead() undo move keep track of best answer return best answer

  10. Example: Tic Tac Toe class Move { //Class to represent a move for tic tac toe public: Move( ); Move( int r, int c); int row; int col; }; Move::Move( ) { row = 3; col = 3; } Move::Move(int r, int c) { row = r; col = c; }

  11. The Playing Board class Board { public: Board( ); bool done( ) const; //true if game done void print( ) const; void instructions( ) const; bool better(int value, int old_value) const; //true if value better than old void play(Move try_it); //play given move int worst_case( ) const; //worst possible case value int evaluate( ) const; //how good is configuration? int legal_moves(Stack &moves) const; //List of legal moves private: int squares[3][3]; //0 => unoccupied, 1=> player 1, 2=>player2 int moves_done; int the_winner( ) const; };

  12. Some Board Methods void Board :: play(Move try_it) { squares[try_it.row][try_it.col] = moves_done%2 + 1; moves_done++; } bool Board :: done( ) const { return ( moves_done == 9) || (the_winner( ) > 0); } int Board :: evaluate( ) const { int winner = the_winner( ); if (winner == 1) { return 10 - moves_done; } else if (winner == 2) { return moves_done - 10; } else { return 0; } }

  13. Looking Ahead int look_ahead(const Board &game, int depth, Move &recommended) { if (game.done( ) || depth == 0) { return game.evaluate( ); } else { Stack moves; game.legal_moves(moves); int value, best_value = game.worst_case( ); while (!moves.empty( )) { Move try_it, reply; moves.top(try_it); //get next move Board new_game = game; //make copy of board new_game.play(try_it); //try next move value = look_ahead(new_game, depth - 1, reply); //recursively look ahead if (game.better(value, best_value)) { best_value = value; //value is new best value recommended = try_it; //recommend this move } moves.pop( ); //pop it off the stack } return best_value; //return best value found } }

More Related