2012-04-26 3 views
0

저는 간단한 텍스트 기반 어드벤처 게임을 만드는 프로젝트를 진행하고 있습니다. 조금 복잡하지만 기본적으로 게임에 약 8 개의 객실이 있으며 각각에는 특정 항목이 있습니다. 방에 따라 특정 방향으로 만 이동할 수 있으며 목표는 마지막 방으로 이동하는 것입니다. Item 클래스, Room 클래스, Main Game 클래스가 있습니다.호환되지 않는 유형의 문제를 해결하려면 어떻게합니까?

지난 세 가지 방법으로 많은 문제가 있습니다. (나머지 코드를 컨텍스트로 포함 시켰습니다.) 제 지시문은 String의 매개 변수를 사용한다고하지만 메서드는 "항목"을 처리해야합니다. 데이터 유형 항목은 다른 클래스에서 작성되며 4 개의 매개 변수 (문자열 이름, 문자열 설명, int 가중치 및 부 울린 식용 가능)로 구성됩니다. 누군가 올바른 방향으로 나를 가리킬 수 있습니까? 당신은 다른 개체 항목을해야 무엇을위한 문자열을 사용하는

import java.util.ArrayList; 

public class Game 
{ 
    private ArrayList<Item> itemsHeld; 
    private Room planetHarvest; 
    private Room planetReach; 
    private Room alphaHalo; 
    private Room newMombasa; 
    private Room deltaHalo; 
    private Room voi; 
    private Room theArk; 
    private Room forerunnerShieldWorld; 
    private Item DMR; 
    private Item Launcher; 
    private Item Shotgun; 
    private Item Health; 
    private Item Key; 
    private Room currentLocation; 
    private String msg; 

    public Game(){ 
     createRooms(); 
     ArrayList<Item> itemsHeld = new ArrayList<Item>(0); 
     currentLocation = planetHarvest; 
    } 

    private void createRooms(){ 
     DMR = new Item("DMR", "The Designated Marksman Rifle fires a medium-range shot that defeats one enemy at a time.", 
     10, false); 
     Launcher = new Item("Launcher", "The rocket launcher fires a long-range rocket that defeats two enemies at a time.", 
     30, false); 
     Shotgun = new Item("Shotgun", "The shotgun fires a short-range buckshot blast that defeats one enemy at a time.", 
     10, false); 
     Health = new Item("Health", "The health pack heals you if you've been attacked.", 10, true); 
     Key = new Item("Key", "The Key is the secret to saving the galaxy.", 10000, false); 

     planetHarvest = new Room("a farming colony at the edge of human-controlled space. Though usually quiet, it has now been disturbed by an invasion from extraterrestrials who call themselves the Covenant. The Covenant will not let you escape without a fight.", DMR); 
     planetReach = new Room("twenty-seven years in the future. The Covenant has invaded every human colony world and bombarded each one with plasma, reducing the surface to molten glass. Planet Reach is the only human colony that remains. The aliens know their campaign to exterminate humanity is almost over and they show it in their ferocity on the battlefield.", Launcher); 
     alphaHalo = new Room("a strange, alien, planet-sized ring following your escape from the doomed planet Reach. The inside of the mysterious non-Covenant hoop structure has continents, oceans, ecosystems, breathable atmosphere – and two relentless foes. The Covenant has arrived; so has the Flood, a terrifying alien parasite that will eat both you and the Covenant. On top of that, you learn that this Halo is not just a habitat but also a weapon that will destroy all life in the galaxy if you don’t stop it.", Key); 
     newMombasa = new Room("an African city on Earth, humanity's last safe haven in the universe. A Covenant invasion force has landed. You’ll need to fend them off and follow them to wherever they’re headed next.", Health); 
     deltaHalo = new Room("where the Covenant and the Flood are already waiting. The same rules from Alpha Halo apply here, but now you learn that there are a total of eleven Halos and that the only way to stop them all is to shut them down from a structure outside the galaxy called the Ark. Meanwhile, the Covenant is still attacking Earth.", Key); 
     voi = new Room("The Flood has arrived and is infecting humans and aliens; meanwhile, the Covenant is in civil war and some of the aliens are helping you. Following your escape from Delta Halo, all of the Halos in the galaxy have been put on standby, so if you don’t find the Ark soon then it’s the end of life as we know it.", Shotgun); 
     theArk = new Room("a massive installation many times bigger than even a Halo. Shutting down the Ark will mean saving the galaxy. However, most of the Covenant and the Flood are still after you. Finish the fight.", Key); 
     forerunnerShieldWorld = new Room("an artificial planet made by a super-advanced alien civilization that disappeared 100,000 years ago – the makers of the Halos and the Ark. The war against the Covenant is finally over, the Flood has been defeated, and the galaxy is safe. The mysteries of the Forerunners, however, are just beginning to be unraveled."); 

     planetHarvest.addNeighbor("Slipspace Forward", planetReach); 

     planetReach.addNeighbor("Slipspace Out", alphaHalo); 

     alphaHalo.addNeighbor("Slipspace Back", planetReach); 
     alphaHalo.addNeighbor("Slipspace Forward", newMombasa); 

     newMombasa.addNeighbor("Slipspace Out", deltaHalo); 
     newMombasa.addNeighbor("Forward", voi); 

     deltaHalo.addNeighbor("Slipspace Back", newMombasa); 

     voi.addNeighbor("Slipspace Out", theArk); 
     voi.addNeighbor("Back", newMombasa); 

     theArk.addNeighbor("Slipspace Unknown", forerunnerShieldWorld); 
    } 

