Пример #1
0
        protected Point FindMove()
        {
            Point move;

            searchtimes = 0;
            if (turnCount == 0)
            {
                return(new Point(7, 7));
            }
            else if (turnCount == 1)
            {
                Random rand = new Random();
                for (int i = 0; i < 15; i++)
                {
                    for (int j = 0; j < 15; j++)
                    {
                        if (board[i, j] != PieceType.Empty)
                        {
                            return(new Point(i + 1, j + 1));
                        }
                    }
                }
                return(new Point(7, 7));
            }
            else
            {
                BoardOfFIR newBd = new BoardOfFIR();
                newBd.boardCopy(this);
                Evalute e = getEvalute();
                newBd.FindBestMove(-10000, 10000, AIdepth, turn, out move, e);
                //ShowInfo(newBd.searchtimes);
            }
            return(move);
        }
Пример #2
0
        Evalute getEvalute()
        {
            Evalute e = new Evalute();

            statistics();
            e.judge[0] = this.judge[0];
            e.judge[1] = this.judge[1];
            return(e);
        }
Пример #3
0
        Evalute PointEvaluteChange(Point p, PieceType color)
        {
            PieceType origin = board[p.X, p.Y];
            Evalute   e1     = PointEvalute(p);

            board[p.X, p.Y] = color;
            Evalute e2 = PointEvalute(p);

            board[p.X, p.Y] = origin;
            return(e2 - e1);
        }
Пример #4
0
        Evalute PointEvalute(Point p)
        {
            Evalute e = new Evalute();

            judge[0].init();
            judge[1].init();
            lineStatistics(new Point(0, p.Y), direction.Horizontal);
            lineStatistics(new Point(p.X, 0), direction.Vertical);
            int min = Math.Min(p.X, p.Y);

            lineStatistics(new Point(p.X - min, p.Y - min), direction.DiagonalUp);
            if (p.X + p.Y > 14)
            {
                lineStatistics(new Point(p.X + p.Y - 14, 14), direction.DiagonalDown);
            }
            else
            {
                lineStatistics(new Point(0, p.X + p.Y), direction.DiagonalDown);
            }
            e.judge[0] = this.judge[0];
            e.judge[1] = this.judge[1];
            return(e);
        }
