예제 #1
0
        public static void test_auxiliar_methods()
        {
            Console.WriteLine("test_auxiliar_methods()");
            initialize_test_gameboards();

            Pentago_Rules pr_110 = new Pentago_Rules(Pentago_Rules.EvaluationFunction.func1, Pentago_Rules.NextStatesFunction.all_states, false);

            Pentago_Rules pr_111 = new Pentago_Rules(Pentago_Rules.EvaluationFunction.func1, Pentago_Rules.NextStatesFunction.all_states, true);

            Console.WriteLine("test1");
            foreach (Pentago_Move pm in pr_110.possible_plays(board1))
            {
                Console.WriteLine(pm.ToString());
            }

            Console.WriteLine("test2");
            Console.WriteLine(pr_110.next_states(board1r).Length); //expected 8
            Console.WriteLine(pr_111.next_states(board1r).Length); //expected 1+2+1+2 = 6

            /*Console.WriteLine("test2 a)");
             * foreach (Pentago_GameBoard gb in pr_110.next_states(board1r))
             *  gb.print_board();
             * Console.WriteLine("test2 b)");
             * foreach (Pentago_GameBoard gb in pr_111.next_states(board1r))
             *  gb.print_board();*/
        }
예제 #2
0
    static public void testMinMax()
    {
        initialize_test_gameboards();
        Pentago_Rules wrules = new Pentago_Rules(Pentago_Rules.EvaluationFunction.control,
                                                 Pentago_Rules.NextStatesFunction.all_states,
                                                 Pentago_Rules.IA_PIECES_WHITES, false);
        MINMAX        test_w = new MINMAX(MINMAX.VERSION.minmax, wrules, 6);
        Pentago_Rules brules = new Pentago_Rules(Pentago_Rules.EvaluationFunction.control,
                                                 Pentago_Rules.NextStatesFunction.all_states,
                                                 Pentago_Rules.IA_PIECES_BLACKS, false);
        MINMAX test_b = new MINMAX(MINMAX.VERSION.minmax, brules, 6);
        bool?  player;

        boardMinMax.print_board();
        while (!boardMinMax.game_ended(out player))
        {
#if DEBUG_MIN_MAX
            alpha_beta_test.debugBoard = (o) => { o.print_board(); };
#endif
            applyPrintMoves2(test_w.run(boardMinMax));
#if PLAY_AGAINST_MIN_MAX
            Console.WriteLine("Place a piece: square,x,y     square E[0,3]      x,y E[0,2]");
            int[]        input = Console.ReadLine().Split(',').Select <string, int>(o => Convert.ToInt32(o)).ToArray();
            Pentago_Move pm    = new Pentago_Move(input[0], input[1], input[2]);
            pm.apply_move2board(boardAlphaBeta);
            Console.WriteLine("Rotate a square: square,dir     square E[0,3]      dir E[0-anti,1-clock]");
            input = Console.ReadLine().Split(',').Select <string, int>(o => Convert.ToInt32(o)).ToArray();
            pm    = new Pentago_Move(input[0], input[1] == 0 ? Pentago_Move.rotate_anticlockwise : Pentago_Move.rotate_clockwise);
            pm.apply_move2board(boardAlphaBeta);
            boardAlphaBeta.print_board();
#else
            applyPrintMoves2(test_b.run(boardMinMax));
#endif
        }
    }
예제 #3
0
    static void test1dot2vsC_1(int config)
    {
        Pentago_Rules wrules = new Pentago_Rules(EVFC.control,
                                                 Pentago_Rules.NextStatesFunction.all_states,
                                                 Pentago_Rules.IA_PIECES_WHITES, false);
        MINMAX        minmax_w = new MINMAX(MINMAX.VERSION.alphabeta, wrules, 0);
        Pentago_Rules brules   = new Pentago_Rules(EVFC.oneDotTwo,
                                                   Pentago_Rules.NextStatesFunction.all_states,
                                                   Pentago_Rules.IA_PIECES_BLACKS, false);

        if (config == 1)
        {
            brules.setHeur12_AD_PO();
        }
        if (config == 2)
        {
            brules.setHeur12_PD_AO();
        }
        if (config == 3)
        {
            brules.setHeur12_UD();
        }
        if (config == 4)
        {
            brules.setHeur12_UO();
        }
        if (config == 5)
        {
            brules.setHeur12_P();
        }
        MINMAX minmax_b = new MINMAX(MINMAX.VERSION.alphabeta, brules, 0);

        test_aux(minmax_w, minmax_b, false);
    }
예제 #4
0
    static void test_versus()
    {
        foreach (EVFC evfc1 in Enum.GetValues(typeof(EVFC)))
        {
            foreach (EVFC evfc2 in Enum.GetValues(typeof(EVFC)))
            {
                if (evfc1 == EVFC.control ||
                    evfc2 == EVFC.control
                    )
                {
                    continue;
                }

                Pentago_Rules wrules = new Pentago_Rules(evfc1,
                                                         Pentago_Rules.NextStatesFunction.all_states,
                                                         Pentago_Rules.IA_PIECES_WHITES, false);
                MINMAX        minmax_w = new MINMAX(MINMAX.VERSION.alphabeta, wrules, 0);
                Pentago_Rules brules   = new Pentago_Rules(evfc2,
                                                           Pentago_Rules.NextStatesFunction.all_states,
                                                           Pentago_Rules.IA_PIECES_BLACKS, false);
                MINMAX minmax_b = new MINMAX(MINMAX.VERSION.alphabeta, brules, 0);

                test_aux(minmax_w, minmax_b, true);
            }
        }
    }
예제 #5
0
    static void testAvsC_1(EVFC heur, int config)
    {
        Pentago_Rules wrules = new Pentago_Rules(EVFC.control,
                                                 Pentago_Rules.NextStatesFunction.all_states,
                                                 Pentago_Rules.IA_PIECES_WHITES, false);
        MINMAX        minmax_w = new MINMAX(MINMAX.VERSION.alphabeta, wrules, 0);
        Pentago_Rules brules   = new Pentago_Rules(heur,
                                                   Pentago_Rules.NextStatesFunction.all_states,
                                                   Pentago_Rules.IA_PIECES_BLACKS, false);

        if (config == 1)
        {
            brules.setA_setup1();
        }
        if (config == 2)
        {
            brules.setA_setup2();
        }
        if (config == 3)
        {
            brules.setA_setup3();
        }
        if (config == 4)
        {
            brules.setA_setup4();
        }

        MINMAX minmax_b = new MINMAX(MINMAX.VERSION.alphabeta, brules, 0);

        test_aux(minmax_w, minmax_b, false);
    }
예제 #6
0
    static void printAllMoves(List <Pentago_Move> allMoves, Pentago_GameBoard board, Pentago_Rules rules)
    {
        int   i = 0;
        float board_value;

        foreach (Pentago_Move move in allMoves)
        {
            if (board.get_player_turn() == Pentago_GameBoard.whites_turn)
            {
                Console.Write("White ");
            }
            else
            {
                Console.Write("Black ");
            }
            if (board.get_turn_state() == Pentago_GameBoard.turn_state_rotate)
            {
                Console.WriteLine("rotate");
            }
            else
            {
                Console.WriteLine("place");
            }
            move.apply_move2board(board);
            board.print_board();
//            Pentago_GameBoard gb = board.Clone();
            board_value = rules.evaluate(board);
            Console.WriteLine(board_value);
            i++;
            if (i % 2 == 0)
            {
                Console.WriteLine("|-|-|-|-|-|-|-|-|");
            }
        }
    }
    public static void test_auxiliar_methods()
    {
        Console.WriteLine("test_auxiliar_methods()");
        initialize_test_gameboards();

        Pentago_Rules pr_110 = new Pentago_Rules(Pentago_Rules.EvaluationFunction.func1, Pentago_Rules.NextStatesFunction.all_states, false);

        Pentago_Rules pr_111 = new Pentago_Rules(Pentago_Rules.EvaluationFunction.func1, Pentago_Rules.NextStatesFunction.all_states, true);

        Console.WriteLine("test1");
        foreach (Pentago_Move pm in pr_110.possible_plays(board1))
        {
            Console.WriteLine(pm.ToString());
        }

        Console.WriteLine("test2");
        Console.WriteLine(pr_110.next_states(board1r).Length); //expected 8
        Console.WriteLine(pr_111.next_states(board1r).Length); //expected 1+2+1+2 = 6

        /*Console.WriteLine("test2 a)");
         * foreach (Pentago_GameBoard gb in pr_110.next_states(board1r))
         *    gb.print_board();
         * Console.WriteLine("test2 b)");
         * foreach (Pentago_GameBoard gb in pr_111.next_states(board1r))
         *    gb.print_board();*/

        //RAW test... time is not measured... just 2 have a practical perception
        int a, b;

        Console.WriteLine("test3-time 1e5");
        for (int i = 0; i < 100000; i++)
        {
            Pentago_Rules.calculate_available_classes(board1, out a, out b);
        }
        Console.WriteLine("test3-ended 1e5");

        /* Console.WriteLine("test3-time 1e6");
        *  for (int i = 0; i < 1000000; i++)
        *    Pentago_Rules.calculate_available_classes(board1, out a, out b);
        *  Console.WriteLine("test3-ended 1e6");
        *
        *  Console.WriteLine("test3-time 1e7");
        *  for (int i = 0; i < 10000000; i++)
        *    Pentago_Rules.calculate_available_classes(board1, out a, out b);
        *  Console.WriteLine("test3-ended 1e7");
        *
        *  Console.WriteLine("test3-time 1e8");
        *  for (int i = 0; i < 100000000; i++)
        *    Pentago_Rules.calculate_available_classes(board1, out a, out b);
        *  Console.WriteLine("test3-ended 1e8");*/

        Pentago_Rules rulestest = new Pentago_Rules();

        for (int i = 0; i < 100; i++)
        {
            Console.WriteLine(rulestest.ControlHeuristic(null));
        }
    }
예제 #8
0
    static public void testPerformnace(int numOfBorads)
    {
        Pentago_GameBoard[] testBoardsWhites = new Pentago_GameBoard[numOfBorads];
        Pentago_GameBoard[] testBoardsBlacks = new Pentago_GameBoard[numOfBorads];

        for (int i = 0; i < numOfBorads; i++)
        {
            int numPieces = GenerateRandomBoard.GetRandomNumber(0, 17);
            Console.WriteLine("Num of pieces " + numPieces);
            GenerateRandomBoard rndBoard;

            Console.WriteLine("white");

            rndBoard = new GenerateRandomBoard(numPieces - 1, true); // -1 prencipio de iguldade de peças do tabuleiro
            rndBoard.generateNewBoard();
            testBoardsWhites[i] = rndBoard.Pentago_gb;
            testBoardsWhites[i].print_board();

            Console.WriteLine("black");

            rndBoard = new GenerateRandomBoard(numPieces, false);
            rndBoard.generateNewBoard();
            testBoardsBlacks[i] = rndBoard.Pentago_gb;
            testBoardsBlacks[i].print_board();
        }

        Pentago_Rules wrules = new Pentago_Rules(Pentago_Rules.EvaluationFunction.controlHeuristic,
                                                 Pentago_Rules.NextStatesFunction.all_states,
                                                 Pentago_Rules.IA_PIECES_WHITES, false);
        MINMAX        test_w = new MINMAX(MINMAX.VERSION.minmax, wrules, 6);
        Pentago_Rules brules = new Pentago_Rules(Pentago_Rules.EvaluationFunction.controlHeuristic,
                                                 Pentago_Rules.NextStatesFunction.all_states,
                                                 Pentago_Rules.IA_PIECES_BLACKS, false);
        MINMAX test_b = new MINMAX(MINMAX.VERSION.minmax, brules, 6);

        TimeSpan test1 = Performance.PerformanceTimes(test_w, testBoardsWhites);
        TimeSpan test2 = Performance.PerformanceTimes(test_b, testBoardsBlacks);

        TimeSpan ts = test1.Add(test2);

        string elapsedTime1 = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                            test1.Hours, test1.Minutes, test1.Seconds,
                                            test1.Milliseconds / 10);

        string elapsedTime2 = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                            test2.Hours, test2.Minutes, test2.Seconds,
                                            test2.Milliseconds / 10);

        // Format and display the TimeSpan value.
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                           ts.Hours, ts.Minutes, ts.Seconds,
                                           ts.Milliseconds / 10);

        Console.WriteLine("RunTime " + elapsedTime);
        Console.WriteLine("RunTime 1 " + elapsedTime1);
        Console.WriteLine("RunTime 2 " + elapsedTime2);
    }
    static public void testHeuristicA()
    {
        initialize_test_gameboards();
        Pentago_Rules rules = new Pentago_Rules(Pentago_Rules.EvaluationFunction.A,
                                                Pentago_Rules.NextStatesFunction.all_states,
                                                Pentago_Rules.IA_PIECES_WHITES, false);

        Console.WriteLine(rules.heuristicA(boardHeuristicA));
    }
예제 #10
0
    static public void testHeuristicA()
    {
        initialize_test_gameboards();
        Pentago_Rules rules = new Pentago_Rules(Pentago_Rules.EvaluationFunction.heuristicAstar,
                                                Pentago_Rules.NextStatesFunction.check_symmetries,
                                                Pentago_Rules.IA_PIECES_WHITES, true);

        Console.WriteLine(rules.heuristicAstar(boardHeuristicA));
    }
예제 #11
0
    static void test01_1()
    {
        Pentago_Rules wrules = new Pentago_Rules(EVFC.control,
                                                 Pentago_Rules.NextStatesFunction.all_states,
                                                 Pentago_Rules.IA_PIECES_WHITES, false);
        MINMAX        minmax_w = new MINMAX(MINMAX.VERSION.alphabeta, wrules, 0);
        Pentago_Rules brules   = new Pentago_Rules(EVFC.one,
                                                   Pentago_Rules.NextStatesFunction.all_states,
                                                   Pentago_Rules.IA_PIECES_BLACKS, false);
        MINMAX minmax_b = new MINMAX(MINMAX.VERSION.alphabeta, brules, 0);

        test_aux(minmax_w, minmax_b, false);
    }
예제 #12
0
    static public void testAlphaBeta()
    {
        initialize_test_gameboards();
        Pentago_Rules wrules = new Pentago_Rules(Pentago_Rules.EvaluationFunction.oneDotTwo,
                                                 Pentago_Rules.NextStatesFunction.all_states,
                                                 Pentago_Rules.IA_PIECES_WHITES, false);
        MINMAX        alpha_beta_test_w = new MINMAX(MINMAX.VERSION.alphabeta, wrules, 0);
        Pentago_Rules brules            = new Pentago_Rules(Pentago_Rules.EvaluationFunction.A,
                                                            Pentago_Rules.NextStatesFunction.all_states,
                                                            Pentago_Rules.IA_PIECES_BLACKS, false);
        MINMAX alpha_beta_test_b = new MINMAX(MINMAX.VERSION.alphabeta, brules, 0);
        bool?  player;

/*        boardAlphaBeta.print_board();
 *      Console.WriteLine("|-|-|-|-|-|-|-|-|");*/
        while (!emptyBoard.game_ended(out player))
        {
#if DEBUG_ALPHA_BETA
            alpha_beta_test.debugBoard = (o) => { o.print_board(); };
#endif
            applyPrintMoves(alpha_beta_test_w.run(emptyBoard), emptyBoard);
            Console.WriteLine("|-|-|-|-|-|-|-|-|");
#if PLAY_AGAINST_HUMAN
            Console.WriteLine("Place a piece: square,x,y     square E[0,3]      x,y E[0,2]");
            int[]        input = Console.ReadLine().Split(',').Select <string, int>(o => Convert.ToInt32(o)).ToArray();
            Pentago_Move pm    = new Pentago_Move(input[0], input[1], input[2]);
            pm.apply_move2board(boardAlphaBeta);
            Console.WriteLine("Rotate a square: square,dir     square E[0,3]      dir E[0-anti,1-clock]");
            input = Console.ReadLine().Split
                        (',').Select <string, int>(o => Convert.ToInt32(o)).ToArray();
            pm = new Pentago_Move(input[0], input[1] == 0 ? Pentago_Move.rotate_anticlockwise : Pentago_Move.rotate_clockwise);
            pm.apply_move2board(boardAlphaBeta);
            boardAlphaBeta.print_board();
#else
            applyPrintMoves(alpha_beta_test_b.run(emptyBoard), emptyBoard);
            Console.WriteLine("|-|-|-|-|-|-|-|-|");
#endif
        }
        if (player == null)
        {
            Console.WriteLine("Tie");
        }
        else if (player == Pentago_Rules.IA_PIECES_BLACKS)
        {
            Console.WriteLine("Black wins");
        }
        else
        {
            Console.WriteLine("White wins");
        }
    }
예제 #13
0
    static void test_h12_notrelaxed1()
    {
        Pentago_Rules wrules = new Pentago_Rules(EVFC.oneDotTwo,
                                                 Pentago_Rules.NextStatesFunction.all_states,
                                                 Pentago_Rules.IA_PIECES_WHITES, false);

        wrules.HEUR12RELAXED = false;
        MINMAX minmax_w = new MINMAX(MINMAX.VERSION.alphabeta, wrules, 0);

        foreach (EVFC evfc2 in Enum.GetValues(typeof(EVFC)))
        {
            Pentago_Rules brules = new Pentago_Rules(evfc2,
                                                     Pentago_Rules.NextStatesFunction.all_states,
                                                     Pentago_Rules.IA_PIECES_BLACKS, false);
            MINMAX minmax_b = new MINMAX(MINMAX.VERSION.alphabeta, brules, 0);

            test_aux(minmax_w, minmax_b, true);
        }
    }
예제 #14
0
    static void test_AstarNoRot2()
    {
        Pentago_Rules brules = new Pentago_Rules(EVFC.Astar,
                                                 Pentago_Rules.NextStatesFunction.all_states,
                                                 Pentago_Rules.IA_PIECES_BLACKS, false);

        brules.setAstarSettings(1.13f, 1.15f, 1.17f, 1.19f, false);
        MINMAX minmax_b = new MINMAX(MINMAX.VERSION.alphabeta, brules, 0);

        foreach (EVFC evfc2 in Enum.GetValues(typeof(EVFC)))
        {
            Pentago_Rules wrules = new Pentago_Rules(evfc2,
                                                     Pentago_Rules.NextStatesFunction.all_states,
                                                     Pentago_Rules.IA_PIECES_WHITES, false);
            ;
            MINMAX minmax_w = new MINMAX(MINMAX.VERSION.alphabeta, wrules, 0);

            test_aux(minmax_w, minmax_b, false);
        }
    }
예제 #15
0
    static public void testPerfomanceSuite4Depth(int depth)
    {
        Pentago_Rules all = new Pentago_Rules(Pentago_Rules.EvaluationFunction.one,
                                              Pentago_Rules.NextStatesFunction.all_states,
                                              Pentago_Rules.IA_PIECES_WHITES, false);
        Pentago_Rules sym = new Pentago_Rules(Pentago_Rules.EvaluationFunction.one,
                                              Pentago_Rules.NextStatesFunction.check_symmetries,
                                              Pentago_Rules.IA_PIECES_WHITES, false);
        Pentago_Rules remdups = new Pentago_Rules(Pentago_Rules.EvaluationFunction.one,
                                                  Pentago_Rules.NextStatesFunction.all_states,
                                                  Pentago_Rules.IA_PIECES_WHITES, true);
        Pentago_Rules dupsNsym = new Pentago_Rules(Pentago_Rules.EvaluationFunction.one,
                                                   Pentago_Rules.NextStatesFunction.check_symmetries,
                                                   Pentago_Rules.IA_PIECES_WHITES, true);

        MINMAX[] totest =
        {
            new MINMAX(MINMAX.VERSION.minmax,    all,      depth),
            new MINMAX(MINMAX.VERSION.minmax,    sym,      depth),
            new MINMAX(MINMAX.VERSION.minmax,    remdups,  depth),
            new MINMAX(MINMAX.VERSION.minmax,    dupsNsym, depth),
            new MINMAX(MINMAX.VERSION.alphabeta, all,      depth),
            new MINMAX(MINMAX.VERSION.alphabeta, sym,      depth),
            new MINMAX(MINMAX.VERSION.alphabeta, remdups,  depth),
            new MINMAX(MINMAX.VERSION.alphabeta, dupsNsym, depth),
        };

        int[] pieces2test = new int[24].Select((elem, ind) => ind).ToArray();

        add2File("CompareMinmaxsWithEqualPieces/comp_D" + depth.ToString(), totest.Aggregate <MINMAX, String>(""
                                                                                                              , (sum, elem) =>
                                                                                                              sum += ";" + elem.version.ToString() + "_" + ((Pentago_Rules)elem.rules).nsf.ToString() + "_" + ((Pentago_Rules)elem.rules).remove_repeated_states_on_nextStates.ToString()
                                                                                                              ));
        foreach (int i in pieces2test)
        {
            testPerformance(numTests, i, depth, totest);
        }
    }
예제 #16
0
    public static void play(int depth, Pentago_Rules.EvaluationFunction ef, Pentago_Rules.NextStatesFunction nsf, bool remove_duplicates, bool ia_pieces)
    {
        Pentago_GameBoard gb          = new Pentago_GameBoard();
        bool?         winning_player  = null;
        Pentago_Rules rules           = new Pentago_Rules(ef, nsf, ia_pieces, remove_duplicates);
        MINMAX        alpha_beta_test = new MINMAX(MINMAX.VERSION.alphabeta, rules, depth);
        Stopwatch     sw = new Stopwatch();

        while (!gb.game_ended(out winning_player))
        {
            gb.print_board();

            if (gb.get_player_turn() != ia_pieces)
            {
                Console.WriteLine("Your turn");
                if (gb.get_turn_state() == Pentago_GameBoard.turn_state_addpiece)
                {
                    Console.WriteLine("Place a piece: square,x,y     square E[0,3]      x,y E[0,2]");
                    int[] input;
                    try
                    {
                        input = Console.ReadLine().Split(',').Select <string, int>(o => Convert.ToInt32(o)).ToArray();
                        if (input.Length < 3)
                        {
                            continue;
                        }
                    }
                    catch { continue; }
                    Pentago_Move pm = new Pentago_Move(input[0], input[1], input[2]);
                    if (!pm.is_move_possible(gb))
                    {
                        continue;
                    }
                    pm.apply_move2board(gb);
                }
                else
                {
                    Console.WriteLine("Rotate a square: square,dir     square E[0,3]      dir E[0-anti,1-clock]");
                    int[] input;
                    try
                    {
                        input = Console.ReadLine().Split(',').Select <string, int>(o => Convert.ToInt32(o)).ToArray();
                        if (input.Length < 2)
                        {
                            continue;
                        }
                    }
                    catch { continue; }
                    Pentago_Move pm = new Pentago_Move(input[0], input[1] == 0 ? Pentago_Move.rotate_anticlockwise : Pentago_Move.rotate_clockwise);
                    if (!pm.is_move_possible(gb))
                    {
                        continue;
                    }
                    pm.apply_move2board(gb);
                }
            }
            else
            {
                Console.WriteLine("IA's turn");
                sw.Start();
                applyMoves(alpha_beta_test.run(gb), gb);
                sw.Stop();
                Console.WriteLine("Time={0}", sw.Elapsed);
            }
        }
        if (winning_player == null)
        {
            Console.WriteLine("\nGAME ENDED IN DRAW");
        }
        else if (winning_player != ia_pieces)
        {
            Console.WriteLine("\nGAME ENDED - YOU WON");
        }
        else
        {
            Console.WriteLine("\nGAME ENDED - IA WON");
        }
    }
예제 #17
0
    public static void play()
    {
        Pentago_GameBoard gb         = new Pentago_GameBoard();
        bool?         winning_player = null;
        Pentago_Rules rules          = new Pentago_Rules();

        rules.setHeuristic1Bias(0);

        while (!gb.game_ended(out winning_player))
        {
            Console.Clear();
            gb.print_board();

            if (gb.get_player_turn() == Pentago_GameBoard.whites_turn)
            {
                Pentago_GameBoard newbg     = gb;
                float             bestSoFar = float.NegativeInfinity;
                Pentago_Move[]    moves     = rules.possible_plays(gb);
                bool ended = false;
                //int count = 0;
                foreach (Pentago_Move move in moves)
                {
                    // Console.WriteLine(move.ToString());
                    Pentago_GameBoard gb_aux = move.state_after_move(gb);
                    // gb_aux.print_board();
                    // count++;
                    //  if (count > 5) break;
                    bool?winner;
                    if (gb_aux.game_ended(out winner))
                    {
                        if (winner != null && winner == Pentago_GameBoard.whites_turn)
                        {
                            move.apply_move2board(gb); ended = true;
                        }
                    }
                    if (ended)
                    {
                        break;
                    }

                    foreach (Pentago_GameBoard gbs in rules.next_states(gb_aux))
                    {
                        if (gbs.game_ended(out winner))
                        {
                            if (winner != null && winner == Pentago_GameBoard.whites_turn)
                            {
                                gb = gbs;  ended = true;
                            }
                        }
                        if (ended)
                        {
                            break;
                        }

                        float value = rules.heuristic1dot2(gbs.board);

                        if (value > bestSoFar)
                        {
                            bestSoFar = value;
                            newbg     = gbs;
                        }
                    }
                    if (ended)
                    {
                        break;
                    }
                }
                if (ended)
                {
                    break;
                }

                gb = newbg;

                continue;
            }
            else
            {
                Console.WriteLine("Black's turn");
            }

            if (gb.get_turn_state() == Pentago_GameBoard.turn_state_addpiece)
            {
                Console.WriteLine("Place a piece: square,x,y     square E[0,3]      x,y E[0,2]");
                int[] input;
                try {
                    input = Console.ReadLine().Split(',').Select <string, int>(o => Convert.ToInt32(o)).ToArray();
                    if (input.Length < 3)
                    {
                        continue;
                    }
                } catch { continue; }
                Pentago_Move pm = new Pentago_Move(input[0], input[1], input[2]);
                if (!pm.is_move_possible(gb))
                {
                    continue;
                }
                pm.apply_move2board(gb);
            }
            else
            {
                Console.WriteLine("Rotate a square: square,dir     square E[0,3]      dir E[0-anti,1-clock]");
                int[] input;
                try
                {
                    input = Console.ReadLine().Split(',').Select <string, int>(o => Convert.ToInt32(o)).ToArray();
                    if (input.Length < 2)
                    {
                        continue;
                    }
                }
                catch { continue; }
                Pentago_Move pm = new Pentago_Move(input[0], input[1] == 0? Pentago_Move.rotate_anticlockwise:Pentago_Move.rotate_clockwise);
                if (!pm.is_move_possible(gb))
                {
                    continue;
                }
                pm.apply_move2board(gb);
            }
        }

        Console.Clear();
        gb.print_board();
        if (winning_player == null)
        {
            Console.WriteLine("\nGAME ENDED IN DRAW");
        }
        else if (winning_player == Pentago_GameBoard.whites_turn)
        {
            Console.WriteLine("\nGAME ENDED - WHITE WON");
        }
        else
        {
            Console.WriteLine("\nGAME ENDED - BLACK WON");
        }
    }
