首页 > 解决方案 > 将空的锯齿状数组与 3x3 锯齿状数组进行比较,以匹配不匹配但行匹配的数字序列

问题描述

我正在制作井字游戏。我正在使用两个锯齿状数组来确定获胜者。一个数组只是空的,并在 switch 语句中分配了网格编号。另一个拥有所有可能的组合(可能不是最好的方法。)

int[][] sequence = //Player fills this up with grid selection, like 1,2 3
        {
            new int[] {0,0,0}, //players selects [0][0] put a 1 here etc.
            new int[] {0,0,0},
            new int[] {0,0,0},

        };

这是另一个包含所有可能获胜组合的锯齿状阵列。

int[][] getsequence = 
     {
            new int[] {1,2,3},
            new int[] {4,5,6},
            new int[] {7,8,9},
            new int[] {1,4,7},
            new int[] {2,5,8},
            new int[] {3,6,9},
            new int[] {1,5,9},
            new int[] {9,5,1},
            new int[] {3,2,1},
            new int[] {6,5,4},
            new int[] {9,8,7},
            new int[] {7,4,1},
            new int[] {8,5,2},
            new int[] {9,6,3},
            new int[] {3,5,7},
            new int[] {7,5,3}

这是查看是否存在匹配的算法。

public static void checkWinner(Player p)
    {

        foreach (int[] row in p.Sequence)
        {
            foreach (int[] row2 in p.GetSequence)
            {
                if (Enumerable.SequenceEqual(row, row2))
                {
                    Console.WriteLine("Win");
                }
            }
        }

    }

这是 switch 语句有点控制这些东西。

public static string[,] updateGameBoard(string[,] matrix, int selection, Player p)
    {
        switch (selection)
        {
            case 1:
                matrix[0, 0] = p.Type;
                p.Sequence[0][0] = 1;
                break;
            case 2:
                matrix[0, 1] = p.Type;
                p.Sequence[0][1] = 2;
                break;
            case 3:
                matrix[0, 2] = p.Type;
                p.Sequence[0][2] = 3;
                break;
            case 4:
                matrix[1, 0] = p.Type;
                p.Sequence[1][0] = 4;
                break;
            case 5:
                matrix[1, 1] = p.Type;
                p.Sequence[1][1] = 5;
                break;
            case 6:
                matrix[1, 2] = p.Type;
                p.Sequence[1][2] = 6;
                break;
            case 7:
                matrix[2, 0] = p.Type;
                p.Sequence[2][0] = 7;
                break;
            case 8:
                matrix[2, 1] = p.Type;
                p.Sequence[2][1] = 8;
                break;
            case 9:
                matrix[2, 2] = p.Type;
                p.Sequence[2][2] = 9;
                break;
            default:
                Console.WriteLine("Error");
                break;

        }

        Console.WriteLine();
        return matrix;
    }

矩阵二维数组只是我将数字转换为“O”或“X”的一种方式,这就是p.Type正在做的事情。p是玩家对象。

string[,] matrix =
        {
            {"1","2","3" },
            {"4","5","6" },
            {"7","8","9" }
        };

无论如何,它似乎几乎可以工作了。123、456 等行似乎有效,玩家赢得了比赛。但是,例如,如果我将所有“O”都放在 147 中,则不会发生任何事情。我调试了它,foreach 循环中的行似乎是问题所在。我会说,当我调试时,p.GetSequence(row2) 将每个数组显示为 123、456、789 等p.Sequence。foreach 循环中的 (row) 显示 100,然后在下一个顶部迭代中显示 400,然后是 700。所以这很有意义我认为行有效,但列无效。因为他们不匹配。无论如何,这是我的理论。如何将列作为行进行比较?

最后,我不知道这是否重要,但我有一个确实有效的版本(排序)。Sequence并且getSequence只是全局的,而不是在播放器对象中。问题是它会说玩家最终获胜,这不是按顺序排列的。再加上每个玩家都需要自己的锯齿状数组,否则你将如何将 X 与 O 分开。

是的,我认为它,我需要将列转换为一行。就像我在调试 getSequence 时所说的,它将值显示为行,序列将值显示为列。当我将值作为列时,但如果我输入 123 它是行。我看到有关如何执行此操作的文章。但是,如果有人有简单的方法,请告诉我。

标签: c#

解决方案


我想通了,可能不是最好的算法,但它有效。即使找到匹配,内部循环仍然滚动 16 次,所以我只使用了一个布尔值。

if (player1.Turn > 2)
        {
            foreach (int[] row in player1.Sequence)
            {
                foreach (int[] row2 in player1.GetSequence)
                {
                    foreach (int[] col in player1.TrackCol)
                    {
                        if (Enumerable.SequenceEqual(row, row2))
                        {
                            winner = true;
                        }
                        else if(Enumerable.SequenceEqual(col, row2))
                        {
                            winner = true; 

                        }
                    }
                }
            }
        }

这是 switch 语句:

public static string[,] updateGameBoard(string[,] matrix, int selection, Player p)
    {
        switch (selection)
        {
            case 1:
                matrix[0, 0] = p.Type;
                p.Sequence[0][0] = 1;
                p.TrackCol[0][0] = 1;
                break;
            case 2:
                matrix[0, 1] = p.Type;
                p.Sequence[0][1] = 2;
                p.TrackCol[0][0] = 2;
                break;
            case 3:
                matrix[0, 2] = p.Type;
                p.Sequence[0][2] = 3;
                p.TrackCol[0][0] = 3; 
                break;
            case 4:
                matrix[1, 0] = p.Type;
                p.Sequence[1][0] = 4;
                p.TrackCol[0][1] = 4;
                break;
            case 5:
                matrix[1, 1] = p.Type;
                p.Sequence[1][1] = 5;
                p.TrackCol[0][1] = 5; 
                break;
            case 6:
                matrix[1, 2] = p.Type;
                p.Sequence[1][2] = 6;
                p.TrackCol[0][1] = 6; 
                break;
            case 7:
                matrix[2, 0] = p.Type;
                p.Sequence[2][0] = 7;
                p.TrackCol[0][2] = 7;
                break;
            case 8:
                matrix[2, 1] = p.Type;
                p.Sequence[2][1] = 8;
                p.TrackCol[0][2] = 8; 
                break;
            case 9:
                matrix[2, 2] = p.Type;
                p.Sequence[2][2] = 9;
                p.TrackCol[0][2] = 9; 
                break;
            default:
                Console.WriteLine("Error");
                break;

推荐阅读