Ejemplo n.º 1
0
 public BaseSolve()
 {
     HoleId   = new uint[91];
     EmList   = new Empties[Constants.MaxEmpties];
     EmHead   = new Empties();
     bestMove = MVPASS;
 }
Ejemplo n.º 2
0
        public string GetColumnNameByExpression <T>(Expression <Func <T, object> > field)
        {
            MemberInfo memberInfo = ReflectionMemberInformer.GetMemberInfo(field);

            KeyValuePair <string, string> keyValue =
                Empties.Single(keyValuePair => keyValuePair.Value == memberInfo.MemberName);

            return(keyValue.Key);
        }
Ejemplo n.º 3
0
 public void Process()
 {
     Empties.RemoveAll(e => !e.IsDirty);
     ProcessBullets();
     ProcessEnemies();
     ProcessBonuses();
     ProcessPlayer();
     ProcessInteractions();
 }
Ejemplo n.º 4
0
 private void Initialize(int[,] matrix)
 {
     Barriers             = new List <Barrier>();
     DammageableBarriers  = new List <DammageableBarrier>();
     EnemySpawns          = new List <EnemySpawn>();
     Configuration.Width  = matrix.GetLength(1);
     Configuration.Height = matrix.GetLength(0);
     for (int i = 0; i < Configuration.Height; i++)
     {
         for (int j = 0; j < Configuration.Width; j++)
         {
             var item = DefineItem(matrix[i, j], i, j);
             if (item.Type == ItemType.Wall)
             {
                 Barriers.Add((Barrier)item);
             }
             else if (item.Type == ItemType.DammageableWall)
             {
                 DammageableBarriers.Add((DammageableBarrier)item);
             }
             else if (item.Type == ItemType.EnemySpawn)
             {
                 EnemySpawns.Add((EnemySpawn)item);
             }
             else if (item.Type == ItemType.Enemy)
             {
                 Enemies.Add((Enemy)item);
             }
             else if (item.Type == ItemType.Bonus)
             {
                 Bonuses.Add((Bonus)item);
             }
             else if (item.Type == ItemType.Empty)
             {
                 Empties.Add((Empty)item);
             }
             else if (item.Type == ItemType.Water)
             {
                 Waters.Add((Water)item);
             }
         }
     }
     GenerateEnemies();
     GenerateBonuses();
 }
Ejemplo n.º 5
0
        private int FastestFirstSolve(ChessType[] board, int alpha, int beta, ChessType color, int depth, int empties, int discdiff, int prevmove)
        {
            lock (this)
            {
                int       i, j;
                int       score    = -Constants.HighestScore - 1;
                ChessType oppcolor = 2 - color;
                int       sqnum;
                int       eval;
                int       flipped;
                int       moves, mobility;
                int       best_value, best_index;
                Empties   em, old_em;
                Empties[] move_ptr = new Empties[Constants.MaxEmpties];
                int       holepar;
                int[]     goodness = new int[Constants.MaxEmpties];
                //
                bool fFoundPv = false;
                //
                //nodes++;
                if (depth == 0)
                {
                    nodes++;
                    //return evalation.MidEval2(board, color, oppcolor, empties,EmHead);
                    return(QuiescenceSolve(board, alpha, beta, color, 2, empties, discdiff, prevmove));
                }

                moves = 0;
                for (old_em = EmHead, em = old_em.Succ; em != null; old_em = em, em = em.Succ)
                {
                    sqnum   = em.Square;
                    flipped = RuleUtils.DoFlips(board, sqnum, color, oppcolor);
                    if (flipped > 0)
                    {
                        //
                        board[sqnum] = color;
                        old_em.Succ  = em.Succ;
                        //
                        mobility = count_mobility(board, oppcolor);
                        //
                        RuleUtils.UndoFlips(board, flipped, oppcolor);
                        old_em.Succ     = em;
                        board[sqnum]    = ChessType.EMPTY;
                        move_ptr[moves] = em;
                        goodness[moves] = -mobility;
                        moves++;
                    }
                }

                if (moves != 0)
                {
                    for (i = 0; i < moves; i++)
                    {
                        //
                        best_value = goodness[i];
                        best_index = i;
                        for (j = i + 1; j < moves; j++)
                        {
                            if (goodness[j] > best_value)
                            {
                                best_value = goodness[j];
                                best_index = j;
                            }
                        }
                        em = move_ptr[best_index];
                        move_ptr[best_index] = move_ptr[i];
                        goodness[best_index] = goodness[i];
                        //
                        sqnum        = em.Square;
                        holepar      = em.HoleId;
                        j            = RuleUtils.DoFlips(board, sqnum, color, oppcolor);
                        board[sqnum] = color;

                        em.Pred.Succ = em.Succ;
                        if (em.Succ != null)
                        {
                            em.Succ.Pred = em.Pred;
                        }

                        nodes++;
                        //
                        if (fFoundPv)
                        {
                            //
                            eval = -FastestFirstSolve(board, -alpha - 1, -alpha, oppcolor, depth - 1, empties - 1, -discdiff - 2 * j - 1, sqnum);
                            if ((eval > alpha) && (eval < beta))
                            {
                                eval = -FastestFirstSolve(board, -beta, -eval, oppcolor, depth - 1, empties - 1, -discdiff - 2 * j - 1, sqnum);
                                //eval = -FastestFirstMidSolve(board, -beta, -alpha, oppcolor, depth - 1, empties - 1, -discdiff - 2 * j - 1, sqnum);
                            }
                        }
                        else
                        {
                            eval = -FastestFirstSolve(board, -beta, -alpha, oppcolor, depth - 1, empties - 1, -discdiff - 2 * j - 1, sqnum);
                        }
                        //
                        RuleUtils.UndoFlips(board, j, oppcolor);
                        board[sqnum] = ChessType.EMPTY;
                        em.Pred.Succ = em;
                        if (em.Succ != null)
                        {
                            em.Succ.Pred = em;
                        }

                        if (eval > score)
                        {
                            score = eval;
                            if (eval > alpha)
                            {
                                if (eval >= beta)
                                {
                                    //
                                    return(score);
                                }
                                alpha    = eval;
                                fFoundPv = true;
                            }
                        }
                    }
                }
                else
                {
                    if (prevmove == 0)    //
                    {
                        if (discdiff > 0) //
                        {
                            return(Constants.HighestScore);
                        }
                        if (discdiff < 0)//
                        {
                            return(-Constants.HighestScore);
                        }
                        return(0);//
                    }
                    else  /* I pass: */
                    {
                        score = -FastestFirstSolve(board, -beta, -alpha, oppcolor, depth, empties, -discdiff, 0);
                    }
                }
                return(score);
            }
        }
