2012-03-05 1 views
0

오픈 소스 C# 텍스트 기반 모험을 개선하여 C#을 배우고 있습니다. 모든 플레이어 데이터를 파일로 저장하고로드하는 중입니다. 일을 쉽게하기 위해 확인 업데이트]'Project1.Player'는 '유형'이지만 '변수'처럼 사용됩니다.

'Project1.Player' is a 'type' but is used like a 'variable' Program.cs:72 

, 나는 현재 상태로 내 모든 코드를 업데이트 :

내 플레이어 클래스를 직렬화하려고하면 컴파일러 오류가 발생합니다. 나는 모든 시간을 얻고있다 새로운 오류가 나는 선수 참조는 다음과 같습니다 The name 'player' does not exist in the current context

Program.cs :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Runtime.Serialization; 
using System.Runtime.Serialization.Formatters.Binary; 

namespace Project1 
{ 
    class Program 
    { 
     public static int windowWidth = 80; 
     public static int windowHeight = 35; 
     public static bool run = true; 
     public static string errorMessage; 
     public static bool isError = false; 

     static void Main(string[] args) 
     { 
      // setup console window 
      Console.Clear(); 
      // set size 
      Console.SetWindowSize(windowWidth, windowHeight); 
      // remove scroll bar with buffer size equal to window size 
      Console.BufferWidth = windowWidth; 
      Console.BufferHeight = windowHeight; 

      // generate world 
      Player player = new Player(); 
      World.GenerateWorld(); 

      // begin character creation 
      string name = Text.Prompt("Welcome stranger. What is your name?"); 
      player.name = name; 
      player.inventory.Add(new Item("Candle", "A single white candle.", 22)); 

      Text.WriteLine("Thanks, " + name); 
      Text.WriteLine("Press any key to get started."); 
      Console.ReadKey(); 
      Console.Clear(); 
      Text.WriteLine("The last thing you remember is battling the Ancient LichLord deep within a cavern beneath Death Mountain. Now suddenly, you are surrounded by the darkness of true night as you stand in front of an ancient stone castle of complex architecture. Looking up, you notice the faint glow of a light coming from an old clock tower, high above the castle. The doors to the castle are large and covered in an ancient mold. Unsure of what to do, you drag open the large wooden door, light the candle you found in your pocket, and step into the castle. \n \nPress a key..."); 
      Console.ReadKey(); 
      Console.Clear(); 

      while (run) 
      { 
       if (!isError) 
       { 
        Text.SetPrompt(); 
        World.LocationDescription(); 

        string temp = Text.Prompt(""); 
        Console.Clear(); 
        player.Do(temp); 
       } 
       // there is an error 
       else 
       { 
        DisplayError(); 
       } 
      } 
     } 

     // stream writer to write to file. 
     public static void SaveGame() 
     { 
      try 
      { 
       using (Stream stream = File.Open("save.dat", FileMode.Create)) 
       { 
        BinaryFormatter bin = new BinaryFormatter(); 
        bin.Serialize(stream, player); 
       } 
      } 
      catch (IOException) 
      { 
      } 
     } 

     public static void LoadGame() 
     { 
      if (File.Exists("save.dat")) 
      { 
       try 
       { 
       using (Stream stream = File.Open("save.dat", FileMode.Open)) 
        { 
         BinaryFormatter bin = new BinaryFormatter(); 
         var player = bin.Deserialize(stream); 
        } 
       } 
       catch (IOException) 
       { 
       } 
      } 
      else 
      { 
       Program.SetError("The savegame does not exist!"); 
      } 
     } 
     // stream reader to find file 
     //public static void LoadGame() 
     //{ 
     // try 
     // { 
     //  using (Stream stream = File.Open("data.bin", FileMode.Open)) 
     //  { 
     //   BinaryFormatter bin = new BinaryFormatter(); 
     //   World.map = (List<Location>)bin.Deserialize(stream); 
     //  } 
     // } 
     // catch (IOException) 
     // { 
     // } 
     //} 

     public static void WinGame() 
     { 
      run = false; 
      Text.WriteLine("As you place the crown upon your head, your vision begins to blur and you fall to the floor. You wake up in a hot cavern, lit by a few torches on the wall. This is the cavern of the Ancient LichLord, and you have escaped his twisted maze. "); 
     } 

