3

我的兄弟向我提出了一个挑战,通过制作一个只在 main 方法中运行而不是使用其他类的游戏来帮助我的学习,以确保我仍然记得我的旧东西。根据我的猫捉老鼠游戏,玩家是寻找奶酪的老鼠,同时避开所有的猫。当你进入一个空的“房间”(牢房)时,游戏应该会给你一个线索,告诉你你离奶酪有多远。现在游戏运行了,但我的线索越来越高,以至于它超过了迷宫中的房间数量。我很困惑问题出在哪里。

这是代码

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
4

2 回答 2

5

你没有clue在大的 do-while 循环中重置为零所以对我来说,线索除了更高之外别无他法似乎是合乎逻辑的,因为它不断将新计数添加到自身(clue = clue + clueCount

于 2013-07-24T19:20:01.280 回答
5

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

于 2013-07-24T19:21:17.297 回答