static int CalculateMove(Game game, IPlayer computer, IPlayer player)
        {
            int result = EngineManager.CalculateMove(computer, player);

            while (!TableInputValidator(game, result))
            {
                result = EngineManager.CalculateMove(computer, player);
            }
            return(result);
        }
Ejemplo n.º 2
0
        internal static int WithAllCombinationsCalculateBlock(List <int> engineMoves, List <int> playerMoves)
        {
            List <int> blocks = new List <int>();

            foreach (var combination in EngineManager.GetAllRiskyCombinationsOfTwo(engineMoves, playerMoves))
            {
                if (!engineMoves.Contains(EngineManager.CalculateBlock(combination[0], combination[1])))
                {
                    blocks.Add(EngineManager.CalculateBlock(combination[0], combination[1]));
                }
            }
            var rand = new Random();

            return(blocks[rand.Next(0, blocks.Count)]);
        }
        private static List <int> ChooseBestMovesInCombinationsLeft(List <MoveCombination> combinationsLeft,
                                                                    List <int> engineMoves, List <int> playerMoves) // TODO check this method
        {
            var theChoosenOnes = new List <int>();

            // This cycle checks each move of engineMoves if they are completing
            // any of the combinationsLeft, if it is completing one then calculate the block
            // for that combination and return inmidiatly theChoosenOnes with just the block move in it
            Console.WriteLine();
            foreach (var combination in combinationsLeft)
            {
                var counter = 0;
                var move1   = new int();
                var move2   = new int();
                foreach (var move in engineMoves)
                {
                    if (combination.Moves.Contains(move))
                    {
                        if (counter == 0)
                        {
                            move1 = move;
                        }
                        counter++;
                    }
                    if (counter == 2)
                    {
                        move2 = move;
                        theChoosenOnes.Add(EngineManager.CalculateBlock(move1, move2)); // here it adds the 0
                        return(theChoosenOnes);
                    }
                }
            }

            theChoosenOnes = FindRepeatedNumberTwoTimes(combinationsLeft, engineMoves, playerMoves);

            if (theChoosenOnes.Count == 0)
            {
                theChoosenOnes = AddAllPosibleMoves(combinationsLeft, engineMoves, playerMoves);
            }
            return(theChoosenOnes);
        }
