Exemple #1
0
        /// <summary>
        /// Invokes the specified contestant.
        /// </summary>
        /// <param name="contestant">The contestant.</param>
        private void InvokePlayer(Contestant contestant)
        {
            #region Implementation

            //increment the index every time a player is invoked
            CurrentPlayerIndex++;

            //only invoke the player if they still have remaining attempts
            if (contestant.IsInPlay)
            {
                //if the player is a human player, pass the human's
                //guess along to the interface implementation
                if (contestant.Player.IsHuman)
                {
                    ((IHumanPlayer)contestant.Player).Guess(contestant.HumanGuess);
                }

                try
                {
                    //call the player to have another crack
                    string guess = contestant.Player.TakeTurn(contestant.CurrentPhrase.Strip());

                    if (!string.IsNullOrWhiteSpace(guess))
                    {
                        bool isGoodGuess = false;

                        //if the player returns a single letter, then they're just eliminating that letter
                        //rather than guessing the entire word
                        if (guess.Length == 1)
                        {
                            Trace.WriteLine(string.Format("Player '{0}' has guessed the letter '{1}'", contestant.Name, guess));

                            if (CurrentPhrase.Contains(guess, StringComparison.OrdinalIgnoreCase))
                            {
                                if (contestant.IncrementHits(guess))
                                {
                                    //if the phrase contains the letter update the player's progress
                                    contestant.CurrentPhrase = UpdateWord(contestant.CurrentPhrase, guess);
                                    isGoodGuess = true;

                                    Trace.WriteLine(string.Format("Player '{0}'s phrase has now been updated to '{1}'",
                                        contestant.Name, contestant.CurrentPhrase));
                                }
                            }

                            guess = contestant.CurrentPhrase;
                        }
                        else
                        {
                            Trace.WriteLine(string.Format("Player '{0}' has guessed the phrase '{1}'", contestant.Name, guess));

                            //Has the player guessed the complete word
                            if (CurrentPhrase.Equals(guess, StringComparison.OrdinalIgnoreCase))
                            {
                                //if the phrase contains the letter update the player's progress
                                contestant.CurrentPhrase = guess;
                                contestant.IncrementHits(guess);
                                contestant.HasCompletedChallenge = true;
                                isGoodGuess = true;
                                Trace.WriteLine(string.Format("Player '{0}' has correctly guessed the phrase!", contestant.Name));
                            }
                        }

                        //if not increment the number of attempts if and
                        //disqualify them if they've exceeded the max chances
                        if (!isGoodGuess)
                        {
                            contestant.Attempts++;
                            contestant.IsInPlay = contestant.Attempts < MaxNumberOfChances;

                            if (contestant.IsInPlay)
                            {
                                Trace.WriteLine(string.Format("Player '{0}' now has {1} chance(s) remaining",
                                    contestant.Name, (MaxNumberOfChances - contestant.Attempts)));
                            }
                            else
                            {
                                Trace.WriteLine(string.Format("Player '{0}' has run out of chances - for you, the war is over!", contestant.Name));
                            }
                        }
                    }
                    else
                    {
                        //a player that returns nothing is disqualified
                        contestant.IsInPlay = false;
                        contestant.IsDisqualified = true;
                        Trace.WriteLine(string.Format("Player '{0}' returned an invalid result and has been disqualified", contestant.Name));
                    }
                }
                catch
                {
                    //a player that throws an exception is disqualified
                    contestant.IsInPlay = false;
                    contestant.IsDisqualified = true;
                    Trace.WriteLine(string.Format("Player '{0}' threw an exception and has been disqualified", contestant.Name));
                }

                contestant.TotalAttempts++;
                Hangman.Executioner.UpdateLifeLineImage(contestant);
                Trace.WriteLine(Environment.NewLine);

                //update the UI once all players have had a go or there is a game changer
                if (contestant.IsDisqualified || CurrentPlayerIndex >= _playersInDraw.Count)
                {
                    MatchStatus = EvaluateRound();
                }
            }

            #endregion
        }
        private void AddPlayer()
        {
            #region Implementation

            if (string.IsNullOrWhiteSpace(HumanPlayerName))
            {
                var result = MessageBox.Show("Please specify a player name", "Invalid Player", MessageBoxButton.OKCancel, MessageBoxImage.Information, MessageBoxResult.OK);
            }
            else
            {
                try
                {
                    if (ValidateTeamName(HumanPlayerName))
                    {
                        IPlayer player = God.CreateHuman(HumanPlayerName);
                        var contestant = new Contestant(HumanPlayerName, player);

                        _tournamentPlayers.Add(contestant);
                        HumanPlayerName = string.Empty;

                        //check that we have enough
                        EvaluateContestants();
                    }
                    else
                    {
                        MessageBox.Show("That name has already been taken, please choose another.");
                    }
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message, "Cannot Create Player");
                }
            }

            #endregion
        }
Exemple #3
0
        /// <summary>
        /// Updates the life line image.
        /// </summary>
        /// <param name="contestant">The contestant.</param>
        /// <returns></returns>
        public void UpdateLifeLineImage(Contestant contestant)
        {
            #region Implementation

            string image = string.Empty;
            LifeLine lifelIne = LifeLine.Full;

            if (contestant.IsInPlay)
            {
                //convert the contestants remaining chances into a lifeline
                int remainder = MaxNumberOfChances - contestant.Attempts;
                lifelIne = (LifeLine)remainder;
            }
            else
            {
                lifelIne = LifeLine.Dead;
            }

            //call the relevant method depending on the rules of the game
            switch (MaxNumberOfChances)
            {
                case 3:
                    image = Get3LifeLineImage(lifelIne);
                    break;

                case 4:
                    image = Get4LifeLineImage(lifelIne);
                    break;

                case 5:
                    image = Get5LifeLineImage(lifelIne);
                    break;

                default:
                    image = Get6LifeLineImage(lifelIne);
                    break;
            }

            //create the image
            Uri source = new Uri(image, UriKind.Relative);
            contestant.StatusImage = new BitmapImage(source);

            #endregion
        }