C#Winforms Connect Four:如何检查列是否已满?

时间:2016-02-23 20:23:44

标签: c# multidimensional-array

所以我是C#的初学者,我一直在使用'csharpdemos'的Tic Tac Toe教程尝试开发Connect 4游戏。截至目前,除了一个问题外,还可以玩游戏。我有一个int movemade,它跟踪用户所做的动作,因此它可以确定接下来是谁。但是,当填充列时,虽然没有设置任何值,但当用户单击该列时,仍然会使movemade增加1。这意味着转弯将不同步。我将在下面显示我的代码。

public class Board
{
    public int movesMade = 0;


    private Holder[,] holders = new Holder[5, 5];

    public const int CT1 = 0;
    public const int CT2 = 1;
    public const int B = 2;

    public void startBoard()
    {
        for (int x = 0; x < 5; x++)
        {
            for (int y = 0; y < 5; y++)
            {

                holders[x, y] = new Holder();
                holders[x, y].setValue(B);
                holders[x, y].setLocation(new Point(x, y));

            }
        }
    }

    public void detectHit(Point location)
    {
        int x = 0;
        int y = 0;

        if ((location.X < 100) )
        {
            x = 0;
            y = 4;

            if (holders[0, 4].getValue() == CT1 || holders[0, 4].getValue() == CT2)
                y = 3;
            if (holders[0, 3].getValue() == CT1 || holders[0, 3].getValue() == CT2)
                y = 2;
            if (holders[0, 2].getValue() == CT1 || holders[0, 2].getValue() == CT2)
                y = 1;
            if (holders[0, 1].getValue() == CT1 || holders[0, 1].getValue() == CT2)
                y = 0;









        }
        else if (location.X > 100 && location.X < 200)
        {
            x = 1;
            y = 4;

            if (holders[1, 4].getValue() == CT1 || holders[1, 4].getValue() == CT2)
                y = 3;
            if (holders[1, 3].getValue() == CT1 || holders[1, 3].getValue() == CT2)
                y = 2;
            if (holders[1, 2].getValue() == CT1 || holders[1, 2].getValue() == CT2)
                y = 1;
            if (holders[1, 1].getValue() == CT1 || holders[1, 1].getValue() == CT2)
                y = 0;



        }
        else if (location.X > 200 && location.X < 300 )
        {
            x = 2;
            y = 4;

            if (holders[2, 4].getValue() == CT1 || holders[2, 4].getValue() == CT2)
                y = 3;
            if (holders[2, 3].getValue() == CT1 || holders[2, 3].getValue() == CT2)
                y = 2;
            if (holders[2, 2].getValue() == CT1 || holders[2, 2].getValue() == CT2)
                y = 1;
            if (holders[2, 1].getValue() == CT1 || holders[2, 1].getValue() == CT2)
                y = 0;



        }
        else if (location.X > 300 && location.X < 400)
        {
            x = 3;
            y = 4;

            if (holders[3, 4].getValue() == CT1 || holders[3, 4].getValue() == CT2)
                y = 3;
            if (holders[3, 3].getValue() == CT1 || holders[3, 3].getValue() == CT2)
                y = 2;
            if (holders[3, 2].getValue() == CT1 || holders[3, 2].getValue() == CT2)
                y = 1;
            if (holders[3, 1].getValue() == CT1 || holders[3, 1].getValue() == CT2)
                y = 0;



        }
        else if (location.X > 400 )
        {
            x = 4;
            y = 4;

                if (holders[4, 4].getValue() == CT1 || holders[4, 4].getValue() == CT2)
                    y = 3;
                if (holders[4, 3].getValue() == CT1 || holders[4, 3].getValue() == CT2)
                    y = 2;
                if (holders[4, 2].getValue() == CT1 || holders[4, 2].getValue() == CT2)
                    y = 1;
                if (holders[4, 1].getValue() == CT1 || holders[4, 1].getValue() == CT2)
                    y = 0;






        }


        movesMade++;


        if (holders[x, 0].getValue() == CT2)
        { 
            movesMade = 1;
            movesMade++;
        }

        if (holders[x, 0].getValue() == CT1)
        {
            movesMade = 0;
            movesMade++;
        }




        if (movesMade % 2 == 0)
        {


            if (holders[x, 0].getValue() == 2)
            {
                Drawing.drawCt1(new Point(x, y));
                holders[x, y].setValue(CT1);
                Drawing.drawCt2(new Point(x, y));
                holders[x, y].setValue(CT2);        
            }
            if (holders[0,y].getValue() == 1 || holders[0,y].getValue() == 0)




            if (detectColumn())
            {
                MessageBox.Show("WINNER!");
            }
            if (detectRow())
            {
                MessageBox.Show("WINNER!");
            }
            if (detectDiagonal())
            {
                MessageBox.Show("WINNER!");
            }
        }
        else
        {


                if (holders[x, 0].getValue() == 2)
                {

                    Drawing.drawCt2(new Point(x, y));
                    holders[x, y].setValue(CT2);
                    Drawing.drawCt1(new Point(x, y));
                    holders[x, y].setValue(CT1);


                }





                if (detectColumn())
                {
                    MessageBox.Show("WINNER!");
                }
                if (detectRow())
                {
                    MessageBox.Show("WINNER!");
                }
                if (detectDiagonal())
                {
                    MessageBox.Show("WINNER!");
                }
        }


    }

