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); }
//returns shuffledDeck dominoOutput computerDomino computerDeck public (IList <Domino>, List <Domino>, string, Domino) ComputerSearchEngine_NewDominoShuffledDeck(IList <Domino> shuffledDeck, string dominoOutput, Domino computerDomino, Domino playerDomino, List <Domino> computerDeck) { Random rand = new Random(); //draw another card from the deck while (true) { int digit = rand.Next(1, shuffledDeck.Count) - 1; var domino = shuffledDeck[digit]; if (domino.FirstN == playerDomino.SecondN) { dominoOutput += " (C) " + domino.ToString(); computerDomino = domino; break; } else if (domino.SecondN == playerDomino.SecondN) { dominoOutput += " (C) " + domino.Reverse(); computerDomino = new Domino(domino.Reverse()); break; } else { computerDeck.Add(domino); } shuffledDeck.RemoveAt(digit); } return(shuffledDeck, computerDeck, dominoOutput, computerDomino); }
//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); } } }
//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); } }
//Who is first for drawing the dominoes public (List <List <Domino> >, List <Domino>) PlayerIsFirst(string whoIsFirst) { var rand = new Random(); var shuffledDeck = Shuffle(); var playersDecks = new List <List <Domino> >(); var playerDeck = new List <Domino>(); var computerDeck = new List <Domino>(); if (whoIsFirst == "player") { Console.Write($"Enter seven digits (1-{shuffledDeck.Count}) seperated with space : "); var digits = Console.ReadLine() .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .Select(int.Parse) .ToList(); foreach (var item in digits) { Domino domino = shuffledDeck[item - 1]; playerDeck.Add(domino); } playersDecks.Add(playerDeck); digits.ForEach(d => shuffledDeck.RemoveAt(d - 1)); var lD = new List <int>(); for (int i = 0; i <= 6; i++) { int r = rand.Next(1, shuffledDeck.Count); while (digits.Contains(r)) { r = rand.Next(1, shuffledDeck.Count); } computerDeck.Add(shuffledDeck[r - 1]); shuffledDeck.RemoveAt(r - 1); } playersDecks.Add(computerDeck); } else { for (int i = 0; i <= 6; i++) { int r = rand.Next(1, shuffledDeck.Count); computerDeck.Add(shuffledDeck[r - 1]); shuffledDeck.RemoveAt(r - 1); } playersDecks.Add(computerDeck); Console.Write("Enter seven digits (1-21) seperated with space : "); var digits = Console.ReadLine() .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .Select(int.Parse) .ToList(); foreach (var item in digits) { playerDeck.Add(shuffledDeck[item - 1]); } digits.ForEach(d => shuffledDeck.RemoveAt(d - 1)); playersDecks.Add(playerDeck); var cD = playersDecks[0]; playersDecks[0] = playersDecks[1]; playersDecks[1] = cD; } return(playersDecks, shuffledDeck); }