    private void setWelcomeMessage(){ 
     msg = "You are John-117, an augmented soldier fighting for Earth and its colonies in the year 2552. You battle the Covenant, an alien religious alliance, and the Flood, an alien parasite that infects both humans and Covenant and turns them into zombies. Your battles take you to planetary colonies like Harvest and Reach, ancient alien ring-worlds called Halos, futuristic cities in Africa, and the Halos’ control station called the Ark, located outside the galaxy. The goal of the game is to defeat all enemies and make it to the safety of the Forerunner Shield World."; 
    } 

    public String getMessage(){ 
     return msg; 
    } 

    public void help(){ 
     msg = "Every place you enter is crawling with Covenent and/or Flood. You'll have to neutralize them before they neutralize you. As a Reclaimer, you're the only one who can use Keys on a Halo or Ark. Some places involve more than one mission."; 
    } 

    public void look(){ 
     msg = currentLocation.getLongDescription(); 
    } 

    public void move (String direction){ 
     Room nextRoom = currentLocation.getNeighbor(direction); 
     if (nextRoom == null){ 
      msg = "You can't go there."; 
     }else{ 
      currentLocation = nextRoom; 
      msg = currentLocation.getLongDescription(); 
     } 
    } 

    public void list(){ 
     if(itemsHeld != null){ 
      msg += itemsHeld; 
     }else{ 
      msg = "You are not holding anything."; 
     } 
    } 

    public boolean gameOver(){ 
     if(currentLocation == forerunnerShieldWorld){ 
      msg = "You defeated the Covenant and survived the war. You won!"; 
     } 
     return false; 
    } 

    public void pickup(){ 
     if(currentLocation.hasItem()){ 
      if(currentLocation.getItem().getWeight() < 50){ 
       msg = "You are now holding the " + currentLocation.getItem().getName(); 
       itemsHeld.add(currentLocation.getItem()); 
       currentLocation.removeItem(); 
      }else{ 
       msg = "This item is too heavy to pick up."; 
      } 
     }else{ 
      msg = "There is no item to pick up."; 
     } 
    } 

    private Item checkForItem (String name){ 
     if(itemsHeld.contains(name)){ 
      return item; 
     }else{ 
      return null; 
     } 
    } 

    public void drop (String item){ 
     if(itemsHeld.contains(item)){ 
      if(currentLocation.getItem() != null){ 
       msg = "You successfully dropped the " + item; 
       itemsHeld.remove(item); 
       currentLocation.addItem(item); 
      }else{ 
       msg = "There's no room to drop that here."; 
      } 
     }else{ 
      msg = "You are not holding the " + item; 
     } 
    } 

    public void eat (String item){ 
     if(itemsHeld.contains(item)){ 
      if(item.isEdible()){ 
       msg = "You have eaten the " + item.getName(); 
      }else{ 
       msg = "You can't eat that!"; 
      } 
     }else{ 
      msg = "You are not holding a " + item.getName(); 
     } 
    } 
} 
+0

무엇이 문제입니까? 컴파일 타임 오류? 런타임 예외? 어느 것이 구체적으로? 그것이 처음 시작하는 곳입니다. – pamphlet

+0

컴파일 오류가 발생했습니다. 또한 BlueJ는 "symbol - variable item"을 찾을 수 없으며 항목을 변수로 선언하지 않았기 때문에 BlueJ는이를 이해합니다. 그러나 매개 변수는 String이어야하므로 둘 다 수행하는 방법을 알지 못합니다. –

답변

0

...

공공 무효 식사 (문자열 항목) {
...
경우 (item.isEdible()) {

해당 코드 비트가 무엇인지는 item 문자열에 대해 isEdible() 문자열 클래스 메서드를 호출합니다. 분명히 그런 메서드가 없으며 다른 (사용자 지정) Object에 대해 String을 혼동하게됩니다.

public void eat (Item item) { 

난 당신이 피곤 실수를했다고 생각하지만, 그게 아니라면 경우 I는 Item 객체와 함께 당신을 도울 수 있습니다 .. :

올바른 방법은 다음과 같이 아이템 객체를 사용하는 것

1

item.GetName()을 키로 사용하여 itemsHeld에 HashMap을 사용합니다.

0

String이라는 항목 이름을 Item 개체로 변환 할 수있는 방법이 필요합니다. 항목이 포함 된 간단한 HashMap이 작동합니다.

이 항목을 Item 개체로 사용하면 isEdible()과 같이 회원 메서드를 호출 할 수 있습니다.

관련 문제