我真的不懂面向对象设计的原理?? 所以我有课
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;
}
}
我想改变这一点,以便乌普斯需要被多次射击(你选择多少次)才能被杀死。每次被射击时,它都会跑到随机的相邻房间(不是玩家所在的房间)。
我假设我需要将 public booleanshoot()
方法更改为循环并调用 public Room getRandomRoom()
如下。
但我真的不明白如何做到这一点,特别是因为 boolean 方法的使用对我来说非常困惑。 有谁知道我在哪里可以找到学习面向对象设计基础知识的信息?
public Room getRandomRoom() {
Random rng = new Random();
int i = rng.nextInt(Map.NUM_ROOMS);
return myRooms.get(i);
}
稍后我们将在类中使用implements
将所有危险分成不同的类。但并非它们都属于 Map 和 Room 类。
最佳答案
如果没有 wumpus 类,那将会变得困惑、有限,甚至效率更低。你的问题不是你没有获得 OO,而是你被限制使用它。
没有课。 您必须将 myWumpusShotCount 添加到房间 然后在你的射击函数中,添加 1,测试是否为 3,如果是,则杀死它,否则随机选择一个房间并在其中设置 hasWumpus 和 WumpusShotCount
如果你有一个 wumpus 类,它会有一个属性(property) room ,另一个是它运送了多少子弹以及射击时的行为,即 wumpus 的状态和 wumpus 的行为将由 wumpus 而不是 room 实现。这就是面向对象。
关于java - 基本面向对象设计java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12850134/