java - 为什么程序检测不到我赢了(汉诺塔)?

标签 java bluej

我正在为学校做作业。这是汉诺塔任务。 (我还没有添加较大磁盘覆盖较小磁盘的代码)。当我将 tower3 设置为 4, 3, 2, 1 时,它说我赢了,但是当我在玩游戏时这样做时,什么也没有发生。请帮忙!该截止日期为美国东部时间周一晚上 8:30。

import java.util.Scanner;

/**
 * Simulates a tower that can hold disks.
 * @author S. Camilleri
 * @author <Hasan Zafar>
 */
public class Challenge {
    public static void main(String[] args) { 
        Scanner input = new Scanner(System.in);

        // This array holds the disks. A 0 represents no disk.
        int[] tower = {4,3,2,1};
        int[] tower2 = {0,0,0,0};
        int[] tower3 = {0,0,0,0};

        // This index represents the first available empty spot for a disk.
        int index = 0;

        int towerCounter = 0;
        int length = tower.length;
        int length2 = tower2.length;
        int length3 = tower3.length;
        int diskChoice = 1;
        int i;
        int held = 0;
        int placeChoice; 

        boolean playing = true;    
        while (playing)
        {
            //Check if Won
            if (tower3[0] == 4 && tower3[1] == 3 && tower3[2] == 2 && tower[3] == 1) {
                System.out.println("Congratulations! You win!");
                playing = false;
                break;

            }

            /********************
             * Display the towers
             ********************/
            System.out.println();
            //Tower1
            System.out.print("{ ");

            for (int x=0; x<length; x++)
            {
                System.out.print(tower[x]);
            }

            System.out.println();
            //Tower2
            System.out.print("{ ");
            towerCounter = 0;

            for (int x=0; x<length2; x++)
            {
                System.out.print(tower2[x]);
            }

            System.out.println();
            //Tower3
            System.out.print("{ ");
            towerCounter = 0;

            for (int x=0; x<length3; x++)
            {
                System.out.print(tower3[x]);
            }



            /********************
             * Select the highest disk from the tower
             ********************/

            System.out.println();
            System.out.println("Pick a tower (The disk highest on that tower will be chosen)");
            diskChoice = input.nextInt();

            // If user uses the first tower
            if (diskChoice == 1) {
                i = 3;
                while (tower[i] == 0) {
                    i--;

                }
                held = tower[i];
                tower[i] = 0;
            } else if (diskChoice == 2) { // If user uses the second tower
                i = 3;
                while (tower2[i] == 0) {
                    i--;

                }
                held = tower2[i];
                tower2[i] = 0;
            } else if (diskChoice == 3) { // If user uses the third tower
                i = 3;
                while (tower3[i] == 0) {
                    i--;

                }
                held = tower3[i];
                tower3[i] = 0;
            } 

            /********************
             * Place the disk
             ********************/

            System.out.println("Where would you like to place" + " " + held + "?");
            placeChoice = input.nextInt();

            if (placeChoice == 1) {
                i = 3;
                if (tower[3] == 0){
                    while (tower[i] == 0) {

                        i--;
                        if (i == 0) {
                            break;
                        }

                    }
                }

                if (tower[i] == 0) {
                    tower[i] = held;
                } else if (tower[i] != 0) {
                    tower[i+1] = held;
                }
            } else if (placeChoice == 2) {
                i = 3;

                if (tower2[3] == 0){
                    while (tower2[i] == 0) {

                        i--;
                        if (i == 0) {
                            break;
                        }

                    }
                }
                if (tower2[i] == 0) {
                    tower2[i] = held;
                } else if (tower2[i] != 0) {
                    tower2[i+1] = held;
                }
            } else if (placeChoice == 3) {
                i = 3;
                if (tower3[3] == 0){
                    while (tower3[i] == 0) {

                        i--;
                        if (i == 0) {
                            break;
                        }

                    }
                }
                if (tower3[i] == 0) {
                    tower3[i] = held;
                } else if (tower3[i] != 0) {
                    tower3[i+1] = held;
                }
            }



        }
    } 
}

如果我手动将 tower3 设置为 4321,则表示我赢了。 如果我在游戏中将 tower3 设置为 4321,它就会继续播放。

最佳答案

我明白了。我忘了在 win 语句处写上 3。

关于java - 为什么程序检测不到我赢了(汉诺塔)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55896465/

相关文章:

java - JFreeChart确定网格的尺寸

java - jdbc与mysql的连接

java - 将 OpenSSL 与 Java Applet 结合使用

java - [严重]通过Java调用selenium的driver.get()方法时出现: Timed out receiving message from renderer: 298. 674

java - 文件索引程序不工作

java - 有什么简单的方法来处理这个java游戏中的ConcurrentModificationException吗?

java - 程序进行覆盖

java - 你能将枚举类传递给方法吗(java)

java - 如何初始化ArrayList

c# - 从最低到最高显示一系列数字