我需要为我的C#课制作游戏。我决定使用Battleships,这是一款非常简单的游戏。我有一个Dictionary<string, string>
,其中包含所有电路板插槽(A-J,1-10)的值(例如,A5,F3,J6)。我有一个方法可以重置这些键,但先检查它是否为空(我会在init中使用它来实际先将键添加到字典中)。如果是,则它在for
循环内使用for
循环(一个用于字母,一个用于数字),用于添加键(A1,A2,A3 ...,B1,B2, B3 ......等)。我添加了一些调试代码,显示密钥中的内容(我使用for
变量i
为密钥构造一个字符串)。它工作正常(例如:添加值 - &gt; BoardPieces.Add(KeyToAdd, i.ToString());
然后读取值 - &gt; Console.WriteLine("Added key to {0} -> {1}", KeyToAdd, BoardPieces[KeyToAdd]);
。它可以工作!但是,当程序返回方法并返回Main()
时,当我尝试检查词典中的值时,它表明没有任何东西
Source Code&lt; - 不确定要放在这里的代码,所以我把源代码(第81行的相关方法)
答案 0 :(得分:1)
返回Main方法时它为空的原因是此方法包含一个在ResetBoard()中初始化的实例的单独实例。
我认为您可能想在Game类中使用单个Variables实例,并从那里访问它。
这看起来像是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace Battleships
{
class ReadWrite
{
public void GetUsername()
{
}
}
class Variables
{ // Variables for the game - win, lose, board slots, user names, highscores
public volatile Dictionary<string, string> BoardPieces = new Dictionary<string, string>(); // board slots like A1, D6 etc.
private string username;
private string settingsDirectory;
private string settingsFile;
public string _setDir
{
get
{
return settingsDirectory;
}
set
{
settingsDirectory = value;
}
}
public string _setFile
{
get
{
return settingsFile;
}
set
{
settingsFile = value;
}
}
public string _user
{
get
{
return username;
}
set
{
username = value;
}
}
}
class Game
{
private Variables vars = new Variables();
public Variables _vars
{
get
{
return vars;
}
set
{
vars = value;
}
}
public void DrawBoard() // Board should be 10x10 (1-10, A-J)
{ // should be A | B | C | D etc.
//Console.WriteLine(" | A | B | C | D | E | F | G | H ");
//Console.WriteLine("-----------------------------------");
//Console.WriteLine(" 1 | {0} | {1} | {2} | {3} | {4} | {5} | {6} | {7} ");
// test code:
for (int i = 0; i < 8; i++)
{
if (i == 0)
{
Console.WriteLine(" | A | B | C | D | E | F | G | H | I | J ");
Console.WriteLine("-------------------------------------------");
}
else
{ // {0} = 1-10, lines of the board itself
Console.WriteLine("I would've written lines 1-10");
//Console.WriteLine(" {0} | {1} | {2} | {3} | {4} | {5} | {6} | {7} | {8} ", i);
//Console.WriteLine("-------------------------------------------");
}
}
}
public void ResetBoard()
{
//if (vars.BoardPieces.Count == 0)
if (true)
{
for (int i = 1; i <= 10; i++) // letters A-J
{
for (int n = 1; n <= 10; n++)
{
Console.WriteLine("Processing n = {0}", n);
string KeyToAdd = ((char)(i + 64)).ToString() + n;
//Console.WriteLine("The character printed is {0}", (char)(i + 65));
vars.BoardPieces.Add(KeyToAdd, i.ToString());
// Console.WriteLine("Written {0} to BoardPieces[{1}]", i, KeyToAdd);
Console.WriteLine("The current value in BoardPieces[{0}] is {1}", KeyToAdd, vars.BoardPieces[KeyToAdd]);
}
}
}
}
static void Main(string[] args)
{
Game prog = new Game();
prog.DrawBoard();
prog.ResetBoard();
prog.DrawBoard();
foreach (KeyValuePair<string, string> keyvalue in prog._vars.BoardPieces)
{
Console.WriteLine("Key: {0}\t Value: {1}", keyvalue.Key, keyvalue.Value);
}
if (prog._vars.BoardPieces.Count == 0)
{
Console.WriteLine("No keys found!");
}
Console.ReadKey();
}
}
}