Homework 12 • Due Sun., 11/24 ( MT sections ) Mon., 11/25 ( WTh sections ) • Problems (2 of them) http://www.cs.hmc.edu/courses/2002/fall/cs5/week_12/homework.html • Tutors available Saturday afternoons Lac Lab Sunday afternoons Lac Lab Sunday evenings Lac Lab and AC Monday evenings Lac Lab and AC M. Beaumont-Gay & M. Yi in LAC E. Flynn, Y.M. Kim, & A. Klose in AC M. Yi, C. Wottawa, & A. Utter in LAC A. Kangas, P. Scott, A. Pipkin in AC Problem 2 / Final Project A “film database” application: 5 classes class name data member data member type Self-referential data ? A film database application: class Director String fname FilmDB filmDB String lname class Film String title int year String rating double review Director dir FilmDB filmDB // method skeletons… class Film { // data members Director dir class FilmDB int count Film[] films class Director { private String fname; private String lname; private FilmDB filmDB; // method skeletons class FilmDB { // data members Film[] films films[0] films[1] // method skeletons Problem 2 - skeleton code Classes Data Members CS5App none Film String title int year String rating double review Director dir Methods public static void main(String[] args) public static void printMenu() public Film(String title, int year, String rating, double review, Director dir) public String getTitle() public int getYear() public String getRating() public double getReview() public void display() Classes Director DirectorDB FilmDB Data Members Methods String fname String lname FilmDB filmDB public Director(String fname, String lname, int capacity) public String getFullName() public FilmDB getFilmDB() int count Director[] dirs public DirectorDB(int capacity) public void displayAllDirectors() public void displayFilmsByDirector(String fname, String lname) public Director findDirectorByName(String fname, String lname) public void addDirector(Director dir) public int getCount() public boolean isFull() int count Film[] Films public public public public public public public public public public FilmDB(int capacity) boolean isFull() int getCount() void addFilm(Film f) Film findFilmByTitle(String title) void displayAllFilms() void displayFilmsByTitle(String titlepiece) void displayFilmsByYear(int year) void displayFilmsByRating(String rating) void displayFilmsByReview(double minreview) Problem 2 • Implement the main menu (in the CS5App class) Please choose an action from the following list: 0 1 2 3 4 5 6 Display Display Display Display Display Display Display All Directors All Films Films by Title Films by Director Films by Year Films by Rating (G, PG, …) Films by Review (0 to 10) 7 Add a New Film 9 Quit • Create skeleton versions of the necessary classes... Care to Swap ? Suppose we want to swap the variables x and y … int x = 42; int y = 5; int x int y Problem 1 - recursion void sort(double[] arr, int L, int H) 80 L 97 42 75 23 H Iterative vs. Recursive void moveMinToLeft(double[] arr, int L, int H) 80 L 97 42 75 23 H Iterative vs. Recursive void moveMinToLeft(double[] arr, int L, int H) 80 L 97 42 75 23 H Swapping Suppose we want to swap the H-1st and the Hth values... 80 L 97 42 75 23 H-1 H Problem 1 - recursion void sort(double[] arr, int L, int H) 80 L 97 42 75 23 H Problem 1 - recursion public static double harmonic(int N) Problem 1 - recursion public static String reverse(int N) public static int chooseMove3Ply(Board b, char ox) for (int m=0 ; m<b.getCols() ; ++m) { if (b.allowsMove(m)) { Board b2 = b.newAddMove(m,ox); if ( b2.winsFor(ox) || b2.isFull() ) score = evaluate(b,ox); else { int m2 = chooseMove2Ply(b2,opp(ox)); Board b3 = b2.newAddMove(m2,opp(ox)); if ( b3.winsFor(opp(ox)) || b3.isFull() ) score = evaluate(b,ox); else { int m3 = chooseMove1Ply(b3,ox); Board b4 = b3.newAddMove(m3, ox); score = evaluate(b4, ox); } } if (score > BSSF) { BSSF = score; BMSF = m; } } } // end if (b.allowsMove(m)) // end for (int m=0 ; … return BMSF; Ex. Cr. evaluating move m by finding score remembering the best score and the best move m so far Looking N Ply ahead! 1-ply search 2-ply search 3-ply search N-ply search chooseMove1Ply chooseMove2Ply chooseMove3Ply evaluate 1 - ply 2 - ply (N-1) - ply ... int chooseMove(Board b, char ox, int ply) With a 4-ply lookahead, X will think every move looks equivalent on this board With a 5-ply lookahead, X will know to move to column 3 0 1 2 3 4 5 6 ‘X’ ‘O’