Ejemplo n.º 4
0
        public static bool CheckState(IPlayer MyPlayer) // TODO : player moves with 555 or 333 says it wins
        {
            var PassedListOfMoves   = MyPlayer.Moves;
            var AllPosibilitiesList = new List <MoveCombination>();

            if (PassedListOfMoves.Count < 3)
            {
                return(false);
            }

            int[] arr = new int[PassedListOfMoves.Count];
            for (int i = 0; i < PassedListOfMoves.Count; i++)
            {
                arr[i] = PassedListOfMoves[i];
            }
            int r = 3;
            int n = arr.Length;

            PrintCombination(MyPlayer, arr, n, r);

            for (int i = 0; i <= UnOrderedMovesList.Count - 3; i = i + 3)
            {
                AllPosibilitiesList.Add(new MoveCombination(UnOrderedMovesList[i], UnOrderedMovesList[i + 1], UnOrderedMovesList[i + 2]));
            }

            foreach (var combination in AllPosibilitiesList)
            {
                if (EngineManager.CheckCombinationWithWiningCombinations(combination))
                {
                    UnOrderedMovesList.Clear();
                    return(true);
                }
            }
            UnOrderedMovesList.Clear();
            return(false);
        }
        private string Play()
        {
            switch (MyGame.MyPlayer.Moves.Count)
            {
            case 1:
                MyGame.MyEngine.Moves.Add(BlockingStrategy.FirstMove(MyGame.MyPlayer));
                return($" Computer First move is: {MyGame.MyEngine.Moves[0]} ");

            case 2:
                if (EngineManager.GetAllRiskyCombinationsOfTwo(MyGame.MyEngine.Moves, MyGame.MyPlayer.Moves).Count > 0)
                {
                    MyGame.MyEngine.Moves.Add(BlockingStrategy.WithAllCombinationsCalculateBlock(MyGame.MyEngine.Moves, MyGame.MyPlayer.Moves));
                    return($" Computer Second move is: {MyGame.MyEngine.Moves[1]} ");
                }
                MyGame.MyEngine.Moves.Add(WiningStrategy.CalculateWiningMove(MyGame.MyEngine.Moves, MyGame.MyPlayer.Moves));
                return($" Computer Second move is: {MyGame.MyEngine.Moves[1]} ");

            case 3:
                if (WinnerStateChecker.CheckState(MyGame.MyPlayer))
                {
                    return("YOU WIN!");
                }
                if (EngineManager.GetAllRiskyCombinationsOfTwo(MyGame.MyEngine.Moves, MyGame.MyPlayer.Moves).Count > 0 &&
                    EngineManager.GetAllRiskyCombinationsOfTwo(MyGame.MyPlayer.Moves, MyGame.MyEngine.Moves).Count < 1)
                {
                    MyGame.MyEngine.Moves.Add(BlockingStrategy.WithAllCombinationsCalculateBlock(MyGame.MyEngine.Moves, MyGame.MyPlayer.Moves));
                    if (WinnerStateChecker.CheckState(MyGame.MyEngine))
                    {
                        return($" Computer Third move is: {MyGame.MyEngine.Moves[2]}, Haha, You loose");
                    }
                    return($" Computer Third move is: {MyGame.MyEngine.Moves[2]} ");
                }
                MyGame.MyEngine.Moves.Add(WiningStrategy.CalculateWiningMove(MyGame.MyEngine.Moves, MyGame.MyPlayer.Moves));
                if (WinnerStateChecker.CheckState(MyGame.MyEngine))
                {
                    return($" Computer Third move is: {MyGame.MyEngine.Moves[2]}, oh Haha, You loose");
                }
                return($" Computer Third move is: {MyGame.MyEngine.Moves[2]} ");

            case 4:
                if (WinnerStateChecker.CheckState(MyGame.MyPlayer))
                {
                    return("YOU WIN!");
                }

                if (EngineManager.GetAllRiskyCombinationsOfTwo(MyGame.MyEngine.Moves, MyGame.MyPlayer.Moves).Count > 0 &&
                    EngineManager.GetAllRiskyCombinationsOfTwo(MyGame.MyPlayer.Moves, MyGame.MyEngine.Moves).Count < 1)
                {
                    MyGame.MyEngine.Moves.Add(BlockingStrategy.WithAllCombinationsCalculateBlock(MyGame.MyEngine.Moves, MyGame.MyPlayer.Moves));
                    if (WinnerStateChecker.CheckState(MyGame.MyEngine))
                    {
                        return($" Computer Fourth move is: {MyGame.MyEngine.Moves[3]}, Haha, You loose");
                    }
                    return($" Computer Fourth move is: {MyGame.MyEngine.Moves[3]} ");
                }
                MyGame.MyEngine.Moves.Add(WiningStrategy.CalculateWiningMove(MyGame.MyEngine.Moves, MyGame.MyPlayer.Moves));
                if (WinnerStateChecker.CheckState(MyGame.MyEngine))
                {
                    return($" Computer Fourth move is: {MyGame.MyEngine.Moves[3]}, Haha, You loose");
                }
                return($" Computer Fourth move is: {MyGame.MyEngine.Moves[3]} ");

            case 5:
                if (WinnerStateChecker.CheckState(MyGame.MyPlayer))
                {
                    return("YOU WIN!");
                }
                if (EngineManager.GetAllRiskyCombinationsOfTwo(MyGame.MyEngine.Moves, MyGame.MyPlayer.Moves).Count > 0 &&
                    EngineManager.GetAllRiskyCombinationsOfTwo(MyGame.MyPlayer.Moves, MyGame.MyEngine.Moves).Count < 1)
                {
                    MyGame.MyEngine.Moves.Add(BlockingStrategy.WithAllCombinationsCalculateBlock(MyGame.MyEngine.Moves, MyGame.MyPlayer.Moves));
                    if (WinnerStateChecker.CheckState(MyGame.MyEngine))
                    {
                        return($" Computer Fifth move is: {MyGame.MyEngine.Moves[4]}, Haha, You loose");
                    }
                    return($" Computer Fifth move is: {MyGame.MyEngine.Moves[4]} ");
                }
                MyGame.MyEngine.Moves.Add(WiningStrategy.CalculateWiningMove(MyGame.MyEngine.Moves, MyGame.MyPlayer.Moves));
                if (WinnerStateChecker.CheckState(MyGame.MyEngine))
                {
                    return($" Computer Fifth move is: {MyGame.MyEngine.Moves[4]}, Haha, You loose");
                }
                return($" Computer Fifth move is: {MyGame.MyEngine.Moves[4]} ");


            default:
                return("Inesperated Error");
            }
        }
        public void RunPartialGames()
        {
            int  move       = 0;
            bool validation = true;

TryAgain:

            Console.WriteLine("Choose game table");
            while (validation)
            {
                try
                {
                    move       = Convert.ToInt32(Console.ReadLine());
                    validation = !TableInputValidator(MyGame, move);
                }
                catch // validating correct input
                {
                    Console.WriteLine("Incorrect input, Only numbers from 1 to 9");

                    Console.WriteLine("Press enter your input again");

                    validation = true;
                }
            }


            while (!WinnerStateChecker.CheckState(MyGame.MyPlayer) && !WinnerStateChecker.CheckState(MyGame.MyEngine))
            {
                int  ComputerMove;
                bool tableValidation = true;

                while (tableValidation)
                {
                    NextTable = UltimateGame[move - 1].RunGame();

                    if (UltimateGame[NextTable - 1].Winned) // Checking if somebody won the table before and handle choosing
                                                            //table until we start again with new table move
                    {
                        Console.WriteLine("Table closed, computer will choose another one");
                        // -------------- choosing next table logic
                        move = EngineManager.ComputerChooseTable(MyGame);

                        Console.WriteLine($"Choosed table: {move}");

                        WinnerStateChecker.DisplayMovesOfPartialGame(move, UltimateGame);

                        ComputerMove = CalculateMove(MyGame, UltimateGame[move - 1].GetEngine(), UltimateGame[move - 1].GetPlayer());

                        Console.WriteLine($"Calculated Move: {ComputerMove}");

                        UltimateGame[NextTable - 1].AddToEnginemove(ComputerMove);

                        NextTable = ComputerMove;

                        if (WinnerStateChecker.CheckState(UltimateGame[NextTable - 1].GetEngine())) // Checking if computer won last table, if so handling logic.
                        {
                            Console.WriteLine($"Computer Won table {NextTable}");

                            WinnerStateChecker.DisplayMovesOfPartialGame(NextTable, UltimateGame);

                            MyGame.MyEngine.Moves.Add(NextTable);

                            if (Game9x9Checker(MyGame.MyEngine))
                            {
                                Console.WriteLine("Computer WON 9x9 Game");
                                MyGame.Over = true;
                                break;
                            }

                            UltimateGame[NextTable - 1].ChangeWinnedState();

                            goto TryAgain;
                        }

                        move            = ComputerMove;
                        tableValidation = true;
                    }
                    else
                    {
                        tableValidation = false;
                    }

                    if (WinnerStateChecker.CheckState(UltimateGame[move - 1].GetPlayer()) && tableValidation != true) // Checking if player won, if so handling choosing next table logic
                    {
                        // -------------- choosing next table logic
                        Console.WriteLine($"Player won table: {move}");

                        WinnerStateChecker.DisplayMovesOfPartialGame(move, UltimateGame);

                        MyGame.MyPlayer.Moves.Add(move);

                        if (Game9x9Checker(MyGame.MyPlayer))
                        {
                            Console.WriteLine("Player WON 9x9 Game");
                            MyGame.Over = true;
                            break;
                        }

                        UltimateGame[move - 1].ChangeWinnedState();

                        move = EngineManager.ComputerChooseTable(MyGame);

                        Console.WriteLine($"Choosed table :{move}");

                        WinnerStateChecker.DisplayMovesOfPartialGame(move, UltimateGame);

                        ComputerMove = CalculateMove(MyGame, UltimateGame[move - 1].GetEngine(), UltimateGame[move - 1].GetPlayer());

                        Console.WriteLine($"Calculated Move: {ComputerMove} ");

                        UltimateGame[move - 1].AddToEnginemove(ComputerMove);

                        if (WinnerStateChecker.CheckState(UltimateGame[move - 1].GetEngine()))
                        {
                            Console.WriteLine($"Computer Won table {move}");

                            WinnerStateChecker.DisplayMovesOfPartialGame(move, UltimateGame);

                            MyGame.MyEngine.Moves.Add(move);

                            if (Game9x9Checker(MyGame.MyEngine))
                            {
                                Console.WriteLine("Computer WON 9x9 Game");
                                MyGame.Over = true;
                                break;
                            }

                            UltimateGame[move - 1].ChangeWinnedState();

                            goto TryAgain;
                        }
                        move            = ComputerMove;
                        tableValidation = true;
                    }
                    else
                    {
                        tableValidation = false;
                    }
                }



                ComputerMove = CalculateMove(MyGame, UltimateGame[NextTable - 1].GetEngine(), UltimateGame[NextTable - 1].GetPlayer());

                WinnerStateChecker.DisplayMovesOfPartialGame(NextTable, UltimateGame);

                Console.WriteLine($"Calculated Move: {ComputerMove} ");

                UltimateGame[NextTable - 1].AddToEnginemove(ComputerMove);

                if (WinnerStateChecker.CheckState(UltimateGame[NextTable - 1].GetEngine()))
                {
                    Console.WriteLine($"Computer Won table: {NextTable}");

                    WinnerStateChecker.DisplayMovesOfPartialGame(NextTable, UltimateGame);

                    MyGame.MyEngine.Moves.Add(NextTable);

                    if (Game9x9Checker(MyGame.MyEngine))
                    {
                        Console.WriteLine("Engine wins 9x9 Game");
                        MyGame.Over = true;
                        break;
                    }

                    UltimateGame[NextTable - 1].ChangeWinnedState();
                    goto TryAgain;
                }
                move = ComputerMove;
            }
        }
Ejemplo n.º 7
0
 internal static bool ThereIsANeedToBlock(List <int> engineMoves, List <int> playerMoves)
 {
     return(EngineManager.GetAllRiskyCombinationsOfTwo(engineMoves, playerMoves).Count > 0);
 }