Example #1
0
        /// <summary>
        /// Handles receiving a word.
        /// </summary>
        ///<param name="e">The exception that was thrown when a word could not be received</param>
        ///<param name="message">The message containing the word</param>
        ///<param name="payload">The player that sent the message</param>
        private void wordPlayed(string message, Exception e, object payload)
        {
            System.Diagnostics.Debug.WriteLine("Receiving a word");
            // multiple words are not allowed to come in at once, it might corrupt the scores
            lock (scoreLock)
            {
                if (receiveCallbackCheck(message, e, payload))
                {
                    return;
                }

                Player player   = ((Player)payload);
                Player opponent = getOpponent(player);
                // message format is:  WORD (word)

                System.Diagnostics.Debug.WriteLine("Received " + message + " from " + player.Name);

                if (message.Contains("WORD "))
                {
                    String word      = message.Substring("WORD ".Length).ToUpper();
                    int    wordValue = computeScore(word);

                    // First, check to see if the word was already played.
                    // Constant lookup time ensures efficiency.
                    if (!player.ValidWords.Contains(word) && !player.InvalidWords.Contains(word))
                    {
                        // if the word is invalid,
                        // take off 1 point, and add to the invalid words list.
                        if (wordValue == -1)
                        {
                            player.Score += wordValue;
                            player.InvalidWords.Add(word);
                        }
                        // else check to see if the opponent has played it
                        else
                        {
                            // if the opponent played the word, no one gets points for it
                            if (opponent.ValidWords.Contains(word))
                            {
                                //Subtract value of word from player 2's score.
                                opponent.Score -= wordValue;
                            }
                            //Else, add word value to player's score.
                            else
                            {
                                player.Score += wordValue;
                            }
                            //Add word to list of words that have been played.
                            player.ValidWords.Add(word);
                        }
                    }
                    sendScores();
                }
                else
                {
                    //If message is not a valid command, send the ignoring statement.
                    player.Ignoring(message);
                }
                player.Socket.BeginReceive(wordPlayed, player);
            }
        }