// Function to finish the different rounds of game, // The game will be ended if one of player has exceeded // maximum scrore to win. private void endGame() { int differenceScore = GinRummy.Score(HumanCards, ComputerCards); round += 1; if (differenceScore > 0) { firstWinnerScore += differenceScore; RaiseNotification("You win at Round" + round, "Round" + round); } else { secondWinnerScore += (differenceScore * -1); RaiseNotification("Computer wins at Round" + round, "Round" + round); } scoreLabel(firstWinnerScore, secondWinnerScore); if (firstWinnerScore >= winingScore) { RaiseNotification("Congratulations, You have win this game", "You wins"); resetGame(); } else if (secondWinnerScore >= winingScore) { RaiseNotification("Sorry, Computer Player wins this game", "Computer Wins"); resetGame(); } else { resetGame(); } }
async private void HumanCards_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { HumanDeadwood = "Calculating ..."; // this might take a while, so let's do it in the background int deadwood = await Task.Run(() => GinRummy.Deadwood(HumanCards)); HumanDeadwood = "Deadwood: " + deadwood; }
async private void ArtificialPlayer() { if (takenFromDeckOrDiscardPile) { while (true) { ComputerThoughts = "Calculating your impending doom ..."; foreach (var card in ComputerCards) { computerSeen.Remove(card); } GinRummy.sortByRankAndSuit(ComputerCards); var x = ComputerPlayer.averageAllOfThePossibleDeadwoods(computerSeen, ComputerCards); var pickupFromDeckOrDiscard = await Task.Run(() => ComputerPlayer.ComputerPickupDiscard(ComputerCards, Discards.First(), computerSeen)); if (pickupFromDeckOrDiscard) // picking up from the deck { ComputerThoughts = "You can not comprehend how much I know the top of the deck will benefit me, fool!"; ComputerCards.Add(DrawTopCardFromDeck()); await Task.Delay(5000); var cardToAddToDiscard = ComputerPlayer.ComputerMove(ComputerCards); ComputerCards.Remove(cardToAddToDiscard.Item2.Value); Discards.Add(cardToAddToDiscard.Item2.Value); } else // picking up from the discard { ComputerThoughts = "Ahh this card will do very nicely, my thanks for allowing me to have it, fool!"; await Task.Delay(2000); var topOfDiscard = Discards[Discards.Count - 1]; ComputerCards.Add(topOfDiscard); Discards.Remove(topOfDiscard); await Task.Delay(5000); var cardToAddToDiscard = ComputerPlayer.ComputerMove(ComputerCards); ComputerCards.Remove(cardToAddToDiscard.Item2.Value); Discards.Add(cardToAddToDiscard.Item2.Value); } ComputerThoughts = "I now end my turn. Your move, human." + GinRummy.Deadwood(ComputerCards); break; } canNowDiscard = false; takenFromDeckOrDiscardPile = false; } }
private void ComputerTurn() { var takeDiscard = ComputerPlayer.ComputerPickupDiscard(ComputerCards, Discards[Discards.Count - 1], RemainingDeck); if (takeDiscard) { var discardCard = Discards[Discards.Count - 1]; Discards.Remove(discardCard); ComputerCards.Add(discardCard); } else { Cards.Card remainingDeckCards = RemainingDeck[RemainingDeck.Count - 1]; RemainingDeck.Remove(remainingDeckCards); ComputerCards.Add(remainingDeckCards); } var compMove = ComputerPlayer.ComputerMove(ComputerCards); ComputerCards.Remove(compMove.Item2.Value); Discards.Add(compMove.Item2.Value); var scores = GinRummy.Score(ComputerCards, HumanCards); if (compMove.Item1.IsGin) { compScore += scores; if (humanScore < 100 && compScore < 100) { RaiseNotification("Human Score: " + humanScore + ". Computer Score: " + compScore, "Scores So Far"); CleanUpCards(); } } else if (compMove.Item1.IsKnock) { if (scores > 0) { compScore += scores; if (humanScore < 100 && compScore < 100) { RaiseNotification("Human Score: " + humanScore + ". Computer Score: " + compScore, "Scores So Far"); CleanUpCards(); } } else { humanScore += Math.Abs(scores); if (humanScore < 100 && compScore < 100) { RaiseNotification("Human Score: " + humanScore + ". Computer Score: " + compScore, "Scores So Far"); CleanUpCards(); } } } }
// Function to end the game when player, // when button is clicked private void GinClick() { int deadwoodScore = GinRummy.Deadwood(HumanCards); if (deadwoodScore == 10) { endGame(); } else { RaiseNotification("You have not reached 0 points", "Invalid action"); } }
private async void ButtonClick() { var scores = GinRummy.Score(HumanCards, ComputerCards); if (scores > 0) { humanScore += scores; } else { compScore += Math.Abs(scores); } RaiseNotification("Human Score: " + humanScore + ". Computer Score: " + compScore, "Scores So Far"); CleanUpCards(); }
private void KnockClick() { if (Deadwood < 10 && dealFinishied) { int currentScore = GinRummy.Score(HumanCards, ComputerCards); int humanDeadwood = GinRummy.Deadwood(HumanCards); int computerDeadwood = GinRummy.Deadwood(ComputerCards); if (currentScore > 0) { HumanScore += currentScore; RaiseNotification("It seems I have underestimated you, I only had a Deadwood of " + computerDeadwood + " while you had a " + humanDeadwood, "Human Knock"); } else { ComputerScore += (currentScore * -1); RaiseNotification("You fell into my trap human I had a score of " + ComputerScore + " while you had a weak score of " + HumanScore, "Human Undercut"); } nextRound(); // Do something to check what the current score is and see if it is the end of the game } }
private void GinClick() { ComputerScore += 50; CheckForEndGame(); if (Deadwood == 0 && dealFinishied) { int currentScore = GinRummy.Score(HumanCards, ComputerCards); if (currentScore > 0) { HumanScore += currentScore; } // Do something to check what the current score is and see if it is the end of the game } else if (!dealFinishied) { RaiseNotification("Go back to 104, script kiddie.", "Can't go Gin during the deal"); } else { RaiseNotification("You don't have a low enough Deadwood to go Gin yet, noob!", "Too much Deadwood!"); } }
async private void HumanCards_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { HumanDeadwood = "Calculating ..."; Score = "Human Score: " + humanScore + " - Computer Score: " + compScore; // this might take a while, so let's do it in the background int deadwood = await Task.Run(() => GinRummy.Deadwood(HumanCards)); HumanDeadwood = "Deadwood: " + deadwood; if (deadwood == 0) { ButtonName = "GIN"; } else { ButtonName = "Knock"; } if (humanScore > 99 || compScore > 99) { humanScore = 0; compScore = 0; CleanUpCards(); } }
async private void ComputerCards_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { int deadwood = await Task.Run(() => GinRummy.Deadwood(ComputerCards)); }
static bool Turn(Player player, Player opponent) { Console.WriteLine(player.Name); player.Cards.Sort(); foreach (var card in player.Cards) { Console.Write("{0} ", ToString(card)); } Console.WriteLine(); Console.Write("Thinking ... "); var pickupDiscard = ComputerPlayer.ComputerPickupDiscard(player.Cards, Discards.Peek(), RemainingDeck); Console.WriteLine(); if (pickupDiscard) { var card = Discards.Pop(); player.Cards.Add(card); Console.WriteLine("picks up {0} from Discard pile", ToString(card)); } else { var card = RemainingDeck.Pop(); player.Cards.Add(card); Console.WriteLine("picks up {0} from Remaining Deck", ToString(card)); } var move = ComputerPlayer.ComputerMove(player.Cards); var discard = move.Item2; if (FSharpOption <Cards.Card> .get_IsSome(discard)) { Discards.Push(discard.Value); player.Cards.Remove(discard.Value); Console.WriteLine("Discard {0}", ToString(discard.Value)); } else { Console.WriteLine("Discard nothing"); } Console.WriteLine("deadwood score = {0}", GinRummy.Deadwood(player.Cards)); var action = move.Item1; if (action.IsGin) { Console.WriteLine("Gin!"); } else if (action.IsKnock) { Console.WriteLine("Knock!"); } if (!action.IsContinue) { Console.WriteLine(player.Name + " goes out first ..."); var score = GinRummy.Score(player.Cards, opponent.Cards); if (score > 0) { player.Score += score; } else { opponent.Score -= score; } } return(!action.IsContinue); }