2016-01-16 27 views
0

我嘗試編寫一個簡單的控制檯應用程序與河內塔。但我陷入了一個困境。處理INT到列表的名稱

在我的程序中,我要求一個人寫信給哪個塔想放置磁盤,但是:我有3個列表作爲塔,我會向玩家索要一個數字,現在我該如何構建一個「比較方法」?因爲我不希望複製的同一段代碼6倍......

class Towers : Disks 
{ 
    //public Towers(int Value, int WhereItIs) : base(Value, WhereItIs) { } 

    public List<Disks> Tower1 = new List<Disks>(); 
    public List<Disks> Tower2 = new List<Disks>(); 
    public List<Disks> Tower3 = new List<Disks>(); 

    public void Compare(int dyskFrom, int dyskWhere) { 
    } 

    public void Display() { 
     foreach(var i in Tower1){ 
      Console.WriteLine("Where: {0} | Value: {1}", i.WhereItIs, i.Value); 
     } 
    } 

    public void Build(int TowerHigh) { 
     for (int i = TowerHigh; i > 0; i--) { 
      Tower1.Add(new Disks {Value = i, WhereItIs = 1 }); 
     } 
    } 
} 

class Disks 
{ 
    public int Value; //wartosc krazka 
    public int WhereItIs; //na ktorej wiezy sie on znajduje 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     Towers Tower = new Towers(); 
     int TowerHigh; 
     Console.Write("Podaj wysokość wieży: "); 
     TowerHigh = int.Parse(Console.ReadLine()); 

     Tower.Build(TowerHigh); 
     Tower.Display(); 
     Tower.Compare(1, 2); 
    } 
} 

回答

0

它更容易創建的Stack<Disk>()數組。這樣你可以按索引選擇一個塔。

我會使用堆棧,因爲那通常是您需要的功能。

是這樣的:(在瀏覽器中鍵入的,沒有語法檢查)

class Disk 
{ 
    public int Size {get; set;} 
} 

static void Main(string[] args) 
{ 
    // create the Stack<Disk> array 
    Stack<Disk>[] towers = new Stack<Disk>[3]; 

    // create each tower 
    for(int i=0;i<towers.Length;i++) 
    { 
     towers[i] = new Stack<Disk>(); 

     // fill the towers. 
     for(int j=3;j>0;j--) 
      towers[i].Enqueue(new Disk { Size = j }); 
    } 

    bool isHoldingDisk = false; 

    while(true) 
    { 
     DisplayTowers(towers); 

     if(isHoldingDisk) 
      Console.WriteLine("On what tower do you want to place it?"); 
     else 
      Console.WriteLine("Choose a tower to pick a disk"); 

     var key = Console.ReadKey(true); 

     var chosenTowerIndex = 0; 

     switch(key) 
     { 
      case(Key.Escape): 
       break; 

      case(Key.D1): 
       chosenTowerIndex = 0; 
       break; 

      case(Key.D1): 
       chosenTowerIndex = 1; 
       break; 

      case(Key.D1): 
       chosenTowerIndex = 2; 
       break; 

      // etc... 
     } 

     if((chosenTowerIndex < 1) || (chosenTowerIndex >= towers.Length)) 
      continue; 

     // check here if you are holding a disk 

     if(isHoldingDisk) 
     { 
      // logic for testing if you can place the disk here... 
      // using towers[chosenTowerIndex] 

      // check if it is completed... 
      if(completed) 
       break; 

      isHoldingDisk = false; 
     } 
     else 
     { 
      // check if you can pickup a disk....(if there is any) 
      isHoldingDisk = true; 
     } 
    } 

    // final display... 
    DisplayTowers(towers); 

    Console.WriteLine("Thanks for playing"); 
} 

static void DisplayTowers(Stack<Disk>[] towers) 
{ 
    // draw some fancy asc-ii art... 
}