Statement of the Problem Problem - how to place eight queens on a chessboard so that no two queens...

12
Statement of the Problem Problem - how to place eight queens on a chessboard so that no two queens can attack each other:

Transcript of Statement of the Problem Problem - how to place eight queens on a chessboard so that no two queens...

Page 1: Statement of the Problem Problem - how to place eight queens on a chessboard so that no two queens can attack each other:

Statement of the Problem

Problem - how to place eight queens on a chessboard so that no two queens can attack each other:

                                                 

Page 2: Statement of the Problem Problem - how to place eight queens on a chessboard so that no two queens can attack each other:

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.

Page 3: Statement of the Problem Problem - how to place eight queens on a chessboard so that no two queens can attack each other:

PointersWe 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.

                                                                                        

Page 4: Statement of the Problem Problem - how to place eight queens on a chessboard so that no two queens can attack each other:

CRC Card for Queen

                                                                                                                                                            

Page 5: Statement of the Problem Problem - how to place eight queens on a chessboard so that no two queens can attack each other:

CRC Card for Queen - Backside

                                                                                             

Page 6: Statement of the Problem Problem - how to place eight queens on a chessboard so that no two queens can attack each other:

InitializationInitialization will set each queen to point to a neighbor, and set column value

Finding First SolutionFinding 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

Page 7: Statement of the Problem Problem - how to place eight queens on a chessboard so that no two queens can attack each other:

Advancing to Next Positionfunction 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 QueenTwo 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

Page 8: Statement of the Problem Problem - how to place eight queens on a chessboard so that no two queens can attack each other:

import java.awt.*;import java.applet.*;

class Queen {// data fields

private int row;private int column;private Queen neighbor;

// constructorQueen (int c, Queen n) {

// initialize data fieldsrow = 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;}

elsereturn false;

row = 1;return findSolution();

}

Page 9: Statement of the Problem Problem - how to place eight queens on a chessboard so that no two queens can attack each other:

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);}

Page 10: Statement of the Problem Problem - how to place eight queens on a chessboard so that no two queens can attack each other:

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;}

}

Page 11: Statement of the Problem Problem - how to place eight queens on a chessboard so that no two queens can attack each other:

The Result

Page 12: Statement of the Problem Problem - how to place eight queens on a chessboard so that no two queens can attack each other:

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.