diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2017-02-06 11:41:36 -0500 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2017-02-06 11:41:36 -0500 |
| commit | 89cdf3efb49335e7c07a68a5a64657eeec2288a6 (patch) | |
| tree | cdc0fd8165e65b1637fa54cac11c932acefc8a89 /labs/lab6_toh | |
| download | coe0445-master.tar.gz coe0445-master.tar.bz2 coe0445-master.zip | |
Diffstat (limited to 'labs/lab6_toh')
| -rw-r--r-- | labs/lab6_toh/THComponent.java | 113 | ||||
| -rw-r--r-- | labs/lab6_toh/THFrame.java | 84 | ||||
| -rw-r--r-- | labs/lab6_toh/TowerOfHanoi.java | 164 | ||||
| -rw-r--r-- | labs/lab6_toh/TowersTester.java | 185 | ||||
| -rw-r--r-- | labs/lab6_toh/lab06_towers_of_hanoi.pdf | bin | 0 -> 130942 bytes |
5 files changed, 546 insertions, 0 deletions
diff --git a/labs/lab6_toh/THComponent.java b/labs/lab6_toh/THComponent.java new file mode 100644 index 0000000..3a61dc5 --- /dev/null +++ b/labs/lab6_toh/THComponent.java @@ -0,0 +1,113 @@ +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.geom.Rectangle2D; +import java.util.Random; + +import javax.swing.JComponent; + +@SuppressWarnings("serial") +public class THComponent extends JComponent +{ + private int width; + private int height; + private int leftMargin = 10; + private int rightMargin = 10; + private int topMargin = 10; + private int bottomMargin = 10; + private int baseHeight = 10; + private double poleWidth = 10; + private TowerOfHanoi towers; + private int numberOfDiscs; + private double largestDiscSize; + private double smallestDiscSize = 20; + private double defaultReductionSize = 20.0; + private double defaultDiscHeight = 20.0; + private double[] discSizes; + + public THComponent(TowerOfHanoi aTowers) + { + towers = aTowers; + numberOfDiscs = towers.getNumberOfDiscs(); + discSizes = new double[numberOfDiscs]; + } + + public void paintComponent(Graphics g) + { + Graphics2D g2 = (Graphics2D) g; + + width = this.getWidth(); + height = this.getHeight(); + + int drawingWidth = width - (leftMargin + rightMargin); + int drawingHeight = height - (topMargin + bottomMargin); + + // draw the base; + + Rectangle2D.Double rect = new Rectangle2D.Double(leftMargin , height - (bottomMargin + baseHeight), drawingWidth, baseHeight); + g2.draw(rect); + + // draw poles; + + double halfDistance = ((double) drawingWidth / 6); + largestDiscSize = (halfDistance * 2) - poleWidth; + + for(int i = 1; i <= 5; i = i + 2) + { + rect.setRect((leftMargin + (halfDistance * i)) - (poleWidth / 2), topMargin, poleWidth, drawingHeight - baseHeight); + g2.draw(rect); + } + + // draw discs + + double totalReduction = (largestDiscSize - smallestDiscSize) / (numberOfDiscs - 1); + double actualReduction; + + if(totalReduction > defaultReductionSize) + { + actualReduction = defaultReductionSize; + } + else + { + actualReduction = totalReduction; + } + + double totalHeight = drawingHeight - baseHeight; + double tempDiscHeight = totalHeight / (numberOfDiscs + 1); + double actualDiscHeight; + + if(tempDiscHeight > defaultDiscHeight) + { + actualDiscHeight = defaultDiscHeight; + } + else + { + actualDiscHeight = tempDiscHeight; + } + + for(int i = 0; i < numberOfDiscs; i++) + { + discSizes[i] = largestDiscSize - (actualReduction * ((numberOfDiscs - 1) - i)); + } + + int[][] t = new int[3][]; + for(int i = 0; i < 3; i++) + { + t[i] = towers.getArrayOfDiscs(i); + } + + for(int i = 0; i <= 2; i++) + { + for(int j = 0; j < t[i].length; j++) + { + double x = (leftMargin + (halfDistance * (1 + (2 * i)))) - (discSizes[t[i][j]] / 2); + double y = ((topMargin + drawingHeight) - baseHeight) - (actualDiscHeight * (j + 1)); + rect.setRect(x,y,discSizes[t[i][j]],actualDiscHeight); + g2.setColor(Color.GREEN); + g2.fill(rect); + g2.setColor(Color.BLACK); + g2.draw(rect); + } + } + } +} diff --git a/labs/lab6_toh/THFrame.java b/labs/lab6_toh/THFrame.java new file mode 100644 index 0000000..0c672e2 --- /dev/null +++ b/labs/lab6_toh/THFrame.java @@ -0,0 +1,84 @@ +import javax.swing.JFrame; + +public class THFrame +{ + public static void main(String[] args) throws InterruptedException + { + int numDiscs = 4; + TowerOfHanoi towers = new TowerOfHanoi(numDiscs); + THComponent thc = new THComponent(towers); + + + JFrame frame = new JFrame(); + frame.setTitle("Tower of Hanoi"); + frame.setSize(500,500); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + frame.add(thc); + + frame.setVisible(true); + + Thread.sleep(5000); + + towers.moveTopDisc(0, 1); + thc.repaint(); + Thread.sleep(500); + + towers.moveTopDisc(0, 2); + thc.repaint(); + Thread.sleep(500); + + towers.moveTopDisc(1, 2); + thc.repaint(); + Thread.sleep(500); + + towers.moveTopDisc(0, 1); + thc.repaint(); + Thread.sleep(500); + + towers.moveTopDisc(2, 0); + thc.repaint(); + Thread.sleep(500); + + towers.moveTopDisc(2, 1); + thc.repaint(); + Thread.sleep(500); + + towers.moveTopDisc(0, 1); + thc.repaint(); + Thread.sleep(500); + + towers.moveTopDisc(0, 2); + thc.repaint(); + Thread.sleep(500); + + towers.moveTopDisc(1, 2); + thc.repaint(); + Thread.sleep(500); + + towers.moveTopDisc(1, 0); + thc.repaint(); + Thread.sleep(500); + + towers.moveTopDisc(2, 0); + thc.repaint(); + Thread.sleep(500); + + towers.moveTopDisc(1, 2); + thc.repaint(); + Thread.sleep(500); + + towers.moveTopDisc(0, 1); + thc.repaint(); + Thread.sleep(500); + + towers.moveTopDisc(0, 2); + thc.repaint(); + Thread.sleep(500); + + towers.moveTopDisc(1, 2); + thc.repaint(); + Thread.sleep(500); + + } +} diff --git a/labs/lab6_toh/TowerOfHanoi.java b/labs/lab6_toh/TowerOfHanoi.java new file mode 100644 index 0000000..51a394c --- /dev/null +++ b/labs/lab6_toh/TowerOfHanoi.java @@ -0,0 +1,164 @@ + +public class TowerOfHanoi +{ + // TO DO: Instance Variables + private HStack towers[] = new HStack[3]; + private int numDiscs; + /* Construct the Towers of Hanoi (3 towers) with aNumDisc + * on the first tower. Each tower can be identified by an + * integer number (0 for the first tower, 1 for the second + * tower, and 2 for the third tower). Each disc can be identified + * by an integer number starting from 0 (for the smallest disc) + * and (aNumDisc - 1) for the largest disc. + */ + public TowerOfHanoi(int aNumDiscs) + { + towers[0] = new HStack(); + towers[1] = new HStack(); + towers[2] = new HStack(); + for(int i = 1; i <= aNumDiscs; i++) + { + towers[0].push(aNumDiscs-i); + } + numDiscs = aNumDiscs; + } + + /* Returns an array of integer representing the order of + * discs on the tower (from bottom up). The bottom disc should + * be the first element in the array and the top disc should be + * the last element of the array. The size of the array MUST + * be the number of discs on the tower. For example, suppose + * the tower 0 contains the following discs 0,1,4,6,7,8 (from top + * to bottom). This method should return the array [8,7,6,4,1,0] + * (from first to last). + * @param tower the integer identify the tower number. + * @return an array of integer representing the order of discs. + */ + public int[] getArrayOfDiscs(int tower) + { + return towers[tower].toArray(); + } + + /* Gets the total number of discs in this Towers of Hanoi + * @return the total number of discs in this Towers of Hanoi + */ + public int getNumberOfDiscs() + { + // TO DO + return numDiscs; + } + + /* Gets the number of discs on a tower. + * @param tower the tower identifier (0, 1, or 2) + * @return the number of discs on the tower. + */ + public int getNumberOfDiscs(int tower) + { + return towers[tower].getLength(); + } + + /* Moves the top disc from fromTower to toTower. Note that + * this operation has to follow the rule of the Tower of Hanoi + * puzzle. First fromTower must have at least one disc and second + * the top disc of toTower must not be smaller than the top disc + * of the fromTower. + * @param fromTower the source tower + * @param toTower the destination tower + * @return ture if successfully move the top disc from + * fromTower to toTower. + */ + public boolean moveTopDisc(int fromTower, int toTower) + { + if(towers[fromTower].getLength() == 0) + { + return false; + } + if(towers[toTower].getLength() == 0) + { + towers[toTower].push(towers[fromTower].pop()); + return true; + } + else if(towers[fromTower].peek() < towers[toTower].peek()) + { + towers[toTower].push(towers[fromTower].pop()); + return true; + } + else + { + return false; + } + } + + public class HStack + { + private HNode head; + private int length; + public HStack() + { + length = 0; + } + public int peek() + { + return head.getData(); + } + public int pop() + { + int output = head.getData(); + head = head.getNext(); + length--; + return output; + } + public void push(int i) + { + head = new HNode(i,head); + length++; + } + public boolean hasNext() + { + return head != null; + } + public int getLength() + { + return length; + } + public String toString() + { + String output = ""; + for(HNode tmp = head; tmp != null; tmp=tmp.getNext()) + { + output += tmp.getData() + " "; + } + return output; + } + public int[] toArray() + { + int[] output = new int[length]; + int i = length-1; + for(HNode tmp = head; tmp != null; tmp = tmp.getNext()) + { + output[i] = tmp.getData(); + i--; + } + return output; + } + + } + public class HNode + { + private HNode next = null; + private int data; + public HNode(int d, HNode n) + { + data = d; + next = n; + } + public int getData() + { + return data; + } + public HNode getNext() + { + return next; + } + } +} diff --git a/labs/lab6_toh/TowersTester.java b/labs/lab6_toh/TowersTester.java new file mode 100644 index 0000000..c8aed2f --- /dev/null +++ b/labs/lab6_toh/TowersTester.java @@ -0,0 +1,185 @@ + +public class TowersTester +{ + public static void main(String[] args) + { + int numDiscs = 7; + int point = 0; + + TowerOfHanoi t = new TowerOfHanoi(numDiscs); + + // Testing the method getNumberOfDiscs() + + System.out.println("Construct the Table of Hanoi puzzle with 7 disks using the following statement:"); + System.out.println(" TowerOfHanoi t = new TowerOfHanoi(7);"); + System.out.println("All test will be base on results of manipulating TowerOfHanoi t.\n"); + + System.out.print("Testing the method getNumberOfDiscs(): "); + + if(t.getNumberOfDiscs() != numDiscs) + { + System.out.println("FAIL"); + System.out.println("The method getNumberOfDiscs() should return " + numDiscs + "."); + System.out.println("But your method getNumberOfDiscs() returns " + t.getNumberOfDiscs() + ".\n"); + } + else + { + point++; + System.out.println("PASS"); + } + System.out.println("Your current point is " + point + ".\n"); + + // Testing initial puzzle + + System.out.println("Testing initial puzzle"); + + int[][] tower0 = {{6,5,4,3,2,1,0},{},{}}; + + int[] nd0 = {7,0,0}; + + if(testPuzzle(t, nd0, tower0)) + { + point++; + } + System.out.println("Your current point is " + point + ".\n"); + + System.out.println("Move top disc from tower 0 to tower 1"); + if(testMTD(t.moveTopDisc(0, 1), true, 0, 1)) + { + point++; + } + System.out.println("Your current point is " + point + ".\n"); + System.out.println("Testing the current puzzle"); + int[][] tower1 = {{6,5,4,3,2,1},{0},{}}; + int[] nd1 = {6,1,0}; + if(testPuzzle(t, nd1, tower1)) + { + point++; + } + System.out.println("Your current point is " + point + ".\n"); + + // Move + + System.out.println("Move top disc from tower 0 to tower 2"); + if(testMTD(t.moveTopDisc(0, 2), true, 0, 2)) + { + point++; + } + System.out.println("Your current point is " + point + ".\n"); + System.out.println("Testing the current puzzle"); + int[][] tower2 = {{6,5,4,3,2},{0},{1}}; + int[] nd2 = {5,1,1}; + if(testPuzzle(t, nd2, tower2)) + { + point++; + } + System.out.println("Your current point is " + point + ".\n"); + + // Move + + System.out.println("Move top disc from tower 1 to tower 2"); + if(testMTD(t.moveTopDisc(1, 2), true, 1, 2)) + { + point++; + } + System.out.println("Your current point is " + point + ".\n"); + System.out.println("Testing the current puzzle"); + int[][] tower3 = {{6,5,4,3,2},{},{1,0}}; + int[] nd3 = {5,0,2}; + if(testPuzzle(t, nd3, tower3)) + { + point++; + } + System.out.println("Your current point is " + point + ".\n"); + + // Move (false) + + System.out.println("Move top disc from tower 0 to tower 2 (MUST RETURN FALSE)"); + if(testMTD(t.moveTopDisc(0, 2), false, 0, 2)) + { + point++; + } + System.out.println("Your current point is " + point + ".\n"); + System.out.println("Testing the current puzzle (MUST REMAIN UNCHANGED FROM PREVIOUS TEST)"); + if(testPuzzle(t, nd3, tower3)) + { + point++; + } + System.out.println("Your current point is " + point + ".\n"); + + } + + private static boolean testMTD(boolean aResult, boolean eResult, int fromTower, int toTower) + { + System.out.print("Test return value of the method moveTopDisc(): "); + if(aResult != eResult) + { + System.out.println("FAIL"); + System.out.println("t.moveTopDisc(" + fromTower + "," + toTower + ") should return " + eResult + "."); + System.out.println("But your method moveTopDisc(" + fromTower + "," + toTower + ") return " + aResult + "."); + } + else + { + System.out.println("PASS"); + } + + return aResult == eResult; + } + + private static boolean testPuzzle(TowerOfHanoi t, int[] numDiscs, int[][] tower) + { + boolean result = true; + + for(int i = 0; i < 3; i++) + { + System.out.print("Testing the method getNumberOfDiscs(" + i + "): "); + + if(t.getNumberOfDiscs(i) != numDiscs[i]) + { + System.out.println("FAIL"); + System.out.println("The method getNumberOfDiscs(" + i + ") should return " + numDiscs[i] + "."); + System.out.println("But your method getNumberOfDiscs(" + i + ") returns " + t.getNumberOfDiscs(i) + ".\n"); + result = false; + } + else + { + System.out.println("PASS"); + } + + // Testing the method getArrayOfDiscs(0) + + System.out.print("Testing the method getArrayOfDiscs(" + i + "): "); + + if(!arrayToString(tower[i]).equals(arrayToString(t.getArrayOfDiscs(i)))) + { + System.out.println("FAIL"); + System.out.println("The method getArrayOfDiscs(" + i + ") should return the array " + arrayToString(tower[i]) + "."); + System.out.println("But your method getArrayOfDiscs(" + i + ") returns the array " + arrayToString(t.getArrayOfDiscs(i)) + ".\n"); + result = false; + } + else + { + System.out.println("PASS"); + } + } + + return result; + } + + private static String arrayToString(int[] array) + { + String result = "["; + + if(array.length > 0) + { + for(int i = 0; i < array.length - 1; i++) + { + result = result + array[i] + ","; + } + + result = result + array[array.length - 1]; + } + + return result + "]"; + } +} diff --git a/labs/lab6_toh/lab06_towers_of_hanoi.pdf b/labs/lab6_toh/lab06_towers_of_hanoi.pdf Binary files differnew file mode 100644 index 0000000..6ae3eb0 --- /dev/null +++ b/labs/lab6_toh/lab06_towers_of_hanoi.pdf |
