Esempio n. 1
0
        /// <summary>
        /// 初始化所有委托
        /// </summary>
        public void InitHandle()
        {
            //如果两项委托为空
            if (GeneticAlgorithm.fitnessCalculator == null || Genome.genomeCreater == null)
            {
                //适应度度量函数
                GeneticAlgorithm.fitnessCalculator = delegate(Genome gene)
                {
                    //打开人工智能探路标记
                    Conf.AIRouterState = true;

                    //当前串中最大的威力
                    double maxPower = double.MinValue;

                    //当前串中最小的威力
                    double minPower = double.MaxValue;

                    //当前串中最大的威力棋子的位置
                    int maxPowerIndex = 0;

                    //当前串中最小的威力棋子的位置
                    int minPowerIndex = 0;

                    //设定当前添加的棋子类型
                    ChessType currentType = AIType;

                    for (int i = 0; i < gene.Genes.Length; i++)
                    {
                        //模拟更新棋盘中的棋子类型
                        Chess.UpdateChessTypeForAI(gene.Genes[i], currentType);

                        //计算单子威力*位权
                        double tempPower = SinglePower(gene.Genes[i], currentType) * 1 * Math.Pow(0.96, i);

                        if (tempPower > maxPower)
                        {
                            maxPowerIndex = i;
                            maxPower      = tempPower;
                        }

                        if (tempPower < minPower)
                        {
                            minPowerIndex = i;
                            minPower      = tempPower;
                        }

                        //当前的最大威力值>权值*100  退出循环
                        if (maxPower > (100 * Math.Pow(0.96, i)))
                        {
                            break;
                        }

                        //转换下棋方,currentTpye变换
                        if (currentType == ChessType.BLACK)
                        {
                            currentType = ChessType.WHITE;
                        }
                        else
                        {
                            currentType = ChessType.BLACK;
                        }
                    }

                    //赋值适应度
                    if (Math.Abs(minPower) > Math.Abs(maxPower))
                    {
                        gene.BestGenIndex = minPowerIndex;
                        gene.Fitness      = minPower;
                    }
                    else
                    {
                        gene.Fitness      = maxPower;
                        gene.BestGenIndex = maxPowerIndex;
                    }

                    //恢复对棋盘的改变
                    for (int i = 0; i < gene.Genes.Length; i++)
                    {
                        Chess.UpdateChessTypeForAI(gene.Genes[i], ChessType.NULL);
                    }

                    //关闭人工智能探路标记
                    Conf.AIRouterState = false;
                };

                //创建随机基因组的函数
                Genome.genomeCreater = createGensHandle;
            }
        }
Esempio n. 2
0
 private bool IsWinLeftOblique(ChessType chessType)
 {
     return(GetConnectCount(chessType, 1, 1) + GetConnectCount(chessType, -1, -1) - 1 == GameDef.win_count);
 }
Esempio n. 3
0
        private static int CtDrctnlFlips(ChessType[] board, int boardIndex, int inc, ChessType color, ChessType oppcolor)
        {
            int count;
            int pt = boardIndex + inc;

            if (board[pt] == oppcolor)
            {
                count = 1;
                pt   += inc;
                if (board[pt] == oppcolor) /*2*/
                {
                    count = 2;
                    pt   += inc;
                    if (board[pt] == oppcolor) /*3*/
                    {
                        count = 3;
                        pt   += inc;
                        if (board[pt] == oppcolor)/*4*/
                        {
                            count = 4;
                            pt   += inc;
                            if (board[pt] == oppcolor) /*5*/
                            {
                                count = 5;
                                pt   += inc;
                                if (board[pt] == oppcolor)/*6*/
                                {
                                    count = 6;
                                    pt   += inc;
                                }
                            }
                        }
                    }
                }
                if (board[pt] == color)
                {
                    return(count);
                }
            }
            return(0);
        }
Esempio n. 4
0
 public static ChessCharacterAttribute GetChessProperties(ChessType chessType)
 {
     return((ChessCharacterAttribute)typeof(ChessType)
            .GetField(chessType.ToString(), BindingFlags.Public | BindingFlags.Static)
            .GetCustomAttributes(false)[0]);
 }
