基本面向对象设计java

基本面向对象设计java,java,oop,object,Java,Oop,Object,我真的不懂面向对象设计的原理?? 所以我有课 Map类保存房间并连接所有房间,将所有危险随机放置到房间中,返回微粒房间,然后返回随机房间 及 玩家类可以轮流游戏,将玩家从一个房间移动到另一个房间,射入房间并进行游戏 也 房间等级如下 import java.util.ArrayList; public class Room { private int myRoomID; private ArrayList<Room> myNeighbours; priva

我真的不懂面向对象设计的原理?? 所以我有课

Map类保存房间并连接所有房间,将所有危险随机放置到房间中,返回微粒房间,然后返回随机房间

玩家类可以轮流游戏,将玩家从一个房间移动到另一个房间,射入房间并进行游戏

房间等级如下

import java.util.ArrayList;

public class Room
{
    private int myRoomID;
    private ArrayList<Room> myNeighbours;

    private boolean myHasBats;
    private boolean myHasPit;
    private boolean myHasWumpus;

    public Room(int id) {
        myRoomID = id; 
        myNeighbours = new ArrayList<Room>();
    }

    public int getRoomID() {
        return myRoomID;
    }

    public ArrayList<Room> getNeighbours() {
        return myNeighbours;
    }

    public void connectTo(Room room) {
        myNeighbours.add(room);
    }    

    public boolean hasBats() {
        return myHasBats;
    }

    public void setHasBats(boolean flag) {
        myHasBats = flag;
    }

    public boolean hasPit() {
        return myHasPit;
    }

    public void setHasPit(boolean flag) {
        myHasPit = flag;
    }

    public boolean hasWumpus() {
        return myHasWumpus;
    }

    public void setHasWumpus(boolean flag) {
        myHasWumpus = flag;
    }

    public void checkBats() {
        boolean bats = false;
        for (Room r : myNeighbours) {
            if (r.hasBats()) {
                bats = true;
            }
        }
        if (bats) {
            System.out.println("I hear squeaking!");
        }
    }

    public void checkPit() {
        boolean pit = false;
        for (Room r : myNeighbours) {
            if (r.hasPit()) {
                pit = true;
            }
        }
        if (pit) {
            System.out.println("I feel a draft!");
        }
    }

    public void checkWumpus() {
        boolean wumpus = false;
        for (Room r : myNeighbours) {
            if (r.hasWumpus()) {
                wumpus = true;
            }
        }
        if (wumpus) {
            System.out.println("I smell a wumpus!");
        }
    }

    public boolean enter(Player player) {
        System.out.println("You are in Room " + myRoomID);
        System.out.print("Exits lead to rooms");

        for (Room r : myNeighbours) {
            System.out.print(" " + r.getRoomID());
        }
        System.out.println();
        checkBats();
        checkPit();
        checkWumpus();

        if (myHasBats) {
            System.out.println("A flock of bats picks you up and carries you off to another room!");
            return player.moveRandom();
        }
        else if (myHasPit) {
            System.out.println("You fall into a bottomless pit!");
            return true;
        }
        else if (myHasWumpus) {
            System.out.println("You have been eaten by a wumpus!");            
            return true;
        }

        return false;
    }

public boolean shoot()


        if (myHasWumpus) {

            System.out.println("You killed the Wumpus!");

            return true;


        }
        else {

            System.out.println("Your arrow falls with a clatter to the floor!");

            return false;


        }

    }

稍后,我们将在类中使用
实现
,将所有危险划分为多个类。但并非如此,他们都在地图和房间课上

如果没有一个wumpus类,它将变得混乱、有限,甚至更加低效。你的问题不是你没有得到OO,而是你被限制使用它

没有离开教室。 您必须将myWumpusShotCount添加到房间中 然后在你的shot函数中,加上1,测试它是否是3,如果是的话,杀死它,否则随机选择一个房间,并在其中设置hasWumpus和WumpusShotCount


如果你有一个wumpus类,它将有一个属性室,另一个属性室用于显示它运送了多少子弹以及射击时的行为,即wumpus的状态和wumpus的行为将由wumpus实现,而不是房间。这是面向对象的。

在Tony Hopkinson的帮助下,我提出了这段代码,但它还没有编译。它说找不到symbol-method
getRandomRoom()来调用该方法
getRandomRoom();`来自Map类。 把wumpus随机送到另一个房间

public boolean shoot() {

 if (myHasWumpus) {
   myWumpusShotCount = 3;
   for(int i = 0; i< myWumpusShotCount; i++){
      if(myWumpusShotCount<=i){ 
          System.out.println("You killed the Wumpus!");
          return true;
      }
      else {
         Room wRoom = getRandomRoom(); 
         System.out.println("You killed the Wumpus!");
         return false;
         myWumpusShotCount++;

      }

        System.out.println("Your arrow falls with a clatter to the floor!");
        return false;
}


         System.out.println("You killed the Wumpus!");
         return true;
      }
   }
        System.out.println("Your arrow falls with a clatter to the floor!");
        return false;
}
public boolean shot(){
如果(myHasWumpus){
myWumpusShotCount=3;
对于(int i=0;i如果(mywumpusshotcount)我想我只需要发布
public boolean shoot()
方法,但我太过沉迷于此,以至于我甚至不知道从哪里开始……@user1721548我们现在就开始吧。你的问题最好有对你有用的最小范围(如果必要,提出更多问题),而不是一个关于你在做作业或其他任何事情时发生的每件事的问题。@user1721548这样做既可以帮助我们更快地给你一个更好的答案,因为一次可以接受的东西更少,而且如果你必须将设计分解为你所遇到的孤立问题,它可能会帮助你对设计进行推理调用
getRandomRoom()的正确语法是什么
?我使用的是getter方法吗?请看第二个答案。根据您的描述,它不必是公共的,如果是我,我会在room类中添加一个类型为Random的静态属性,并使getRandomroom成为一个静态函数。您不需要为每个调用都引用一个新的。提示从私有方法开始,然后使用哼哼,可儿看得见。把一些私人的东西公之于众,以后要比把一些东西公之于众,私之于众容易得多。。。。
public boolean shoot() {

 if (myHasWumpus) {
   myWumpusShotCount = 3;
   for(int i = 0; i< myWumpusShotCount; i++){
      if(myWumpusShotCount<=i){ 
          System.out.println("You killed the Wumpus!");
          return true;
      }
      else {
         Room wRoom = getRandomRoom(); 
         System.out.println("You killed the Wumpus!");
         return false;
         myWumpusShotCount++;

      }

        System.out.println("Your arrow falls with a clatter to the floor!");
        return false;
}


         System.out.println("You killed the Wumpus!");
         return true;
      }
   }
        System.out.println("Your arrow falls with a clatter to the floor!");
        return false;
}