CS 111- Introduction to Computer Science
Homework #14


Due: beginning of class 42

Breakthrough Part 2 - Undo

1. Extend BreakthroughState with clone(): Extend class BreakthroughState.java according to this specification.  This specification differs from the previous specification in that BreakthroughState is now cloneable with a deep clone/copy.  In general, one makes an object cloneable (i.e. able to be copied) by (1) declaring the class to implement Cloneable (e.g. public class YourClassNameHere implements Cloneable { ... }), and (2) creating a method like the following:

        public Object clone() {
            try {
		YourClassNameHere copy = (YourClassNameHere) super.clone(); // shallow clone - fields are merely assigned
		// make deep copies of the reference (i.e. Object) fields of this object to the copy object.
                return copy;
            }
            catch (CloneNotSupportedException e) { // shouldn't get here if you've done part (1)
                throw new InternalError(e.toString());
            }
        }

2. Testing BreakthroughState: Test your class using the provided extended GUI.  Usage: java Breakthrough.  Use the "u" key to "undo" moves.

3. Extend BreakthroughGame: Extend class BreakthroughGame.java according to this specificationTest input and a transcript with input is provided.  For this extension the "undo" command will undo a previous move.  If there was no previous move, print "Cannot undo."  Use the Stack class and BreakthroughState.clone() for this implementation.

Note: There are often better, more memory-efficient means of implementing undo functionality.  The purpose of this exercise is to provide experience with deep cloning/copying and the stack data structure.