Lecture of 19 February 1996

Reading Assignment

Read pages 103-134 in Budd.

Case Study: Eight Queens

Dylan Eight-Queens Example

module: dylan author: Joseph N. Wilson copyright: 1996 Joseph N. Wilson description: Dylan example of the eight queens program strategy employed by Budd in Chapter 5 of his book *An Introduction to Object-Oriented Programming*. define class <queen> (<object>) slot row :: <integer>; slot column :: <integer>, init-keyword: column:; slot neighbor :: <queen>, init-keyword: neighbor:; end class <queen>; define method first-position (q :: <queen>) => <boolean>; // // Finds the first unattacked board configuration for queen q. // if (first-position (q.neighbor)) q.row := 1; test-or-advance (q); else #f; end if; end method first-position; define method test-or-advance (q :: <queen>) => <boolean>; // // Tries to insure that Queen q's position is not attacked. // If current position is unattacked, returns #t. // Otherwise it attempts to get to next board configuration and // returns value given by next-position. // if (check-row (q.neighbor, row: q.row, col: q.column)) next-position (q); else #t; end if; end method test-or-advance; define method check-row (q :: <queen>, #key row: r, col: c) => <boolean>; // // Checks to see if Queen q or any of her neighbors can attack position // given by keyword parameters row: col: // // Returns #t if position can be attacked, #f otherwise. // if (r = q.row) #t; else let cd = q.column - c; if (q.row + cd = r | q.row - cd = r) #t; else check-row (q.neighbor, row: r, col: c); end if; end if; end method check-row; define method next-position (q :: <queen>) => <boolean>; // // Tries to modify the board to get the next unattacked board // configuration for queen q. // // Returns #t if it succeeds. // Returns #f if no more unattacked positions exist. // block (exit) if (q.row = 8) if (~ next-position (q.neighbor)) exit (#f); else q.row := 0; end if; end if; q.row := q.row + 1; test-or-advance (q); end block; end method next-position; define method print-board (q :: <queen>) princ (q.column); princ (' '); for (i from 1 below q.row) princ (". "); end for; princ ("Q "); for (i from q.row + 1 to 8) princ (". "); end for; print (""); print-board (q.neighbor); end method print-board; define method print (q :: <queen>) print (q.neighbor); princ (q.row); princ (" "); print (q.column); end method print; // // Class <null-queen> is used to terminate the recursive structure // employed in <queen> // define class <null-queen> (<object>) end class <null-queen>; define method first-position (q :: <null-queen>) #t; end method first-position; define method next-position (q :: <null-queen>) #f; end method next-position; define method check-row (q :: <null-queen>, #key row: r, col: c) #f; end method check-row; define method print-board (q :: <null-queen>) end method print-board; define method print (q :: <null-queen>) end method print; define method queens () let last-queen = make (<null-queen>); for (i from 1 to 8) last-queen := make (<queen>, neighbor: last-queen, column: i); end; if (first-position (last-queen)) print-board (last-queen); print (last-queen); end if; end method queens;

Programming Assignment Due Wednesday 28 February

Modify the Dylan program to solve exercises 1 and 2 of page 84. Don't worry about filtering rotations.

Static and Dynamic Binding

The Container Problem


This document is copyright 1996 by Joseph N. Wilson.