垄断 - 租金未转移给其他参与者

时间:2014-01-16 06:14:19

标签: c# .net

我正试图在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]);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您好像正在检查哪个玩家登陆该物业,然后从相应的帐户中减去deduct金额。如果那是对的,那么问题出在这里:

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

您会注意到p2owned [roll]的状态设置为“False”而不是“Owned”。这就是为什么你应该将公共代码分解为方法:如果你决定改变一个实现细节,你只需要在一个地方改变它。

答案 1 :(得分:1)

我不知道Monopoly的确切规则,但我建议你看看面向对象的编程。不是将所有数据保留在数组typeownedcostp1ownedp2owned等中,而是使用对象将相关数据组合在一起。例如。你可以创建一个类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;
}