11 project 2 towers of hanoi. 22 towers of hanoi is a well known puzzle that can be very difficult...

12
1 Project 2 Towers of Hanoi

Upload: stewart-scott

Post on 05-Jan-2016

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 11 Project 2 Towers of Hanoi. 22 Towers of Hanoi is a well known puzzle that can be very difficult to solve manually but can be programmed very easily

11

Project 2

Towers of Hanoi

Page 2: 11 Project 2 Towers of Hanoi. 22 Towers of Hanoi is a well known puzzle that can be very difficult to solve manually but can be programmed very easily

22

Towers of Hanoi

Towers of Hanoi is a well known puzzle that can be very difficult to solve manually but can be programmed very easily with a recursive solution.

http://en.wikipedia.org/wiki/Tower_of_Hanoi

Write a C# console application to show the moves to solve the puzzle for an arbitrary number of disks. Call the pegs A, B, and C. Let the user specify the number of disks.

Page 3: 11 Project 2 Towers of Hanoi. 22 Towers of Hanoi is a well known puzzle that can be very difficult to solve manually but can be programmed very easily

3

Specifications

Get the number of disks from the user. Require the number to be greater than 0 and

no more than 10.

Start with all disks on Peg A

Output a message describing each move.

After each move show the configuration of the pegs.

Your output should match the sample runs for the same input.

Page 4: 11 Project 2 Towers of Hanoi. 22 Towers of Hanoi is a well known puzzle that can be very difficult to solve manually but can be programmed very easily

4

Character Graphics

Represent disk n by a string of 2n-1 X's

X

XXX

XXXXX

XXXXXXX

Example: A stack of four disks.

Page 5: 11 Project 2 Towers of Hanoi. 22 Towers of Hanoi is a well known puzzle that can be very difficult to solve manually but can be programmed very easily

5

Sample Run

Continued on next slide.

Page 6: 11 Project 2 Towers of Hanoi. 22 Towers of Hanoi is a well known puzzle that can be very difficult to solve manually but can be programmed very easily

6

Sample Run (continued)

Page 7: 11 Project 2 Towers of Hanoi. 22 Towers of Hanoi is a well known puzzle that can be very difficult to solve manually but can be programmed very easily

7

Sample Run

Page 8: 11 Project 2 Towers of Hanoi. 22 Towers of Hanoi is a well known puzzle that can be very difficult to solve manually but can be programmed very easily

8

Specifications

Define a class to represent a peg and the disks stacked on top of it. Essentially identical to a stack.

Each Peg should have a name, consisting of a single character.

Enforce the constraint that you can't put a larger disk on top of a smaller one.

Throw exception on violation of this rule or stack overflow. (Shouldn’t happen)

Page 9: 11 Project 2 Towers of Hanoi. 22 Towers of Hanoi is a well known puzzle that can be very difficult to solve manually but can be programmed very easily

9

Specifications

Avoid throwing an exception on invalid input. Use int.TryParse() to convert strings to ints.

Make your output match the sample runs.

Page 10: 11 Project 2 Towers of Hanoi. 22 Towers of Hanoi is a well known puzzle that can be very difficult to solve manually but can be programmed very easily

1010

Ground Rules You may work with one other student.

OK to work alone if you prefer.

If you do work as a pair Work together!

Both members are expected to contribute. Both members should understand the program in

detail. Submit a single program.

Do not share your code with other students. Before or after submitting the project. OK to discuss the project.

Do not copy any other student’s work. Don’t look at anyone else’s program. Don’t let anyone look at your program.

Page 11: 11 Project 2 Towers of Hanoi. 22 Towers of Hanoi is a well known puzzle that can be very difficult to solve manually but can be programmed very easily

1111

Ground Rules

Except for code posted on the class web site

Do not copy code from the Internet or any other source.

Write your own code.

Page 12: 11 Project 2 Towers of Hanoi. 22 Towers of Hanoi is a well known puzzle that can be very difficult to solve manually but can be programmed very easily

1212

Submission

Project is due by 11:59 PM, Wednesday, Jan. 26. Late projects will not be accepted.

Deliverables: Zipped project folder. Use the Windows "Send to Compressed Folder"

command to zip your folder.

Submit your file via Blackboard Assignments. Not Email

If you work with another student, include both names in the submission comments.

Other student submit Blackboard comment with both names.

End of Presentation