Ejemplo n.º 6
0
        private int QuiescenceSolve(ChessType[] board, int alpha, int beta, ChessType color, int depth, int empties, int discdiff, int prevmove)
        {
            lock (this)
            {
                int i, j;
                int score1;
                int score2 = -Constants.HighestScore;

                ChessType oppcolor = 2 - color;
                int       sqnum;
                int       eval;
                int       flipped;
                int       moves, mobility;
                int       best_value, best_index;
                Empties   em, old_em;
                Empties[] move_ptr = new Empties[Constants.MaxEmpties];
                int       holepar;
                int[]     goodness = new int[Constants.MaxEmpties];

                //
                //nodes++;
                score1 = evalation.MidEval2(board, color, oppcolor, empties, EmHead);
                if (score1 >= beta || score1 >= 3000 || depth == 0)
                {
                    return(score1);
                }
                else if (score1 > alpha)
                {
                    alpha = score1;
                }
                moves = 0;
                for (old_em = EmHead, em = old_em.Succ; em != null; old_em = em, em = em.Succ)
                {
                    sqnum   = em.Square;
                    flipped = RuleUtils.DoFlips(board, sqnum, color, oppcolor);
                    if (flipped > 0)
                    {
                        //
                        board[sqnum] = color;
                        old_em.Succ  = em.Succ;
                        //
                        mobility = count_mobility(board, oppcolor);
                        //
                        RuleUtils.UndoFlips(board, flipped, oppcolor);
                        old_em.Succ     = em;
                        board[sqnum]    = ChessType.EMPTY;
                        move_ptr[moves] = em;
                        goodness[moves] = -mobility;
                        moves++;
                    }
                }

                if (moves != 0)
                {
                    for (i = 0; i < moves; i++)
                    {
                        best_value = goodness[i];
                        best_index = i;
                        for (j = i + 1; j < moves; j++)
                        {
                            if (goodness[j] > best_value)
                            {
                                best_value = goodness[j];
                                best_index = j;
                            }
                        }
                        em = move_ptr[best_index];
                        move_ptr[best_index] = move_ptr[i];
                        goodness[best_index] = goodness[i];

                        sqnum        = em.Square;
                        holepar      = em.HoleId;
                        j            = RuleUtils.DoFlips(board, sqnum, color, oppcolor);
                        board[sqnum] = color;

                        em.Pred.Succ = em.Succ;
                        if (em.Succ != null)
                        {
                            em.Succ.Pred = em.Pred;
                        }

                        //
                        nodes++;
                        //
                        eval = -evalation.MidEval2(board, oppcolor, color, empties - 1, EmHead);
                        // -QuiescenceSolve(board, -beta, -alpha, oppcolor, depth - 1, empties - 1, -discdiff - j - j - 1, sqnum);

                        RuleUtils.UndoFlips(board, j, oppcolor);
                        board[sqnum] = ChessType.EMPTY;
                        em.Pred.Succ = em;
                        if (em.Succ != null)
                        {
                            em.Succ.Pred = em;
                        }

                        if (eval > score2)
                        {
                            score2 = eval;
                            if (eval > alpha)
                            {
                                if (eval >= beta)
                                {
                                    //
                                    return(score2);
                                }
                                alpha = eval;
                            }
                        }
                    }
                }
                else
                {
                    if (prevmove == 0)//
                    {
                        if (discdiff > 0)
                        {
                            return(Constants.HighestScore);
                        }
                        if (discdiff < 0)
                        {
                            return(-Constants.HighestScore);
                        }
                        return(0);
                    }
                    else  /* I pass: */
                    {
                        nodes++;
                        score2 = -QuiescenceSolve(board, -beta, -alpha, oppcolor, depth, empties, -discdiff, 0);
                        return(score2);
                    }
                }
                return((2 * score1 + score2) / 3); // (5 * score1 + 3 * score2) / 8;
            }
        }