Esempio n. 5
0
 private bool IsWinHorizontal(ChessType chessType)
 {
     return(GetConnectCount(chessType, 1, 0) + GetConnectCount(chessType, -1, 0) - 1 == GameDef.win_count);
 }
Esempio n. 6
0
 // Use this for initialization
 void Start()
 {
     chessPath = new Stack <GameObject>();
     grid      = new int[15, 15];
     turn      = ChessType.black;
 }
Esempio n. 7
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.StaEval2(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);
            }
        }
Esempio n. 8
0
        public static ChessType GetOppositeChessType(ChessType chessType)
        {
            ChessType retChess = chessType == ChessType.Black ? ChessType.White : ChessType.Black;

            return(retChess);
        }
Esempio n. 9
0
    // 检查斜边情况
    int CheckBiasLink(int px, int py, ChessType type)
    {
        int ret       = 0;
        int linkCount = 1;

        // 左下
        for (int x = px - 1, y = py - 1; x >= 0 && y >= 0; x--, y--)
        {
            if (Get(x, y) == type)
            {
                linkCount++;

                if (linkCount >= WinChessCount)
                {
                    return(linkCount);
                }
            }
            else
            {
                break;
            }
        }

        // 右上
        for (int x = px + 1, y = py + 1; x < Board.CrossCount && y < Board.CrossCount; x++, y++)
        {
            if (Get(x, y) == type)
            {
                linkCount++;

                if (linkCount >= WinChessCount)
                {
                    return(linkCount);
                }
            }
            else
            {
                break;
            }
        }

        ret       = linkCount;
        linkCount = 1;
        // 左上
        for (int x = px - 1, y = py + 1; x >= 0 && y < Board.CrossCount; x--, y++)
        {
            if (Get(x, y) == type)
            {
                linkCount++;

                if (linkCount >= WinChessCount)
                {
                    return(linkCount);
                }
            }
            else
            {
                break;
            }
        }


        // 右下
        for (int x = px + 1, y = py - 1; x < Board.CrossCount && y >= 0; x++, y--)
        {
            if (Get(x, y) == type)
            {
                linkCount++;

                if (linkCount >= WinChessCount)
                {
                    return(linkCount);
                }
            }
            else
            {
                break;
            }
        }

        return(Mathf.Max(ret, linkCount));
    }
Esempio n. 10
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);
        }
Esempio n. 11
0
        public int NoParEndSolve(ChessType[] board, int alpha, int beta, ChessType color, int empties, int discdiff, int prevmove)
        {
            int       score = -highestScore;
            ChessType oppcolor = 2 - color;
            int       sqnum = -1, j, j1;
            int       eval;
            Empties   em, old_em;

            nodes++;
            for (old_em = EmHead, em = old_em.Succ; em != null; old_em = em, em = em.Succ)
            {
                sqnum = em.Square;
                j     = RuleUtils.DoFlips(board, sqnum, color, oppcolor);
                if (j > 0)
                {
                    board[sqnum] = color;
                    old_em.Succ  = em.Succ;
                    if (empties == 2)
                    {
                        j1 = RuleUtils.CountFlips(board, EmHead.Succ.Square, oppcolor, color);
                        if (j1 > 0)//
                        {
                            eval = discdiff + 2 * (j - j1);
                        }
                        else //pass
                        {
                            j1   = RuleUtils.CountFlips(board, EmHead.Succ.Square, color, oppcolor);
                            eval = discdiff + 2 * j;
                            if (j1 > 0)//
                            {
                                eval += 2 * (j1 + 1);
                            }
                            else
                            {//both pass
                                if (eval >= 0)
                                {
                                    eval += 2;
                                }
                            }
                        }
                    }
                    else
                    {
                        eval = -NoParEndSolve(board, -beta, -alpha,
                                              oppcolor, empties - 1, -discdiff - 2 * j - 1, sqnum);
                    }
                    RuleUtils.UndoFlips(board, j, oppcolor);
                    board[sqnum] = ChessType.EMPTY;
                    old_em.Succ  = em;
                    //purning
                    if (eval > score)
                    {
                        score = eval;

                        if (empties == searchDepth)
                        {
                            bestMove = sqnum;
                        }
                        if (eval > alpha)
                        {
                            if (eval >= beta)
                            { // purning
                                return(score);
                            }
                            alpha = eval;
                        }
                    }
                }
            }

            if (score == -highestScore)
            {
                if (empties == searchDepth)
                {
                    bestMove = MVPASS;
                }
                if (prevmove == 0)
                {
                    if (discdiff > 0)
                    {
                        return(discdiff + empties);
                    }
                    if (discdiff < 0)
                    {
                        return(discdiff - empties);
                    }
                    return(0);
                }
                else
                {
                    return(-NoParEndSolve(board, -beta, -alpha, oppcolor, empties, -discdiff, 0));
                }
            }
            return(score);
        }
Esempio n. 12
0
        public int ParEndSolve(ChessType[] board, int alpha, int beta, ChessType color, int empties, int discdiff, int prevmove)
        {
            int       score    = -highestScore;
            ChessType oppcolor = 2 - color;
            int       sqnum    = -1;
            int       j;
            int       eval;
            Empties   em, old_em;
            int       holepar;

            //
            nodes++;

            for (old_em = EmHead, em = old_em.Succ; em != null; old_em = em, em = em.Succ)
            {
                holepar = em.HoleId;
                holepar = em.Square;
                holepar = em.HoleId;
                sqnum   = em.Square;
                j       = RuleUtils.DoFlips(board, sqnum, color, oppcolor);

                if (j > 0)
                {
                    //
                    board[sqnum] = color;
                    //
                    RegionParity ^= (uint)holepar;
                    //
                    old_em.Succ = em.Succ;
                    if (empties <= 1 + Constants.Use_Parity)
                    {
                        eval = -NoParEndSolve(board, -beta, -alpha, oppcolor, empties - 1, -discdiff - 2 * j - 1, sqnum);
                    }
                    else
                    {
                        eval = -ParEndSolve(board, -beta, -alpha, oppcolor, empties - 1, -discdiff - 2 * j - 1, sqnum);
                    }
                    //
                    RuleUtils.UndoFlips(board, j, oppcolor);
                    RegionParity ^= (uint)holepar;
                    board[sqnum]  = ChessType.EMPTY;
                    old_em.Succ   = em;

                    if (eval > score)
                    {
                        score = eval;
                        //=========================================
                        if (empties == searchDepth)
                        {
                            bestMove = sqnum;
                            //StaticMethod.UpdateMessage(score, nodes,sqnum);
                            // StaticMethod.UpdateThinkingMove(sqnum);
                        }
                        if (eval > alpha)
                        {
                            if (eval >= beta)
                            {
                                return(score);
                            }
                            alpha = eval;
                        }
                    }
                }
            }

            if (score == -highestScore)
            {
                if (empties == searchDepth)
                {
                    bestMove = MVPASS;
                }

                if (prevmove == 0)
                {
                    if (discdiff > 0)
                    {
                        return(discdiff + empties);
                    }
                    if (discdiff < 0)
                    {
                        return(discdiff - empties);
                    }
                    return(0);
                }
                else
                {
                    return(-ParEndSolve(board, -beta, -alpha, oppcolor, empties, -discdiff, 0));
                }
            }
            return(score);
        }
