Exemplo n.º 1
0
        private void NewGame(bool p1human, bool p2human)
        {
            board       bb = new board(this);
            playerclass p0 = new playerclass(xchar, bb.xv, p1human, "Computer player");
            playerclass p1 = new playerclass(ochar, bb.ov, p2human, "Computer player");

            DateTime before   = new DateTime(2019, 01, 01);
            TimeSpan newest   = new TimeSpan(0);
            string   newestfn = "";

            foreach (string fn in Directory.GetFiles(Directory.GetCurrentDirectory()))
            {
                if (File.GetCreationTime(fn) - before > newest)
                {
                    newest   = File.GetCreationTime(fn) - before;
                    newestfn = fn;
                }
            }
            using (StreamReader sr = new StreamReader(newestfn))
            {
                string pstring = sr.ReadLine();
                p0.import(pstring);
                p1.import(pstring);
            }

            if (p0.ishuman)
            {
                p0.name = "Human player";
            }

            if (p1.ishuman)
            {
                p1.name = "Human player";
            }

            NewGame(p0, p1, bb);
        }
Exemplo n.º 2
0
        public int[] computermove(playerclass p, bool dodisplay, int depth, DateTime starttime)
        {
            //move[0] = x
            //move[1] = y
            //move[2] = threatlevel
            Random rnd = new Random();

            if (boardcount() < 3)
            {
                return(randommove(p, 3));
            }
            else if (boardcount() < 5)
            {
                return(randommove(p, 3));
            }

            int[] move = randommove(p);
            //bool forced = false;

            if (depth > parent.maxdepth)
            {
                parent.memo("maxdepth");
                return(move);
            }
            if (DateTime.Now - starttime > parent.timepermove)
            {
                parent.memo("timeout");
                return(move);
            }

            for (int i = 0; i < board.size; i++)
            {
                for (int j = 0; j < board.size; j++)
                {
                    if (b[i, j] == 0)
                    {
                        if (win(i, j, p.val))
                        {
                            move[0] = i;
                            move[1] = j;
                            return(move);
                        }
                    }
                }
            }

            for (int i = 0; i < board.size; i++)
            {
                for (int j = 0; j < board.size; j++)
                {
                    if (b[i, j] == 0)
                    {
                        if (win(i, j, -p.val))
                        {
                            move[0] = i;
                            move[1] = j;
                            return(move);
                        }
                    }
                }
            }

            SortedDictionary <double, int[]> cvorder = new SortedDictionary <double, int[]>();

            int    maxprio = -1;
            int    maxoff  = -1;
            int    maxdef  = -1;
            double maxcv   = -1;

            for (int i = 0; i < board.size; i++)
            {
                for (int j = 0; j < board.size; j++)
                {
                    if (b[i, j] == 0)
                    {
                        cvx[i, j] = new cellvalueclass();
                        foreach (Tuple <int, int> uv in uvlist)
                        {
                            cvx[i, j] = cvx[i, j].merge(evaluateline(i, j, uv, p.val));
                        }
                        cvx[i, j].f["density"] = (int)Math.Abs(getdensity(i, j, 2) - p.par.pp["optimaldensity"]);

                        cvo[i, j] = new cellvalueclass();
                        foreach (Tuple <int, int> uv in uvlist)
                        {
                            cvo[i, j] = cvo[i, j].merge(evaluateline(i, j, uv, -p.val));
                        }

                        cv[i, j] = cvx[i, j].totalvalue(p.par)
                                   + p.par.pp["oxratio"] * cvo[i, j].totalvalue(p.par)
                                   + p.par.pp["noise"] * rnd.NextDouble();
                        prio[i, j] = 2 * cvx[i, j].priority() + cvo[i, j].priority();
                        double ccvv = cv[i, j];
                        if (ccvv > 0.3)
                        {
                            while (cvorder.ContainsKey(ccvv))
                            {
                                ccvv += 0.001;
                            }
                            int[] cvmv = new int[5] {
                                i, j, 0, 0, 0
                            };
                            cvorder.Add(-ccvv, cvmv);
                        }

                        if (prio[i, j] > maxprio)
                        {
                            maxprio = prio[i, j];
                            maxcv   = cv[i, j];
                            move[0] = i;
                            move[1] = j;
                        }
                        else if (prio[i, j] == maxprio)
                        {
                            if (cv[i, j] > maxcv)
                            {
                                maxcv   = cv[i, j];
                                move[0] = i;
                                move[1] = j;
                            }
                        }
                        if (cvx[i, j].priority() > maxoff)
                        {
                            maxoff = cvx[i, j].priority();
                        }
                        if (cvo[i, j].priority() > maxdef)
                        {
                            maxdef = cvx[i, j].priority();
                        }
                    }
                }
            }

            move[2] = maxprio;
            move[3] = maxoff;
            move[4] = maxdef;

            bool reachedlimit = false;
            bool win4found    = false;
            bool win3found    = false;

            if (move[2] < 1200) //search for VCF
            {
                playerclass other = p.otherplayer();
                //for (int i = 0; i < board.size; i++)
                foreach (double ccvv in cvorder.Keys)
                {
                    int i = cvorder[ccvv][0];
                    int j = cvorder[ccvv][1];
                    if (reachedlimit || win4found)
                    {
                        break;
                    }
                    //for (int j = 0; j < board.size; j++)
                    {
                        if (b[i, j] == 0)
                        {
                            if (cvx[i, j].f["fours"] > 0)
                            {
                                if (dodisplay)
                                {
                                    parent.memo("Testing " + i + "," + j);
                                }
                                board testboard = this.copy();
                                testboard.smallsize();
                                testboard.b[i, j] = p.val;
                                testboard.set(testboard.computermove(other, dodisplay, depth + 1, starttime), -p.val);
                                if (dodisplay)
                                {
                                    testboard.DisplayBoard();
                                }
                                int[] nextmove = testboard.computermove(p, dodisplay, depth + 1, starttime);
                                if (dodisplay)
                                {
                                    parent.memo("nextmove[2] = " + nextmove[2]);
                                }
                                if (nextmove[3] >= 600)
                                {
                                    move[0] = i;
                                    move[1] = j;
                                    move[2] = nextmove[2];
                                    parent.memo("==== 4-win found! ====");
                                    win4found = true;
                                    break;
                                }
                                else if (nextmove[2] < 0)
                                {
                                    reachedlimit = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                if (dodisplay)
                {
                    this.DisplayBoard();
                }
            }

            if (depth > parent.maxdepth)
            {
                parent.memo("maxdepth");
                return(move);
            }
            if (DateTime.Now - starttime > parent.timepermove)
            {
                parent.memo("timeout");
                return(move);
            }
            if (win4found)
            {
                parent.memo("win4found");
                return(move);
            }

            if (move[2] < 10) //search for VCT
            {
                playerclass other = p.otherplayer();
                //for (int i = 0; i < board.size; i++)
                //{
                foreach (double ccvv in cvorder.Keys)
                {
                    int i = cvorder[ccvv][0];
                    int j = cvorder[ccvv][1];

                    if (reachedlimit || win3found)
                    {
                        break;
                    }

                    //for (int j = 0; j < board.size; j++)
                    {
                        if (b[i, j] == 0)
                        {
                            if (cvx[i, j].f["threes"] > 0)
                            {
                                if (dodisplay)
                                {
                                    parent.memo("Testing " + i + "," + j);
                                }
                                board testboard = this.copy();
                                testboard.smallsize();
                                testboard.b[i, j] = p.val;
                                testboard.set(testboard.computermove(other, dodisplay, depth + 1, starttime), -p.val);
                                if (dodisplay)
                                {
                                    testboard.DisplayBoard();
                                }
                                int[] nextmove = testboard.computermove(p, dodisplay, depth + 1, starttime);
                                if (dodisplay)
                                {
                                    parent.memo("nextmove[2] = " + nextmove[2]);
                                }
                                if (nextmove[3] >= 10)
                                {
                                    move[0]   = i;
                                    move[1]   = j;
                                    move[2]   = nextmove[2];
                                    win3found = true;
                                    parent.memo("==== 3-win found! ====");
                                    break;
                                }
                                else if (nextmove[2] < 0)
                                {
                                    reachedlimit = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                if (dodisplay)
                {
                    this.DisplayBoard();
                }
            }

            //parent.memo("Maxprio = " + maxprio);
            //parent.memo("Maxcv = " + maxcv);

            return(move);
        }
Exemplo n.º 3
0
        private void NewGame(playerclass p0, playerclass p1)
        {
            board bb = new board(this);

            NewGame(p0, p1, bb);
        }