Ejemplo n.º 1
0
    public void setUpComputer()
    {
        pMoves         = new Moves();
        cMoves         = new Moves();
        availableMoves = new Moves();
        mg             = new MoveGenerator();

        games = new GameCollection(new Game(0, 1, 2), new Game(3, 4, 5), new Game(0, 3, 6), new Game(2, 4, 6),
                                   new Game(1, 4, 7), new Game(2, 5, 8), new Game(0, 4, 8), new Game(6, 7, 8));

        availableMoves = games.toMoveArray();
    }
Ejemplo n.º 2
0
    public int getMove(GameCollection games, Moves pMoves, Moves cMoves, Moves av)
    {
        Random      r      = new Random();
        Moves       finalM = new Moves();
        IsAvailable iA     = new IsAvailable(av);

        /*
         * ####################################################################
         * ####################################################################
         * ####################################################################
         */
        if (cMoves.getMoves().Length + pMoves.getMoves().Length == 0) // Console.WriteLine("### 000 ###");
        {
            Moves z = games.toMoveMap();
            for (int i = 0; i < 300; i++)
            {
                int mySelection = z.getMoves()[r.Next(z.getMoves().Length)];
                if (iA.check(mySelection))
                {
                    Console.WriteLine("selected move: " + mySelection);
                    return(mySelection);
                }
                if (i >= 299)
                {
                    Console.WriteLine("giving up");
                }
            }
        }

        /*
         * ####################################################################
         * ####################################################################
         * ####################################################################
         */

        if (cMoves.getMoves().Length == 0 && pMoves.getMoves().Length == 1) // Console.WriteLine("### 001 ###");
        {
            GameCollection mm = new GameCollection();

            int theMove;
            theMove = pMoves.getMoves()[0];

            Index myIdenx = games.getIndexGameContains(theMove);

            Console.WriteLine("index -> " + myIdenx);
            Console.WriteLine("games -> " + games);

            bool skip = false;

            for (int i = 0; i < games.getSize(); i++)
            {
                for (int j = 0; j < myIdenx.getSize(); j++)
                {
                    if (i == myIdenx.getIndexArray()[j])
                    {
                        skip = true;
                    }
                }
                if (skip)
                {
                    skip = false; continue;
                }
                mm.addGame(games.getGame(i));
            }

            Console.WriteLine("selection -> " + mm);

            Moves z = mm.toMoveMap();
            for (int i = 0; i < 300; i++)
            {
                int mySelection = z.getMoves()[r.Next(z.getMoves().Length)];
                if (iA.check(mySelection))
                {
                    Console.WriteLine("selected move: " + mySelection);
                    return(mySelection);
                }
                if (i >= 299)
                {
                    Console.WriteLine("giving up");
                }
            }
        }

        /*
         * ####################################################################
         * ####################################################################
         * ####################################################################
         */

        if (cMoves.getMoves().Length + pMoves.getMoves().Length >= 3) // Console.WriteLine("### 002 ###");
        {
            Gen lol = new Gen();

            GameCollection p = new GameCollection();
            GameCollection c = new GameCollection();

            GameCollection playerSelection   = new GameCollection();
            GameCollection computerSelection = new GameCollection();

            lol.combinations(pMoves.getMoves(), p);
            lol.combinations(cMoves.getMoves(), c);

            Console.WriteLine("player combination: " + p);
            Console.WriteLine("computer combination: " + c);

            // combinacoes prontas, pesquisar jogos
            for (int i = 0; i < p.getSize(); i++)
            {
                int myIdenx = games.getIndexGameContains(p.getGame(i).getGameArray()[0], p.getGame(i).getGameArray()[1]);

                if (myIdenx != -1)
                {
                    playerSelection.addGame(games.getGame(myIdenx));
                }
            }

            for (int i = 0; i < c.getSize(); i++)
            {
                int myIdenx = games.getIndexGameContains(c.getGame(i).getGameArray()[0], c.getGame(i).getGameArray()[1]);

                if (myIdenx != -1)
                {
                    computerSelection.addGame(games.getGame(myIdenx));
                }
            }

            Console.WriteLine("playerSelection = " + playerSelection);
            Console.WriteLine("computerSelection = " + computerSelection);

            Console.WriteLine("playerSelection.toMoveArray = " + playerSelection.toMoveArray());
            Console.WriteLine("computerSelection.toMoveArray = " + computerSelection.toMoveArray());

            if (computerSelection.getSize() > 0)
            {
                for (int i = 0; i < computerSelection.toMoveArray().getMoves().Length; i++)
                {
                    int mySelection = computerSelection.toMoveArray().getMoves()[i];
                    Console.WriteLine("computer iA.check -> " + iA.check(mySelection));
                    Console.WriteLine("mySelection -> " + mySelection);
                    if (iA.check(mySelection))
                    {
                        return(mySelection);
                    }
                }
            }

            if (playerSelection.getSize() > 0)
            {
                for (int i = 0; i < playerSelection.toMoveArray().getMoves().Length; i++)
                {
                    int mySelection = playerSelection.toMoveArray().getMoves()[i];
                    Console.WriteLine("player iA.check -> " + iA.check(mySelection));
                    Console.WriteLine("mySelection -> " + mySelection);
                    if (iA.check(mySelection))
                    {
                        return(mySelection);
                    }
                }
            }
            else
            {
                Console.WriteLine("foideus");
            }

            /*
             * for (int i = 0; i < computerSelection.toMoveArray().getMoves().Length; i++)
             * {
             * int mySelection = computerSelection.toMoveArray().getMoves()[i];
             * if (iA.check(mySelection))
             * {
             * return mySelection;
             * }
             * }
             */
        }

        Console.WriteLine("tudo errado.");

        /*
         * ####################################################################
         * ####################################################################
         * ####################################################################
         */

        if (true) // last resort
        {
            GameCollection mm = new GameCollection();

            int theMove;
            theMove = cMoves.getMoves()[0];

            Index myIdenx = games.getIndexGameContains(theMove);

            Console.WriteLine("index -> " + myIdenx);
            Console.WriteLine("games -> " + games);

            for (int i = 0; i < games.getSize(); i++)
            {
                for (int j = 0; j < myIdenx.getSize(); j++)
                {
                    if (i == myIdenx.getIndexArray()[j])
                    {
                        mm.addGame(games.getGame(i));
                    }
                }
            }

            Console.WriteLine("selection -> " + mm);

            Moves z = mm.toMoveMap();
            for (int i = 0; i < 300; i++)
            {
                int mySelection = z.getMoves()[r.Next(z.getMoves().Length)];
                if (iA.check(mySelection))
                {
                    Console.WriteLine("selected move: " + mySelection);
                    return(mySelection);
                }
                if (i >= 299)
                {
                    Console.WriteLine("giving up");
                }
            }
        }
        // real last resort
        if (true)
        {
            GameCollection mm = new GameCollection();
            Console.WriteLine("Last Resort");
            Console.WriteLine("Available moves -> " + av);
            Console.WriteLine("Games -> " + games);
            Console.WriteLine("cMoves -> " + cMoves);
            Console.WriteLine("pMoves -> " + pMoves);

            GameCollection gaminhoLastResourt = new GameCollection();
            GameCollection pGames             = new GameCollection();

            for (int i = 0; i < cMoves.getMoves().Length; i++)
            {
                Index holyDog = games.getIndexGameContains(cMoves.getMoves()[i]);
                for (int j = 0; j < holyDog.getIndexArray().Length; j++)
                {
                    gaminhoLastResourt.addGame(games.getGame(holyDog.getIndexArray()[j]));
                }
            }

            Console.WriteLine("gaminho -> " + gaminhoLastResourt.toMoveArray());

            if (gaminhoLastResourt.getSize() > 0)
            {
                for (int i = 0; i < gaminhoLastResourt.toMoveArray().getMoves().Length; i++)
                {
                    int mySelection = gaminhoLastResourt.toMoveArray().getMoves()[i];
                    Console.WriteLine("player iA.check -> " + iA.check(mySelection));
                    Console.WriteLine("mySelection -> " + mySelection);
                    if (iA.check(mySelection))
                    {
                        return(mySelection);
                    }
                }
            }
            else
            {
                Console.WriteLine("foideus de novo, #semata");
            }
        }
        return(-1);
    }