     #region Error Handling 
     public static void SetError(string aText) 
     { 
      isError = true; 
      errorMessage = aText; 
     } 

     public static void UnsetError() 
     { 
      isError = false; 
      errorMessage = ""; 
     } 

     public static void DisplayError() 
     { 
      Text.WriteColor("|r|" + errorMessage + "|g|"); 
      Text.BlankLines(2); 
      UnsetError(); 
     } 
     #endregion 
    } 
} 

Player.cs :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime.Serialization; 
using System.Runtime.Serialization.Formatters.Binary; 

namespace Project1 
{ 
    [Serializable()] 
    class Player 
    { 
     public string name; 
     //public static string desc; 
     //public static int[] stats; 
     public int location; 
     public List<Item> inventory = new List<Item>(); 
     public string[] possibleActions = new string[] { "move", "m", "look", "l", "take", "t", "drop", "d", "use", "u", "inventory", "i", "help", "h", "quit", "exit", "save", "load", "name" }; 

     /// <summary> 
     /// Player action, array of predicate and target 
     /// </summary> 
     /// <param name="aAction"></param> 
     /// <returns></returns> 
     public void Do(string aText) 
     { 
      string verb = ""; 
      string noun = ""; 

      // check if there is a space in the given command 
      if (aText.IndexOf(' ') != -1) 
      { 
       // split the string into the verb and noun 
       string[] temp = aText.Split(new char[] { ' ' }, 2); 
       verb = temp[0].ToLower(); 
       noun = temp[1].ToLower(); 
      } 
      else 
      { 
       // verb only 
       verb = aText.ToLower(); 
      } 

      if (IsAction(verb)) 
      { 
       // do whatever action 
       switch (verb) 
       { 
        case "move": 
        case "m": 
         if (noun == "") 
          Program.SetError("You must specify a location to move."); 
         else 
          MoveTo(noun); 
         break; 

        case "look": 
        case "l": 
         if (noun == "") 
          noun = World.map[player.location].name; 

         LookAt(noun); 
         break; 

        case "take": 
        case "t": 
         if (noun == "") 
          Program.SetError("You must specify an item to take."); 
         else 
          PickUpItem(noun); 
         break; 

        case "drop": 
        case "d": 
         if (noun == "") 
          Program.SetError("You must specify an item to drop."); 
         else 
          DropItem(noun); 
         break; 

        case "use": 
        case "u": 
         if (noun == "") 
          Program.SetError("You must specify an item to use."); 
         else 
          UseItem(noun); 
         break; 

        case "inventory": 
        case "i": 
         ListInventory(); 
         break; 

        case "help": 
        case "h": 
         ListActions(); 
         break; 

        case "quit": 
        case "exit": 
         QuitPrompt(); 
         break; 

        case "save": 
         Program.SaveGame(); 
         break; 

        case "load": 
         Program.LoadGame(); 
         break; 
        case "name": 
         Console.WriteLine("Your name is {0}", player.name); 
         Console.WriteLine(""); 
         break; 
       } 
      } 
      else 
      { 
       // not a real action 
       Program.SetError("Action not found."); 
      } 
     } 

     public void MoveTo(string location) 
     { 
      // is location? 
      if (World.IsLocation(location)) 
      { 
       int locationId = World.GetLocationIdByName(location); 

       if (World.IsLocationExit(location)) 
       { 
        // set the player's new location 
        player.location = locationId; 
       } 
       else 
       { 
        Program.SetError("You can't get there from here."); 
       } 
      } 
      else 
      { 
       Program.SetError("That is not a real location."); 
      } 
     } 

     public void LookAt(string noun) 
     { 
      // is location? 
      if (World.IsLocation(noun)) 
      { 
       Console.Clear(); 
       World.ShowHiddenItems(); 
       Text.BlankLines(2); 
      } 
      // is item? 
      else if (Item.IsItemInInventory(noun) || Item.IsItemInLocation(noun)) 
      { 
       Console.Clear(); 
       Text.WriteLine(Item.GetItemDescByName(noun)); 
       Text.BlankLines(2); 
      } 
     } 

