Пример #1
0
        PlayerHasToChoose(int digit, Domino computerDomino, string result, IList <Domino> shuffledDeck, Domino playerDomino,
                          List <Domino> playerDeck, string dominoOutput)
        {
            if (result.ToLower() == "a")
            {
                //add new domino
                while (true)
                {
                    Console.Title = ($"Your dominoes are : \n" + String.Join(",   ", playerDeck));
                    Console.Write($"Select a number (1-{shuffledDeck.Count})");

                    digit        = int.Parse(Console.ReadLine()) - 1;
                    playerDomino = shuffledDeck[digit];
                    shuffledDeck.Remove(playerDomino);
                    playerDeck.Add(playerDomino);

                    if (playerDomino.FirstN == computerDomino.SecondN)
                    {
                        dominoOutput += " (P) " + playerDomino.ToString();

                        break;
                    }
                    else if (playerDomino.SecondN == computerDomino.SecondN)
                    {
                        dominoOutput += " (P) " + playerDomino.Reverse();
                        playerDomino  = new Domino(playerDomino.Reverse());
                        break;
                    }
                }
                //Clear the console
                Console.Clear();
            }
            else if (result.ToLower() == "r")
            {
                //reverse
                dominoOutput += " (P) " + playerDeck[digit].Reverse();
                playerDomino  = new Domino(playerDeck[digit].Reverse());
                playerDeck.RemoveAt(digit);
            }
            else
            {
                //Just add it to the dominoes
                playerDomino  = playerDeck[digit];
                dominoOutput += " (P) " + playerDeck[digit];
                playerDeck.RemoveAt(digit);
            }
            return(digit, playerDomino, computerDomino, shuffledDeck, playerDeck, dominoOutput);
        }
Пример #2
0
        //returns computerDomino and dominoOutput(the string result)
        public (Domino, string) IsThereAnAppropriateDominoInTheComputerDeck(Domino domino, Domino playerDomino)
        {
            var    computerDomino = new Domino();
            string compResult     = "";

            if (domino.FirstN == playerDomino.SecondN)
            {
                compResult    += " (C) " + domino.ToString();
                computerDomino = domino;
                return(computerDomino, compResult);
            }
            else if (domino.SecondN == playerDomino.SecondN)
            {
                compResult    += " (C) " + domino.Reverse();
                computerDomino = new Domino(domino.Reverse());
                return(computerDomino, compResult);
            }
            else
            {
                return(computerDomino, compResult);
            }
        }
