オープンソースのC#テキストベースの冒険を改良して、C#を学びます。私はすべてのプレーヤーデータをファイルに保存して読み込む作業をしています。'Project1.Player'は 'タイプ'ですが、 '変数'のように使用されます
私は私のプレーヤーのクラスをシリアル化しようとすると、コンパイラはエラーをスロー:
'Project1.Player' is a 'type' but is used like a 'variable' Program.cs:72
は、物事を簡単にするために[OK]を を更新し、私はその現在の状態にすべての私のコードを更新しました。私はプレーヤーを参照するたびに取得しています 新しいエラーは次のとおりです。 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つのことに注意してください:
- 私はC#を学んでいます。私はJSの背景から来ています。
- 私はプレーヤーのデータを保存しようとしているところに深刻な問題があることを認識していますが、それが私がここにいる理由です。
- 大量のデータをプログラムに保存しようとしたのは今回が初めてです。ここでは多くの実験が行われています。
現在、このエラーが発生することを行う: 'メンバー「Project1.Player.name」は、インスタンス参照してアクセスすることができません。代わりに型名で修飾してください。 ' –
他にも触れられているように、 'Player'には静的なフィールドとメソッドを含めるべきではないでしょう。私は私の答えを更新します。 –
@ ryansworld10 - 回答が更新されました。 –