for循环在简单的java汽车游戏中

时间:2016-02-02 20:38:05

标签: java for-loop

这是一项任务,因此规定了很多结构。我们有一个包含两个类的包,一个Car和一个City。在 City main方法中,cars已设置为以9,0相互碰撞。我们必须进行编码,以便程序检测到汽车崩溃的时间。我正在查看 City 中的嵌套for循环,并且无法弄清楚如何使其工作。

汽车类:

public class Car {
    private int x ,y;
    private int facing; 
    /*
     * where 0 = north, 1 = east, 2 = south, 3 = west.
     */
    private int distance; 

    public Car(int x, int y, int facing){
        //"this" only has to be used because 
        //the variable names are identical
        //"this" indicates that the variable
        //declared at the top is being referenced.
        //the x on the right-hand side is the local
        //variable in the method
        this.x = x;
        this.y = y;
        this.facing = facing;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getFacing() {
        return facing;
    }

    public void setFacing(int facing) {
        this.facing = facing;
    }

    public int getDistance() {
        return distance;
    }

    public void setDistance(int distance) {
        this.distance = distance;
    }

    public void turnLeft() {
        if (facing == 0) {
            facing = 3;
        }
        else {
            facing -= 1;
        }
    }

    public void turnRight() {
        if (facing == 3) {
            facing = 0;
        }
        else {
            facing += 1;
        }
    }

    public void move (int distance) {
        if (facing == 0) {
            distance = distance + y; 
            y = distance;
        }
        else if (facing == 1) {
            distance = distance + x;
            x = distance;
        }
        else if (facing == 2) {
            distance = y - distance;
            y = distance;
        }
        else {
            distance = x - distance;
            x = distance;
        }
    }
}

城市课程:

import java.util.*;

public class City {
    private List<Car> cars = new ArrayList<Car>();
    private int sizeX;
    private int sizeY;

    public City (int sizeX, int sizeY) {
    }

public void addCar(Car car) {
    cars.add(car);
    }

    public void moveCar(Car car, int distance) {
        for (int n = 0; n<distance; n++) {
            car.move(1);

            for (int i = 0; i<cars.size(); i++) {
                Car otherCar = cars.get(i);
                if (car != otherCar) {
                    ////this is where I'm getting stuck
                    //trying to figure out how to use this
                    //to detect when cars run into each other
                }
            }
        }
        System.out.print("Car is at "+ "" + car.getX() + ","+ car.getY() +"\n");
    }

    public int getSizeX() {
        return sizeX;
    }

    public void setSizeX(int sizeX) {
        this.sizeX = sizeX;
    }

    public int getSizeY() {
        return sizeY;
    }

    public void setSizeY(int sizeY) {
        this.sizeY = sizeY;
    }

    public static void main(String[] args) {
        City city = new City(10,10);
        Car c1 = new Car(0,0,1);
        Car c2 = new Car(9,9,2);
        city.addCar(c1);
        city.addCar(c2);
        city.moveCar(c1,9);
        city.moveCar(c2,9);
    }
}

1 个答案:

答案 0 :(得分:3)

从本质上讲,这是你的问题:

if (car != otherCar)

原因是:carotherCar都是Car类型的对象,因此==只会检查它们是否是完全相同的实例。

传统的做法是覆盖equals()中的Car。当两辆车都在同一个坐标位置时,你似乎试图定义相等,所以我会在这里提供这个提示。

如果您的某个参数为null或不属于Car类型,或者它是相同的实例( <}> {{1}我留下作为读者的练习。

==