Esempio n. 13
0
        private static int getUnStab(ChessType[] board, ChessType color)
        {
            int num  = 0;
            int half = 0;

            //if (board[10] != color && board[11] == color && (board[17] != color || !(board[12] == color && board[13] == color && board[14] == color && board[15] == color && board[16] == color)))
            //    num++;
            //if (board[17] != color && board[16] == color && (board[10] != color || !(board[11] == color && board[12] == color && board[13] == color && board[14] == color && board[15] == color)))
            //    num++;
            //if (board[10] != color && board[19] == color && (board[73] != color || !(board[28] == color && board[37] == color && board[46] == color && board[55] == color && board[64] == color)))
            //    num++;
            //if (board[20] == color)
            //    num++;
            //if (board[25] == color)
            //    num++;
            //if (board[65] == color)
            //    num++;
            //if (board[70] == color)
            //    num++;
            //return num;
            if (board[10] != color && board[20] == color)
            {
                if ((board[80] == color) && (board[30] == color && board[40] == color && board[50] == color && board[60] == color && board[70] == color))
                {
                    half++;
                }
                else
                {
                    num++;
                }
            }
            if (board[17] != color && board[25] == color)
            {
                if ((board[73] == color) && (board[33] == color && board[41] == color && board[49] == color && board[57] == color && board[65] == color))
                {
                    half++;
                }
                else
                {
                    num++;
                }
            }
            //if (board[17] != color && board[26] == color && (board[80] != color || !(board[35] == color && board[44] == color && board[53] == color && board[62] == color && board[71] == color)))
            //    num++;
            //if (board[73] != color && board[64] == color && (board[10] != color || !(board[19] == color && board[28] == color && board[37] == color && board[46] == color && board[55] == color)))
            //    num++;
            if (board[73] != color && board[65] == color)
            {
                if ((board[17] == color) && (board[25] == color && board[33] == color && board[41] == color && board[49] == color && board[57] == color))
                {
                    half++;
                }
                else
                {
                    num++;
                }
            }
            if (board[80] != color && board[70] == color)
            {
                if ((board[10] == color) && (board[20] == color && board[30] == color && board[40] == color && board[50] == color && board[60] == color))
                {
                    half++;
                }
                else
                {
                    num++;
                }
            }
            //if (board[80] != color && board[71] == color && (board[17] != color || !(board[26] == color && board[35] == color && board[44] == color && board[53] == color && board[62] == color)))
            //    num++;
            //if (board[73] != color && board[74] == color && (board[80] != color || !(board[75] == color && board[76] == color && board[77] == color && board[78] == color && board[79] == color)))
            //    num++;
            //if (board[80] != color && board[79] == color && (board[73] != color || !(board[74] == color && board[75] == color && board[76] == color && board[77] == color && board[78] == color)))
            //    num++;
            return(num + num + num + half);
        }
Esempio n. 14
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);
        }
