public void Test1()
        {
            var first = new[]
            {
                Played.Self, Played.Empty, Played.Empty,
                Played.Empty, Played.Self, Played.Empty,
                Played.Empty, Played.Empty, Played.Self,
            };
            var empty = new[]
            {
                Played.Empty, Played.Empty, Played.Empty,
                Played.Empty, Played.Empty, Played.Empty,
                Played.Empty, Played.Empty, Played.Empty,
            };
            var board = new[]
            {
                first, empty, empty,
                empty, empty, empty,
                empty, empty, empty,
            };
            var score = Constants.WinCorner + Constants.CenterSquareAnyBoard + Constants.SmallBoardWin;

            Assert.AreEqual(AlgoHeuristic.ComputeScore(board, AlgoHeuristic.GetBoardResult(board)), score);
            Invert(board);
            Assert.AreEqual(AlgoHeuristic.ComputeScore(board, AlgoHeuristic.GetBoardResult(board)), score * -1);
        }
        public void TestScoringEmpty()
        {
            var board = new Played[9][];

            for (var i = 0; i < 9; i++)
            {
                board[i] = new Played[9];
                for (var j = 0; j < 9; j++)
                {
                    board[i][j] = Played.Empty;
                }
            }

            var score = AlgoHeuristic.ComputeScore(board, AlgoHeuristic.GetBoardResult(board));

            Assert.AreEqual(score, 0);
        }
Exemple #3
0
        private static int NegaMax(GameState gameState, int depth, int alpha, int beta, int color)
        {
            if (depth == 0 || gameState.IsTerminated())
            {
                if (gameState.Score != null)
                {
                    return(gameState.Score.Value);
                }
                var score = color * AlgoHeuristic.ComputeScore(gameState.Board, gameState.BoardResult);
                gameState.Score = score;
                return(score);
            }

            if (gameState.ChildStates == null)
            {
                var stopWatch = new Stopwatch();
                stopWatch.Start();
                gameState.GenerateChildrenState(1);
                stopWatch.Stop();
                Monitoring.ChildrenGenerationTime += stopWatch.Elapsed.TotalSeconds;
            }

            var value = int.MinValue;

            foreach (var childState in gameState.ChildStates)
            {
                value = Math.Max(value, -1 * NegaMax(childState, depth - 1, -beta, -alpha, -color));
                alpha = Math.Max(alpha, value);
                if (alpha >= beta)
                {
                    break;
                }
            }

            gameState.Score = value;
            return(value);
        }
        public void Test2()
        {
            var board = new[]
            {
                new[]
                {
                    Played.Opponent, Played.Self, Played.Empty,
                    Played.Empty, Played.Empty, Played.Empty,
                    Played.Empty, Played.Empty, Played.Empty,
                },

                new[]
                {
                    Played.Empty, Played.Self, Played.Empty,
                    Played.Opponent, Played.Empty, Played.Empty,
                    Played.Empty, Played.Empty, Played.Empty,
                },
                new[]
                {
                    Played.Empty, Played.Self, Played.Empty,
                    Played.Empty, Played.Empty, Played.Empty,
                    Played.Empty, Played.Self, Played.Empty,
                },
                new[]
                {
                    Played.Self, Played.Empty, Played.Empty,
                    Played.Opponent, Played.Opponent, Played.Opponent,
                    Played.Empty, Played.Self, Played.Empty,
                },
                new[]
                {
                    Played.Self, Played.Opponent, Played.Empty,
                    Played.Self, Played.Empty, Played.Empty,
                    Played.Self, Played.Opponent, Played.Opponent,
                },
                new[]
                {
                    Played.Empty, Played.Empty, Played.Opponent,
                    Played.Empty, Played.Self, Played.Empty,
                    Played.Opponent, Played.Empty, Played.Empty,
                },
                new[]
                {
                    Played.Empty, Played.Empty, Played.Opponent,
                    Played.Opponent, Played.Empty, Played.Empty,
                    Played.Self, Played.Self, Played.Self,
                },
                new[]
                {
                    Played.Empty, Played.Empty, Played.Empty,
                    Played.Empty, Played.Empty, Played.Empty,
                    Played.Opponent, Played.Empty, Played.Empty,
                },
                new[]
                {
                    Played.Opponent, Played.Self, Played.Empty,
                    Played.Empty, Played.Empty, Played.Self,
                    Played.Empty, Played.Opponent, Played.Self,
                }
            };

            Assert.AreEqual(44, AlgoHeuristic.ComputeScore(board, AlgoHeuristic.GetBoardResult(board)));
            Invert(board);
            Assert.AreEqual(-44, AlgoHeuristic.ComputeScore(board, AlgoHeuristic.GetBoardResult(board)));
        }