Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 制作垄断游戏时我的阵列出现问题_C#_Arrays_String_Multidimensional Array - Fatal编程技术网

C# 制作垄断游戏时我的阵列出现问题

C# 制作垄断游戏时我的阵列出现问题,c#,arrays,string,multidimensional-array,C#,Arrays,String,Multidimensional Array,我试图创建一个数组来存储垄断板上所有可用的位置,但在创建数组时,我得到的错误是 无法隐式转换类型“(string,string,string,string,string,string)” 串 此语法: ("Go", "+200", "CurrentPlayer", "", ""); 实际上是创建字符串的元组,而不是字符串数组 修复方法是重写代码以利用C#的面向对象特性。我建议您通过创建一个类来保存相关字段,从而简化代码: public interface IMonopolySquare {

我试图创建一个数组来存储垄断板上所有可用的位置,但在创建数组时,我得到的错误是

无法隐式转换类型“(string,string,string,string,string,string)” 串

此语法:

("Go", "+200", "CurrentPlayer", "", "");
实际上是创建字符串的元组,而不是字符串数组

修复方法是重写代码以利用C#的面向对象特性。我建议您通过创建一个类来保存相关字段,从而简化代码:

public interface IMonopolySquare
{
     public string Name { get; }
     public void PlayerLandsOnEvent(Player player);
     public void PlayerPassesSquareEvent(Player player);
     public void SetOwner(Player player);
}

public class GoSquare : IMonopolySquare
{
     public string Name { get => "Go" }

     public void PlayerLandsOnEvent(Player player)
     {
          // Do nothing - player has to pass to receive £200.
     }

     public void PlayerPassesSquareEvent(Player player)
     {
         player.AddMoney(200);
     }

    public void SetOwner(Player player)
    {
        throw new Exception ("You can't buy go!!");
    }
}

public class PropertySquare : IMonopolySquare
{
    private Player _owner = null;
    private int _rentWithoutHouse;
    private Color _color;

    public PropertySquare(
        string name,
        int rentWithoutHouse,
        Color color)
    {
        Name = name;
        _rentWithoutHouse = rentWithoutHouse;
        _color = color;
    }

    public string Name {get;}
     public void PlayerLandsOnEvent(Player player)
     {
         if (_owner != null && _owner != player)
         {
             player.SubtractMoney(_rentWithoutHouse); 
         }
     }

     public void PlayerPassesSquareEvent(Player player)
     {
         // Do nothing.
     }

    public void SetOwner(Player player)
    {
        if (owner != null)
        {
            throw new Exception("Can't buy something that's already been bought!");
        }
        else
        {
            _owner = player;
        }
    }
}

// the Player class is left as an exercise for the reader...
然后你的“董事会”就变得简单多了:

var board = new IMonopolySquare[] {
    new GoSquare(),
    new PropertySquare("Old Kent Road", "2", Color.Brown),
    // etc.
}
此语法:

("Go", "+200", "CurrentPlayer", "", "");
实际上是创建字符串的元组,而不是字符串数组

修复方法是重写代码以利用C#的面向对象特性。我建议您通过创建一个类来保存相关字段,从而简化代码:

public interface IMonopolySquare
{
     public string Name { get; }
     public void PlayerLandsOnEvent(Player player);
     public void PlayerPassesSquareEvent(Player player);
     public void SetOwner(Player player);
}

public class GoSquare : IMonopolySquare
{
     public string Name { get => "Go" }

     public void PlayerLandsOnEvent(Player player)
     {
          // Do nothing - player has to pass to receive £200.
     }

     public void PlayerPassesSquareEvent(Player player)
     {
         player.AddMoney(200);
     }

    public void SetOwner(Player player)
    {
        throw new Exception ("You can't buy go!!");
    }
}

public class PropertySquare : IMonopolySquare
{
    private Player _owner = null;
    private int _rentWithoutHouse;
    private Color _color;

    public PropertySquare(
        string name,
        int rentWithoutHouse,
        Color color)
    {
        Name = name;
        _rentWithoutHouse = rentWithoutHouse;
        _color = color;
    }

    public string Name {get;}
     public void PlayerLandsOnEvent(Player player)
     {
         if (_owner != null && _owner != player)
         {
             player.SubtractMoney(_rentWithoutHouse); 
         }
     }

     public void PlayerPassesSquareEvent(Player player)
     {
         // Do nothing.
     }

    public void SetOwner(Player player)
    {
        if (owner != null)
        {
            throw new Exception("Can't buy something that's already been bought!");
        }
        else
        {
            _owner = player;
        }
    }
}

// the Player class is left as an exercise for the reader...
然后你的“董事会”就变得简单多了:

var board = new IMonopolySquare[] {
    new GoSquare(),
    new PropertySquare("Old Kent Road", "2", Color.Brown),
    // etc.
}

(“Go”、“+200”、“CurrentPlayer”、”、”)
是由四个字符串组成的值元组
BoardPos[0,0,0,0,0]
是多维数组中的单个字符串。
(“Go”、“+200”、“CurrentPlayer”、“,”)
是由四个字符串组成的值元组
BoardPos[0,0,0,0,0]
是多维数组中的单个字符串。不应该
GoSquare
实现
IMonopolySquare
?太棒了!非常感谢您不要
GoSquare
实现
IMonopolySquare
?太棒了!非常感谢你