2013-12-07 113 views
-1

我需要为我的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)字典添加不保存

回答

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(); 
     } 
    } 
} 
+0

可以得到设定的目标?好棒。 – Ilan321

+1

绝对:)属性(get-set)仅提供对字段的访问权限,而不管它是基元还是“引用类型”对象。 –