예제 #18
0
    public static void play()
    {
        Pentago_GameBoard gb         = new Pentago_GameBoard();
        bool?         winning_player = null;
        Pentago_Rules brules         = new Pentago_Rules(Pentago_Rules.EvaluationFunction.heuristicA,
                                                         Pentago_Rules.NextStatesFunction.all_states,
                                                         Pentago_Rules.IA_PIECES_BLACKS, false);
        MINMAX alpha_beta_test_b = new MINMAX(MINMAX.VERSION.alphabeta, brules, 6);

        while (!gb.game_ended(out winning_player))
        {
            Console.Clear();
            gb.print_board();

            if (gb.get_player_turn() == Pentago_GameBoard.whites_turn)
            {
                Console.WriteLine("White's turn");
                if (gb.get_turn_state() == Pentago_GameBoard.turn_state_addpiece)
                {
                    Console.WriteLine("Place a piece: square,x,y     square E[0,3]      x,y E[0,2]");
                    int[] input;
                    try
                    {
                        input = Console.ReadLine().Split(',').Select <string, int>(o => Convert.ToInt32(o)).ToArray();
                        if (input.Length < 3)
                        {
                            continue;
                        }
                    }
                    catch { continue; }
                    Pentago_Move pm = new Pentago_Move(input[0], input[1], input[2]);
                    if (!pm.is_move_possible(gb))
                    {
                        continue;
                    }
                    pm.apply_move2board(gb);
                }
                else
                {
                    Console.WriteLine("Rotate a square: square,dir     square E[0,3]      dir E[0-anti,1-clock]");
                    int[] input;
                    try
                    {
                        input = Console.ReadLine().Split(',').Select <string, int>(o => Convert.ToInt32(o)).ToArray();
                        if (input.Length < 2)
                        {
                            continue;
                        }
                    }
                    catch { continue; }
                    Pentago_Move pm = new Pentago_Move(input[0], input[1] == 0 ? Pentago_Move.rotate_anticlockwise : Pentago_Move.rotate_clockwise);
                    if (!pm.is_move_possible(gb))
                    {
                        continue;
                    }
                    pm.apply_move2board(gb);
                }
            }
            else
            {
                Console.WriteLine("Black's turn");
                applyPrintMoves(alpha_beta_test_b.run(gb), gb);
            }
        }

        Console.Clear();
        gb.print_board();
        if (winning_player == null)
        {
            Console.WriteLine("\nGAME ENDED IN DRAW");
        }
        else if (winning_player == Pentago_GameBoard.whites_turn)
        {
            Console.WriteLine("\nGAME ENDED - WHITE WON");
        }
        else
        {
            Console.WriteLine("\nGAME ENDED - BLACK WON");
        }
    }
예제 #19
0
    static public void testPerformnace(int numOfBorads)
    {
        using (Process p = Process.GetCurrentProcess())
        {
            p.PriorityClass = ProcessPriorityClass.High;
        }

        Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;

        Pentago_GameBoard[] testBoardsWhites = new Pentago_GameBoard[numOfBorads];
        Pentago_GameBoard[] testBoardsBlacks = new Pentago_GameBoard[numOfBorads];

        for (int i = 0; i < numOfBorads; i++)
        {
            int numPieces = GenerateRandomBoard.GetRandomNumber(0, 16);
            GenerateRandomBoard rndBoard = new GenerateRandomBoard(numPieces, true);
            rndBoard.generateNewBoard();
            testBoardsWhites[i] = rndBoard.Pentago_gb;
        }

        for (int i = 0; i < numOfBorads; i++)
        {
            int numPieces = GenerateRandomBoard.GetRandomNumber(0, 16);
            GenerateRandomBoard rndBoard = new GenerateRandomBoard(numPieces, false);
            rndBoard.generateNewBoard();
            testBoardsBlacks[i] = rndBoard.Pentago_gb;
        }



        Pentago_Rules wrulesm = new Pentago_Rules(Pentago_Rules.EvaluationFunction.one,
                                                  Pentago_Rules.NextStatesFunction.all_states,
                                                  Pentago_Rules.IA_PIECES_WHITES, false);
        Pentago_Rules brulesm = new Pentago_Rules(Pentago_Rules.EvaluationFunction.one,
                                                  Pentago_Rules.NextStatesFunction.all_states,
                                                  Pentago_Rules.IA_PIECES_BLACKS, false);
        MINMAX   test_wm = new MINMAX(MINMAX.VERSION.minmax, wrulesm, 4);
        MINMAX   test_bm = new MINMAX(MINMAX.VERSION.minmax, brulesm, 4);
        TimeSpan test1m  = Performance.PerformanceTimes(test_wm, testBoardsWhites);
        TimeSpan test2m  = Performance.PerformanceTimes(test_bm, testBoardsBlacks);

        TimeSpan tsm = test1m.Add(test2m);

        // Format and display the TimeSpan value.
        string elapsedTimem = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                            tsm.Hours, tsm.Minutes, tsm.Seconds,
                                            tsm.Milliseconds / 10);

        Console.WriteLine("RunTime " + elapsedTimem);



        Pentago_Rules wrules = new Pentago_Rules(Pentago_Rules.EvaluationFunction.one,
                                                 Pentago_Rules.NextStatesFunction.all_states,
                                                 Pentago_Rules.IA_PIECES_WHITES, false);
        Pentago_Rules brules = new Pentago_Rules(Pentago_Rules.EvaluationFunction.one,
                                                 Pentago_Rules.NextStatesFunction.all_states,
                                                 Pentago_Rules.IA_PIECES_BLACKS, false);
        MINMAX test_w = new MINMAX(MINMAX.VERSION.alphabeta, wrules, 4);
        MINMAX test_b = new MINMAX(MINMAX.VERSION.alphabeta, brules, 4);

        TimeSpan test1 = Performance.PerformanceTimes(test_w, testBoardsWhites);
        TimeSpan test2 = Performance.PerformanceTimes(test_b, testBoardsBlacks);

        TimeSpan ts = test1.Add(test2);

        // Format and display the TimeSpan value.
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                           ts.Hours, ts.Minutes, ts.Seconds,
                                           ts.Milliseconds / 10);

        Console.WriteLine("RunTime " + elapsedTime);
    }
