简体   繁体   English

为什么程序不能检测到我赢了(河内的塔)?

[英]Why won't the program detect that I won (Towers Of Hanoi)?

I am doing an assignment for school. 我正在上学。 It is a towers of hanoi assignment. 这是一个河内任务的塔。 (I didn't add the larger-disk-over-smaller-disk code yet). (我没有添加更大磁盘 - 更小磁盘的代码)。 When I set tower3 as 4, 3, 2, 1, it says I won, but when I do it while playing the game, nothing happens. 当我将tower3设置为4,3,2,1时,它表示我赢了,但是当我在玩游戏时这样做时,没有任何反应。 Please help! 请帮忙! This is due Monday 8:30pm EST. 这是美国东部时间周一晚上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;
                }
            }



        }
    } 
}

If I set tower3 as 4321 manually, it says I won. 如果我手动将tower3设置为4321,则表示我赢了。 If I set tower3 as 4321 in-game, it just keeps playing. 如果我在比赛中将tower3设置为4321,它就会继续玩。

I figured it out. 我想到了。 I forgot to write the 3 at the of win statement. 我忘了写win语句中的3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM