RESTful Scrabble

RESTfulScrabbleThe roughly 1.5 hour final exam period of senior year’s Networking for Online Games tasked us with creating a scrabble game that used the Pearson Longman Dictionary’s RESTful api to check the validity of the player’s words.  Given a random set or letters–a, b, and c are always present, as the service only let us check words beginning with those letters–players must make a word, which is then checked against the dictionary, and if the word exists, they are granted points based on the same rules as Scrabble.

Download the game and source file here.

Source Code:

</pre>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using System.Net;
using System.IO;

namespace ConsoleApplication1
{
 class Program
 {
 static void Main(string[] args)
 {
 Random rand = new Random();
 bool running = true;
 int letter;
 string userString = "";
 char[] userWord = new char[9];
 int totalPoints = 0;

while (CheckRunning(running, ref totalPoints))
 {
 //display instructions and pick 7 random letters
 Console.WriteLine("Create a word beginning with A, B, or C\nthat is at least 3 letters long");
 string letters = "a b c ";
 for (int i = 2; i < 9; ++i)
 {
 letter = rand.Next(100, 122);
 char c = Convert.ToChar(letter);
 letters += c + " ";
 }
 Console.WriteLine("These are your letters:\n" + letters + "\n\n");

//Get user input
 Console.WriteLine("Enter your word: ");
 userString = Console.ReadLine();
 userString = userString.ToLower();
 userWord = userString.ToCharArray();

//Check to make sure word is not too short
 if (userWord.Length < 3)
 {
 Console.WriteLine("\nWord is less than three letters long!");
 running = GameOver(totalPoints); //End the game, display total score
 continue;
 }
 //Check to make sure word only uses the random letters
 if (!CheckLetters(letters, userWord))
 {
 Console.WriteLine("\nWord contains a letter not given!");
 running = GameOver(totalPoints); //End the game, display total score
 continue;
 }

if (AskDictionary(userString))
 {
 //Get the points for each letter, based on Scrabble rules
 int tempPoints = 0;
 foreach (char c in userWord)
 {
 tempPoints += GetPoints(c);
 }
 //Add to player's total points, display the words points to the screen
 totalPoints += tempPoints;
 Console.WriteLine("\nThis is a word!\nYou get " + tempPoints + " points!\n\n");
 }
 else
 {
 Console.WriteLine("\nThis is not a word!");
 running = GameOver(totalPoints); //End the game, display total score
 continue;
 }
 }
 }

//Check the user's word against the random letters
 //Returns false if player uses a letter that is not a random letter
 static bool CheckLetters(string letters, char[] word)
 {
 foreach (char c in word)
 {
 if (!letters.Contains(c))
 {
 return false;
 }
 }

return true;
 }

//Determines whether the game should keep running
 //Allows players to restart game if they want, once they lose
 static bool CheckRunning(bool running, ref int points)
 {
 if (!running)
 {
 Console.WriteLine("\n\nPlay again? Y/N: ");
 string c = Console.ReadLine();

//Reset score, clear screen, keep playing the game
 if (c.Contains('y'))
 {
 points = 0;
 Console.Clear();
 running = true;
 }
 else
 running = false;
 }

return running;
 }

//Lookup the word in the longman dictionary to determine if it is an actual word
 static bool AskDictionary(string word)
 {
 //Make webrequest to Longman, get response
 string url = "http://api.pearson.com/longman/dictionary/entry.xml?q=" + word; //+ "dev key goes here";
 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
 try
 {
 HttpWebResponse re = (HttpWebResponse)request.GetResponse();
 }
 catch (Exception e)
 {
 Console.WriteLine(e.Message);
 Console.WriteLine("Unable to connect to the Longman dictionary!\nAre you connected to the internet?");
 return false;
 }
 HttpWebResponse response = (HttpWebResponse)request.GetResponse();

//Convert response to string
 Stream responseStream = response.GetResponseStream();
 StreamReader reader = new StreamReader(responseStream);
 string responseString = reader.ReadToEnd();
 reader.Close();
 responseStream.Close();
 response.Close();

//Convert string to XML reader
 StringBuilder outputString = new StringBuilder();
 using (XmlReader xmlReader = XmlReader.Create(new StringReader(responseString)))
 {
 //If the word exists, get its definition and write it to the screen, otherwise return false
 if (xmlReader.ReadToFollowing("Head"))
 {
 if (xmlReader.ReadToFollowing("DEF"))
 {
 outputString.AppendLine(xmlReader.ReadElementContentAsString());
 Console.WriteLine("The definition of " + word + " is:\n" + outputString);
 }
 return true;
 }
 }

return false;
 }

//Get the point value for a letter, based on Scrabble rules
 static int GetPoints(char c)
 {
 int points = 0;

if (c == 'e' || c == 'a' || c == 'i' || c == 'o' || c == 'n' || c == 'r' || c == 't' || c == 't' || c == 'l' || c == 's' || c == 'u')
 points = 1;
 else if (c == 'd' || c == 'g')
 points = 2;
 else if (c == 'b' || c == 'c' || c == 'm' || c == 'p')
 points = 3;
 else if (c == 'f' || c == 'h' || c == 'v' || c == 'w' || c == 'y')
 points = 4;
 else if (c == 'k')
 points = 5;
 else if (c == 'j' || c == 'x')
 points = 8;
 else if (c == 'q' || c == 'z')
 points = 10;

return points;
 }

//Prepare to end game, display total score to user
 static bool GameOver(int points)
 {
 Console.WriteLine("\n\nGame Over!\nYour total score was: " + points + "\nPress 'Enter' to continue");
 Console.ReadLine();

return false;
 }
 }
}
<pre>