Ejemplo n.º 1
0
        private ChessWeight GetWeight(int m_x, int m_y, GobangPlayer m_player)
        {
            int[] m_Weight = new int[4];
            for (int i = 0; i < 4; i++)
            {
                m_Weight[i] = GetWeightSingle(i, m_x, m_y, m_player);
            }
            ChessWeight cw = new ChessWeight(m_Weight, m_player);

            return(cw);
        }
Ejemplo n.º 2
0
        public Task <Point> CalculateComputerAI(GobangPlayer player)
        {
            return(Task.Run <Point>(() =>
            {
                Task.Delay(100);
                GobangPlayer enemy = GobangPlayer.Player1;
                if (player == enemy)
                {
                    enemy = GobangPlayer.Player2;
                }
                ChessWeight[,] m_Weights_opinion = new ChessWeight[21, 21];
                ChessWeight[,] m_Weights_enemy = new ChessWeight[21, 21];
                for (int i = 0; i < 21; i++)
                {
                    for (int j = 0; j < 21; j++)
                    {
                        if (main_chess[i, j].GoBangPlayer != GobangPlayer.NoPlayer)
                        {
                            m_Weights_opinion[i, j] = new ChessWeight(new int[] { -1, -1, -1, -1 }, player);
                            m_Weights_enemy[i, j] = new ChessWeight(new int[] { -1, -1, -1, -1 }, enemy);
                            continue;
                        }
                        m_Weights_opinion[i, j] = GetWeight(i, j, player);
                        m_Weights_enemy[i, j] = GetWeight(i, j, enemy);
                        m_Weights_opinion[i, j].WeightOppinent = m_Weights_enemy[i, j].WeightMax;
                        m_Weights_enemy[i, j].WeightOppinent = m_Weights_opinion[i, j].WeightMax;
                    }
                }
                List <Point> MaxPointComputer = new List <Point>();
                int MaxPlayer1 = 0;
                int MaxPlayer2 = 0;
                List <Point> MaxPointPlayer1 = new List <Point>();
                for (int i = 0; i < 21; i++)
                {
                    for (int j = 0; j < 21; j++)
                    {
                        if (m_Weights_opinion[i, j].TotalWeight > MaxPlayer1)
                        {
                            MaxPlayer1 = m_Weights_opinion[i, j].TotalWeight;
                            MaxPointComputer.Clear();
                            MaxPointComputer.Add(new Point(i, j));
                        }
                        else if (m_Weights_opinion[i, j].TotalWeight == MaxPlayer1)
                        {
                            MaxPointComputer.Add(new Point(i, j));
                        }

                        if (m_Weights_enemy[i, j].TotalWeight > MaxPlayer2)
                        {
                            MaxPlayer2 = m_Weights_enemy[i, j].TotalWeight;
                            MaxPointPlayer1.Clear();
                            MaxPointPlayer1.Add(new Point(i, j));
                        }
                        else if (m_Weights_enemy[i, j].TotalWeight == MaxPlayer2)
                        {
                            MaxPointPlayer1.Add(new Point(i, j));
                        }
                    }
                }
                if ((MaxPlayer1 > 49 && MaxPlayer2 < 200) || (MaxPlayer1 >= 200) && MaxPlayer2 >= 200)
                {
                    int MaxTemp = 0;
                    Point MaxPoint = new Point();
                    for (int i = 0; i < MaxPointComputer.Count; i++)
                    {
                        if (MaxTemp < m_Weights_opinion[MaxPointComputer[i].X, MaxPointComputer[i].Y].WeightOppinent)
                        {
                            MaxTemp = m_Weights_opinion[MaxPointComputer[i].X, MaxPointComputer[i].Y].WeightOppinent;
                            MaxPoint = MaxPointComputer[i];
                        }
                    }
                    return MaxPoint;
                }
                if (MaxPlayer1 >= MaxPlayer2)
                {
                    int MaxTemp = 0;
                    Point MaxPoint = new Point();
                    for (int i = 0; i < MaxPointComputer.Count; i++)
                    {
                        if (MaxTemp < m_Weights_opinion[MaxPointComputer[i].X, MaxPointComputer[i].Y].WeightOppinent)
                        {
                            MaxTemp = m_Weights_opinion[MaxPointComputer[i].X, MaxPointComputer[i].Y].WeightOppinent;
                            MaxPoint = MaxPointComputer[i];
                        }
                    }
                    return MaxPoint;
                }
                else
                {
                    int MaxTemp = 0;
                    Point MaxPoint = new Point();
                    for (int i = 0; i < MaxPointPlayer1.Count; i++)
                    {
                        if (m_Weights_enemy[MaxPointPlayer1[i].X,
                                            MaxPointPlayer1[i].Y].WeightOppinent > MaxTemp)
                        {
                            MaxTemp = m_Weights_enemy[MaxPointPlayer1[i].X,
                                                      MaxPointPlayer1[i].Y].WeightOppinent;
                            MaxPoint = MaxPointPlayer1[i];
                        }
                    }
                    return MaxPoint;
                }
            }));
        }