Download presentation
Presentation is loading. Please wait.
1
CS 106 Introduction to Computer Science I 04 / 02 / 2008 Instructor: Michael Eckmann
2
Michael Eckmann - Skidmore College - CS 106 - Spring 2008 Today’s Topics Comments and/or Questions? lab questions? recursion –continue with the Towers of Hanoi Card and Deck implementation
3
Towers of Hanoi Three pegs Start with different size discs on first peg Move them all to the third peg using the following rules – Move only one disc at a time – Can't place a larger disc on a smaller disc – All discs must be on some peg except for the disc in transit between pegs Let's see an applet on the web.
4
Towers of Hanoi Do a solution with 3 discs. Do a solution with 4 discs. See any patterns emerging? Is it recursive?
5
Towers of Hanoi To move N discs from first peg to third – Move topmost N-1 discs from first peg to second – Move the largest disc from the first peg to third – Move the N-1 discs from the second peg to the third
6
Towers of Hanoi During the solution, each peg takes on a role – Either the start peg, end peg, or intermediate peg. – Notice: In the first and third parts, notice that they are the same as the whole, with the roles of the pegs changing. First part: – Move from: first peg (start) – To: second peg (end) – Using as intermediate: third peg (intermediate) Third part: – Move from: second peg (start) – To: third peg (end) – Using as intermediate: first peg (intermediate)
7
Towers of Hanoi void moveTower(int numDiscs, int start, int end, int intermed) { if (numDiscs == 1) moveOneDisc(start, end); else { moveTower(numDiscs – 1, start, intermed, end); moveOneDisc(start, end); moveTower(numDiscs – 1, intermed, end, start); } void moveOneDisc(int p1, int p2) { System.out.println(“Move the disc from peg “ + p1 + “ to peg “ + p2); }
8
Towers of Hanoi Let's put this in a program and run it. Does anyone think that an iterative solution would be possible? If so, do you think it'll be easier? You can think about this on your own if you wish and try to come up with an iterative (that is, non-recursive) solution.
9
Back to Object Oriented programming Let's design and implement classes to represent a Deck and a Card (like in a typical deck of 52 playing cards.)
10
Card/Deck implementation Purpose of these classes will be to create a general Card class and general Deck class that could be used for an arbitrary card game. So, nothing game specific is desired to be in the classes. What data describes a Card? What data describes a Deck? What behaviors does a Card exhibit? What behaviors does a Deck exhibit?
11
Card/Deck implementation What data describes a Card? – Value and suit (types for these?) What data describes a Deck? – An ordered group of some fixed number of cards What behaviors does a Card exhibit? – None really independent of a Deck What behaviors does a Deck exhibit? – Can shuffle it – Can cut it – Can deal a card from it
12
Shuffle method Ideas for the shuffle method – try to simulate human shuffling – or just swap 2 cards a bunch of times to get a mixed deck – or other ideas?
13
Cut method Ideas for the cut method Have another array of 52 Cards to copy into (then later copy this array back into original.) Generate some random number between 0 and 51 and use that as the place to cut the cards. Keeping track of starting and ending indicies is a challenge but its doable if we're careful. Draw copying idea on board. – How many loops and what will each one do?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.