Esempio n. 1
0
                public Root(Board b, int d)
                {
                    Count = 1;

                    board = b;
                    depth = d;

                    if (depth > Deepness)
                    {
                        Deepness = depth;
                    }

                    Winner = board.Winner;

                    Evaluation = board.Evaluate();

                    if (depth == 0)
                    {
                        List <Board> boards;

                        boards = board.GetNearMoves(2);

                        boards.Sort();
                        if (Maximize)
                        {
                            boards.Reverse();
                        }

                        moves = new List <Root>(boards.Count);

                        for (int i = 0; i < boards.Count; i++)
                        {
                            Root r = new Root(boards[i], depth + 1);

                            if (r.Winner != Board.Brick.Empty)
                            {
                                if (r.Winner == board.Turn)
                                {
                                    Winner = board.Turn;
                                    Best   = r;
                                    moves  = null;
                                }
                            }
                            else
                            {
                                moves.Add(r);
                            }
                        }

                        if (moves?.Count == 0)
                        {
                            Winner = board.Turn == Board.Brick.White ? Board.Brick.Black : Board.Brick.White;
                        }
                    }
                }
Esempio n. 2
0
                public bool Visit()
                {
                    if (Count == 1)
                    {
                        List <Board> boards;

                        boards = board.GetNearMoves(2);

                        boards.Sort();
                        if (Maximize)
                        {
                            boards.Reverse();
                        }

                        moves = new List <Root>(boards.Count);

                        for (int i = 0; i < boards.Count; i++)
                        {
                            Root r = new Root(boards[i], depth + 1);

                            if (r.Winner != Board.Brick.Empty)
                            {
                                if (r.Winner == board.Turn)
                                {
                                    Winner = board.Turn;
                                    Best   = r;
                                    moves  = null;
                                    return(true);
                                }
                            }
                            else
                            {
                                moves.Add(r);
                            }
                        }

                        if (moves.Count == 0)
                        {
                            Winner = board.Turn == Board.Brick.White ? Board.Brick.Black : Board.Brick.White;
                            return(true);
                        }
                    }
                    else
                    {
                        double best  = moves[0].GetUCB1(worst, range);
                        int    index = 0;

                        for (int i = 0; i < moves.Count; i++)
                        {
                            if (moves[i].GetUCB1(worst, range) > best)
                            {
                                best  = moves[i].GetUCB1(worst, range);
                                index = i;
                            }
                        }

                        if (moves[index].Visit())
                        {
                            if (moves[index].Winner == board.Turn)
                            {
                                Winner = board.Turn;
                                Best   = moves[index];
                                moves  = null;
                                return(true);
                            }
                            else
                            {
                                moves.RemoveAt(index);
                            }
                        }

                        if (moves.Count == 0)
                        {
                            Winner = board.Turn == Board.Brick.White ? Board.Brick.Black : Board.Brick.White;
                            return(true);
                        }
                    }

                    FindBest();

                    Count++;
                    return(false);
                }