我需要为我的C#类做一个游戏。我决定参加战列舰,相当简单的游戏。我有一个Dictionary<string, string>
,其中包含所有板插槽(A-J,1-10)的值(例如A5,F3,J6)。我有一个重置这些键的方法,但是首先检查它是否为空(我将在init中使用它来首先实际将键添加到字典中)。如果是,则在for
循环(一个用于字母,一个用于数字)内部使用for
循环来添加键(A1,A2,A3 ...,B1,B2,B3 ...等)。我添加了一些调试代码,显示密钥中的内容(我使用for
变量i
构建密钥的字符串)。它工作正常(例如:增加值 - >BoardPieces.Add(KeyToAdd, i.ToString());
,然后读取值 - >Console.WriteLine("Added key to {0} -> {1}", KeyToAdd, BoardPieces[KeyToAdd]);
。它的工作原理!然而,当程序返回该方法并返回到Main()
,当我尝试检查字典中的值时,它显示什么
Source Code < - 不确定要放什么代码在这里,所以我把源代码(相关法以线81)字典添加不保存
Q
字典添加不保存
-1
A
回答
1
,这是空当您返回到Main方法的原因是,该方法中包含一个单独的实例到在ResetBoard()中初始化的实例
我认为你可能想在你的游戏cl中使用一个变量实例屁股,并从那里访问它。
这看起来是这样的:
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();
}
}
}
相关问题
- 1. 添加字典
- 2. 使用字典来保存多项式系数;添加
- 3. 字将不会被添加到字典
- 4. 用if语句添加不存在的字典关键字(Python)
- 5. NSMutableArray/NSMutable字典不保存数据writeToFile
- 6. servicestack.redis不保存字典物业
- 7. 字典添加()例外锁定字典
- 8. C#在字典中添加字典
- 9. 如何添加字典中的字典
- 10. 添加到字典中的字典
- 11. 将字典添加到字典
- 12. 保存BMP到字典C#
- 13. python 3保存字典
- 14. 保存字典JSON文件
- 15. Coredata关系,保存字典
- 16. 添加-AzureAccount:给定的关键是不存在的字典
- 17. 如果条目不存在,将条目添加到字典中
- 18. Python字典添加值
- 19. 添加键defaultdict(字典)
- 20. 添加变量字典
- 21. 添加到字典中c#
- 22. Python:添加字典项目。
- 23. 将项添加字典
- 24. 如何添加字典值?
- 25. 在字典中添加值
- 26. 添加多个值字典
- 27. 添加全局字典
- 28. 添加类到字典
- 29. 如何添加字典?
- 30. “在Python中添加”字典?
可以得到设定的目标?好棒。 – Ilan321
绝对:)属性(get-set)仅提供对字段的访问权限,而不管它是基元还是“引用类型”对象。 –