コード例 #1
0
        static int ScoreMove(DotsGame game, LineInfo move)
        {
            System.Diagnostics.Debug.Assert(game.IsValid(move));

            int completedSquares = 0;
            int openSquares      = 0;

            switch (move.LineType)
            {
            case LineType.Horizontal:
                //  _
                // | |   <-- missing line is 'move'
                //
                if ((move.Y - 1) >= 0)
                {
                    int sides = 0;
                    sides += game.GetVertical(move.X, move.Y - 1) == Player.Invalid ? 0 : 1;
                    sides += game.GetVertical(move.X + 1, move.Y - 1) == Player.Invalid ? 0 : 1;
                    sides += game.GetHorizontal(move.X, move.Y - 1) == Player.Invalid ? 0 : 1;

                    if (sides == 3)
                    {
                        // circle takes the square
                        completedSquares++;
                    }

                    if (sides == 2)
                    {
                        // leaves a square for the next move (could be us!)
                        openSquares++;
                    }
                }

                //
                // |_|  <-- missing line is 'move'
                //
                if ((move.Y + 1) < game.Height)
                {
                    int sides = 0;
                    sides += game.GetVertical(move.X, move.Y) == Player.Invalid ? 0 : 1;
                    sides += game.GetVertical(move.X + 1, move.Y) == Player.Invalid ? 0 : 1;
                    sides += game.GetHorizontal(move.X, move.Y + 1) == Player.Invalid ? 0 : 1;

                    if (sides == 3)
                    {
                        // circle takes the square
                        completedSquares++;
                    }

                    if (sides == 2)
                    {
                        // leaves a square for the next move (could be us!)
                        openSquares++;
                    }
                }

                break;

            case LineType.Vertical:
                //  _
                // |_  <-- missing line is 'move'
                //
                if ((move.X - 1) >= 0)
                {
                    int sides = 0;
                    sides += game.GetHorizontal(move.X - 1, move.Y) == Player.Invalid ? 0 : 1;
                    sides += game.GetHorizontal(move.X - 1, move.Y + 1) == Player.Invalid ? 0 : 1;
                    sides += game.GetVertical(move.X - 1, move.Y) == Player.Invalid ? 0 : 1;

                    if (sides == 3)
                    {
                        // circle takes the square
                        completedSquares++;
                    }

                    if (sides == 2)
                    {
                        // leaves a square for the next move (could be us!)
                        openSquares++;
                    }
                }


                //  _
                //  _|   <-- missing line is 'move'
                //
                if ((move.X + 1) < game.Width)
                {
                    int sides = 0;
                    sides += game.GetHorizontal(move.X, move.Y) == Player.Invalid ? 0 : 1;
                    sides += game.GetHorizontal(move.X, move.Y + 1) == Player.Invalid ? 0 : 1;
                    sides += game.GetVertical(move.X + 1, move.Y) == Player.Invalid ? 0 : 1;

                    if (sides == 3)
                    {
                        // circle takes the square
                        completedSquares++;
                    }

                    if (sides == 2)
                    {
                        // leaves a square for the next move (could be us!)
                        openSquares++;
                    }
                }

                break;
            }


            int score = 0; // 0 means it isn't a determinmental move

            if (completedSquares == 2)
            {
                score = 10;
            }
            else if (completedSquares == 1 && openSquares == 1)
            {
                score = 10;
            }
            else if (completedSquares == 1)
            {
                score = 5;
            }
            else if (openSquares == 1)
            {
                score = -5;
            }
            else if (openSquares == 2)
            {
                score = -10;
            }

            return(score);
        }
コード例 #2
0
        private void HandleClick(MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            if (Game == null || Game.GameOver || PlayerSettings.GetPlayerController(Game.CurrentPlayer) == PlayerController.Computer)
            {
                return;
            }

            LineInfo clickedLine = new LineInfo();

            // first row of dots starts at `PreferedBorder` and is `PreferedDotSize` wide
            // give a few pixes of fudge to either side. Use PreferedDotSize/2 as fudge.
            int fudge = PreferedDotSize / 2;

            for (int i = 0; i < Game.Width; ++i)
            {
                if (e.X > PreferedBorder + i * PreferedSpacer - fudge &&
                    e.X < PreferedBorder + i * PreferedSpacer + PreferedDotSize + fudge)
                {
                    clickedLine.LineType = LineType.Vertical;
                    clickedLine.X        = i;
                    break;
                }

                if (e.X > PreferedBorder + i * PreferedSpacer + PreferedDotSize)
                {
                    clickedLine.X = i;
                }
            }

            for (int i = 0; i < Game.Height; ++i)
            {
                if (e.Y > PreferedBorder + i * PreferedSpacer - fudge &&
                    e.Y < PreferedBorder + i * PreferedSpacer + PreferedDotSize + fudge)
                {
                    if (clickedLine.LineType == LineType.Vertical)
                    {
                        clickedLine.LineType = LineType.None;
                        break;
                    }

                    clickedLine.LineType = LineType.Horizontal;
                    clickedLine.Y        = i;
                    break;
                }

                if (e.Y > PreferedBorder + i * PreferedSpacer + PreferedDotSize)
                {
                    clickedLine.Y = i;
                }
            }

            // don't allow selection of already played moves
            switch (clickedLine.LineType)
            {
            case LineType.Horizontal:
                if (Game.GetHorizontal(clickedLine.X, clickedLine.Y) != Player.Invalid)
                {
                    clickedLine.LineType = LineType.None;
                }
                break;

            case LineType.Vertical:
                if (Game.GetVertical(clickedLine.X, clickedLine.Y) != Player.Invalid)
                {
                    clickedLine.LineType = LineType.None;
                }
                break;
            }

            if ((clickedLine.LineType == selectedLine.LineType && clickedLine.X == selectedLine.X && clickedLine.Y == selectedLine.Y) ||
                (e.Clicks > 1))
            {
                if (clickedLine.LineType != LineType.None)
                {
                    Game.RecordMove(clickedLine);
                }

                selectedLine.LineType = LineType.None;
            }
            else
            {
                selectedLine.LineType = clickedLine.LineType;
                selectedLine.X        = clickedLine.X;
                selectedLine.Y        = clickedLine.Y;
            }

            Parent.Refresh();

            RunComputerPlayers();
        }