コード例 #1
0
        public void Running(int[,] board, int now)
        {
            Random random = new Random((int)DateTime.Now.Ticks);
            int    maxScore = -1, bestLevel = 1000;

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (board[i, j] != 0)
                    {
                        continue;
                    }
                    for (int k = 0; k <= 12; k++)
                    {
                        cntOther[k] = cntSelf[k] = 0;
                    }
                    for (int d = 0; d < 4; d++)
                    {
                        int s     = 1;
                        int empty = 0;
                        for (int x = 1; true; x++)
                        {
                            int nowX = i + dx[d] * x, nowY = j + dy[d] * x;
                            if (!InRange(nowX, nowY) || board[nowX, nowY] == 3 - now)
                            {
                                break;
                            }
                            if (board[nowX, nowY] == 0)
                            {
                                empty++;
                                break;
                            }

                            s++;
                        }

                        for (int x = 1; true; x++)
                        {
                            int nowX = i - dx[d] * x, nowY = j - dy[d] * x;
                            if (!InRange(nowX, nowY) || board[nowX, nowY] == 3 - now)
                            {
                                break;
                            }
                            if (board[nowX, nowY] == 0)
                            {
                                empty++;
                                break;
                            }

                            s++;
                        }

                        cntSelf[Math.Min((s - 1) * 3 + empty, 12)]++;
                    }

                    for (int d = 0; d < 4; d++)
                    {
                        int s     = 1;
                        int empty = 0;
                        for (int x = 1; true; x++)
                        {
                            int nowX = i + dx[d] * x, nowY = j + dy[d] * x;
                            if (!InRange(nowX, nowY) || board[nowX, nowY] == now)
                            {
                                break;
                            }
                            if (board[nowX, nowY] == 0)
                            {
                                empty++;
                                break;
                            }

                            s++;
                        }

                        for (int x = 1; true; x++)
                        {
                            int nowX = i - dx[d] * x, nowY = j - dy[d] * x;
                            if (!InRange(nowX, nowY) || board[nowX, nowY] == now)
                            {
                                break;
                            }
                            if (board[nowX, nowY] == 0)
                            {
                                empty++;
                                break;
                            }

                            s++;
                        }

                        cntOther[Math.Min((s - 1) * 3 + empty, 12)]++;
                    }

                    if (cntSelf[12] > 0)
                    {
                        x = i;
                        y = j;
                        return;
                    }

                    level[i, j] = 0;
                    if (cntOther[12] > 0)
                    {
                        level[i, j] = 1;
                    }
                    else if (cntSelf[11] > 0 || cntSelf[10] > 1)
                    {
                        level[i, j] = 2;
                    }
                    score[i, j] = 0;
                    for (int k = 0; k <= 12; k++)
                    {
                        score[i, j] += cntSelf[k] * (minValue[k] + random.Next(maxValue[k] - minValue[k] + 1));
                        score[i, j] += cntOther[k] * (otherMinValue[k] + random.Next(otherMaxValue[k] - otherMinValue[k] + 1));
                    }

                    score[i, j] += (30 - 2 * (Math.Abs(i - 7) + Math.Abs(j - 7)));
                }
            }
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (board[i, j] == 0)
                    {
                        if (level[i, j] > 0 && level[i, j] < bestLevel)
                        {
                            bestLevel = level[i, j];

                            board[i, j] = 1;
                            int f = forbidden.forbidden(board, i, j);
                            board[i, j] = 0;
                            if (f > 0)
                            {
                                continue;
                            }

                            x = i;
                            y = j;
                        }

                        if (bestLevel == 1000 && score[i, j] > maxScore)
                        {
                            maxScore = score[i, j];

                            board[i, j] = 1;
                            int f = forbidden.forbidden(board, i, j);
                            board[i, j] = 0;
                            if (f > 0)
                            {
                                continue;
                            }

                            x = i;
                            y = j;
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: HLNN/Gomoku
        private void ChessBoard_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (game.Start && game.AllowToMove)
                {
                    game.AllowToMove = false;

                    int x = (e.X - 5) / 50;
                    int y = (e.Y - 5) / 50;

                    if (game.Chess[x, y] != 0)
                    {
                        MessageBox.Show("这个地方已经下过了!!!", "WRONG");
                    }
                    else
                    {
                        if (x >= 0 && x < 15 && y >= 0 && y < 15)
                        {
                            if (game.WhoToMove == 1)
                            {
                                game.Chess[x, y] = 1;
                                int f = forbidden.forbidden(game.Chess, x, y);
                                game.Chess[x, y] = 0;

                                if (f != 0)
                                {
                                    DialogResult dialogResult = DialogResult.Yes;
                                    switch (f)
                                    {
                                    case 1:
                                        dialogResult = MessageBox.Show("三三禁手!!!是否撤回该步?", "FORBIDDEN", MessageBoxButtons.YesNo);
                                        break;

                                    case 2:
                                        dialogResult = MessageBox.Show("四四禁手!!!是否撤回该步?", "FORBIDDEN", MessageBoxButtons.YesNo);
                                        break;

                                    case 3:
                                        dialogResult = MessageBox.Show("长连禁手!!!是否撤回该步?", "FORBIDDEN", MessageBoxButtons.YesNo);
                                        break;
                                    }

                                    if (dialogResult == DialogResult.Yes)
                                    {
                                        game.AllowToMove = true;
                                        return;
                                    }
                                    else
                                    {
                                        MessageBox.Show("WHITE WIN!!!", "WIN");
                                        game.Start = false;
                                        return;
                                    }
                                }
                            }

                            game.BlackTimeThis   = 0;
                            game.WhiteTimeThis   = 0;
                            black_time_this.Text = "步时: 0秒";
                            white_time_this.Text = "步时: 0秒";

                            game.Move(x, y);

                            Image    img = ChessBoard.Image;
                            Graphics gra = Graphics.FromImage(img);
                            switch (game.WhoToMove)
                            {
                            case 1:
                                gra.FillEllipse(blackBrush, x * 50 + 15, y * 50 + 15, 30, 30);
                                break;

                            case 2:
                                gra.FillEllipse(whiteBrush, x * 50 + 15, y * 50 + 15, 30, 30);
                                break;
                            }
                            ChessBoard.Image = img;


                            if (check_win())
                            {
                                return;
                            }

                            if (game.HumanVShuman)
                            {
                                game.WhoToMove = 3 - game.WhoToMove;
                            }

                            if (game.HumanVSai)
                            {
                                game.WhoToMove = 3 - game.WhoToMove;

                                ai_move();

                                if (check_win())
                                {
                                    return;
                                }

                                game.WhoToMove = 3 - game.WhoToMove;
                            }
                        }
                    }

                    game.AllowToMove = true;
                }
            }
            catch
            {
            }
        }