Ejemplo n.º 7
0
        public void PrepareToSolve(ChessType[] board)
        {
            int       i, sqnum;
            uint      k;
            int       z;
            const int MAXITERS = 1;

            k = 1;
            for (i = 10; i <= 80; i++)
            {
                if (board[i] == ChessType.EMPTY)
                {
                    if (board[i - 10] == ChessType.EMPTY)
                    {
                        HoleId[i] = HoleId[i - 10];
                    }
                    else if (board[i - 9] == ChessType.EMPTY)
                    {
                        HoleId[i] = HoleId[i - 9];
                    }
                    else if (board[i - 8] == ChessType.EMPTY)
                    {
                        HoleId[i] = HoleId[i - 8];
                    }
                    else if (board[i - 1] == ChessType.EMPTY)
                    {
                        HoleId[i] = HoleId[i - 1];
                    }
                    else
                    {
                        HoleId[i] = k; k <<= 1;
                    }
                }
                else
                {
                    HoleId[i] = 0;
                }
            }

            for (z = MAXITERS; z > 0; z--)
            {
                for (i = 80; i >= 10; i--)
                {
                    if (board[i] == ChessType.EMPTY)
                    {
                        k = HoleId[i];
                        if (board[i + 10] == ChessType.EMPTY)
                        {
                            HoleId[i] = minu(k, HoleId[i + 10]);
                        }
                        if (board[i + 9] == ChessType.EMPTY)
                        {
                            HoleId[i] = minu(k, HoleId[i + 9]);
                        }
                        if (board[i + 8] == ChessType.EMPTY)
                        {
                            HoleId[i] = minu(k, HoleId[i + 8]);
                        }
                        if (board[i + 1] == ChessType.EMPTY)
                        {
                            HoleId[i] = minu(k, HoleId[i + 1]);
                        }
                    }
                }
                for (i = 10; i <= 80; i++)
                {
                    if (board[i] == ChessType.EMPTY)
                    {
                        k = HoleId[i];
                        if (board[i - 10] == ChessType.EMPTY)
                        {
                            HoleId[i] = minu(k, HoleId[i - 10]);
                        }
                        if (board[i - 9] == ChessType.EMPTY)
                        {
                            HoleId[i] = minu(k, HoleId[i - 9]);
                        }
                        if (board[i - 8] == ChessType.EMPTY)
                        {
                            HoleId[i] = minu(k, HoleId[i - 8]);
                        }
                        if (board[i - 1] == ChessType.EMPTY)
                        {
                            HoleId[i] = minu(k, HoleId[i - 1]);
                        }
                    }
                }
            }
            k = 0;
            Empties pt = EmHead;

            for (i = 60 - 1; i >= 0; i--)
            {
                sqnum = worst2best[i];
                if (board[sqnum] == ChessType.EMPTY)
                {
                    EmList[k]      = new Empties();
                    pt.Succ        = EmList[k];
                    EmList[k].Pred = pt;
                    k++;
                    pt        = pt.Succ;
                    pt.Square = sqnum;
                    pt.HoleId = (int)HoleId[sqnum];
                }
                pt.Succ = null;
            }
        }
Ejemplo n.º 8
0
        public int FastestFirstEndSolve(ChessType[] board, int alpha, int beta, ChessType color, int empties, int discdiff, int prevmove)
        {
            int       i, j;
            int       score    = -highestScore;
            ChessType oppcolor = 2 - color;
            int       sqnum    = -1;
            int       ev;
            int       flipped;
            int       moves, mobility;
            int       best_value, best_index;
            Empties   em, old_em;

            Empties[] move_ptr = new Empties[64];
            int       holepar;

            int[] goodness = new int[64];
            moves = 0;
            nodes++;
            for (old_em = EmHead, em = old_em.Succ; em != null; old_em = em, em = em.Succ)
            {
                sqnum   = em.Square;
                flipped = RuleUtils.DoFlips(board, sqnum, color, oppcolor);
                if (flipped > 0)
                {
                    board[sqnum] = color;
                    old_em.Succ  = em.Succ;
                    mobility     = count_mobility(board, oppcolor);
                    RuleUtils.UndoFlips(board, flipped, oppcolor);
                    old_em.Succ     = em;
                    board[sqnum]    = ChessType.EMPTY;
                    move_ptr[moves] = em;
                    goodness[moves] = -mobility;
                    moves++;
                }
            }

            if (moves != 0)
            {
                for (i = 0; i < moves; i++)
                {
                    best_value = goodness[i];
                    best_index = i;
                    for (j = i + 1; j < moves; j++)
                    {
                        if (goodness[j] > best_value)
                        {
                            best_value = goodness[j];
                            best_index = j;
                        }
                    }
                    em = move_ptr[best_index];
                    move_ptr[best_index] = move_ptr[i];
                    goodness[best_index] = goodness[i];

                    sqnum         = em.Square;
                    holepar       = em.HoleId;
                    j             = RuleUtils.DoFlips(board, sqnum, color, oppcolor);
                    board[sqnum]  = color;
                    RegionParity ^= (uint)holepar;
                    em.Pred.Succ  = em.Succ;
                    if (em.Succ != null)
                    {
                        em.Succ.Pred = em.Pred;
                    }
                    if (empties <= Constants.Fastest_First + 1)
                    {
                        ev = -ParEndSolve(board, -beta, -alpha, oppcolor, empties - 1,
                                          -discdiff - 2 * j - 1, sqnum);
                    }
                    else
                    {
                        ev = -FastestFirstEndSolve(board, -beta, -alpha, oppcolor,
                                                   empties - 1, -discdiff - 2 * j - 1, sqnum);
                    }
                    RuleUtils.UndoFlips(board, j, oppcolor);
                    RegionParity ^= (uint)holepar;
                    board[sqnum]  = ChessType.EMPTY;
                    em.Pred.Succ  = em;
                    if (em.Succ != null)
                    {
                        em.Succ.Pred = em;
                    }

                    if (ev > score)
                    {
                        score = ev;
                        if (empties == searchDepth)
                        {
                            bestMove = sqnum;
                        }
                        if (ev > alpha)
                        {
                            if (ev >= beta)
                            {
                                return(score);
                            }
                            alpha = ev;
                        }
                    }
                }
            }
            else
            {
                if (prevmove == 0)
                {
                    if (discdiff > 0)
                    {
                        return(discdiff + empties);
                    }
                    if (discdiff < 0)
                    {
                        return(discdiff - empties);
                    }
                    return(0);
                }
                else  /* I pass: */
                {
                    score = -FastestFirstEndSolve(board, -beta, -alpha, oppcolor, empties, -discdiff, 0);
                }

                if (empties == searchDepth)
                {
                    bestMove = MVPASS;
                }
            }
            return(score);
        }