예제 #20
0
    // testa qual a euristica mais rápida entre heuristics1 e heuristicsA
    static public void testPerformnace1(int numOfBorads)
    {
        Pentago_GameBoard[] testBoardsWhites = new Pentago_GameBoard[numOfBorads];
        Pentago_GameBoard[] testBoardsBlacks = new Pentago_GameBoard[numOfBorads];

        for (int i = 0; i < numOfBorads; i++)
        {
            int numPieces = GenerateRandomBoard.GetRandomNumber(0, 17);
            Console.WriteLine("Num of pieces " + numPieces);
            GenerateRandomBoard rndBoard;

            Console.WriteLine("white");

            rndBoard = new GenerateRandomBoard(numPieces == 0? 0: numPieces - 1, true); // -1 prencipio de iguldade de peças do tabuleiro
            rndBoard.generateNewBoard();
            testBoardsWhites[i] = rndBoard.Pentago_gb;
            testBoardsWhites[i].print_board();

            Console.WriteLine("black");

            rndBoard = new GenerateRandomBoard(numPieces, false);
            rndBoard.generateNewBoard();
            testBoardsBlacks[i] = rndBoard.Pentago_gb;
            testBoardsBlacks[i].print_board();
        }

        // heuristics1
        Pentago_Rules wrules1 = new Pentago_Rules(Pentago_Rules.EvaluationFunction.heuristic1,
                                                  Pentago_Rules.NextStatesFunction.all_states,
                                                  Pentago_Rules.IA_PIECES_WHITES, false);
        MINMAX        test_w1 = new MINMAX(MINMAX.VERSION.minmax, wrules1, 4);
        Pentago_Rules brules1 = new Pentago_Rules(Pentago_Rules.EvaluationFunction.heuristic1,
                                                  Pentago_Rules.NextStatesFunction.all_states,
                                                  Pentago_Rules.IA_PIECES_BLACKS, false);
        MINMAX test_b1 = new MINMAX(MINMAX.VERSION.minmax, brules1, 4);

        // heuristicsA
        Pentago_Rules wrulesA = new Pentago_Rules(Pentago_Rules.EvaluationFunction.heuristicA,
                                                  Pentago_Rules.NextStatesFunction.all_states,
                                                  Pentago_Rules.IA_PIECES_WHITES, false);
        MINMAX        test_wA = new MINMAX(MINMAX.VERSION.minmax, wrulesA, 4);
        Pentago_Rules brulesA = new Pentago_Rules(Pentago_Rules.EvaluationFunction.heuristicA,
                                                  Pentago_Rules.NextStatesFunction.all_states,
                                                  Pentago_Rules.IA_PIECES_BLACKS, false);
        MINMAX test_bA = new MINMAX(MINMAX.VERSION.minmax, brulesA, 4);



        TimeSpan test1_1 = Performance.PerformanceTimes(test_w1, testBoardsWhites);
        TimeSpan test2_1 = Performance.PerformanceTimes(test_b1, testBoardsBlacks);

        TimeSpan ts_1 = test1_1.Add(test2_1);

        // Format and display the TimeSpan value.
        string elapsedTime_1 = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                             ts_1.Hours, ts_1.Minutes, ts_1.Seconds,
                                             ts_1.Milliseconds / 10);

        Console.WriteLine("RunTime heuristic1 " + elapsedTime_1);
        //////////////////////////////////////////////////////////////////////////////
        TimeSpan test1_A = Performance.PerformanceTimes(test_wA, testBoardsWhites);
        TimeSpan test2_A = Performance.PerformanceTimes(test_bA, testBoardsBlacks);

        TimeSpan ts_A = test1_A.Add(test2_A);

        // Format and display the TimeSpan value.
        string elapsedTime_A = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                             ts_A.Hours, ts_A.Minutes, ts_A.Seconds,
                                             ts_A.Milliseconds / 10);

        Console.WriteLine("RunTime heuristicA " + elapsedTime_A);
    }
