C# 词典增补部分不包括';储蓄
我需要为我的C班做一个游戏。我决定选择战舰,这是一个相当简单的游戏。我有一个C# 词典增补部分不包括';储蓄,c#,dictionary,save,C#,Dictionary,Save,我需要为我的C班做一个游戏。我决定选择战舰,这是一个相当简单的游戏。我有一个字典,其中包含所有板插槽(a-J,1-10)的值(例如,A5、F3、J6)。我有一个重置这些键的方法,但是首先检查它是否为空(我将在init使用它,首先将键实际添加到字典中)。如果是,则在添加键(A1、A2、A3…、B1、B2、B3…等)的for循环(一个用于字母,一个用于数字)中使用for循环。我添加了一些显示键中内容的调试代码(我使用for变量I为键构造了一个字符串)。它工作正常(例如:Add value->Boar
字典
,其中包含所有板插槽(a-J,1-10)的值(例如,A5、F3、J6)。我有一个重置这些键的方法,但是首先检查它是否为空(我将在init使用它,首先将键实际添加到字典中)。如果是,则在添加键(A1、A2、A3…、B1、B2、B3…等)的for
循环(一个用于字母,一个用于数字)中使用for
循环。我添加了一些显示键中内容的调试代码(我使用for
变量I
为键构造了一个字符串)。它工作正常(例如:Add value->BoardPieces.Add(KeyToAdd,i.ToString());
然后读取value->Console.WriteLine(“Added key to{0}->{1}”,KeyToAdd,BoardPieces[KeyToAdd]);
,它工作正常!但是,当程序返回方法并返回到Main()
,当我尝试检查字典中的值时,它显示没有任何值。返回Main方法时它为空的原因是该方法包含一个单独的实例,该实例与在ResetBoard()中初始化的实例不同 我认为您可能希望在游戏类中有一个变量实例,并从那里访问它 这看起来像:
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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
Net系统;
使用System.Net.Sockets;
名称空间战舰
{
类读写
{
public void GetUsername()
{
}
}
类变量
{//游戏变量-赢、输、棋盘位置、用户名、高分
public volatile Dictionary BoardPieces=new Dictionary();//板槽,如A1、D6等。
私有字符串用户名;
私有字符串设置目录;
私有字符串设置文件;
公共字符串_setDir
{
得到
{
返回设置目录;
}
设置
{
设置目录=值;
}
}
公共字符串设置文件
{
得到
{
返回设置文件;
}
设置
{
设置文件=值;
}
}
公共字符串\u用户
{
得到
{
返回用户名;
}
设置
{
用户名=值;
}
}
}
班级游戏
{
私有变量vars=新变量();
公共变量
{
得到
{
返回变量;
}
设置
{
vars=值;
}
}
public void DrawBoard()//板应为10x10(1-10,A-J)
{//应该是A | B | C | D等。
//Console.WriteLine(“A | B | C | D | E | F | G | H”);
//Console.WriteLine(“-----------------------------------------”;
//Console.WriteLine(“1{0}{1}{2}{3}{4}{5}{6}{7}”);
//测试代码:
对于(int i=0;i<8;i++)
{
如果(i==0)
{
Console.WriteLine(“A | B | C | D | E | F | G | H | I | J”);
Console.WriteLine(“-----------------------------------------”;
}
其他的
{/{0}=1-10,线路板本身的线路
WriteLine(“我会写第1-10行”);
//Console.WriteLine(“{0}{1}{2}{3}{4}{5}{6}{7}{8}”,i);
//Console.WriteLine(“-----------------------------------------”;
}
}
}
公共教育委员会()
{
//if(vars.BoardPieces.Count==0)
如果(真)
{
对于(int i=1;i您可以获取集合对象吗?太棒了。绝对:)属性(get set)只提供对字段的访问,而不管它是基元对象还是“引用类型”对象。