     public void PickUpItem(string item) 
     { 
      // is item? 
      if (Item.IsItemInLocation(item)) 
      { 
       // get description 
       string desc = Item.GetItemDescByName(item); 
       int actionLocationId = Item.GetItemActionLocationIdByName(item); 
       // remove item from location 
       Item.RemoveItemFromLocation(item); 
       // add item to inventory 
       player.inventory.Add(new Item(item, desc, actionLocationId)); 
      } 
      else 
      { 
       Program.SetError("Item not found in this location."); 
      } 
     } 

     public void DropItem(string item) 
     { 
      //is item? 
      if (Item.IsItemInInventory(item)) 
      { 
       string desc = Item.GetItemDescByName(item); 
       int actionLocationId = Item.GetItemActionLocationIdByName(item); 

       // remove item from inventory 
       RemoveInventoryItem(item); 

       // add item to location 
       World.map[player.location].items.Add(new Item(item, desc, actionLocationId)); 
      } 
      else 
      { 
       Program.SetError("Item not in inventory."); 
      } 
     } 

     public void RemoveInventoryItem(string item) 
     { 
      for (int i = 0; i < player.inventory.Count; i++) 
      { 
       if (player.inventory[i].name.ToLower() == item.ToLower()) 
       { 
        player.inventory.RemoveAt(i); 
       } 
      } 
     } 

     public void UseItem(string item) 
     { 
      // is item? 
      if (Item.IsItemInInventory(item)) 
      { 
       // get item actionLocationId 
       int itemActionLocationId = Item.GetItemActionLocationIdByName(item); 
       if (itemActionLocationId == player.location) 
       { 
        World.UseItemInLocation(item); 
       } 
       else 
       { 
        Program.SetError("You're not sure how that helps here."); 
       } 
      } 
      else 
      { 
       Program.SetError("Item not in inventory"); 
      } 
     } 

     public void ListInventory() 
     { 
      Text.WriteLine("\n-- Inventory -- \n"); 

      for (int i = 0; i < player.inventory.Count; i++) 
      { 
       Text.WriteLine(i.ToString() + ": "+ player.inventory[i].name); 
      } 

      Text.BlankLines(2); 
     } 

     public void ListActions() 
     { 
      Text.BlankLines(2); 

      //"move", "look", "take", "drop", "use", "inventory", "help" 
      Text.WriteLine("\n-- Actions -- \n"); 
      Text.WriteLine("move - travel to another location. usage: move entrance hall (or) m entrance hall"); 
      Text.WriteLine("look - look at a location or an item. usage: look entrance hall (or) look sword (or) l entrance hall (or) l sword"); 
      Text.WriteLine("take - pick up an item in a location. usage: take sword (or) t sword"); 
      Text.WriteLine("drop - drop an item from your inventory. usage: drop sword (or) d sword"); 
      Text.WriteLine("use - use an item in your inventory. usage: use key (or) u key"); 
      Text.WriteLine("inventory - show items in your inventory. usage: inventory (or) i"); 
      Text.WriteLine("help - show this screen. usage: help (or) h"); 
      Text.WriteLine("exit - quit the game. usage: quit (or) exit"); 

      Text.BlankLines(2); 
     } 

     /// <summary> 
     /// Check the legitmacy of the action 
     /// </summary> 
     /// <param name="aText"></param> 
     /// <returns></returns> 
     public bool IsAction(string aText) 
     { 
      for (int i = 0; i < possibleActions.Length; i++) 
      { 
       if (aText == possibleActions[i]) 
       { 
        return true; 
       } 
      } 
      return false; 
     } 

     public void QuitPrompt() 
     { 
      Text.WriteLine("Are you sure you want to leave? y/n"); 
      string answer = Console.ReadLine().ToLower(); 

      if (answer == "y" || answer == "yes") 
      { 
       Program.run = false; 
      } 
     } 
    } 
} 

