예제 #1
0
파일: Logic.cs 프로젝트: Coldani3/Connect4
        /// <summary>
        /// The equivalent of dropping one of the dots into the frame
        /// </summary>
        /// <param name="index">1-7, the horizontal left to right coordinate of the hole to "drop" it into.</param>
        public void DropDot(int index)
        {
            if (index <= Columns && index >= 1)
            {
                Connect4Colour[] column = ConnectFourGrid[index - 1];
                int columnCount         = column.Count(x => x != null);

                if (columnCount < Rows)
                {
                    column[Utillity.Clamp(columnCount, 0, Rows)] = CurrPlayerColour;
                    //check if there are matches whenever a dot drops
                    LookForFourInARow(index - 1, columnCount);

                    if (!GameWon)
                    {
                        //swap colour
                        if (CurrPlayerColour == Connect4Colour.Red)
                        {
                            CurrPlayerColour = Connect4Colour.Yellow;
                        }
                        else
                        {
                            CurrPlayerColour = Connect4Colour.Red;
                        }
                    }
                }
            }
        }
예제 #2
0
파일: Logic.cs 프로젝트: Coldani3/Connect4
        private bool IsFourInARowForDir(int gridX, int gridY, int xChange, int yChange)
        {
            int matchCount = 0;

            int xUpperBound = Utillity.Clamp(gridX + (3 * xChange), 0, Columns - 1);
            int yUpperBound = Utillity.Clamp(gridY + (3 * yChange), 0, Rows - 1);
            //yes it's hacky
            //no i don't care, it works
            bool xRan    = false;
            bool yRan    = false;
            bool skipOne = false;

            for (int x = Utillity.Clamp(gridX - (3 * xChange), 0, Columns - 1); x <= xUpperBound && !xRan; x += xChange)
            {
                for (int y = Utillity.Clamp(gridY - (3 * yChange), 0, Rows - 1); y <= yUpperBound && !yRan; y += yChange)
                {
                    if (ConnectFourGrid[x][y] == CurrPlayerColour)
                    {
                        matchCount++;

                        if (matchCount >= 4)
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        matchCount = 0;
                    }

                    if (yChange == 0)
                    {
                        yRan    = true;
                        skipOne = true;
                    }
                }

                if (xChange == 0)
                {
                    xRan = true;
                }
                if (skipOne)
                {
                    continue;
                }

                if (yChange == 0)
                {
                    if (ConnectFourGrid[x][gridY] == CurrPlayerColour)
                    {
                        matchCount++;

                        if (matchCount >= 4)
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        matchCount = 0;
                    }
                }
            }

            return(false);
        }