    public bool detectColumn()
    {
        bool isWon = false;

        for (int x = 0; x < 5; x++)
        {

                    if ((holders[x, 0].getValue() == CT1 && holders[x, 1].getValue() == CT1 && holders[x, 2].getValue() == CT1 && holders[x, 3].getValue() == CT1) || (holders[x, 1].getValue() == CT1 && holders[x, 2].getValue() == CT1 && holders[x, 3].getValue() == CT1 && holders[x, 4].getValue() == CT1) || (holders[x, 0].getValue() == CT2 && holders[x, 1].getValue() == CT2 && holders[x, 2].getValue() == CT2 && holders[x, 3].getValue() == CT2) || (holders[x, 1].getValue() == CT2 && holders[x, 2].getValue() == CT2 && holders[x, 3].getValue() == CT2 && holders[x, 4].getValue() == CT2))
                    {
                        return true;
                    }


        }
        return isWon;
    }

    public bool detectRow()
    {
        bool isWon = false;

        for (int y = 0; y < 5; y++)
        {
            switch (y)
            {
                case 0:
                    if ((holders[0, y].getValue() == CT1 && holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1) || (holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1 && holders[4, y].getValue() == CT1) || (holders[0, y].getValue() == CT2 && holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2) || (holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2 && holders[4, y].getValue() == CT2))
                    {
                        return true;
                    }
                    break;
                case 1:
                    if ((holders[0, y].getValue() == CT1 && holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1) || (holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1 && holders[4, y].getValue() == CT1) || (holders[0, y].getValue() == CT2 && holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2) || (holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2 && holders[4, y].getValue() == CT2))
                    {
                        return true;
                    }
                    break;
                case 2:
                    if ((holders[0, y].getValue() == CT1 && holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1) || (holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1 && holders[4, y].getValue() == CT1) || (holders[0, y].getValue() == CT2 && holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2) || (holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2 && holders[4, y].getValue() == CT2))
                    {
                        return true;
                    }
                    break;
                case 3:
                    if ((holders[0, y].getValue() == CT1 && holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1) || (holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1 && holders[4, y].getValue() == CT1) || (holders[0, y].getValue() == CT2 && holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2) || (holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2 && holders[4, y].getValue() == CT2))
                    {
                        return true;
                    }
                    break;
                case 4:
                    if ((holders[0, y].getValue() == CT1 && holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1) || (holders[1, y].getValue() == CT1 && holders[2, y].getValue() == CT1 && holders[3, y].getValue() == CT1 && holders[4, y].getValue() == CT1) || (holders[0, y].getValue() == CT2 && holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2) || (holders[1, y].getValue() == CT2 && holders[2, y].getValue() == CT2 && holders[3, y].getValue() == CT2 && holders[4, y].getValue() == CT2))
                    {
                        return true;
                    }
                    break;




            }


        }
        return isWon;
    }

    public bool detectDiagonal()
    {
        bool isWon = false;

        //If statements for checking if counters are in diagonal.There are only 8 total possible diagonal wins therefore 8 if statments

        //Diagonal from [0,0] to [3,3] for each counter
        if ((holders[0, 0].getValue() == CT1 && holders[1, 1].getValue() == CT1 && holders[2, 2].getValue() == CT1 && holders[3, 3].getValue() == CT1) || (holders[0, 0].getValue() == CT2 && holders[1, 1].getValue() == CT2 && holders[2, 2].getValue() == CT2 && holders[3, 3].getValue() == CT2))
        {
            isWon = true;
        }
        //Diagonal from [0,1] to [3,4] for each counter
        if ((holders[0, 1].getValue() == CT1 && holders[1, 2].getValue() == CT1 && holders[2, 3].getValue() == CT1 && holders[3, 4].getValue() == CT1) || (holders[0, 1].getValue() == CT2 && holders[1, 2].getValue() == CT2 && holders[2, 3].getValue() == CT2 && holders[3, 4].getValue() == CT2))
        {
            isWon = true;
        }

//Diagonal from [0, 3] to [3, 0] for each counter
        else if ((holders[0, 3].getValue() == CT1 && holders[1, 2].getValue() == CT1 && holders[2, 1].getValue() == CT1 && holders[3, 0].getValue() == CT1) || (holders[0, 3].getValue() == CT2 && holders[1, 2].getValue() == CT2 && holders[2, 1].getValue() == CT2 && holders[3, 0].getValue() == CT2))
        {
            isWon = true;
        }
        //Diagonal from [0, 4] to [3, 1] for each counter     
        else if ((holders[0, 4].getValue() == CT1 && holders[1, 3].getValue() == CT1 && holders[2, 2].getValue() == CT1 && holders[3, 1].getValue() == CT1) || (holders[0, 4].getValue() == CT2 && holders[1, 3].getValue() == CT2 && holders[2, 2].getValue() == CT2 && holders[3, 1].getValue() == CT2))
        {
            isWon = true;
        }
        //Diagonal from [1,0] to [4,3] for each counter
        if ((holders[1, 0].getValue() == CT1 && holders[2, 1].getValue() == CT1 && holders[3, 2].getValue() == CT1 && holders[4, 3].getValue() == CT1) || (holders[1, 0].getValue() == CT2 && holders[2, 1].getValue() == CT2 && holders[3, 2].getValue() == CT2 && holders[4, 3].getValue() == CT2))
        {
            isWon = true;
        }
        //Diagonal from [1,1] to [4,4] for each counter
        else if ((holders[1, 1].getValue() == CT1 && holders[2, 2].getValue() == CT1 && holders[3, 3].getValue() == CT1 && holders[4, 4].getValue() == CT1) || (holders[1, 1].getValue() == CT2 && holders[2, 2].getValue() == CT2 && holders[3, 3].getValue() == CT2 && holders[4, 4].getValue() == CT2))
        {
            isWon = true;
        }
        //Diagonal from [1, 3] to [4, 0] for each counter
        else if ((holders[1, 3].getValue() == CT1 && holders[2, 2].getValue() == CT1 && holders[3, 1].getValue() == CT1 && holders[4, 0].getValue() == CT1) || (holders[1, 3].getValue() == CT2 && holders[2, 2].getValue() == CT2 && holders[3, 1].getValue() == CT2 && holders[4, 0].getValue() == CT2))
        {
            isWon = true;
        }
        //Diagonal from [1, 4] to [4, 1] for each counter     
        else if ((holders[1, 4].getValue() == CT1 && holders[2, 3].getValue() == CT1 && holders[3, 2].getValue() == CT1 && holders[4, 1].getValue() == CT1) || (holders[1, 4].getValue() == CT2 && holders[2, 3].getValue() == CT2 && holders[3, 2].getValue() == CT2 && holders[4, 1].getValue() == CT2))
        {
            isWon = true;
        }
        return isWon;

    }












}

class Holder
{
    private Point location;

    private int value = Board.B;

    public void setLocation(Point p)
    {
        location = p;
    }
    public Point getLocation()
    {
        return location;
    }

    public int getValue()
    {
        return value;
    }
    public void setValue(int i)
    {
        value = i;
    }




}

}

希望有人可以提供帮助!

1 个答案:

答案 0 :(得分:0)

除非移动是有效的,否则您不应更新您的movesMade变量。我会将移动本身的验证封装为一个独立的游戏逻辑,只有在有效时才更新moveMade。列的状态将决定移动是否有效。对于数据模型,每个元素具有3个可能值的2维数组可以正常工作。然后,如果每个列都已满,移动是否有效等,那么推断它是微不足道的。

我还简单地使用布尔值(例如,playerOneActive)来存储其移动,在2人游戏中,例如玩家1 =真,玩家2 =假。

您的detectHit方法中还有很多潜在的代码重复,可以通过另外一种方法来改进if-else-if结构的每个方案。