Item.cs :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Project1 
{ 
    class Item : Entity 
    { 
     public bool isHidden = false; 
     public int actionLocationId; 
     public string actionName; 

     public Item(string aName, string aDesc, int aActionLocationId, bool aIsHidden = false, string aActionName = "use") 
      :base(aName, aDesc) 
     { 
      actionLocationId = aActionLocationId; 
      actionName = aActionName; 

      if (aIsHidden) 
       isHidden = aIsHidden; 
     } 

     /// <summary> 
     /// Looks for item in player.inventory 
     /// </summary> 
     /// <param name="aName">name of the Item</param> 
     /// <returns></returns> 
     public static bool IsItemInInventory(string aName) 
     { 
      // look for item in inventory 
      for (int i = 0; i < player.inventory.Count; i++) 
      { 
       if (player.inventory[i].name.ToLower() == aName.ToLower()) 
       { 
        return true; 
       } 
      } 

      // not found 
      return false; 
     } 

     /// <summary> 
     /// Looks for item in current location 
     /// </summary> 
     /// <param name="aName">name of the Item</param> 
     /// <returns></returns> 
     public static bool IsItemInLocation(string aName) 
     { 
      // look for item in location 
      for (int i = 0; i < World.map[player.location].items.Count; i++) 
      { 
       if (World.map[player.location].items[i].name.ToLower() == aName.ToLower()) 
       { 
        return true; 
       } 
      } 

      // not found 
      return false; 
     } 


     /// <summary> 
     /// Items are only items if in player.inventory or player.location 
     /// </summary> 
     /// <param name="aName">name of the Item</param> 
     /// <returns></returns> 
     public static string GetItemDescByName(string aName) 
     { 
      // look for item in location 
      for (int i = 0; i < World.map[player.location].items.Count; i++) 
      { 
       if (World.map[player.location].items[i].name.ToLower() == aName.ToLower()) 
       { 
        return World.map[player.location].items[i].description; 
       } 
      } 

      // look for item in inventory 
      for (int i = 0; i < player.inventory.Count; i++) 
      { 
       if (player.inventory[i].name.ToLower() == aName.ToLower()) 
       { 
        return player.inventory[i].description; 
       } 
      } 

      // not found 
      return "Item not found"; 
     } 

     public static int GetItemActionLocationIdByName(string aName) 
     { 
      // look for item in inventory 
      for (int i = 0; i < player.inventory.Count; i++) 
      { 
       if (player.inventory[i].name.ToLower() == aName.ToLower()) 
       { 
        return player.inventory[i].actionLocationId; 
       } 
      } 

      // look for item in location 
      for (int i = 0; i < World.map[player.location].items.Count; i++) 
      { 
       if (World.map[player.location].items[i].name.ToLower() == aName.ToLower()) 
       { 
        return World.map[player.location].items[i].actionLocationId; 
       } 
      } 
      return -1; 
     } 

     public static void RemoveItemFromLocation(string item) 
     { 
      for (int i = 0; i < World.map[player.location].items.Count; i++) 
      { 
       if (World.map[player.location].items[i].name.ToLower() == item.ToLower()) 
       { 
        World.map[player.location].items.RemoveAt(i); 
       } 
      } 
     } 
    } 
} 

3 가지주의 사항 :

  1. 저는 C#을 배우고 있으며 JS 배경에서 왔습니다.
  2. 플레이어 데이터를 저장하는 방법에 심각한 문제가 있음을 알고 있습니다. 그러나 그것이 내가 여기있는 이유입니다.
  3. 프로그램에서 많은 양의 데이터를 저장하려고 시도한 것은 이번이 처음이므로 여기에서 많은 실험이 진행되고 있습니다.

답변

2
당신은 당신이 그것을 사용하기 전에 Player 개체를 인스턴스화 할 필요가

, 그래서이 :

Player.name = name; 
Player.inventory.Add(new Item("Candle", "A single white candle.", 22)); 

무언가 같이해야합니다

var player = new Player(); 
player.name = name; 
player.inventory.Add(new Item("Candle", "A single white candle.", 22)); 
// etc... 

그냥 당신의 Player. 호출을 모두 교체 Player 개체를 인스턴스화 한 후에 player. (또는 플레이어 변수를 호출하기로 결정한 것) (new 키워드).

UPDATE

귀하의 Player 클래스는 (한 번에 하나가 정의 할 수 있습니다 의미)을 효과적으로 정적이다.

public static Player CurrentPlayer = new Player(); 

당신이 이런 식으로 한 경우 : 당신이 정말로에만 다음 Program에 정적을 정의 Player의 하나 개의 글로벌 인스턴스를 필요로하는 경우 다음

class Player 
{ 
    public string name; 
    public int location; 
    public List<Item> inventory = new List<Item>(); 
    public string[] possibleActions = new string[] { "move", "m", "look", "l", "take", "t", "drop", "d", "use", "u", "inventory", "i", "help", "h", "quit", "exit", "save", "load", "name" }; 

