【问题标题】:Why won't the program detect that I won (Towers Of Hanoi)?为什么程序不会检测到我赢了(河内塔)?
【发布时间】:2019-04-29 03:05:39
【问题描述】:

我正在为学校做作业。这是河内任务的塔。 (我还没有添加大磁盘而不是小磁盘代码)。当我将 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,它就会继续播放。

【问题讨论】:

  • 我没有看到代码的算法问题(虽然,代码本身使用了大量的复制/粘贴,因此难以阅读 + 它没有任何输入清理,因此游戏可以很容易地无限进行)。我建议您阅读程序的中间输出 - 我想您会发现问题所在。
  • 仔细查看您的获胜测试:它应该只查看tower3 条目,但它不是...
  • tower 变量最好命名为 tower1,以便更容易找到这个错字并保持一致性。
  • 或者,为了使变量在视觉上更加明显,您还可以将它们命名为 leftmiddleright。这样你在大声阅读代码时就不必说那么多数字了。

标签: java bluej


【解决方案1】:

我想通了。我忘了在 win 语句中写 3。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2022-11-02
    • 2014-02-18
    相关资源
    最近更新 更多