Ejemplo n.º 9
0
        public int MidEval2(ChessType[] board, ChessType color, ChessType oppcolor, int empties, Empties EmHead)
        {
            int eval = 0;
            //
            int mydisc = 0;
            int opdisc = 0;
            //
            int mymob = 0;
            int opmob = 0;
            //
            int mypotmob = 0;
            int oppotmob = 0;
            //
            int mystab = 0;
            int opstab = 0;

            //
            int deltakeyano = 0;
            //
            int     unstab = 0;
            int     sqnum;
            int     index = empties - 10;
            Empties em    = EmHead.Succ;

            for (; em != null; em = em.Succ)
            {
                sqnum = em.Square;
                if (RuleUtils.AnyFlips(board, sqnum, color, oppcolor))
                {
                    mymob++;
                }
                else if (RuleUtils.AnyPotMobility(board, sqnum, oppcolor))
                {
                    mypotmob++;
                }
                if (RuleUtils.AnyFlips(board, sqnum, oppcolor, color))
                {
                    opmob++;
                }
                else if (RuleUtils.AnyPotMobility(board, sqnum, color))
                {
                    oppotmob++;
                }
            }

            for (int i = 0; i < midBoardLength; i++)
            {
                sqnum = midBoard[i];
                if (board[sqnum] == color)
                {
                    mydisc++;
                    if (RuleUtils.AnyStab(board, sqnum, color))
                    {
                        mystab++;
                    }
                }
                else if (board[sqnum] == oppcolor)
                {
                    opdisc++;
                    if (RuleUtils.AnyStab(board, sqnum, oppcolor))
                    {
                        opstab++;
                    }
                }
            }

            deltakeyano = getDeltaKeyano(board, color, oppcolor);
            unstab      = getUnStab(board, color) - getUnStab(board, oppcolor);

            eval = mydisc * weight_train[index, 0] + opdisc * weight_train[index, 1] +
                   mymob * weight_train[index, 2] + opmob * weight_train[index, 3] +
                   mypotmob * weight_train[index, 4] + oppotmob * weight_train[index, 5] +
                   mystab * weight_train[index, 6] + opstab * weight_train[index, 7] +
                   unstab * weight_train[index, 8] + deltakeyano * weight_train[index, 9];
            eval += get_score_edge_all(board, color, index) / 3;

            return(eval);
        }
Ejemplo n.º 10
0
        private void GenerateEmpties(Point position)
        {
            var item = new Empty(new Point(position));

            Empties.Add(item);
        }