Exemple #1
0
        private MoveS Minimax(string[] reboard, string player, int depth, int random)
        {
            _iter++;
            // board looks like this [] {"0","1","2","3","4","5","6","7","8"}
            //player X == true and player O == false
            //X is human O is an AI
            //array of possible moves

            var array = Avail(reboard);


            //check if current position of the board is winning
            if (WinningMinMax(reboard, HuPlayer))
            {
                var pokusObject = new MoveS();
                pokusObject.Score = -10;
                return(pokusObject);
            }

            if (WinningMinMax(reboard, AiPlayer))
            {
                var pokusObject = new MoveS {
                    Score = 10
                };
                return(pokusObject);
                // or it is a draw
            }

            if (array.Length == 0)
            {
                var pokusObject = new MoveS {
                    Score = 0
                };
                return(pokusObject);
            }

            if (depth <= 0)
            {
                var pokusObject = new MoveS();
                pokusObject.Score = 0;
                return(pokusObject);
            }
            //MoveS is an object with two parameters: index and score
            var moves = new List <MoveS>();

            for (var i = 0; i < array.Length; i++)
            {
                var move = new MoveS();
                move.Index = Convert.ToInt32(reboard[Convert.ToInt32(array[i])]);

                reboard[Convert.ToInt32(array[i])] = player;

                if (player == AiPlayer)
                {
                    //var result = new MoveS();
                    //recursive call for building the tree of possible moves
                    var result = Minimax(reboard, HuPlayer, depth--, 1);
                    move.Score = result.Score;
                }
                else
                {
                    //var result = new MoveS();
                    var result = Minimax(reboard, AiPlayer, depth--, 1);
                    move.Score = result.Score;
                }

                //resets the board value
                reboard[Convert.ToInt32(array[i])] = move.Index.ToString();
                // adding the final object move to a List of moves with score for every move
                moves.Add(move);
            }



            //finding the best move of possible moves
            int bestMove       = 0;
            int bestMoveRandom = 0;

            if (player == AiPlayer)
            {
                var bestScore = -10000;
                for (var i = 0; i < moves.Count; i++)
                {
                    if (moves[i].Score > bestScore)
                    {
                        bestScore      = moves[i].Score;
                        bestMove       = i;
                        bestMoveRandom = bestScore;
                    }
                }
            }
            else
            {
                var bestScore = 10000;
                for (var i = 0; i < moves.Count; i++)
                {
                    if (moves[i].Score < bestScore)
                    {
                        bestScore      = moves[i].Score;
                        bestMove       = i;
                        bestMoveRandom = bestScore;
                    }
                }
            }
            //returning the best move's index for an Ai to play
            if (random != 1)
            {
                return(moves[bestMove]);
            }

            moves = moves.Where(s => s.Score == bestMoveRandom).ToList();
            var tah = _random.Next(0, moves.Count);

            return(moves[tah]);
        }
Exemple #2
0
        public int Minimax(string[] reboard, bool player)
        {
            // iter++;
            //var score = new List<int>();
            var array = Avail(reboard);

            if (WinningMinMax(reboard, true))
            {
                //score.Add(-10);
                return(-10);
            }
            else if (WinningMinMax(reboard, false))
            {
                // score.Add(10);
                return(10);
            }
            else if (array.Length == 0)
            {
                // score.Add(0);
                return(0);
            }

            List <MoveS> moves = new List <MoveS>();

            //  List<int> movesscore = new List<int>();

            for (var i = 0; i < array.Length; i++)
            {
                var move = new MoveS();
                move.index = Convert.ToInt32(array[i]);

                /*int movescore;
                 * int move = Convert.ToInt32(array[i]);*/
                //var movescore = new List<int>();


                if (player)
                {
                    reboard[Convert.ToInt32(array[i])] = "X";
                }
                else
                {
                    reboard[Convert.ToInt32(array[i])] = "O";
                }

                if (!player)
                {
                    var g = new MoveS();
                    g.score    = Minimax(reboard, true);
                    move.score = g.score;
                }
                else
                {
                    var g = new MoveS();
                    g.score    = Minimax(reboard, false);
                    move.score = g.score;
                }

                reboard[Convert.ToInt32(array[i])] = move.index.ToString();
                moves.Add(move);
                pokus.Add(move);
                //   movesscore.Add(movescore);
            }


            int bestMove = 0;

            if (player == false)
            {
                var bestScore = -10000;
                for (var i = 0; i < moves.Count; i++)
                {
                    if (moves[i].score > bestScore)
                    {
                        bestScore = moves[i].score;
                        bestMove  = i;
                    }
                }
            }
            else
            {
                var bestScore = 10000;
                for (var i = 0; i < moves.Count; i++)
                {
                    if (moves[i].score < bestScore)
                    {
                        bestScore = moves[i].score;
                        bestMove  = i;
                    }
                }
            }
            return(moves[bestMove].index);
        }