2012-10-11 2 views
0

정말 객체 지향 디자인의 원리를 이해하지 못하겠습니까 ?? 그래서 수업이 있습니다기본 객체 지향 디자인 자바

지도 클래스는 모든 방을 연결하고 모든 위험 요소를 임의로 방에 배치하고 미립자 방을 반환 한 다음 임의의 방을 반환합니다.

및 회전을 재생

플레이어 클래스는, 다른 방에서 플레이어를 이동 방으로 촬영하고 게임을 즐기십시오.

다음과 같은 룸 클래스입니다.

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를 사용하려고에. 그러나 그들은 맵과 룸 클래스에 모두 있지 않습니다.

+2

sooo를 muuuuchhh cooode –

+0

나는 단지'공공 부울 촬영을 게시하는 데 필요한 것 같아요()'방법 방에 다른 방을 wumpus 사를 보내려면,하지만 난 어디 모르는 그래서이와 중복입니다 심지어 시작합니다 .... – user1721548

+2

@ user1721548 글쎄, 지금 가서 해보세요. 과제를 수행하는 동안 발생하는 모든 일에 대해 하나의 질문을하는 대신 필요한 경우 가장 작은 범위를 갖는 질문을 선호합니다 (필요한 경우 더 많은 질문을 던집니다). – millimoose

답변

1

지저분한 클래스가 없으면 지저분하고 제한적이고 훨씬 비효율적입니다. 귀하의 문제는 귀하가 물건을 얻지 못하는 것이 아니라 귀하가 그것을 사용하는 것이 제한된다는 것입니다.

수업을 빠뜨리지 마십시오. 당신은이 3 인 경우, 시험을보고 거기에 1을 추가하여 촬영 기능에 다음 방 에 myWumpusShotCount을 추가 한 듣는다고하고 그래서 다른 임의의 방을 선택 죽일 경우

거기에 hasWumpus 및 WumpusShotCount을 설정 당신이 wumpus 클래스를 가지고 있다면 그것은 속성 룸을 가질 것이고, 또 하나는 총알이 얼마나 많은 총을 쏘았는지, 즉 wumpus의 상태와 wumpus의 행동은 룸이 아닌 wumpus에 의해 구현 될 것입니다. 그것은 OO입니다.

+0

'getRandomRoom()'을 호출하는 올바른 구문은 무엇입니까? getter 메서드를 사용하고 있습니까? 두 번째 대답을 참조하십시오. – user1721548

+0

당신이 묘사 한 바에 따르면 그것이 public 일 필요는 없으며, 나 였다면 룸 클래스에 Random 유형의 정적 속성을 추가하고 getRandomroom을 정적 함수로 만들 수도 있습니다. 각 호출에 대해 새 인스턴스를 인스턴스화 할 필요는 없습니다. 팁은 개인적인 방법으로 시작한 다음 그들을 볼 수있게합니다. sometimng 비공개 공개를 만드는 것은 나중에 어떤 것을 공개적으로 만드는 것보다 쉽습니다. –

0

Tony Hopkinson의 도움으로 필자는이 코드를 작성했지만 아직 컴파일하지 않습니다. 그것은지도 클래스에서 심볼을 찾을 수 없다고 말합니다. 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; 
} 
관련 문제