简体   繁体   English

如何更改二维数组中的值

[英]How to change the values in two dimension array

I am stuck with this two dimension array with Unity framework coding with c#.我坚持使用带有 Unity 框架编码的二维数组和 c#。 I am trying to create a Checker game with an Intelligent agent.我正在尝试使用智能代理创建 Checker 游戏。 So I trying to get all the pieces and passing to the Alpha Beta algorithm and get the maximum amount of Opponent side.所以我试图得到所有的部分并传递给 Alpha Beta 算法并获得最大数量的对手方。 But in order to do that I have to check each piece on my board.但为了做到这一点,我必须检查板上的每一块。 I am using 8by8 board on my game.我在我的游戏中使用 8by8 板。 So I used所以我用

Piece[,] pieces = new Pieces[8,8]; Piece[,]pieces = new Pieces[8,8];

code in order to store the pieces.代码以存储碎片。 Now I want to change one piece and retrieve the pieces.现在我想换一件并取回这些件。 I tried in several ways and experimented, But I could not found any to take this array and change one generic object and retrieve the list with the changes.我尝试了几种方法并进行了试验,但我找不到任何方法来获取此数组并更改一个通用对象并检索包含更改的列表。

Please help me to solve this.请帮我解决这个问题。 Thanks in advance.提前致谢。

I written down here about what I experimented.我在这里写下我的实验。 Any suggestions about this matter or did I do something wrong?....关于这件事的任何建议还是我做错了什么?....

internal static void TryMoveInImaginaryWay(Piece[,] piece)
{
    int x = 0;
    int y =2;
    int x1 = 1;
    int y1 = 3;

    Moves move = new Moves();
    move.X = x;
    move.Y = y;
    move.X1 = x1;
    move.Y1 = y1;

    // Copping the pieces state
    Piece p = piece[x, y];

    //I am creating a list with those pieces
    List<Piece> temppiecelist = new List<Piece>();

    foreach (Piece pi in piece)
    {
        if (pi == p)
        {
            piece[x1, y1] = p;
            temppiecelist.Add(null);
        }
        else
        {
            temppiecelist.Add(pi);
        }
    }

    Piece[,] piek = temppiecelist; // can't convert it says Cannot Implicitly convert type "System.Collection.Generic.List<Piece>' to Piece[*,*]'"

    // I know i can't convert like this, but any ideas.
    //Piece[,] piek = new Piece[8, 8];

I am asking about , Is there any way to change above Piece[,] array list values.我在问,有什么办法可以改变上面的 Piece[,] 数组列表值。 And I want the same format as output.我想要与输出相同的格式。

It looks like what you are trying to do is make a copy of the board and then make a move on that board.看起来您正在尝试做的是复制板,然后在该板上移动。

Why on you would get a List involved I cannot figure out;为什么你会涉及到一个列表,我想不通; what led you to believe that a List was the right solution?是什么让您相信 List 是正确的解决方案? I am interested to know;我有兴趣知道; by learning why people come to bad conclusions about program writing, I can help them write better programs.通过了解人们为什么会对程序编写得出错误的结论,我可以帮助他们编写更好的程序。

To make a mutated copy of the board, just copy the board and mutate the copy :要制作板的变异副本,只需复制板变异副本

internal static void TryMoveInImaginaryWay(Piece[,] original)
{
    int x0 = 0;
    int y0 = 2;
    int x1 = 1;
    int y1 = 3;
    Piece[,] copy = (Piece[,])original.Clone();
    copy[x1,y1] = copy[x0,y0];
    copy[x0,y0] = null;

And you're done.你已经完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM