Answer to Question #252463 in Java | JSP | JSF for masedi moncho

Question #252463
Create a program for water sort puzzle in java.
1
Expert's answer
2021-10-17T10:25:28-0400
import java.util.Random;
import java.util.Scanner;

public class Main {
    final static Scanner IN = new Scanner(System.in);
    static String[] colors = {"Red", "Green", "Blue", "Yellow"};
    static String[][] bottles;

    static void fillBottles() {
        Random random = new Random();
        int[] colorAmounts = new int[colors.length];
        int currentColor;
        int currentBottle;
        boolean colorAdded;
        boolean bottlesFilled;
        for (int i = 0; i < colorAmounts.length; i++) {
            colorAmounts[i] = bottles[0].length;
        }
        do {
            bottlesFilled = true;
            currentColor = random.nextInt(colorAmounts.length);
            if (colorAmounts[currentColor] > 0) {
                colorAdded = false;
                while (!colorAdded) {
                    currentBottle = random.nextInt(bottles.length - 1);
                    for (int i = 0; i < bottles[currentBottle].length; i++) {
                        if (bottles[currentBottle][i] == null) {
                            bottles[currentBottle][i] = colors[currentColor];
                            colorAmounts[currentColor]--;
                            colorAdded = true;
                            break;
                        }
                    }
                }
            }
            for (int colorAmount : colorAmounts) {
                if (colorAmount > 0) {
                    bottlesFilled = false;
                    break;
                }
            }
        }
        while (!bottlesFilled);
    }

    static void printBottles() {
        StringBuilder fullOutput = new StringBuilder();
        StringBuilder bottle;
        for (int i = 0; i < bottles.length; i++) {
            bottle = new StringBuilder();

            bottle.append(String.format("%-3s | ", i));
            for (int j = 0; j < bottles[i].length; j++) {
                bottle.append(String.format("%-10s", (bottles[i][j] == null ? "<Empty>" : bottles[i][j])));
            }

            bottle.append('\n');
            for (int j = 0; j < bottle.length(); j++) {
                if (j < 4) {
                    fullOutput.append(" ");
                } else {
                    fullOutput.append("-");
                }
            }
            fullOutput.append('\n');
            fullOutput.append(bottle);
            for (int j = 0; j < bottle.length(); j++) {
                if (j < 4) {
                    fullOutput.append(" ");
                } else {
                    fullOutput.append("-");
                }
            }
            fullOutput.append('\n');
        }
        System.out.println(fullOutput);
    }

    static void readInput() {
        System.out.println("Select bottle index \"From\" and bottle index \"To\" (-1 to Exit)");
        int from = IN.nextInt();
        if (from == -1) {
            System.exit(0);
        }
        int to = IN.nextInt();
        int fromSection = -1;
        int toSection = -1;
        if (from >= 0 && from < bottles.length &&
                to >= 0 && to < bottles.length
                && from != to) {
            for (int i = bottles[from].length - 1; i >= 0; i--) {
                if (bottles[from][i] != null) {
                    fromSection = i;
                    break;
                }
            }
            if (fromSection >= 0) {
                for (int i = 0; i < bottles[to].length; i++) {
                    if (bottles[to][i] == null) {
                        toSection = i;
                        break;
                    }
                }
                if (toSection >= 0) {
                    bottles[to][toSection] = bottles[from][fromSection];
                    bottles[from][fromSection] = null;
                }
            }
        }
    }

    static boolean checkWin() {
        for (int i = 0; i < bottles.length; i++) {
            for (int j = 0; j < bottles[i].length - 1; j++) {
                if (bottles[i][j] != null && !bottles[i][j].equals(bottles[i][j + 1])) {
                    return false;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        bottles = new String[colors.length + 1][4];
        fillBottles();
        printBottles();
        System.out.println("GO!!!");
        while (!checkWin()) {
            readInput();
            printBottles();
        }
        System.out.println("You win!");
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS