[英]Basic Object Oriented Design java
我真的不了解面向對象設計的原理? 所以我有課
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 boolean shoot()
方法更改為循環並按如下所示調用public Room getRandomRoom()
。
但是我真的不明白如何做到這一點,特別是因為布爾方法的使用對我來說非常混亂。 有誰知道我在哪里可以找到信息來學習面向對象設計的基礎知識?
public Room getRandomRoom() {
Random rng = new Random();
int i = rng.nextInt(Map.NUM_ROOMS);
return myRooms.get(i);
}
稍后,我們將在類中使用implements
將所有危害分為幾類。 但並非全部都在“地圖”和“房間”類中。
好吧,如果沒有糟糕的課堂,那將是混亂而有限的,甚至效率更高的。 您的問題不是您沒有獲得OO,而是您被限制使用它。
沒有課。 您將必須在房間中添加一個myWumpusShotCount,然后在射擊功能中添加1,測試是否為3,然后殺死它,否則隨機選擇一個房間並在其中設置hasWumpus和WumpusShotCount
如果您有一個wumpus班級,它將有一個屬性室,另外一個屬性室是關於它運送了多少子彈以及射擊時的行為,即wumpus的狀態和wumpus的行為將由wumpus而非房間來實現。 那是OO。
在Tony Hopkinson的幫助下,我想出了這段代碼,但尚未編譯。 它說找不到符號-方法getRandomRoom() To call the method
從Map類getRandomRoom() To call the method
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.