2014-01-16 46 views
1

我想在C#中做壟斷,但是,我面臨一個未知的租金問題。該計劃應該將規定的租金添加到一個玩家,並從其他玩家中扣除,但這隻有時纔有效。其他時間,每個球員的金額保持不變。壟斷 - 租金沒有被轉移到其他玩家

if (type[roll] == "Land") 
{ 
    if (owned[roll] == "Unowned") 
    { 
     DialogResult dialogResult = MessageBox.Show("You have landed on " + name[roll] + ". This property costs $" + cost[roll] + ". Would you like to purchase it?", "Purchase Property?", MessageBoxButtons.YesNo); 
     if (dialogResult == DialogResult.Yes) 
     { 
      int deduct = Convert.ToInt32(cost[roll]); 
      if (player == true) 
      { 
       money1 -= deduct; 
       p1owned[roll] = "Owned"; 
      } 
      else if (player == false) 
      { 
       money2 -= deduct; 
       p2owned[roll] = "False"; 
      } 
      parking += deduct; 
      owned[roll] = "Owned"; 
     } 
    } 
    else 
    { 
     if (player == true && p2owned[roll] == "Owned") 
     { 
      money2 += Convert.ToInt32(rent[roll]); 
      money1 -= Convert.ToInt32(rent[roll]); 
     } 
     else if (player == false && p1owned[roll] == "Owned") 
     { 
      money1 += Convert.ToInt32(rent[roll]); 
      money2 -= Convert.ToInt32(rent[roll]); 
     } 
    } 
} 
+0

在這種情況下,變量播放器與什麼有關? – miguelarcilla

+3

在else塊的開始處設置一個斷點並進行調試。 – Seano666

+0

'player','money1','money2','roll'是什麼? –

回答

1

好像你正在檢查,看看哪些球員降落在屬性,然後從相應的帳戶中減去deduct量。如果這是正確的,那麼問題就在這裏:

if (player == true) 
{ 
    money1 -= deduct; 
    p1owned[roll] = "Owned"; 
} 
else if (player == false) 
{ 
    money2 -= deduct; 
    p2owned[roll] = "False"; 
} 

你會發現,p2owned [滾動]的狀態被設定爲「假」,而不是「擁有」。這就是爲什麼你應該將通用代碼分解爲方法:如果你決定改變一個實現細節,你只需要在一個地方改變它。

+0

我不同意最終的建議。更好的解決方案是使用枚舉,而不是字符串。那麼將不可能分配未處理的值。 –

+0

@BenVoigt:很好的建議。我不認爲我會這樣去解決這個問題,但是OP現在可以朝着正確的方向前進。 –

1

我不知道壟斷的確切規則,但我建議你看看面向對象編程。而不是將所有數據保存在數組中type,owned,cost,p1owned,p2owned等使用對象將相關數據組合在一起。例如。你可以創建類Property將持有物業的名稱,價格,租金和業主:

public class Property 
{ 
    public string Name { get; set; } 
    public int Price { get; set; } 
    public int Rent { get; set; } 
    public Player Owner { get; set; } 
    public bool IsOwned { get { return Owner != null; } } 
} 

但OOP不只是將相關的數據一起 - 你可以(也應該)把行爲與在同一個對象的數據。這裏是Player類,認爲他擁有的財產的樣品,並管理購買和支付租金:

public class Player 
{ 
    public string Name { get; set; } 
    public int Money { get; set; }   

    public void Buy(Property property) 
    { 
     // TODO: Handle (property.Price > Money) case 
     Money -= property.Price; 
     property.Owner = this;    
    } 

    public void PayRent(Property property) 
    { 
     // TODO: Handle (property.Rent > Money) case 
     Money -= property.Rent; 
     property.Owner.Money += property.Rent; 
    } 
} 

現在上面所有的代碼可以寫在更易讀的方式:

// get rolled property 
var property = properties[rolled]; 

if (property.IsOwned) 
{ 
    if (currentPlayer == property.Owner) 
     return; 

    currentPlayer.PayRent(property); 
    return; 
} 

if (!PlayerWantsToPurchase(property)) // definition of method is below 
    return; 

currentPlayer.Buy(property); 

是不是這段代碼看起來幾乎像純英文文本一樣?

private bool PlayerWantsToPurchase(Property property) 
{ 
    var message = String.Format("You have landed on {0}. This property costs ${1}. Would you like to purchase it?", 
           property.Name, property.Price); 

    var result = MessageBox.Show(message, "Purchase Property?", MessageBoxButtons.YesNo); 
    return result == DialogResult.Yes; 
} 
相關問題