Esempio n. 15
0
        private MinMaxSearchInfo MinMaxSearch(Model pModel, ChessType chessType, bool isMaxLayer, int depth, int alpha, int beta)
        {
            MinMaxSearchCount++;
            int bestScore = isMaxLayer ? -999 : 999;
            MinMaxSearchInfo bestPosInfo = new MinMaxSearchInfo(-1, -1, bestScore);

            Console.WriteLine($"depth: {depth} isMaxLayer: {isMaxLayer} MinMaxSearchCount: {MinMaxSearchCount.ToString()} alpha: {alpha.ToString()} beta: {beta}");

            for (int y = 0; y < GameDef.board_cell_length; y++)
            {
                for (int x = 0; x < GameDef.board_cell_length; x++)
                {
                    var board = pModel.GetBoardByCopy();
                    if (board[y][x] == ChessType.None)
                    {
                        Model cloneModel = pModel.Clone() as Model;
                        cloneModel.PutChessToBoard(x, y, chessType);

                        int         score       = 0;
                        BoradStatus boradStatus = GetBoardStatus(cloneModel, chessType);

                        if (boradStatus == BoradStatus.Nothing) //沒有勝負 繼續往下找
                        {
                            ChessType        nextChessType = Utility.GetOppositeChessType(chessType);
                            MinMaxSearchInfo info          = MinMaxSearch(cloneModel, nextChessType, !isMaxLayer, depth + 1, alpha, beta);

                            //--------- alpha-beta pruning ---------
                            if (isMaxLayer)
                            {
                                alpha = Math.Max(alpha, info.Score);
                            }
                            else
                            {
                                beta = Math.Min(beta, info.Score);
                            }

                            if (alpha >= beta)
                            {
                                return(info);
                            }
                            //--------------------------------------

                            score = info.Score;
                        }
                        else if (boradStatus == BoradStatus.Winlose)
                        {
                            SearchHasResultCount++;
                            score = MyChessType == chessType  ? 1 : -1;//If chess is mychessType get 1 point else get -1 point
                        }
                        else if (boradStatus == BoradStatus.Tie)
                        {
                            SearchHasResultCount++;
                            score = 0;
                        }

                        if (isMaxLayer)
                        {
                            if (score > bestPosInfo.Score)
                            {
                                bestPosInfo.Score = score;
                                bestPosInfo.X     = x;
                                bestPosInfo.Y     = y;
                            }
                        }
                        else
                        {
                            if (score < bestPosInfo.Score)
                            {
                                bestPosInfo.Score = score;
                                bestPosInfo.X     = x;
                                bestPosInfo.Y     = y;
                            }
                        }
                    }
                }
            }

            return(bestPosInfo);
        }
    public virtual int SetCheckScore(int x, int y, ChessType chessType)
    {
        Vector2Int inputPos = new Vector2Int(x, y);
        int        score    = 0;

        for (int md = 0; md < 4; md++)
        {
            ChessBoardManager.MoveDir _moveDir = (ChessBoardManager.MoveDir)md;
            string str = "1";
            int    xDir = 0, yDir = 0;
            switch (_moveDir)
            {
            case ChessBoardManager.MoveDir.TransverseLine:
                xDir = 1; yDir = 0;
                break;

            case ChessBoardManager.MoveDir.VerticalLine:
                xDir = 0; yDir = 1;
                break;

            case ChessBoardManager.MoveDir.LeftSlantLine:
                xDir = 1; yDir = -1;
                break;

            case ChessBoardManager.MoveDir.RightSlantLine:
                xDir = 1; yDir = 1;
                break;

            default:
                break;
            }

            for (int dir = -1; dir <= 1; dir += 2)
            {
                for (int i = 1; i < chessMaxBoard; i++)
                {
                    Vector2Int newPoint = new Vector2Int(inputPos.x + dir * xDir * i, inputPos.y + dir * yDir * i);
                    if (chessBoardManager.CheckBorder(newPoint))
                    {
                        string ch;
                        if (chessBoardManager.GridArray[newPoint.x, newPoint.y] == chessType)
                        {
                            ch = "1";
                        }
                        else if (chessBoardManager.GridArray[newPoint.x, newPoint.y] == ChessType.None)
                        {
                            ch = "0";
                        }
                        else
                        {
                            break;
                        }
                        if (dir < 0)
                        {
                            str = ch + str;
                        }
                        else if (dir > 0)
                        {
                            str = str + ch;
                        }
                        if (ch == "0")
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            int _s;
            if (scoreDic.TryGetValue(str, out _s))
            {
                score += _s;
            }
        }


        return(score);
    }
Esempio n. 17
0
 public TournamentTypeCondition(ChessType value) => _value = value;
Esempio n. 18
0
        /// <summary>
        /// 计算一个棋子的通用威力
        /// </summary>
        /// <param name="point"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static int CommonPower(Point point, ChessType type)
        {
            List <int> powerContainer = new List <int>();

            //计算棋子通用威力

            //单二
            if (checkDirectOnChessForSingle(point, Direction.UP, type) + 1 == 2)
            {
                powerContainer.Add(5);
            }
            if (checkDirectOnChessForSingle(point, Direction.UpLeft, type) + 1 == 2)
            {
                powerContainer.Add(5);
            }
            if (checkDirectOnChessForSingle(point, Direction.Left, type) + 1 == 2)
            {
                powerContainer.Add(5);
            }
            if (checkDirectOnChessForSingle(point, Direction.DownLeft, type) + 1 == 2)
            {
                powerContainer.Add(5);
            }
            if (checkDirectOnChessForSingle(point, Direction.Down, type) + 1 == 2)
            {
                powerContainer.Add(5);
            }
            if (checkDirectOnChessForSingle(point, Direction.DownRight, type) + 1 == 2)
            {
                powerContainer.Add(5);
            }
            if (checkDirectOnChessForSingle(point, Direction.Right, type) + 1 == 2)
            {
                powerContainer.Add(5);
            }
            if (checkDirectOnChessForSingle(point, Direction.UpRight, type) + 1 == 2)
            {
                powerContainer.Add(5);
            }

            //单一
            if (checkDirectOnChessForSingle(point, Direction.UP, type) + 1 == 1)
            {
                powerContainer.Add(1);
            }
            if (checkDirectOnChessForSingle(point, Direction.UpLeft, type) + 1 == 1)
            {
                powerContainer.Add(1);
            }
            if (checkDirectOnChessForSingle(point, Direction.Left, type) + 1 == 1)
            {
                powerContainer.Add(1);
            }
            if (checkDirectOnChessForSingle(point, Direction.DownLeft, type) + 1 == 1)
            {
                powerContainer.Add(1);
            }
            if (checkDirectOnChessForSingle(point, Direction.Down, type) + 1 == 1)
            {
                powerContainer.Add(1);
            }
            if (checkDirectOnChessForSingle(point, Direction.DownRight, type) + 1 == 1)
            {
                powerContainer.Add(1);
            }
            if (checkDirectOnChessForSingle(point, Direction.Right, type) + 1 == 1)
            {
                powerContainer.Add(1);
            }
            if (checkDirectOnChessForSingle(point, Direction.UpRight, type) + 1 == 1)
            {
                powerContainer.Add(1);
            }

            //活二
            if (ChessRule.checkDirectOnChessForActive(point, Direction.Down, type) + ChessRule.checkDirectOnChessForActive(point, Direction.UP, type) + 1 == 2)
            {
                powerContainer.Add(7);
            }
            if (ChessRule.checkDirectOnChessForActive(point, Direction.DownLeft, type) + ChessRule.checkDirectOnChessForActive(point, Direction.UpRight, type) + 1 == 2)
            {
                powerContainer.Add(7);
            }
            if (ChessRule.checkDirectOnChessForActive(point, Direction.DownRight, type) + ChessRule.checkDirectOnChessForActive(point, Direction.UpLeft, type) + 1 == 2)
            {
                powerContainer.Add(7);
            }
            if (ChessRule.checkDirectOnChessForActive(point, Direction.Left, type) + ChessRule.checkDirectOnChessForActive(point, Direction.Right, type) + 1 == 2)
            {
                powerContainer.Add(7);
            }

            //活一
            if (ChessRule.checkDirectOnChessForActive(point, Direction.Down, type) + ChessRule.checkDirectOnChessForActive(point, Direction.UP, type) + 1 == 1)
            {
                powerContainer.Add(3);
            }
            if (ChessRule.checkDirectOnChessForActive(point, Direction.DownLeft, type) + ChessRule.checkDirectOnChessForActive(point, Direction.UpRight, type) + 1 == 1)
            {
                powerContainer.Add(3);
            }
            if (ChessRule.checkDirectOnChessForActive(point, Direction.DownRight, type) + ChessRule.checkDirectOnChessForActive(point, Direction.UpLeft, type) + 1 == 1)
            {
                powerContainer.Add(3);
            }
            if (ChessRule.checkDirectOnChessForActive(point, Direction.Left, type) + ChessRule.checkDirectOnChessForActive(point, Direction.Right, type) + 1 == 1)
            {
                powerContainer.Add(3);
            }

            //排序
            powerContainer.Sort();

            //返回最大的两个值和
            return(powerContainer[powerContainer.Count - 2] + powerContainer[powerContainer.Count - 1]);
        }
Esempio n. 19
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.StaEval2(board, color, oppcolor, empties, EmHead);
                if (score1 >= beta || score1 >= 4000 || 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.StaEval2(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);
            }
        }
Esempio n. 20
0
 void ShowResult(ChessType winside)
 {
     rst.gameObject.SetActive(true);
     rst.Show(winside);
 }
Esempio n. 21
0
 void ShowResult(ChessType winside)
 {
     ResultWindow.gameObject.SetActive(true);
     ResultWindow.Show(winside);
 }
 public ChessInfo(Vector2Int _pos, ChessType _chessType, GameObject _go)
 {
     pos       = _pos;
     chessType = _chessType;
     go        = _go;
 }
Esempio n. 23
0
 public bool IsWin(ChessType chessType)
 {
     //Model.printBoard();
     return(IsWinHorizontal(chessType) || IsWinVertical(chessType) || IsWinRightOblique(chessType) || IsWinLeftOblique(chessType));
 }
Esempio n. 24
0
 public void setData(int index, ChessType type)
 {
     this.index = index;
     this.type  = type;
 }
Esempio n. 25
0
 private bool IsWinVertical(ChessType chessType)
 {
     return(GetConnectCount(chessType, 0, 1) + GetConnectCount(chessType, 0, -1) - 1 == GameDef.win_count);
 }
Esempio n. 26
0
 public void setData(int x, int y, ChessType type)
 {
     this.index++;
     this.grid[x, y].setData(index, type);
 }
Esempio n. 27
0
    public ChessType CheckVictoryCondition(ChessInfo[,] b)
    {
        // check each coloum
        for (int i = 0; i < 4; i++)
        {
            if (b[i, 0] == null)
            {
                continue;
            }
            var baseShape = b[i, 0].baseShape;
            var height    = b[i, 0].height;
            var isConcave = b[i, 0].IsConcave;
            var chessType = b[i, 0].chessType;

            for (int j = 1; j < 4; j++)
            {
                if (b[i, j] == null)
                {
                    break;
                }

                if (b[i, j].baseShape != baseShape)
                {
                    break;
                }

                if (b[i, j].chessType != chessType)
                {
                    break;
                }
                if (j == b.GetLength(1) - 1)
                {
                    print("return " + chessType);
                    return(chessType);
                }
            }


            for (int j = 1; j < 4; j++)
            {
                if (b[i, j] == null || b[i, j].height != height || b[i, j].chessType != chessType)
                {
                    break;
                }
                if (j == b.GetLength(1) - 1)
                {
                    print("return " + chessType);
                    return(chessType);
                }
            }

            for (int j = 0; j < 4; j++)
            {
                if (b[i, j] == null || b[i, j].IsConcave != isConcave || b[i, j].chessType != chessType)
                {
                    break;
                }
                if (j == b.GetLength(1) - 1)
                {
                    print("return " + chessType);
                    return(chessType);
                }
            }
        }

        for (int i = 0; i < 4; i++)
        {
            if (b[0, i] == null)
            {
                continue;
            }
            var baseShape      = b[0, i].baseShape;
            var height         = b[0, i].height;
            var hasCircleOnTop = b[0, i].IsConcave;
            var chessType      = b[0, i].chessType;
            for (int j = 1; j < 4; j++)
            {
                if (b[j, i] == null || b[j, i].baseShape != baseShape || b[j, i].chessType != chessType)
                {
                    break;
                }
                if (j == b.GetLength(1) - 1)
                {
                    print("return " + chessType);
                    return(chessType);
                }
            }

            for (int j = 1; j < 4; j++)
            {
                if (b[j, i] == null || b[j, i].height != height || b[j, i].chessType != chessType)
                {
                    break;
                }
                if (j == b.GetLength(1) - 1)
                {
                    print("return " + chessType);
                    return(chessType);
                }
            }

            for (int j = 1; j < 4; j++)
            {
                if (b[j, i] == null || b[j, i].IsConcave != hasCircleOnTop || b[j, i].chessType != chessType)
                {
                    break;
                }
                if (j == b.GetLength(1) - 1)
                {
                    print("return " + chessType);
                    return(chessType);
                }
            }
        }

        // 正对角线
        if (b[0, 0] != null)
        {
            var baseShape      = b[0, 0].baseShape;
            var height         = b[0, 0].height;
            var hasCircleOnTop = b[0, 0].IsConcave;
            var chessType      = b[0, 0].chessType;
            for (int j = 1; j < 4; j++)
            {
                if (b[j, j] == null || b[j, j].baseShape != baseShape || b[j, j].chessType != chessType)
                {
                    break;
                }
                if (j == b.GetLength(1) - 1)
                {
                    print("return " + chessType);
                    return(chessType);
                }
            }

            for (int j = 1; j < 4; j++)
            {
                if (b[j, j] == null || b[j, j].height != height || b[j, j].chessType != chessType)
                {
                    break;
                }
                if (j == b.GetLength(1) - 1)
                {
                    print("return " + chessType);
                    return(chessType);
                }
            }

            for (int j = 1; j < 4; j++)
            {
                if (b[j, j] == null || b[j, j].IsConcave != hasCircleOnTop || b[j, j].chessType != chessType)
                {
                    break;
                }
                if (j == b.GetLength(1) - 1)
                {
                    print("return " + chessType);
                    return(chessType);
                }
            }
        }

        // 反对角线
        if (b[0, b.GetLength(0) - 1] != null)
        {
            var       baseShape = b[0, b.GetLength(0) - 1].baseShape;
            var       height    = b[0, b.GetLength(0) - 1].height;
            var       isConcave = b[0, b.GetLength(0) - 1].IsConcave;
            ChessType chessType = b[0, b.GetLength(0) - 1].chessType;
            for (int j = 1; j < b.GetLength(0); j++)
            {
                if (b[j, b.GetLength(0) - 1 - j] == null || b[j, b.GetLength(0) - 1 - j].baseShape != baseShape || b[j, b.GetLength(0) - 1 - j].chessType != chessType)
                {
                    break;
                }
                if (j == b.GetLength(1) - 1)
                {
                    print("return " + chessType);
                    return(chessType);
                }
            }

            for (int j = 0; j < b.GetLength(0); j++)
            {
                if (b[j, b.GetLength(0) - 1 - j] == null || b[j, b.GetLength(0) - 1 - j].height != height || b[j, b.GetLength(0) - 1 - j].chessType != chessType)
                {
                    break;
                }
                if (j == b.GetLength(1) - 1)
                {
                    print("return " + chessType);
                    return(chessType);
                }
            }

            for (int j = 0; j < b.GetLength(0); j++)
            {
                if (b[j, b.GetLength(0) - 1 - j] == null ||
                    b[j, b.GetLength(0) - 1 - j].IsConcave != isConcave || b[j, b.GetLength(0) - 1 - j].chessType != chessType)
                {
                    break;
                }
                if (j == b.GetLength(1) - 1)
                {
                    print("return " + chessType);
                    return(chessType);
                }
            }
        }

        return(ChessType.Null);
    }
Esempio n. 28
0
 public AIComputer3X3Player(String name, Form1 view, Model model, RoleMgr roleMgr, ChessType chessType) : base(name, view, model, roleMgr, chessType)
 {
 }
Esempio n. 29
0
        private static bool AnyDrctnlFlips(ChessType[] board, int boardIndex, int inc, ChessType color, ChessType oppcolor)
        {
            int pt = boardIndex + inc;

            if (board[pt] == oppcolor)
            {
                pt += inc;
                if (board[pt] == oppcolor) /*2*/
                {
                    pt += inc;
                    if (board[pt] == oppcolor) /*3*/
                    {
                        pt += inc;
                        if (board[pt] == oppcolor)/*4*/
                        {
                            pt += inc;
                            if (board[pt] == oppcolor) /*5*/
                            {
                                pt += inc;
                                if (board[pt] == oppcolor)/*6*/
                                {
                                    pt += inc;
                                }
                            }
                        }
                    }
                }
                return(board[pt] == color);
            }
            return(false);
        }
Esempio n. 30
0
        public static Player CreatePlayer(GameDef.PlayerType type, Form1 view, Model model, RoleMgr roleMgr, ChessType chessType)
        {
            Console.WriteLine($"Create a {type.ToString()} Player. ChessType is {chessType}");

            switch (type)
            {
            case GameDef.PlayerType.Human1:
                return(new HumanPlayer("Human1 " + chessType, view, model, roleMgr, chessType));

                break;

            case GameDef.PlayerType.Human2:
                return(new HumanPlayer("Human2 " + chessType, view, model, roleMgr, chessType));

                break;

            case GameDef.PlayerType.EasyAI:
                return(new EasyComputerPlayer("EasyAI " + chessType, view, model, roleMgr, chessType));

                break;

            case GameDef.PlayerType.MediumAI:
                return(new MinMaxComputerPlayer("MediumAI " + chessType, view, model, roleMgr, chessType, 1));

                break;

            case GameDef.PlayerType.HardAI:
                return(new MinMaxComputerPlayer("HardAI " + chessType, view, model, roleMgr, chessType, 3));

                break;

            case GameDef.PlayerType.AI3X3:
                return(new AIComputer3X3Player("AI3X3 " + chessType, view, model, roleMgr, chessType));

                break;

            case GameDef.PlayerType.RandomAI:
                return(new RandomComputerPlayer("RandomAI " + chessType, view, model, roleMgr, chessType));

                break;

            default:
                return(null);

                break;
            }
        }