Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.1 kB
4
Indexable
Never
package problem2;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;

public class Problem2 {
    Stack<Integer> towerA = new Stack<>();
    Stack<Integer> towerB = new Stack<>();
    Stack<Integer> towerC = new Stack<>();

    int numberDisks = 0;

    Scanner scanner = new Scanner(System.in);
    public void start(){
        populateTowerA();
        showMoves();
        //solve(numberDisks, towerA, towerB, towerC);
    }

    public void populateTowerA(){
        System.out.print("How many disks do you want: ");
        numberDisks = scanner.nextInt();
        Random random = new Random();

        for (int i = 0; i < numberDisks; i++) {
            int newRandom = random.nextInt(100);
            if (!towerA.contains(newRandom)){
                towerA.push(newRandom);
            }
        }

        sortStack(towerA);

        System.out.println("TOWER A: " + towerA);
    }

    public static void sortStack(Stack<Integer> stack) {
        Stack<Integer> tempStack = new Stack<>();

        while (!stack.isEmpty()) {
            int temp = stack.pop();

            // Move elements from tempStack to stack until we find the correct position for temp
            while (!tempStack.isEmpty() && tempStack.peek() > temp) {
                stack.push(tempStack.pop());
            }

            // Push temp onto tempStack in the correct position
            tempStack.push(temp);
        }

        // Move elements from tempStack back to stack to maintain the desired order
        while (!tempStack.isEmpty()) {
            stack.push(tempStack.pop());
        }
    }

    public void solve(int numberDisks, Stack<Integer> A, Stack<Integer> B, Stack<Integer> C){
        if (numberDisks == 0) {
            return;
        }
        solve(numberDisks - 1, A, C, B);
        int topDisk = A.pop();
        B.push(topDisk);
        System.out.println("Move disk " + topDisk + " from rod " + A + " to rod " + B);
        solve(numberDisks - 1, C, B, A);
    }

    public void showMoves(){
        ArrayList<Integer> copyA = copyStack(towerA);
        ArrayList<Integer> copyB = copyStack(towerB);
        ArrayList<Integer> copyC = copyStack(towerC);

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < numberDisks; i++){

            sb.append(copyA.get(i) == -1 ? "|" : fromA + "\t").append(fromB == -1 ? "|" : fromB + "\t").append(fromC == -1 ? "|" : fromC + "\t");
            System.out.println(sb.toString());

        }
    }

    public static ArrayList<Integer> copyStack(Stack<Integer> originalStack) {
        ArrayList<Integer> copiedStack = new ArrayList<>();
        Stack<Integer> tempStack = new Stack<>();

        for (Integer element : originalStack) {
            tempStack.push(element);
        }

        while (!tempStack.isEmpty()) {
            copiedStack.add(tempStack.pop());
        }

        return copiedStack;
    }
}
Leave a Comment