我的兄弟给了我一个挑战,通过制作只以主要方法运行而不是使用其他类来确保我仍然记得我的旧东西的游戏来帮助我的学习。以此为基础,我进行了猫和老鼠游戏,玩家是老鼠在躲避所有猫咪的同时寻找奶酪。当您输入一个空的“房间”(单元格)时,游戏应该会为您提供一条线索,让您了解距奶酪有多远。现在游戏开始运行,但是我的线索越来越多,直到迷宫中的房间数量超过了它。我对问题出在哪里感到困惑。

这是代码

import java.util.Scanner;
import java.util.Random;

public class CatAndMouse
{
    public static final int MAX = 10;

    public static void main(String args[ ])
    {
        Scanner mouse = new Scanner(System.in);
        Random placement = new Random();

        boolean check = true, gameOver = false, win = false, lose = false;

        final int row = MAX;
        final int col = MAX;
        final int page = MAX;

        int cheeseX, cheeseY, cheeseZ;
        int cheese = 1;

        int catX, catY, catZ;
        int cat = 2;

        int mouseRow;
        int mouseCol;
        int mousePage;
        int mouseMove;

        int empty = 0;

        int clue = 0;
        int clueCount = 0;

        int winQuotes;
        int loseQuotes;

        int [][][]maze = new int [row][col][page];

        for(int i = 0; i < MAX; i++)
        {
            for(int j = 0; j < MAX; j++)
            {
                for(int k = 0; k < MAX; k++)
                {
                    maze[i][j][k] = empty;
                }//page
            }//col
        }//row

        cheeseX = placement.nextInt(row);
        cheeseY = placement.nextInt(col);
        cheeseZ = placement.nextInt(page);


        maze[cheeseX][cheeseY][cheeseZ] = cheese;

        for (int i = 0; i < 500; i++)
        {
            catX = placement.nextInt(row);
            catY = placement.nextInt(col);
            catZ = placement.nextInt(page);

            maze[catX][catY][catZ] = cat;

            if ((maze[catX][catY][catZ]) == (maze[cheeseX][cheeseY][cheeseZ]))
            {
                catX = placement.nextInt(row);
                catY = placement.nextInt(col);
                catZ = placement.nextInt(page);

                maze[catX][catY][catZ] = cat;
            }//if place with cheese
        }//cat placement loop

        System.out.println("Hello there, my name is Q, do you like it? it's short for Q. So you're probably asking yourself \"why am I now a mouse?\"");
        System.out.println("The answer is simple, I was bored and you humans are so much fun to play with, but don't worry I can change you back.");
        System.out.println("All you have to do is win my little game and you'll be back to your old self again, loose and...well just don't lose.");
        System.out.println("In this maze there is a piece of cheese, find it and you win. But be careful now, I added a \'few\' cats to hunt you.");
        System.out.println("Can't make this too easy now can we? But don't worry, you'll be given clues if you're close to the cheese or not");
        System.out.println("The maze itself is 10*10*10 and to move through it enter an integer between 0-9.");
        System.out.println("Now then, let the game begin.");

        System.out.println();

        do
        {
            System.out.print("Enter row: ");
                mouseRow = mouse.nextInt();

            if((mouseRow < 0) || (mouseRow > 9))
            {
                while (check == true)
                {
                    System.out.print("I said, it needs to be an integer between 0-9. Try again: ");
                        mouseRow = mouse.nextInt();

                    if((mouseRow >= 0) && (mouseRow <= 9))
                        check = false;
                }//while closer
            }//row check

            check = true;

            System.out.print("Enter column: ");
                mouseCol = mouse.nextInt();

            if((mouseCol < 0) || (mouseCol > 9))
            {
                while (check == true)
                {
                    System.out.print("I said, it needs to be an integer between 0-9. Try again: ");
                        mouseCol = mouse.nextInt();

                    if((mouseCol >= 0) && (mouseCol <= 9))
                        check = false;
                }//while closer
            }//column check

            check = true;

            System.out.print("Enter page: ");
                mousePage = mouse.nextInt();

                        if((mousePage < 0) || (mousePage > 9))
            {
                while (check == true)
                {
                    System.out.print("I said, it needs to be an integer between 0-9. Try again: ");
                        mousePage = mouse.nextInt();

                    if((mousePage >= 0) && (mousePage <= 9))
                        check = false;
                }//while closer
            }//page check

            check = true;

            mouseMove = maze[mouseRow][mouseCol][mousePage];

            System.out.println();


            /*================[Win/Lose]===============*/

            if (mouseMove == 2)
            {
                gameOver = true;
                lose = true;
            }//loser

            if (mouseMove == 1)
            {
                gameOver = true;
                win = true;
            }//winner

            /*===============[Win/Lose]===============*/


            /*=================[Clue]=================*/

            if(mouseRow == cheeseX)
            {
                System.out.println("In same row as cheese!");

            }//if same row

            else if (mouseRow > cheeseX)
            {
                for(int i = cheeseX; i <= mouseRow; i++)
                {
                    clueCount++;
                }//for loop closer
            }//if mouse is larger

            else
            {
                for(int i = mouseRow; i <= cheeseX; i++)
                {
                    clueCount++;
                }//for loop closer
            }//else cheese is larger

            clue = clue + clueCount;

            if(mouseCol == cheeseY)
            {
                System.out.println("In same column as cheese!");

            }//if same colum

            if (mouseCol > cheeseY)
            {
                for(int i = cheeseY; i <= mouseCol; i++)
                {
                    clueCount++;
                }//for loop closer
            }//if mouse is larger

            else
            {
            for(int i = mouseCol; i <= cheeseY; i++)
                {
                    clueCount++;
                }//for loop closer
            }//else cheese is larger

            clue = clue + clueCount;

            if(mousePage == cheeseZ)
            {
                System.out.println("In same page as cheese!");

            }//if same page

            if (mousePage > cheeseZ)
            {
                for(int i = cheeseZ; i <= mousePage; i++)
                {
                    clueCount++;
                }//for loop closer
            }//if mouse is larger

            else
            {
                for(int i = mousePage; i <= cheeseZ; i++)
                    {
                        clueCount++;
                    }//for loop closer
            }//else cheese is larger

            clue = clue + clueCount;

            System.out.println("You are " + clue + " cells away from the cheese.");

            System.out.println();
            /*=================[Clue]=================*/


        }while (gameOver == false);

        if (win == true)
        {
            winQuotes = (int)(3 * Math.random()) + 1;

            switch (winQuotes)
            {
                case 1:
                    System.out.println("You found the cheese! Now it's time to send you back, but don't worry. I'm sure we'll meet again soon.");
                    break;

                case 2:
                    System.out.println("An excellent job, maybe you were meant to be a mouse all long. What, change you back? Oh fine.");
                    break;

                default:
                    System.out.println("Congradulation, I don't think Captian Picard couldn't have done it better. Maybe I should pay him a visit.");
                    break;
            }//win switch
        }//if you won

        if (lose == true)
        {
            loseQuotes = (int)(3 * Math.random()) + 1;

            switch(loseQuotes)
            {
                case 1:
                    System.out.println("Well at least you fed a hungry cat right? Star Fleet would be so proud to have you on one of their ships.");
                    break;

                case 2:
                    System.out.println("Oh come on, don't tell me you wore a red shirt before I brought you here.");
                    break;

                 default:
                    System.out.println("Maybe I should have brought Captian Janeway here instead, I still owe her for that punch to my face.");
                    break;

             }//lose switch
         }//if you lose
     }//main closer
 } //class closer

最佳答案

您永远不会在clue循环中将do/while变量重新初始化为0。因此,每次运行循环时,您都无需添加clue到当前线索值,而是继续添加到它(clue = clue + clueCount)。

09-25 21:38