정말 객체 지향 디자인의 원리를 이해하지 못하겠습니까 ?? 그래서 수업이 있습니다기본 객체 지향 디자인 자바
지도 클래스는 모든 방을 연결하고 모든 위험 요소를 임의로 방에 배치하고 미립자 방을 반환 한 다음 임의의 방을 반환합니다.
및 회전을 재생
플레이어 클래스는, 다른 방에서 플레이어를 이동 방으로 촬영하고 게임을 즐기십시오.
다음과 같은 룸 클래스입니다.
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 사 번 이상 촬영해야합니다 있도록이를 변경하려면 살해 할 (당신은 얼마나 많은 시간 선택). 총알이 발사 될 때마다 플레이어가있는 임의의 이웃 방으로 이동합니다.
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
를 사용하려고에. 그러나 그들은 맵과 룸 클래스에 모두 있지 않습니다.
sooo를 muuuuchhh cooode –
나는 단지'공공 부울 촬영을 게시하는 데 필요한 것 같아요()'방법 방에 다른 방을 wumpus 사를 보내려면,하지만 난 어디 모르는 그래서이와 중복입니다 심지어 시작합니다 .... – user1721548
@ user1721548 글쎄, 지금 가서 해보세요. 과제를 수행하는 동안 발생하는 모든 일에 대해 하나의 질문을하는 대신 필요한 경우 가장 작은 범위를 갖는 질문을 선호합니다 (필요한 경우 더 많은 질문을 던집니다). – millimoose