    /// <summary> 
    /// Player action, array of predicate and target 
    /// </summary> 
    /// <param name="aAction"></param> 
    /// <returns></returns> 
    public void Do(string aText) 
    { 
     // ... 
    } 

    public void MoveTo(string location) 
    { 
     // ... 
    } 

    public void LookAt(string noun) 
    { 
     // ... 
    } 

    public void PickUpItem(string item) 
    { 
     // ... 
    } 

    public void DropItem(string item) 
    { 
     // ... 
    } 

    public void RemoveInventoryItem(string item) 
    { 
     // ... 
    } 

    public void UseItem(string item) 
    { 
     // ... 
    } 

    public void ListInventory() 
    { 
     // ... 
    } 

    public void ListActions() 
    { 
     // ... 
    } 

    /// <summary> 
    /// Check the legitmacy of the action 
    /// </summary> 
    /// <param name="aText"></param> 
    /// <returns></returns> 
    public bool IsAction(string aText) 
    { 
     // ... 
    } 

    public void QuitPrompt() 
    { 
     // ... 
    } 
} 

을 : 좀 더 뭔가로 변경 권 해드립니다 당신은 최선을 다하고 있음을하지 않는 것이 중요 경우, 다른 한편으로

CurrentPlayer.name = name; 
CurrentPlayer.inventory.Add(new Item("Candle", "A single white candle.", 22)); 

: 당신은 같은 Player 인스턴스에 액세스 할 수 Player의 단일 인스턴스로 Player 클래스에서 static 키워드를 모두 삭제하면됩니다. 그러면 오류가 지나갈 수 있습니다.

UPDATE

아래 (질문의 코드에서 모든 세부 사항없이) 거의 같을 것입니다 내 댓글에서 내 권장 해결 방법 : 정적 CurrentPlayer 필드를 추가 한 후

public class Player 
{ 
    public static Player CurrentPlayer = new Player(); // This is your global instance 
    public string name; 
    // All of your other content here 
} 

public class Program 
{ 
    public static void Main() 
    { 
     // In here we can access our global instance of `Player` to change it as needed 
     Player.CurrentPlayer.name = "Some Name Here"; 
    } 
} 

Player 클래스로 세 코드 파일 모두에서 player에 대한 참조를 모두 Player.CurrentPlayer으로 변경할 수 있습니다.

+0

지금이 오류가 발생하는 것이 이렇게 : '회원'Project1.Player.name이 '인스턴스를 참조하여 액세스 할 수 없습니다; 대신 형식 이름으로 한정하십시오. ' –

+0

다른 언급처럼'플레이어 '는 정적 필드와 메소드를 포함해서는 안됩니다. 내 대답을 업데이트 할게. –

+0

@ ryansworld10 - 답변이 업데이트되었습니다. –

0

코드를 작동 시키려면 플레이어 클래스를 정적으로 구현해야합니다. 그러나 static classes when necessary 만 사용하십시오.

그렇지 않으면 Player 클래스의 인스턴스를 만들어야 작동합니다.

Player player = new Player(); 
player.name = name; 
.... 
0

오류 메시지에 명확하게 명시된 바와 같이, 클래스가 아니라 매개 변수 (예 : 플레이어의 인스턴스)로 변수를 넣어야합니다. 플레이어 클래스.

플레이어 클래스의 모든 것이 정적이어서 클래스의 모든 인스턴스에서 공유되므로 인스턴스 생성이 쓸모 없게되는 것처럼 오해 한 것처럼 보입니다. 플레이어의 인스턴스를 하나만 가지려면 singleton pattern에 대해 읽어야합니다.

예에서 올바른 방법은 플레이어 클래스의 정적 선언을 제거하고 게임에서 사용하기 전에 플레이어 클래스의 새 인스턴스를 초기화하는 것입니다.

예 :

Player p = new Player(); 
p.Name = "Something" 
... 
try 
{ 
    using (Stream stream = File.Open("save.dat", FileMode.Create)) 
    { 
     BinaryFormatter bin = new BinaryFormatter(); 
     bin.Serialize(stream, p); 
    } 
} 
catch (IOException) 
{ 
} 
관련 문제