Exemple #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;
                        }
                    }
                }
        private void BStatistics_Click(object sender, EventArgs e)
        {
            Random rnd = new Random();

            Stopwatch sw = new Stopwatch();

            sw.Start();
            const int mc = 100;

            int[]    count = new int[mc];
            double[] vals  = new double[mc];

            while (sw.ElapsedMilliseconds < 3 * 60000)
            {
                Board b = new Board();
                b.DoMove(new Position(7, 7));
                for (int i = 0; i < mc && b.Winner == Board.Brick.Empty; i++)
                {
                    List <Board> boards = b.GetNearMoves(2);

                    count[i]++;
                    vals[i] += boards.Count;

                    b = boards[rnd.Next(boards.Count)];
                }
            }

            string s = "";

            for (int i = 0; i < mc; i++)
            {
                if (count[i] == 0)
                {
                    vals = vals.Take(i).ToArray();
                    Console.WriteLine("Longest: " + i);
                    break;
                }
                s += i + "\t" + vals[i] / count[i] + "\n";
            }
            File.WriteAllText(@"C:\Users\Martin\Desktop\out.txt", s);

            Console.WriteLine("Done");
        }
Exemple #3
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);
                }