Download presentation
Presentation is loading. Please wait.
Published byEleanor O’Brien’ Modified over 9 years ago
1
Statement of the Problem Problem - how to place eight queens on a chessboard so that no two queens can attack each other:
2
OOP Approach More than just solving the problem, we want to solve the problem in an OOP manner. 1.Structure the program so that the data values themselves discover the solution. 2.Similar to creating a universe and setting it in motion. 3.No single controlling manager. Observations Here are a few observations we can make concerning this problem: 1.Queens can be assigned a column, problem is then to find row positions. 2.One obvious behavior is for a queen to tell if it can attack a given position. 3.Can structure the problem using generators - each queen can be asked to find one solution, then later be asked to find another solution.
3
Pointers We can make each queen point to the next on the left, then send messages only to the rightmost queen. Each queen will in turn send messages only to the neighbor it points to.
4
CRC Card for Queen
5
CRC Card for Queen - Backside
6
Initialization Initialization will set each queen to point to a neighbor, and set column value Finding First Solution Finding first solution, in pseudo-code: function queen.findSolution -> boolean while neighbor.canAttack (row, column) do if not self.advance then return false; // found a solution return true; end We ignore for the moment the question of what to do if you don't have a neighbor
7
Advancing to Next Position function queen.advance -> boolean if row < 8 then begin row : = row + 1; return self.findSolution end // cannot go further, move neighbor if not neighbor.advance then return false row := 1 return self findSolution end The Last Queen Two approaches to handling the leftmost queen: 1.Null pointers - each queen must then test for null pointers before sending a message 2.Special ``sentinel'' value - indicates end of line for queens
8
import java.awt.*; import java.applet.*; class Queen { // data fields private int row; private int column; private Queen neighbor; // constructor Queen (int c, Queen n) { // initialize data fields row = 1; column = c; neighbor = n; } public boolean findSolution() { while (neighbor != null && neighbor.canAttach(row, column)) if (! advance()) return false; return true; } public boolean advance() { if (row < 8) { row++; return findSolution(); } if (neighbor != null) { if (! neighbor.advance()) return false; if (! neighbor.findSolution()) return false; } else return false; row = 1; return findSolution(); }
9
private boolean canAttach(int testRow, int testColumn) { int columnDifference = testColumn - column; if ((row == testRow) || (row + columnDifference == testRow) || (row - columnDifference == testRow)) return true; if (neighbor != null) return neighbor.canAttach(testRow, testColumn); return false; } public void paint (Graphics g) { // first draw neighbor if (neighbor != null) neighbor.paint(g); // then draw ourself // x, y is upper left corner int x = (row - 1) * 50; int y = (column - 1) * 50; g.drawLine(x+5, y+45, x+45, y+45); g.drawLine(x+5, y+45, x+5, y+5); g.drawLine(x+45, y+45, x+45, y+5); g.drawLine(x+5, y+35, x+45, y+35); g.drawLine(x+5, y+5, x+15, y+20); g.drawLine(x+15, y+20, x+25, y+5); g.drawLine(x+25, y+5, x+35, y+20); g.drawLine(x+35, y+20, x+45, y+5); g.drawOval(x+20, y+20, 10, 10); }
10
public void foo(Queen arg, Graphics g) { if (arg.row == 3) g.setColor(Color.red); } public class QSolve extends Applet { private Queen lastQueen; public void init() { int i; lastQueen = null; for (i = 1; i <= 8; i++) { lastQueen = new Queen(i, lastQueen); lastQueen.findSolution(); } public void paint(Graphics g) { // draw board for (int i = 0; i <= 8; i++) { g.drawLine(50 * i, 0, 50*i, 400); g.drawLine(0, 50 * i, 400, 50*i); } // draw queens lastQueen.paint(g); } public boolean mouseDown(java.awt.Event evt, int x, int y) { lastQueen.advance(); repaint(); return true; }
11
The Result
12
Chapter Summary 1.Important not for the problem being solved, but how it is solved. 2.Solution is the result of community of agents working together 3.No single controlling program - control is decentralized 4.Active objects determine their own actions and behavior.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.