Пример #3
0
        //The game
        public void StartPlaying(IList <Domino> shuffledDeck, List <Domino> playerDeck, List <Domino> computerDeck, string whoIsFirst)
        {
            Random rand = new Random();

            if (whoIsFirst == "player")
            {
                //Show the player his/her dominoes
                Console.Title = $"Your dominoes: {String.Join(",   ", playerDeck)}";

                //Draw domino from his pile
                Console.Write($"Play any domino you want (1-{playerDeck.Count}) ");
                int digit        = int.Parse(Console.ReadLine()) - 1;
                var playerDomino = playerDeck[digit];

                //remove the domino
                playerDeck.RemoveAt(digit);

                //start the string output
                string dominoOutput = "(P) " + playerDomino.ToString();
                Console.Title = $"Your dominoes: {String.Join(",   ", playerDeck)}";

                //display the string
                Console.WriteLine(dominoOutput);


                //Create a simple computer domino
                var computerDomino = new Domino();

                while (true)
                {
                    //The compuetr has to choose an appropriate domino

                    //First it has to search in its own deck
                    string compRes       = "";
                    var    newDominoComp = computerDomino;

                    foreach (var domino in computerDeck)
                    {
                        var app = IsThereAnAppropriateDominoInTheComputerDeck(domino, playerDomino);

                        compRes        = app.Item2;
                        computerDomino = app.Item1;
                    }
                    computerDeck.Remove(computerDomino);

                    //almost the same code for the shuffled deck
                    if (string.IsNullOrEmpty(compRes) || string.IsNullOrWhiteSpace(compRes))
                    {
                        var previousComputerDeckCount = computerDeck.Count;
                        var newSearchEngineReturnings = ComputerSearchEngine_NewDominoShuffledDeck(shuffledDeck, dominoOutput, computerDomino, playerDomino, computerDeck);

                        shuffledDeck   = newSearchEngineReturnings.Item1;
                        computerDeck   = newSearchEngineReturnings.Item2;
                        dominoOutput   = newSearchEngineReturnings.Item3;
                        computerDomino = newSearchEngineReturnings.Item4;

                        Console.WriteLine($"The computer has drown {computerDeck.Count - previousComputerDeckCount} dominoes");
                    }
                    else
                    {
                        if (newDominoComp != computerDomino)
                        {
                            dominoOutput += " (C) " + computerDomino.ToString();
                        }
                    }

                    Console.Clear();
                    Console.WriteLine(dominoOutput);

                    //Player has to choose what he wants
                    //Player
                    Console.Write($"Play any domino you wish (1-{playerDeck.Count})");
                    digit = int.Parse(Console.ReadLine()) - 1;

                    Console.Write($"If you press A you can peek another domino from the shuffled" +
                                  " deck or press R to reverse the domino you chose and if you press another" +
                                  " button nothing will happen: ");

                    string result = Console.ReadLine();

                    var choose = PlayerHasToChoose(digit, computerDomino, result, shuffledDeck, playerDomino, playerDeck, dominoOutput);
                    digit          = choose.Item1;
                    playerDomino   = choose.Item2;
                    computerDomino = choose.Item3;
                    shuffledDeck   = choose.Item4;
                    playerDeck     = choose.Item5;
                    dominoOutput   = choose.Item6;
                    //Show the dominoes
                    Console.WriteLine(dominoOutput);

                    //Show the player his dominoes
                    Console.Title = ($"Your dominoes are : \n" + String.Join(",   ", playerDeck));

                    //Clear the console
                    Console.Clear();

                    //show the dominoes
                    Console.WriteLine(dominoOutput);

                    WhoWins(shuffledDeck, playerDeck, computerDeck);
                }
            }
            else
            {
                Domino computerSelectedDomino = computerDeck[rand.Next(1, computerDeck.Count) - 1];
                computerDeck.Remove(computerSelectedDomino);
                string dominoOutput = "(C) " + computerSelectedDomino.ToString();

                Domino newPlayerDomino = new Domino();
                Console.WriteLine(dominoOutput);

                while (true)
                {
                    //Player
                    Console.Write($"Play any domino you wish (1-{playerDeck.Count})");
                    int digit = int.Parse(Console.ReadLine()) - 1;

                    Console.Write($"If you press A you can peek another domino from the shuffled" +
                                  " deck or press R to reverse the domino you chose and if you press another" +
                                  " button nothing will happen: ");

                    string result = Console.ReadLine();

                    var choose = PlayerHasToChoose(digit, computerSelectedDomino, result, shuffledDeck, newPlayerDomino, playerDeck, dominoOutput);
                    digit                  = choose.Item1;
                    newPlayerDomino        = choose.Item2;
                    computerSelectedDomino = choose.Item3;
                    shuffledDeck           = choose.Item4;
                    playerDeck             = choose.Item5;
                    dominoOutput           = choose.Item6;

                    //Show the player his dominoes
                    Console.Title = ($"Your dominoes are : \n" + String.Join(",   ", playerDeck));
                    //Clear the console
                    Console.Clear();
                    //show the dominoes
                    Console.WriteLine(dominoOutput);



                    //Computer
                    //search for appropriate domino
                    string compRes       = "";
                    var    newDominoComp = computerSelectedDomino;

                    foreach (var domino in computerDeck)
                    {
                        var app = IsThereAnAppropriateDominoInTheComputerDeck(domino, newPlayerDomino);
                        compRes = app.Item2;
                        computerSelectedDomino = app.Item1;
                    }
                    computerDeck.Remove(computerSelectedDomino);

                    if (string.IsNullOrEmpty(compRes) || string.IsNullOrWhiteSpace(compRes))
                    {
                        var previousComputerDeckCount = computerDeck.Count;
                        var newSearchEngineReturnings = ComputerSearchEngine_NewDominoShuffledDeck(shuffledDeck, dominoOutput, computerSelectedDomino, newPlayerDomino, computerDeck);

                        shuffledDeck           = newSearchEngineReturnings.Item1;
                        computerDeck           = newSearchEngineReturnings.Item2;
                        dominoOutput           = newSearchEngineReturnings.Item3;
                        computerSelectedDomino = newSearchEngineReturnings.Item4;

                        Console.WriteLine($"The computer has drown {computerDeck.Count - previousComputerDeckCount} dominoes");
                    }
                    else
                    {
                        ////print it
                        if (newDominoComp != computerSelectedDomino)
                        {
                            dominoOutput += " (C) " + computerSelectedDomino.ToString();
                        }
                    }
                    Console.Clear();
                    Console.WriteLine(dominoOutput);

                    WhoWins(shuffledDeck, playerDeck, computerDeck);
                }
            }
        }