Ejemplo n.º 1
0
        private int Search(TicTacToe.Game.TicTacToe game, int alpha, int beta, bool maximizing)
        {
            if (game.IsGameOver())
            {
                var winner = game.GetWinner();
                if (winner == _playerId)
                {
                    return(1);
                }
                if (winner == 0)
                {
                    return(0);
                }
                return(-1);
            }

            if (maximizing)
            {
                var value = -100;
                for (var i = 0; i < 9; i++)
                {
                    if (game.IsPossible(i))
                    {
                        game.DoMove(i, _playerId);
                        var score = Search(game, alpha, beta, false);
                        game.RevertMove(i);
                        value = Math.Max(score, value);
                        alpha = Math.Max(alpha, value);
                        if (alpha >= beta)
                        {
                            return(value);
                        }
                    }
                }

                return(value);
            }
            else
            {
                var value = 100;
                for (var i = 0; i < 9; i++)
                {
                    if (game.IsPossible(i))
                    {
                        game.DoMove(i, OtherPlayer);
                        var score = Search(game, alpha, beta, true);
                        game.RevertMove(i);
                        value = Math.Min(score, value);
                        beta  = Math.Min(beta, value);
                        if (alpha >= beta)
                        {
                            return(value);
                        }
                    }
                }

                return(value);
            }
        }
Ejemplo n.º 2
0
        public int GetMove(TicTacToe.Game.TicTacToe game)
        {
            var state     = game.GetBoard();
            var gameState = state.Select(s => s == _playerId?1:(0)).Concat(state.Select(s => s == _playerId?0:(s == OtherPlayer?1:0))).Concat(state.Select(s => s == _playerId?0:(s == OtherPlayer?0:1))).ToArray();
            var result    = m_network.Calculate(gameState.Select(s => (double)s).ToArray());

            // Console.Error.WriteLine("Weights: " + string.Join(" ", result.Select(r => r.Value)));

            return(result.OrderByDescending(r => r.Value).First(r => game.IsPossible(r.Target)).Target);
        }
Ejemplo n.º 3
0
        public int GetMove(TicTacToe.Game.TicTacToe game)
        {
            var move = -1;

            while (!game.IsPossible(move))
            {
                move = rnd.Next(0, 9);
            }

            return(move);
        }
Ejemplo n.º 4
0
        public int GetMove(TicTacToe.Game.TicTacToe game)
        {
            // if (rnd.NextDouble() < 0.5)
            // {
            //     var move = -1;
            //     while (!game.IsPossible(move))
            //     {
            //         move = rnd.Next(0, 9);
            //     }
//
            //     return move;
            // }
            var bestMoves = new List <int>();
            var bestScore = -10;
            var a         = -100;
            var b         = 100;

            for (var i = 0; i < 9; i++)
            {
                if (game.IsPossible(i))
                {
                    game.DoMove(i, _playerId);
                    var score = Search(game, a, b, false);
                    if (score > bestScore)
                    {
                        bestMoves.Clear();
                        bestMoves.Add(i);
                        bestScore = score;
                    }
                    else if (score == bestScore)
                    {
                        bestMoves.Add(i);
                    }

                    game.RevertMove(i);
                }
            }

            var best  = bestMoves.OrderBy(i => rnd.NextDouble()).First();
            var state = game.GetBoard();

            if (_data.Add(GetHash(state)))
            {
                FileSaver.AddData(string.Join(",", state.Select(s => s == _playerId?1:(0)).Concat(state.Select(s => s == _playerId?0:(s == OtherPlayer?1:0))).Concat(state.Select(s => s == _playerId?0:(s == OtherPlayer?0:1)))) + "," + best + "\n");
            }
            // Console.Error.WriteLine();
            // Console.Error.WriteLine("TIME: " + s.ElapsedMilliseconds);
            // Console.Error.WriteLine("BestScore: " + bestScore);
            return(best);
        }
Ejemplo n.º 5
0
        public int GetMove(TicTacToe.Game.TicTacToe game)
        {
            game.PrintBoard();
            var move = -1;

            while (!game.IsPossible(move))
            {
                try
                {
                    Console.WriteLine();
                    Console.WriteLine("Write a valid move: (0-9)");
                    move = int.Parse(Console.ReadLine());
                }
                catch (Exception)
                {
                    // yolo
                }
            }

            return(move);
        }