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

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Applets An applet is similar to a Java program, but it runs within a Web-browser on a client machine. For security, you cannot change anything on a client.
Assignment – no class Wednesday All: watch the Google Techtalk “Getting C++ Threads Right” by Hans Boehm at the following link in place of Wednesday’s.
13-Jun-15 Model-View-Controller. 2 Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
Jan Containers Yangjun Chen Dept. Business Computing University of Winnipeg.
24-Jun-15 Simple Animation. 2 The bouncing ball The “bouncing ball” is to animation what “Hello world” is to programming With a single Thread, we can.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
Model-View-Controller. Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities are Design.
Unit 071 Review of Applets Learning Outcomes oDistinguish between Java Applications and Java Applets. oWrite applet programs that can load images and play.
29-Jun-15 Model-View-Controller. 2 Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities.
14-Jul-15 Model-View-Controller. 2 Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-x: Critters reading: HW9 Spec.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
Java: Chapter 1 Computer Systems Computer Programming II.
JAVA APPLETS By: Ms. Humaira Siddiqui. Java and the Internet Java is considered to be a language well suited to be used in the internet. In contrast with.
© 2007 Lawrenceville Press Slide 1 Chapter 2 HTML An example HTML document Hello world!
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets.
Repetition - while, for and do “Loops - iterations Chapter Eight.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 12.
Review of CIS 120 Concepts: What you said you want….
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
Sudoku Jordi Cortadella Department of Computer Science.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
QUEUES What are Queues? Creating a Queue Enqueuing and Dequeuing Testing for an Empty Queue.
1 Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking.
Applets An applet is similar to a Java program, but it runs within a Web-browser on a client machine. For security, you cannot change anything on a client.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
SNU OOPSLA Lab. 1 Great Ideas of CS with Java Part 1 WWW & Computer programming in the language Java Ch 1: The World Wide Web Ch 2: Watch out: Here comes.
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
Copyright 2010 by Pearson Education Building Java Programs Homework 8: Critters reading: Critters Assignment Spec.
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
Chomp. How is the game played Human player goes first choose a square, all to the right and down are “eaten” computer takes a turn whoever is forced to.
LECTURE 21: RECURSION & LINKED LIST REVIEW CSC 212 – Data Structures.
1 Java and AWT CPS 470 Spring 1998 Laura Campbell.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
Recitations Sep 8-9 u conditionals: if-else statement u boolean expressions u classes and instances.
Creating an Object that can draw itself The paint method can ‘draw’ because it is passed a graphics environment as a parameter. If a class method is passed.
21-Jun-16 Swing Basics and Model-View- Controller.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Recursion -- Introduction
karel_part4_functions_2
8-Queens Puzzle.
Java Programming: Guided Learning with Early Objects
Class Structure 16-Nov-18.
Jordi Cortadella Department of Computer Science
Week 3 Object-based Programming: Classes and Objects
Class Structure 7-Dec-18.
Building Java Programs
Class Structure 2-Jan-19.
Recap Week 2 and 3.
CS Software Studio Assignment 1
Class Structure 25-Feb-19.
Java Programming Language
Data Structures & Algorithms
PowerPoint Presentation Authors of Exposure Java
Principles of Object Oriented Programming
Presentation transcript:

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.

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.

CRC Card for Queen

CRC Card for Queen - Backside

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

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

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

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

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

The Result

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.