/// <summary>
 /// help method used to get word and score from Words table
 /// </summary>
 /// <param name="GameID">gameid need to check whether exist or not</param>
 /// <param name="UserToken">usertoken need to check whether exist or not</param>
 /// <returns>return the list of words that the player played in this game. </returns>
 public List <WordsPlayed> getWords(String GameID, String UserToken)
 {
     using (SqlConnection conn = new SqlConnection(BoggleDB))
     {
         conn.Open();
         using (SqlTransaction trans = conn.BeginTransaction())
         {
             // From Word table, we selected Word and Score when GameID and Player existed
             using (SqlCommand command = new SqlCommand(getPath("wordsList"), conn, trans))
             {
                 command.Parameters.AddWithValue("@Player", UserToken);
                 command.Parameters.AddWithValue("@GameID", GameID);
                 using (SqlDataReader reader = command.ExecuteReader())
                 {
                     List <WordsPlayed> words = new List <WordsPlayed>();
                     while (reader.Read() != false)
                     {
                         // update the word and score
                         WordsPlayed word = new WordsPlayed();
                         word.Word  = reader["Word"].ToString();
                         word.Score = (Int32)reader["Score"];
                         words.Add(word);
                     }
                     return(words);
                 }
             }
         }
     }
 }
Exemple #2
0
        public override bool Equals(object obj)
        {
            if (!(obj is WordsPlayed) || Word == null)
            {
                return(false);
            }

            WordsPlayed other = (WordsPlayed)obj;

            return(Word.Equals(other.Word));
        }
        /// <summary>
        /// help method to add valid word to the recorded word list
        /// </summary>
        /// <param name="player">the player of the current game. </param>
        /// <param name="word">the word that typied by the user. </param>
        public void helpAdd(Player player, WordsPlayed word)
        {
            bool realAdd = true;

            foreach (WordsPlayed temp in player.words)
            {
                if (temp.Word == word.Word)
                {
                    realAdd = false;
                }
            }
            if (realAdd == true)
            {
                player.words.Add(word);
                player.Score += word.Score;
            }
        }
Exemple #4
0
        /// <summary>
        /// This private helper evaluate player's word input and returns a valid score.
        /// </summary>
        /// <param name="trimmed"></param>
        /// <param name="board"></param>
        /// <returns></returns>
        private int ComputeScore(string trimmed, BoggleBoard board, List <WordsPlayed> words)
        {
            int         wordCount  = trimmed.Length;
            WordsPlayed PlayedWord = new WordsPlayed();

            PlayedWord.Word = trimmed;

            // If word is less than 3 words, no point. Same goes for duplicated inputs.
            if (wordCount < 3 || words.Contains(PlayedWord))
            {
                return(0);
            }

            // If word is not present in the dictionary.txt
            if (!board.CanBeFormed(trimmed) || !dictionary.Contains(trimmed))
            {
                return(-1);
            }

            // Assign score based on the length of word input
            if (wordCount == 3 || wordCount == 4)
            {
                return(1);
            }
            else if (wordCount == 5)
            {
                return(2);
            }
            else if (wordCount == 6)
            {
                return(3);
            }
            else if (wordCount == 7)
            {
                return(5);
            }

            // word inputs > 7 gets 11 points
            return(11);
        }
        /// <summary>
        /// method for play word
        /// </summary>
        /// <param name="user">the UserToek and the word that this player typied. </param>
        /// <param name="GameID">the GameID of this player in . </param>
        /// <returnsplayWordsReturn>return the score of the word that the user type. </returns>
        public playWordsReturn PlayWords(playGameInfo user, String GameID)
        {
            lock (sync)
            {
                int  number;
                bool result = Int32.TryParse(GameID, out number);

                if (user == null || user.Word == null || user.Word.Trim().Length == 0 || GameID == null ||
                    user.UserToken == null || !result || games.ContainsKey(GameID) == false)
                {
                    SetStatus(Forbidden);
                    return(null);
                }

                Game game = games[GameID];
                gameStatus(GameID, "");
                //when the GameState is not active
                if (game.GameState != "active")
                {
                    SetStatus(Conflict);
                    return(null);
                }
                if (game.player1 == null || game.player2 == null || (game.player1.UserToken != user.UserToken && game.player2.UserToken != user.UserToken))
                {
                    SetStatus(Forbidden);
                    return(null);
                }
                //UserToken in the game identified by GameID.
                else
                {
                    int scoreResult = 0;
                    if (game.boggleboard.CanBeFormed(user.Word) && File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory.ToString() + "dictionary.txt").Contains(user.Word))
                    {
                        scoreResult = CalculateScore(user.Word);
                    }
                    else
                    {
                        scoreResult--;
                    }
                    if (game.player1.UserToken == user.UserToken)
                    {
                        WordsPlayed word = new WordsPlayed();
                        word.Word  = user.Word;
                        word.Score = scoreResult;
                        helpAdd(game.player1, word);
                    }
                    else
                    {
                        WordsPlayed word = new WordsPlayed();
                        word.Word  = user.Word;
                        word.Score = scoreResult;
                        helpAdd(game.player2, word);
                    }


                    //user.Words.Add(user.Word);
                    playWordsReturn info = new playWordsReturn();
                    info.Score = scoreResult;
                    SetStatus(OK);
                    return(info);
                }
            }
        }