I am coding a simple mouse and cat game. But I always got a debug error when i try to use setLocation.
我正在编写一个简单的鼠标和猫游戏。但是当我尝试使用setLocation时,我总是遇到调试错误。
Can you tell me how can i fix it? i dont think there is anything wrong.
你能告诉我怎么解决它?我不认为有什么不对。
import java.awt.Point;
import java.util.Random;
public abstract class Animal {
Point location_;
String name_;
Random rng_;
public Animal() {
}
public Animal( String name, Random rng ){
name_=name;
rng_=rng;
}
abstract void move();
public String getName(){
return name_;
}
public Point getLocation(){
return location_;
}
public void setStartLocation(){
int x = rng_.nextInt(4);
int y = rng_.nextInt(4);
// error
location_.setLocation(x, y);
}
}
another file:
import java.util.Random;
public class Cat extends Animal {
public Cat(String name, Random rng){
super(name,rng);
}
void move() {
int i = rng_.nextInt(3);
int x = (int) location_.getX();
int y = (int) location_.getY();
if(i==0 && y<4){
int newY = y++;
location_.move(x,newY);
}
else if (i==1 && y>0){
int newY = y--;
location_.move(x,newY);
}
else if (i==2 && x>0){
int newX = x--;
location_.move(newX,y);
}
else if (i==3 && x<4){
int newX = x++;
location_.move(newX,y);
}
else move();
}
}
another file:
import java.util.Random;
public class Mouse extends Animal {
String mobility;
public Mouse(String name, Random rng){
super(name,rng);
}
void move() {
int i = rng_.nextInt(3);
int x = (int) location_.getX();
int y = (int) location_.getY();
if(i==0 ){
int newY = y++;
location_.move(x,newY);
}
else if (i==1){
int newY = y--;
location_.move(x,newY);
}
else if (i==2){
int newX = x--;
location_.move(newX,y);
}
else if (i==3){
int newX = x++;
location_.move(newX,y);
}
}
public String checkMoblity(){
int x = (int) location_.getX();
int y = (int) location_.getY();
if(x==-1 || x==5){
if(y==0 || y==2 || y==4){
return "drowned";
}
else return "escaped";
}
else if(y==-1 || y==5){
if(x==0 || x==2 || x==4){
return "drowned";
}
else return "escaped";
}
else return null;
}
}
another file:
import java.util.Random;
public class Chase {
public void playGame(){
Random rand = new Random();
//error
Cat cat = new Cat("Tom", rand);
Mouse mouse = new Mouse("Jerry", rand);
do{
cat.setStartLocation();
mouse.setStartLocation();
}while(cat.getLocation()== mouse.getLocation());
String status = Status(cat,mouse);
boolean end = end(cat, mouse);
do{
mouse.move();
if(end==false){
cat.move();
}
}while(end==false);
if (status == "drowned"){
System.out.println(mouse.getName() + " drowned!");
}
else if (status == "escaped"){
System.out.println(mouse.getName() + " escaped!");
}
else if (status == "snack"){
System.out.println(cat.getName() + " got the snack!");
}
}
public String Status(Cat cat, Mouse mouse){
if(mouse.checkMoblity()== "drowned"){
return "drowned";
}
else if(mouse.checkMoblity() == "escaped"){
return "escaped";
}
else if(mouse.getLocation()==cat.getLocation()){
return "snack";
}
else return null;
}
public boolean end(Cat cat, Mouse mouse){
if(mouse.checkMoblity()== "drowned"){
return true;
}
else if(mouse.checkMoblity() == "escaped"){
return true;
}
else if(mouse.getLocation()==cat.getLocation()){
return true;
}
else return false;
}
public static void main(String[] args){
Chase chase = new Chase();
//error
chase.playGame();
}
}
1 个解决方案
#1
1
You should instantiate an object before using it :
您应该在使用之前实例化对象:
public void setStartLocation(){
int x = rng_.nextInt(4);
int y = rng_.nextInt(4);
location_ = new Point(x, y);
}
The setter method setLocation(int, int)
cannot be used if your object Point location_
is not instantiated.
如果未实例化对象Point location_,则无法使用setter方法setLocation(int,int)。
#1
1
You should instantiate an object before using it :
您应该在使用之前实例化对象:
public void setStartLocation(){
int x = rng_.nextInt(4);
int y = rng_.nextInt(4);
location_ = new Point(x, y);
}
The setter method setLocation(int, int)
cannot be used if your object Point location_
is not instantiated.
如果未实例化对象Point location_,则无法使用setter方法setLocation(int,int)。