首页 > 解决方案 > 我正在尝试建立一个国际象棋游戏并且瓷砖出现在错误的位置

问题描述

我正在尝试在控制台应用程序中创建一个国际象棋游戏,现在我正在构建棋盘。瓷砖几乎出现在顶行应该去的正确位置:

白色 黑色 白色 黑色 白色 黑色 白色 黑色

但是我正在创建的电路板的瓷砖是

白色 白色 黑色 白色 黑色 白色 黑色 黑色

这是我的代码对我需要的有用:

public static List<space> GetList(List<space> tiles)
    {
        //simple variable to use where ever i need it
        int k = 0;

        //arrays ment to keep track of the valuse of the x position and the y positions
        int[] minX = new int[8];
        int[] minY = new int[8];
        int[] maxX = new int[8];
        int[] maxY = new int[8];
        int[] minXPosition = new int[64];
        int[] minYPosition = new int[64];
        int[] maxXPosition = new int[64];
        int[] maxYPosition = new int[64];

        //holds the values for the space
        string[] spaceLetter = new string[8];
        string[] space = new string[64];
        int[] spaceNumber = new int[8];

        //holds values for the values of is white
        bool okToSwirtch = false;
        bool[] isWhiteSwitched = new bool[8];
        bool[] isWhiteNotSwitched = new bool[8];
        bool[] isWhite = new bool[64];

        //sets minx at index 0 inorder to get the other values
        minX[0] = 2;
        k = 5;

        //leeps through all the indexes in minx and sets the value of minx at index i to the value of minx at index i - 1 increased by k
        for (int i = 1; i < minX.Length; i++)
        {
            minX[i] = minX[i - 1] + k;
        }

        minY[0] = 2;
        k = 3;

        //loops through all of the indexes of miny and sets the value at miny at index i the the value of miny at index i - 1 increased by k
        for (int i = 1; i < minY.Length; i++)
        {
            minY[i] = minY[i - 1] + k;
        }

        maxX[0] = minX[0] + 4;
        k = 5;

        //loops through all the indexes of maxx and sets the value of max x at index i to the value of max x at index i - 1 increased by k
        for (int i = 1; i < maxX.Length; i++)
        {
            maxX[i] = maxX[i - 1] + k;
        }

        maxY[0] = minY[0] + 2;
        k = 3;

        //loops through all the indexes of max y and sets the value of max y at index i to max y at index i - 1 increased by k
        for (int i = 1; i < maxY.Length; i++)
        {
            maxY[i] = maxY[i - 1] + k;
        }

        k = 0;

        //loops through all of the min x position indexes, and sets min x position at index i the the value of min x at index k
        for (int i = 0; i < minXPosition.Length; i++)
        {
            minXPosition[i] = minX[k];

            //checks if i is not zero and if it can be divided evenly by 8
            if ((i != 0) && (i % 8 == 0))
            {
                k++;
            }
        }

        k = 0;

        //loops through all the indexes in max x position, and sets the value of max x position at index i to the value of max x at index k
        for (int i = 0; i < maxXPosition.Length; i++)
        {
            maxXPosition[i] = maxX[k];

            //checks if i is not 0 and if it's divisible by 8
            if ((i != 0) && (i % 8 == 0))
            {
                k++;
            }
        }

        k = 0;

        //leeps through by all of the min y position indexes and sets the value of min y position at index i to the value of min y at index k
        for (int i = 0; i < minYPosition.Length; i++)
        {
            minYPosition[i] = minY[k];

            k++;

            //checks if k is 8 and resets it back to 0
            if (k == 8)
            {
                k = 0;
            }
        }

        k = 0;

        //same for max y position
        for (int i = 0; i < maxYPosition.Length; i++)
        {
            maxYPosition[i] = maxY[k];

            k++;

            //same for max y position
            if (k == 8)
            {
                k = 0;
            }
        }

        k = 0;

        //sets all of the values of space letter to the values i need it
        spaceLetter[0] = "A";
        spaceLetter[1] = "B";
        spaceLetter[2] = "C";
        spaceLetter[3] = "D";
        spaceLetter[4] = "E";
        spaceLetter[5] = "F";
        spaceLetter[6] = "G";
        spaceLetter[7] = "H";

        //sets all the values of space number to the numbers 1-8
        for (int i = 0; i < spaceNumber.Length; i++)
        {
            spaceNumber[i] = i + 1;
        }

        //sets the value of space at index k to the value of space letter at index x and the value of space number at index y
        for (int x = 0; x < spaceLetter.Length; x++)
        {
            for (int y = 0; y < spaceNumber.Length; y++)
            {
                space[k] = spaceLetter[x] + spaceNumber[y];

                k++;
            }
        }

        k = 0;

        //sets all of the values for is white not switched
        isWhiteNotSwitched[0] = true;
        isWhiteNotSwitched[1] = false;
        isWhiteNotSwitched[2] = true;
        isWhiteNotSwitched[3] = false;
        isWhiteNotSwitched[4] = true;
        isWhiteNotSwitched[5] = false;
        isWhiteNotSwitched[6] = true;
        isWhiteNotSwitched[7] = false;

        //sets all the values for is white switched
        isWhiteSwitched[0] = false;
        isWhiteSwitched[1] = true;
        isWhiteSwitched[2] = false;
        isWhiteSwitched[3] = true;
        isWhiteSwitched[4] = false;
        isWhiteSwitched[5] = true;
        isWhiteSwitched[6] = false;
        isWhiteSwitched[7] = true;

        //loops through all of the indexes of is white
        for (int i = 0; i < isWhite.Length; i++)
        {
            //checks if ok to switch is false
            if (!okToSwirtch)
            {
                isWhite[i] = isWhiteNotSwitched[k];
            }
            else
            {
                isWhite[i] = isWhiteSwitched[k];
            }

            k++;

            //checks if k is 8
            if (k == 8)
            {
                //checks if ok to switch is false or true
                if (!okToSwirtch)
                {
                    okToSwirtch = true;
                }
                else
                {
                    okToSwirtch = false;
                }

                k = 0;
            }
        }

        //adds 64 elements to tiles with all of the stuff i created before
        for (int i = 0; i < 64; i++)
        {
            tiles.Add(new space { minXPosition = minXPosition[i], maxXPosition = maxXPosition[i], minYPosition = minYPosition[i], maxYPosition = maxYPosition[i], spaceValue = space[i], isWhite = isWhite[i] });
        }

        return tiles;
    }

有人可以帮我弄清楚如何将所有瓷砖向左移动一点,以便显示它应该如何

标签: c#

解决方案


错误在这里:

//loops through all of the min x position indexes, and sets min x position at index i the the value of min x at index k
for (var i = 0; i < minXPosition.Length; i++)
{
    minXPosition[i] = minX[k];

    //checks if i is not zero and if it can be divided evenly by 8
    if ((i != 0) && (i % 8 == 0))
    {
        k++;
    }
}

Wheni是 8 的倍数,它应该是下一行的第一项,但k在设置minXPosition.

如果您更改顺序:

//loops through all of the min x position indexes, and sets min x position at index i the the value of min x at index k
for (var i = 0; i < minXPosition.Length; i++)
{
    //checks if i is not zero and if it can be divided evenly by 8
    if ((i != 0) && (i % 8 == 0))
    {
        k++;
    }
    minXPosition[i] = minX[k];
}

那应该可以解决问题。


推荐阅读