Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TowerOfHanoi
- {
- int[] ace;
- int[] dos;
- int[] thrice;
- int first;
- int second;
- int third;
- /* 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)
- {
- ace = new int[aNumDiscs];
- for(int i = 0; i < aNumDiscs; i++){
- ace[i] = aNumDiscs - 1 - i;
- }
- dos = new int[aNumDiscs];
- dos[0] = aNumDiscs;
- thrice = new int[aNumDiscs];
- thrice[0] = aNumDiscs;
- first = aNumDiscs;
- second = 0;
- third = 0;
- }
- /* 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)
- {
- int[] tempTower;
- if(tower == 0){
- tempTower = new int[first];
- for(int i = 0; i < first; i++){
- tempTower[i] = ace[i];
- }
- return tempTower;
- }
- if(tower == 1){
- tempTower = new int[second];
- for(int i = 0; i < second; i++){
- tempTower[i] = dos[i];
- }
- return tempTower;
- }
- if(tower == 2){
- tempTower = new int[third];
- for(int i = 0; i < third; i++){
- tempTower[i] = thrice[i];
- }
- return tempTower;
- }
- return ace;
- }
- public boolean moveTopDisc(int fromTower, int toTower)
- {
- if((fromTower == 0 && first == 0)||(fromTower == 1 && second == 0)
- || (fromTower == 2 && third == 0)){
- return false;
- }
- if(fromTower == 0){
- if(toTower == 1){
- if(second != 0&&ace[first-1]>dos[second-1]){
- return false;
- }
- else{
- dos[second]=ace[first-1];
- ace[first-1] = 0;
- first--;
- second++;
- return true;
- }
- }
- if(toTower == 2){
- if(third != 0&&ace[first-1] > thrice[third-1]){
- return false;
- }
- else{
- thrice[third] = ace[first-1];
- ace[first-1] = 0;
- first--;
- third++;
- return true;
- }
- }
- }
- if(fromTower == 1){
- if(toTower == 0){
- if(first != 0&&dos[second-1]>ace[first-1]){
- return false;
- }
- else{
- ace[first]=dos[second-1];
- dos[second-1] = 0;
- second--;
- first++;
- return true;
- }
- }
- if(toTower == 2){
- if(third!= 0&&dos[second-1] > thrice[third-1]){
- return false;
- }
- else{
- thrice[third] = dos[second-1];
- dos[second-1] = 0;
- second--;
- third++;
- return true;
- }
- }
- }
- if(fromTower == 2){
- if(toTower == 0){
- if(first !=0 && ace[first-1]>dos[second-1]){
- return false;
- }
- else{
- ace[first]=thrice[third-1];
- thrice[third-1] = 0;
- third--;
- first++;
- return true;
- }
- }
- if(toTower == 1){
- if(third !=0&&thrice[third-1] > dos[second-1]){
- return false;
- }
- else{
- dos[second] = thrice[third-1];
- thrice[third-1] = 0;
- third--;
- second++;
- return true;
- }
- }
- }
- return false;
- }
- /* 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()
- {
- return first+second+third;
- }
- /* 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)
- {
- if(tower == 0){
- return first;
- }
- if(tower == 1){
- return second;
- }
- if(tower == 2){
- return third;
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement