/// <summary> /// Starts a new boggle game once a second player has joined and the first player has not left. /// </summary> /// <param name="two">The second player.</param> /// <param name="time">The number of seconds the game will last.</param> /// <param name="letters">A string of 16 letters used for the boggle board. If left empty, a random board is created.</param> /// <param name="dictionary">A reference to the set of legal words.</param> public void startGame(Player two, int time, string letters, ref HashSet<string> dictionary) { gameIsRunning = true; timeLimit = time; this.player2 = two; // Create the board. if (letters == "") board = new BoggleBoard(); else board = new BoggleBoard(letters); this.dictionary = dictionary; commonWords = new HashSet<string>(); // Send START messages to the clients, and begin listening for words. player1.ss.BeginSend("START " + board.ToString() + " " + time.ToString() + " " + player2.name + "\n", (ee, pp) => { }, player1.ss); player2.ss.BeginSend("START " + board.ToString() + " " + time.ToString() + " " + player1.name + "\n", (ee, pp) => { }, player2.ss); player2.ss.BeginReceive(WordReceived, player2); // Start the game timer. Thread timer = new Thread(updateClock); timer.Start(time); }
/// <summary> /// Creates and returns the game summary string. /// </summary> /// <param name="currentPlayer"></param> /// <param name="otherPlayer"></param> /// <returns></returns> private string getSummaryString(Player currentPlayer, Player otherPlayer) { StringBuilder summary = new StringBuilder(); summary.Append("STOP"); // The current player's legal words: summary.Append(" " + currentPlayer.legalWords.Count); foreach (string word in currentPlayer.legalWords) summary.Append(" " + word); // The opponent's legal words: summary.Append(" " + otherPlayer.legalWords.Count); foreach (string word in otherPlayer.legalWords) summary.Append(" " + word); // The common words: summary.Append(" " + commonWords.Count); foreach (string word in commonWords) summary.Append(" " + word); // The current player's illegal words: summary.Append(" " + currentPlayer.illegalWords.Count); foreach (string word in currentPlayer.illegalWords) summary.Append(" " + word); // The opponent's illegal words: summary.Append(" " + otherPlayer.illegalWords.Count); foreach (string word in otherPlayer.illegalWords) summary.Append(" " + word); return summary.ToString(); }
/// <summary> /// Constructs a new boggle game with just one player. Waits for second player but also keeps track if the /// waiting player exits to alert the server that this game is no longer valid. /// </summary> public BoggleGame(Player one) { this.player1 = one; player1.ss.BeginReceive(WordReceived, player1); }
/// <summary> /// Processes the played word. If it's legal, update the scores. /// Then send the SCORE messages. /// </summary> private void processWord(string word, Player currentPlayer, Player otherPlayer) { lock (scoreLock) { if (word.Length < 3 || (!word.All(Char.IsLetter))) return; // Check if the word is legal. if (isLegalWord(word)) { // Check if the other player has already played the word. if (otherPlayer.legalWords.Contains(word)) { // Remove the word otherPlayer.legalWords.Remove(word); // Add it to common words commonWords.Add(word); // Reduce the score otherPlayer.score -= getWordScore(word); } // Otherwise, the other player hasn't played the word. else { // Make sure current player hasn't already played the word, and that it's not a common word. if (!currentPlayer.legalWords.Contains(word) && !commonWords.Contains(word)) { // Add it to their list of legal words. currentPlayer.legalWords.Add(word); // Increment their score. currentPlayer.score += getWordScore(word); } else return; } } // Otherwise, the word is illegal. else { // Make sure the player hasn't already played the word. if (!currentPlayer.illegalWords.Contains(word)) { // Add it to the list of illegal words. currentPlayer.illegalWords.Add(word); // Decrement the score. currentPlayer.score += -1; } else return; } // Send the scores. currentPlayer.ss.BeginSend("SCORE " + currentPlayer.score + " " + otherPlayer.score + "\n", (ee, pp) => { }, currentPlayer); otherPlayer.ss.BeginSend("SCORE " + otherPlayer.score + " " + currentPlayer.score + "\n", (ee, pp) => { }, otherPlayer); } }