2013-07-24 3 views
3

Мой брат предложил мне чат, чтобы помочь мне в учебе, создав игру, которая работает только в основном методе, а не использует другие классы, чтобы я все еще помнил свои старые вещи. Основы, что я пошел с игрой с кошкой и мышей, где игрок - это мышь, ища сыр, избегая всех кошек. Когда вы входите в пустую «комнату» (ячейку), игра должна дать вам представление о том, как далеко вы находитесь от сыра. Теперь игра бежит, но мои подсказки только продолжают идти все выше и выше до такой степени, что она превышает количество комнат в лабиринте. Я обрушился на проблему.Мои подсказки в моей игре отключены (java)

Вот код

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 
+15

«Мой брат предложил мне помолвку, помогающую учиться, делая игру, которая работает только по основному методу, а не по другим классам, чтобы я все еще помнил свои старые вещи». И почему вы не можете разделить вещи на вспомогательные функции внутри одного класса? Наличие одной гигантской функции - плохая практика. –

+0

Интересно, зайдите на этот сайт: [find my problem.com] (http://sscce.org/). – Azad

+4

Gawd, извините, я бы с удовольствием помог, но это всего лишь тонна кода :( – null

ответ

5

Вы никогда не переустанавливаете переменную clue в 0 в вашей петле do/while. Таким образом, каждый раз, когда вы запускаете цикл, вместо того, чтобы устанавливать clue в текущее значение подсказки, вы продолжаете добавлять к нему (clue = clue + clueCount).

5

Вы не сброс clue к нулю в большом сделай в то время как петля Так что, кажется логичной, что ключи не могут ничего, кроме идти выше делать, потому что это продолжайте добавлять новый счет к себе (clue = clue + clueCount)

Смежные вопросы