示例#1
0
        /// <summary>
        /// Helper method to update the score when a player finds a word
        ///
        /// Invariant: The second paramter should always be the player who found the word
        /// and the third parameter should always be the opponent
        /// </summary>
        /// <param name="word">The word found</param>
        /// <param name="playerFoundWord">Player who found it</param>
        /// <param name="opponent">Opponent player</param>
        private void updateScore(string word, Player playerFoundWord, Player opponent)
        {
            // make the message all uppercase
            word = word.ToUpper();

            // If name doesn't start with WORD
            if (!(word.StartsWith("WORD ")))
            {
                // then ignore the command and return
                playerFoundWord.ss.BeginSend("IGNORING\n", (e, o) => { }, playerOne.ss);
                return;
            }

            word = word.Substring(5);

            // if word is less than three characters, don't count it
            if (word.Length < 3)
            {
                return;
            }

            // if the word is not valid
            if (!(dictionary.Contains(word)) || !board.CanBeFormed(word))
            {
                // remove a point from the play who found the work
                playerFoundWord.score--;
                playerFoundWord.illegalWordsFound.Add(word);
            }
            else // otherwise if the word is valid
            {
                if (opponent.legalWordsFound.Contains(word)) // then if opponent already found the word
                {
                    // then remove the word from opponents's list
                    opponent.legalWordsFound.Remove(word);

                    // and add it to the list of common words
                    commonWords.Add(word);

                    // and reduce opponent's score by the value of word
                    opponent.score = opponent.score - getValue(word);
                }
                else // if the opponent hasn't found the word
                {
                    // then if the finder hasn't found the word yet either
                    if (!(playerFoundWord.legalWordsFound.Contains(word)))
                    {
                        // then add the word to player one's list
                        playerFoundWord.legalWordsFound.Add(word);

                        // and increase player one's score by the value of word
                        playerFoundWord.score = playerFoundWord.score + getValue(word);
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// Given a board configuration, returns all the valid words.
        /// </summary>
        private static List <string> AllValidWords(string board)
        {
            BoggleBoard   bb         = new BoggleBoard(board);
            List <string> validWords = new List <string>();

            foreach (string word in dictionary)
            {
                if (word.Length > 2 && bb.CanBeFormed(word))
                {
                    validWords.Add(word);
                }
            }
            return(validWords);
        }
示例#3
0
        /// <summary>
        /// Returns true if the given word is legal.
        /// </summary>
        private bool isLegalWord(string word)
        {
            //if it is not in the dictionary it is not valid
            if (!dictionary.Contains(word))
            {
                return(false);
            }

            //is it a valid word for the current game.
            else
            {
                return(board.CanBeFormed(word));
            }
        }
示例#4
0
        /// <summary>
        /// adds the word to the correct list and updates the score accordingly for both players
        /// </summary>
        /// <param name="word"></param>
        /// <param name="player"></param>
        /// <returns></returns>
        public void addWord(string word, int player)
        {
            //games must be in session
            if (gameInSession)
            {
                //Word is LEGAL and can be formed
                if (legalWords.Contains(word) && board.CanBeFormed(word))
                {
                    legalWord(word, player);
                }

                //WORD is ILLEGAL
                else
                {
                    illegalWord(word, player);
                }
            }
        }
示例#5
0
        /// <summary>
        /// Method that processes the word to see if either of the user gains points or loses points, and handles adding the words to each hashset as neccesary
        /// </summary>
        /// <param name="word"> The word that was passed to the server from the client</param>
        /// <param name="playerPlayed"> List of words that the player who sent the word has played</param>
        /// <param name="otherPlayer"> List of words that the other player has played</param>
        /// <param name="playerIllegal"> List of illegal words that the player who sent the word has played</param>
        /// <param name="otherIllegal"> List of illegal words that the other player has played</param>
        /// <param name="playerScore"> Score of the player that played the word</param>
        /// <param name="otherScore"> Score of the other player </param>
        private void processScore(string word, HashSet <string> playerPlayed, HashSet <string> otherPlayer, HashSet <string> playerIllegal, HashSet <string> otherIllegal, ref int playerScore, ref int otherScore)
        {
            // If the word cannot be formed, the player's score who played the word is reduced by 1, and the word is added to the illegal hashset
            if (!board.CanBeFormed(word) && !playerIllegal.Contains(word))
            {
                playerScore = playerScore - 1;
                playerIllegal.Add(word);
            }
            // Else if the other player has already played this word, it is removed from there list and added
            // To the common list. The other player's score is reduced by the point value of the word
            else if (otherPlayer.Contains(word))
            {
                playedWords.Add(word);

                otherPlayer.Remove(word);

                otherScore = otherScore - scoreWord(word);
            }
            // If the player's played list, or common played words list or player's illegal list contains the word, ignore the word
            else if (playerPlayed.Contains(word) || playedWords.Contains(word) || playerIllegal.Contains(word))
            {
                // do Nothing
            }
            // If the word is in the legal dictionary, add the word to the player played hash, and add the value to the player's score
            else if (legalDictionary.Contains(word))
            {
                playerPlayed.Add(word);

                playerScore = playerScore + scoreWord(word);
            }
            // If none of the above conditions are true, the word is illegal. Score is reduced by 1, and the word is added to the illegal list
            else
            {
                playerScore = playerScore - 1;
                playerIllegal.Add(word);
            }
        }
示例#6
0
文件: Tests.cs 项目: zakpeters/Boggle
        /// <summary>
        /// Given a board configuration, returns all the valid words.
        /// </summary>
        private static IList <string> AllValidWords(string board)
        {
            ISet <string> dictionary = new HashSet <string>();

            using (StreamReader words = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + @"/dictionary.txt"))
            {
                string word;
                while ((word = words.ReadLine()) != null)
                {
                    dictionary.Add(word.ToUpper());
                }
            }
            BoggleBoard   bb         = new BoggleBoard(board);
            List <string> validWords = new List <string>();

            foreach (string word in dictionary)
            {
                if (bb.CanBeFormed(word))
                {
                    validWords.Add(word);
                }
            }
            return(validWords);
        }
示例#7
0
        /// <summary>
        /// Takes a word and adds it to a players list while increasing their score in a game with a particular game ID
        /// Returns the words value
        /// </summary>
        public int?PlayWord(PlayWord w)
        {
            if (w == null)
            {
                SetResponse("NF");
                return(null);
            }
            if (String.IsNullOrEmpty(w.playerToken))
            {
                SetResponse("player");                                          //403 Forbidden Response(invalid player token)
                return(null);
            }
            if (String.IsNullOrEmpty(w.word))
            {
                SetResponse("word");                                          //403 Forbidden Response(invalid player token)
                return(null);
            }
            if (String.IsNullOrEmpty(w.gameToken))
            {
                SetResponse("gt");                                              //403 Forbidden Response (invalid game token)
                return(null);
            }

            SetResponse("Success");
            int  value = WordLookUp(w.word);
            Game game;

            if (Games.TryGetValue(w.gameToken, out game))                                 //Find game
            {
                BoggleBoard bb = new BoggleBoard(game.board);
                if (!bb.CanBeFormed(w.word))                                                           //TODO check to make sure its not negative points
                {
                    value = 0;
                }
                if (game.Player1.userToken == w.playerToken)                                           //Check if player is Player1
                {
                    if (value > 0 || value < 1)                                                        //If word value is greater than 0
                    {
                        if (!game.Player1Words.ContainsKey(w.word))                                    //If Players word list does not contain the word being played
                        {
                            game.Player1Words.Add(w.word, value);                                      //Add it to player word list
                            //game.Player1.wordsPlayed.Add(word, value);                               //Add to word to player words played list
                            //WordListComparer(g.gameToken);                                           //Check if Player lists are unique and removes like words
                        }
                    }
                }
                else if (game.Player2.userToken == w.playerToken)                                      //Repeat previous if the player is player two
                {
                    if (value > 0 || value < 1)
                    {
                        if (!game.Player2Words.ContainsKey(w.word))
                        {
                            game.Player2Words.Add(w.word, value);
                            //game.Player2.wordsPlayed.Add(word, value);                               //Add to words to word played
                            //WordListComparer(g.gameToken);
                        }
                    }
                }
                else
                {
                    SetResponse("player");                                      //403 Forbidden Response(invalid player token)
                    return(null);
                }
            }
            return(value);
        }