예제 #21
0
    public void startGame()
    {
        if (!hackedStart)
        {
            if (IA_SETTINGS.VS_IA == IA_SETTINGS.vsMode.PIA)
            {
                if (IA_SETTINGS.PLAYER_1ST)
                {
                    game_mode = GAME_MODE.PlayerVsIA;
                }
                else
                {
                    game_mode = GAME_MODE.IAvsPlayer;
                }
            }
            else if (IA_SETTINGS.VS_IA == IA_SETTINGS.vsMode.IAIA)
            {
                game_mode = GAME_MODE.IAvsIA;
            }
            else
            {
                game_mode = GAME_MODE.PlayerVSPlayer;
            }
        }

        if (game_mode == GAME_MODE.PlayerVSPlayer || game_mode == GAME_MODE.PlayerVsIA)
        {
            gameState = GAME_STATE.playerselecthole;
        }
        else
        {
            gameState = GAME_STATE.startIA;
        }

        if (game_mode != GAME_MODE.PlayerVSPlayer)
        {
            if (!hackedStart)
            {
                minmax_depth = IA_SETTINGS.IA_DEPTH;
                switch (IA_SETTINGS.IA_HEURISTIC)
                {
                case 0: heuristic = HEURISTIC.control; break;

                case 1: heuristic = HEURISTIC.one; break;

                case 2: heuristic = HEURISTIC.oneDotTwo; break;

                case 3: heuristic = HEURISTIC.A; break;

                case 4: heuristic = HEURISTIC.AplusDiagHack; break;

                case 5: heuristic = HEURISTIC.Astar; break;

                default:
                    break;
                }
            }

            rules = new Pentago_Rules(heuristic,
                                      Pentago_Rules.NextStatesFunction.check_symmetries,
                                      game_mode == GAME_MODE.IAvsIA ? Pentago_Rules.IA_PIECES_WHITES : (
                                          game_mode == GAME_MODE.PlayerVsIA ? Pentago_Rules.IA_PIECES_BLACKS : Pentago_Rules.IA_PIECES_WHITES),
                                      true);

            ia = new MINMAX(MINMAX.VERSION.cut_alphabeta, rules, minmax_depth * 2);
            ia.setCUT(Pentago_Rules.MAX_HEURISTIC_VALUE, Pentago_Rules.MIN_HEURISTIC_VALUE);
            //iaThread = new AI_thread(ia);
        }

        if (game_mode == GAME_MODE.IAvsIA)
        {
            minmax_depth2 = IA_SETTINGS.IA_DEPTH_2;
            switch (IA_SETTINGS.IA_HEURISTIC_2)
            {
            case 0: heuristic2 = HEURISTIC.control; break;

            case 1: heuristic2 = HEURISTIC.one; break;

            case 2: heuristic2 = HEURISTIC.oneDotTwo; break;

            case 3: heuristic2 = HEURISTIC.A; break;

            case 4: heuristic2 = HEURISTIC.AplusDiagHack; break;

            case 5: heuristic2 = HEURISTIC.Astar; break;

            default:
                break;
            }

            rules2 = new Pentago_Rules(heuristic2,
                                       Pentago_Rules.NextStatesFunction.check_symmetries,
                                       Pentago_Rules.IA_PIECES_BLACKS,
                                       true);

            ia2 = new MINMAX(MINMAX.VERSION.cut_alphabeta, rules2, minmax_depth2 * 2);
            ia2.setCUT(Pentago_Rules.MAX_HEURISTIC_VALUE, Pentago_Rules.MIN_HEURISTIC_VALUE);
        }
    }
예제 #22
0
    static public void testHeuristicAGood(int numberTests, Pentago_Rules wrules, Pentago_Rules brules, int depth1, int depth2, bool testHeuristic, bool printTies = false, bool printLosses = false)
    {
        MINMAX alpha_beta_test_w = new MINMAX(MINMAX.VERSION.alphabeta, wrules, depth1);
        MINMAX alpha_beta_test_b = new MINMAX(MINMAX.VERSION.alphabeta, brules, depth2);
        int    black_wins        = 0;
        int    black_losses      = 0;
        int    ties = 0;

        for (int i = 1; i < numberTests + 1; i++)
        {
            List <Pentago_Move> allMoves = new List <Pentago_Move>();
            initialize_test_gameboards();
            bool?player;
            int  rounds = 0;
            while (!emptyBoard.game_ended(out player))
            {
                applyMoves(alpha_beta_test_w.run(emptyBoard), emptyBoard, ref allMoves);
                applyMoves(alpha_beta_test_b.run(emptyBoard), emptyBoard, ref allMoves);
                rounds++;
            }
            if (player == null)
            {
                if (printTies)
                {
                    initialize_test_gameboards();
                    if (testHeuristic == testFirst)
                    {
                        printAllMoves(allMoves, emptyBoard, wrules);
                    }
                    else
                    {
                        printAllMoves(allMoves, emptyBoard, brules);
                    }
                }
                ties++;
            }
            else if (player == Pentago_Rules.IA_PIECES_BLACKS)
            {
                if (testHeuristic == testFirst && printLosses)
                {
                    initialize_test_gameboards();
                    printAllMoves(allMoves, emptyBoard, wrules);
                }
                black_wins++;
            }
            else
            {
                if (testHeuristic == testSecond && printLosses)
                {
                    initialize_test_gameboards();
                    printAllMoves(allMoves, emptyBoard, brules);
                }
                black_losses++;
            }
            if (testHeuristic == testFirst)
            {
                Console.WriteLine(i + " - wins: " + black_losses + ", losses: " + black_wins + ", ties: " + ties);
            }
            else
            {
                Console.WriteLine(i + " - wins: " + black_wins + ", losses: " + black_losses + ", ties: " + ties);
            }
        }
    }