示例#1
0
        static void Main(string[] args)
        {
            //      string str = GeneticAlgorithm.EncodeToGene(-46);
            //     Console.WriteLine(str);
            //    Console.WriteLine(   GeneticAlgorithm.DecodeFromGene(str));
            //     Console.Read();
            //  Console.WriteLine(TwoPointCrossOver("1111111111","0000000000"));
            // Console.ReadKey();

            GeneticAlgorithm.Learn();


            Console.ReadLine();

            int[,] field = new int[10, 25];
            field        = new int[, ]
            {
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }
            };

            var value = (NewTetrisAI.Find(field, 5, new int[1] {
                1
            }, 0, 2, true));

            TetrisEnvironment.Print(field, false);
            Console.WriteLine(value.way);
            Console.WriteLine(value.eval);
            Console.ReadLine();
        }
示例#2
0
    static public float Evaluate(int[,] field, TetrisEnvironment.Vector2[] minopositions, float beforeEval, bool arrayApply, bool test = false)
    {
        holeYs.Clear();



        int[,] field_clone;
        if (arrayApply)
        {
            field_clone = field;
        }
        else
        {
            field_clone = (int[, ])field.Clone();
        }

        int completeLine = 0;

        foreach (TetrisEnvironment.Vector2 pos in minopositions)
        {
            field_clone[pos.x, pos.y] = 1;
        }
        CheckLine(field_clone, ref completeLine);



        int sumofheight = 0;

        //高さ合計計算
        for (int x = 0; x < 10; x++)
        {
            for (int y = 25 - 1; y >= 0; y--)
            {
                if (field_clone[x, y] == 1)
                {
                    maxHeights[x] = y;
                    sumofheight  += y;
                    continue;
                }
            }
        }

        int holeCount = 0;

        //穴計算
        for (int x = 0; x < 10; x++)
        {
            for (int y = 0; y < 25 - 1; y++)
            {
                if (field_clone[x, y] == 0 && field_clone[x, y + 1] == 1)
                {
                    holeCount++;
                    holeYs.Add(new TetrisEnvironment.Vector2(x, y));
                }
            }
        }

        for (int y = 25 - 1 - 1; y >= 0; y--)
        {
            for (int x = 0; x < 10; x++)
            {
                if (field_clone[x, y] == 0 && field_clone[x, y + 1] == 1)
                {
                    holeCount++;
                    holeYs.Add(new TetrisEnvironment.Vector2(x, y));
                }
            }
        }



        int holemax;

        if (holeYs.Count < 4)
        {
            holemax = holeYs.Count;
        }
        else
        {
            holemax = 4;
        }
        //穴の上
        for (int i = 0; i < holemax; i++)
        {
            int ad = 1;
            while (true)
            {
                if (holeYs[i].y + ad < 25 && field_clone[holeYs[i].x, holeYs[i].y + ad] == 1)
                {
                    ad++;
                }
                else
                {
                    holeup[i] = ad;
                    break;
                }
            }
        }


        //一番深いのはノーカンにしよう
        int dekoboko = 0;

        for (int i = 0; i < maxHeights.Length - 1; i++)
        {
            dekoboko += Math.Abs(maxHeights[i] - maxHeights[i + 1]);
        }

        float ajust = 0;

        switch (completeLine)
        {
        case 1:
            ajust += h_line1;
            break;

        case 2:
            ajust += h_line2;
            break;

        case 3:
            ajust += h_line3;
            break;

        case 4:
            ajust += h_line4;
            break;
        }



        if (test)
        {
            TetrisEnvironment.Print(field_clone, false);
            Console.WriteLine($"でこぼこ:{ dekoboko * -0.184 }");
            Console.WriteLine($"高さ合計:{ sumofheight * -0.51f}");
            Console.WriteLine($"穴:{ holeCount * -0.6f }");
            Console.WriteLine($"クリアライン:{ completeLine * 0.76 }");
            Console.WriteLine($"合計:{(float)(sumofheight * -0.51f + completeLine * 0.76 + holeCount * -0.356f + dekoboko * -0.184)}");
            Console.ReadKey();
        }

        return((float)(sumofheight * h_sumofheight + /* completeLine * 0.76+*/ holeCount * h_holeCount + dekoboko * h_dekoboko + beforeEval * 0.6f + ajust + holeup[0] * h_holeup1 + holeup[1] * h_holeup2 + holeup[2] * h_holeup3 + holeup[3] * h_holeup4));
    }