Пример #5
0
        int FindBestMove(int alpha, int beta, int depth, PieceType color, out Point bestP, Evalute e)//该函数用alphabeta搜索,以在今后搜索更深的深度
        {
            bestP = new Point(0, 0);
            int          i, j;
            Point        tP        = new Point();
            PieceType    oppoColor = oppoPiece(color);
            int          temp;
            Point        tempPoint;
            Evalute      tempE;
            int          best     = -10000;
            List <Point> movelist = new List <Point>();//get possible move

            for (i = 0; i < 15; i++)
            {
                for (j = 0; j < 15; j++)
                {
                    tP.X = i; tP.Y = j;
                    if ((int)board[i, j] >= 2 && isAround(tP) && (board [i, j] != PieceType.Banned || color == PieceType.White))
                    {
                        movelist.Add(tP);
                    }
                }
            }
            if (AIdepth == depth)
            {
                for (i = 0; i < 15; i++)
                {
                    for (j = 0; j < 15; j++)
                    {
                        if (board[i, j] == PieceType.Banned)
                        {
                            board[i, j] = PieceType.Empty;
                        }
                    }
                }
            }
            if (e.judge[(int)color].H4 + e.judge[(int)color].L4 > 0)
            {
                for (i = 0; i < 15; i++)
                {
                    for (j = 0; j < 15; j++)
                    {
                        tP.X = i; tP.Y = j;
                        if ((int)board[i, j] >= 2 && checkWin(color, tP) && (board[i, j] != PieceType.Banned || color == PieceType.White))
                        {
                            bestP = tP;
                            return(5000);
                        }
                    }
                }
            }
            if (e.judge[(int)oppoColor].L4 > 0)
            {
                for (i = 0; i < 15; i++)
                {
                    for (j = 0; j < 15; j++)
                    {
                        tP.X = i; tP.Y = j;
                        if ((int)board[i, j] >= 2 && checkWin(oppoColor, tP) && (board[i, j] != PieceType.Banned || oppoColor == PieceType.White))
                        {
                            bestP = tP;
                            return(-5000 + e.getscore(color));
                        }
                    }
                }
            }
            if (e.judge[(int)oppoColor].H4 > 0)
            {
                for (i = 0; i < 15; i++)
                {
                    for (j = 0; j < 15; j++)
                    {
                        tP.X = i; tP.Y = j;
                        if ((int)board[i, j] >= 2 && checkWin(oppoColor, tP) && (board[i, j] != PieceType.Banned || oppoColor == PieceType.White))
                        {
                            bestP = tP;
                            tempE = PointEvaluteChange(tP, color) + e;
                            if (depth == AIdepth)
                            {
                                return(e.getscore(color));
                            }
                            setPieceType(tP, color);
                            temp = -FindBestMove(-beta, -alpha, depth - 1, oppoColor, out tempPoint, tempE);
                            setPieceType(tP, PieceType.Empty);
                            return(temp);
                        }
                    }
                }
            }
            if (e.judge[(int)color].L3 > 0)
            {
                for (i = 0; i < 15; i++)
                {
                    for (j = 0; j < 15; j++)
                    {
                        tP.X = i; tP.Y = j;
                        if ((int)board[i, j] >= 2 && isOpenFour(tP, color) && (board[i, j] != PieceType.Banned || oppoColor == PieceType.White))
                        {
                            bestP = tP;
                            return(5000);
                        }
                    }
                }
            }
            if (e.judge[(int)color].H3 > 0 && AIdepth <= depth + 1)
            {
                if (VCF(out tempPoint, color, e))
                {
                    bestP = tempPoint;
                    return(4000);
                }
            }
            if (depth <= 0)
            {
                searchtimes++;
                return(e.getscore(color));
            }
            foreach (Point p in movelist)
            {
                tempE = PointEvaluteChange(p, color) + e;
                setPieceType(p, color);
                temp = -FindBestMove(-beta, -alpha, depth - 1, oppoColor, out tempPoint, tempE); //tempPoint为下一步最好的走法
                setPieceType(p, PieceType.Empty);
                if (temp > best)                                                                 // 找到最佳值
                {
                    best = temp;
                    if (temp >= beta)// 找到一个Beta走法
                    {
                        bestP = p;
                        break;//提高效率所在
                    }
                    if (temp > alpha)
                    {
                        bestP = p;
                        alpha = temp;                               // '缩小Alpha-Beta边界
                    }
                }
            }
            if (best < -3000 && depth == AIdepth)//输局已定,选择位置得分最高的点
            {
                int better = -10000;
                foreach (Point p in movelist)
                {
                    temp = PointEvaluteChange(p, oppoColor).getscore(oppoColor);
                    if (temp > better)
                    {
                        better = temp;
                        bestP  = p;
                    }
                }
            }
            return(best);
        }
Пример #6
0
        bool VCF(out Point move, PieceType color, Evalute e)
        {
            move = new Point(7, 7);
            Point        tP = new Point();
            int          i, j;
            bool         temp;
            Point        tempP;
            Evalute      tempE1, tempE2;
            PieceType    oppoColor = oppoPiece(color);
            List <Point> movelist  = new List <Point>();

            for (i = 0; i < 15; i++)
            {
                for (j = 0; j < 15; j++)
                {
                    tP.X = i; tP.Y = j;
                    if (board[i, j] == PieceType.Empty && isFour(tP, color))
                    {
                        movelist.Add(tP);
                    }
                }
            }
            foreach (Point p in movelist)
            {
                tempE1 = PointEvaluteChange(p, color) + e;
                setPieceType(p, color);
                for (i = 0; i < 15; i++)
                {
                    for (j = 0; j < 15; j++)
                    {
                        tP.X = i; tP.Y = j;
                        if ((int)board[i, j] >= 2 && checkWin(color, tP))
                        {
                            tempE2 = PointEvaluteChange(tP, oppoColor) + tempE1;
                            if (tempE2.judge[(int)color].H4 > 0)//由于不可能出现活4,只需判断冲4
                            {
                                setPieceType(p, PieceType.Empty);
                                move = p;
                                return(true);
                            }
                            else if (tempE2.judge[(int)oppoColor].L4 + tempE2.judge[(int)oppoColor].H4 > 0)
                            {
                                setPieceType(p, PieceType.Empty);
                                continue;
                            }
                            else if (tempE2.judge[(int)color].L3 > 0)
                            {
                                move = p;
                                setPieceType(p, PieceType.Empty);
                                return(true);
                            }
                            else if (tempE2.judge[(int)color].H3 > 0)
                            {
                                setPieceType(tP, oppoColor);
                                temp = VCF(out tempP, color, tempE2);
                                setPieceType(tP, PieceType.Empty);
                                if (temp)
                                {
                                    move = p;
                                    setPieceType(p, PieceType.Empty);
                                    return(true);
                                }
                            }
                        }
                    }
                }
                setPieceType(p, PieceType.Empty);
            }
            return(false);
        }