Beispiel #1
0
        public int[] randommove(playerclass p, int diameter)
        {
            int    x;
            int    y;
            Random rnd = new Random();

            do
            {
                x = rnd.Next(diameter);
                y = rnd.Next(diameter);
                x = x + (center - diameter / 2);
                y = y + (center - diameter / 2);
            }while (b[x, y] != 0);

            return(new int[5] {
                x, y, -1, -1, -1
            });
        }
Beispiel #2
0
        private void NewGame(playerclass p0, playerclass p1, board bb)
        {
            mainboard = bb;
            inturn    = 0;
            if (!p0.ishuman)
            {
                mainboard.set(board.center, board.center, mainboard.xv);
                inturn = 1;
            }

            DataGridSetup();

            player[0]      = p0;
            player[0].mark = xchar;
            player[0].val  = mainboard.xv;
            player[1]      = p1;
            player[1].mark = ochar;
            player[1].val  = mainboard.ov;
        }
Beispiel #3
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);
        }
Beispiel #4
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);
        }
Beispiel #5
0
 public int[] randommove(playerclass p)
 {
     return(randommove(p, 15));
 }
Beispiel #6
0
 public string logOrCreate(string name, Color color,string password)
 {
     playerclass find = playerslist.Where(c => c.name == name).FirstOrDefault();
     if (find == null)
     {
         Console.WriteLine("New player! - " + name);
         playerclass player = new playerclass(name, color, password);
         playerslist.Add(player);
         MyObject.getRandomPlacePlayer(name);
         return "New player (" + name + ") created";
     }
     else
     {
         if (find.password == password) return "Вы подключились";
     };
     return null;
 }
Beispiel #7
0
        private void NewGame(playerclass p0, playerclass p1)
        {
            board bb = new board(this);

            NewGame(p0, p1, bb);
        }
Beispiel #8
0
        private void EvolveButton_Click(object sender, EventArgs e)
        {
            Label[] playerlabels = new Label[nplayer];

            double mutationrate = 1.0;

            for (int i = 0; i < nplayer; i++)
            {
                playerlabels[i] = new Label();
                playerpool[i]   = new playerclass('z', 0, false, "Player_" + i);
                playerpool[i].mutate(mutationrate);
                playerpool[i].namelabel  = playerlabels[i];
                playerlabels[i].Text     = playerpool[i].name;
                playerlabels[i].Visible  = true;
                playerlabels[i].AutoSize = true;
            }

            mutationrate = 0.5;

            for (int iround = 0; iround < 100; iround++)
            {
                memo("Round " + iround);
                for (int i = 0; i < nplayer; i++)
                {
                    for (int j = 0; j < nplayer; j++)
                    {
                        if (j == i)
                        {
                            continue;
                        }
                        //memo("Playing " + i + " vs. " + j);
                        NewGame(playerpool[i], playerpool[j]);
                        int winner = makeplay(7, 7, false);
                        if (winner >= 0)
                        {
                            int ijwin  = i;
                            int ijlose = j;
                            if (winner > 0)
                            {
                                ijwin  = j;
                                ijlose = i;
                            }
                            //memo(" winner: " + ijwin);
                            playerpool[ijwin].wins++;
                            playerpool[ijlose].losses++;
                        }
                    }
                }

                int maxwinner = -1;
                int maxwin    = -1;
                int maxloser  = -1;
                int maxloss   = -1;

                playerparams ppsum = new playerparams();
                ppsum.zero();

                for (int i = 0; i < nplayer; i++)
                {
                    memo(playerpool[i].name + ": " + playerpool[i].wins + " wins, " + playerpool[i].losses + " losses.");
                    if (playerpool[i].wins > maxwin)
                    {
                        maxwin    = playerpool[i].wins;
                        maxwinner = i;
                    }
                    if (playerpool[i].losses > maxloss)
                    {
                        maxloss  = playerpool[i].losses;
                        maxloser = i;
                    }
                    playerpool[i].wins   = 0;
                    playerpool[i].losses = 0;

                    foreach (string s in playerpool[i].par.pp.Keys)
                    {
                        ppsum.pp[s] += playerpool[i].par.pp[s];
                    }
                }

                foreach (string s in ppsum.pp.Keys)
                {
                    memo(s + ": " + (ppsum.pp[s] / nplayer).ToString("F2"));
                }

                playerclass ppref = new playerclass('z', 0, false, "ref");
                foreach (string s in ppsum.pp.Keys)
                {
                    memo(s + "(diff): " + (ppsum.pp[s] / nplayer - ppref.par.pp[s]).ToString("F2"));
                }


                playerpool[maxloser] = playerpool[maxwinner].spawn(mutationrate);
                if (iround % 10 == 0)
                {
                    playerpool[maxloser].par.restoredefaults();
                    playerpool[maxloser].mutate(3 * mutationrate);
                    playerpool[maxloser].name = "X" + iround;
                }
                mutationrate *= 0.98;
            }

            string fnbase = "evolution";
            string fn;
            int    k = 0;

            do
            {
                fn = fnbase + k + ".txt";
                k++;
            }while (File.Exists(fn));

            using (StreamWriter sw = new StreamWriter(fn))
            {
                for (int i = 0; i < nplayer; i++)
                {
                    string s = playerpool[i].export();
                    memo(s);
                    sw.WriteLine(s);
                }
            }
        }