C# Hangman in Silverlight

About two weeks ago my professor gave us a new assignment. We were supposed to create a hangman game using Silverlight. I hesitated to do it at first. Not because of the game logic, but because I am no good at artistic assignments, allow me to explain. 

I hate art, but some of my friends are really good at it. They can create visually stunning webpages, or edit an image in Photoshop. I can’t do any of that. I guess it’s because I am simplistic my nature. Anyways, during the period we were supposed to show off our assignments, the professor wasn’t too please, simply because I didn’t use the rotations and transformations the way he wanted. However, we have a slight problem. In the assignment outline it reads:
The interface should utilize various Silverlight shapes to implement an animation for the game. Use different types of transforms (translation, rotation, etc) to implement the animation. Use different shapes for this animation.”  

While watching my friends demoing their assignments, I noticed most of them had the hanging man swinging while it was being built. With this in mind I created my game, and had the title rotating when the user won the game. At first he questioned if it was text or a picture. A quick view of my solution explorer showed him it was a picture file I was rotating. I could tell that he wasn’t pleased, because I asked him if he wanted to see my code, and he said he saw all he wanted to see. He usually checks code and questions us to make sure we actually wrote it. I was very excited to show him that I did in fact write the code, but he didn’t check, he simply moved on. With all that being said, I will explain my code here. 
namespace TestApp { public class HangMan { public string Status { get; private set; } public int wordLength { get; private set; } public Word word { get; private set; } private List<string> missedLetters; public HangMan(string status) { this.Status = status; this.word = WordManger.retriveWord(); this.wordLength = word.strWord.Length; missedLetters = new List<string>(); } public void add(string add) { missedLetters.Add(add); } public ICollection<string> Letters { get { return missedLetters; } } public void checkStatus() { int score = missedLetters.Count / wordLength; if (score > 10 && score < 20) { Status = "Damaged"; } } } }

When designing this class, I thought back to XNA, and how it structured its’ game class. It had its constructor, initialize, loadContent, update and draw methods. I aimed to implement that same design in my Hangman class, I wanted hangman to control everything. That, my dear readers, didn’t turn out so well. My next objective was to create a static class to hold all my strings. With that thought, WordManager.cs was born.   


namespace TestApp { public static class WordManger { private static List<Word> wordCol; private static Random randNum; static int number; static WordManger() { wordCol = new List<Word>(); wordCol.Add(new Word("hangman","Type of Game")); wordCol.Add(new Word("linux","Type of Operating System")); wordCol.Add(new Word("office","A Place of Work")); randNum = new Random(); } public static Word retriveWord() { number = randNum.Next(0,wordCol.Count); return wordCol[number]; } } }

A test of my game showed me that the class was flawed. Although it had strings, it would help the player if I had hints. I then created a word class to have 2 strings, one with the word and one with the hint. It then was passed to the Hangman class which then split it up and passed it to the browser. Next came the textboxes, which I called Letterholders, so the letters could actually be displayed to the screen.


using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes; namespace TestApp { public class LetterHolder { private Grid layout; private int numberofBoxes; private double marginX = 227; private double marginY = 170; private int height = 23, width = 27; private TextBox[] textArr; public LetterHolder(Grid layout, int numberofBoxes) { this.layout = layout; this.numberofBoxes = numberofBoxes; textArr = new TextBox[numberofBoxes]; } public void createHolder() { for (int i = 0; i < textArr.Length; i++) { textArr[i] = new TextBox(); textArr[i].Height = height; textArr[i].HorizontalAlignment = HorizontalAlignment.Left; textArr[i].Margin = new Thickness(marginX,marginY,0,0); textArr[i].VerticalAlignment = VerticalAlignment.Top; textArr[i].Width = width; textArr[i].TextAlignment = TextAlignment.Center; textArr[i].Foreground = new SolidColorBrush(Color.FromArgb(255,0,165,0)); textArr[i].Background = new SolidColorBrush(Color.FromArgb(255,10,10,10)); placeHolder(textArr[i]); marginX += 33; } } public TextBox[] TextBoxArr { get { return textArr; } } private void placeHolder(Control add) { layout.Children.Add(add); } } }

I thought of making this class an inner class of hangman. The reason being, the user wasn’t setting the word, it was the Hangman class. It would take the length of the word, then pass it as a parameter to the LetterHolder class. If I created the hangman class the way I wanted to, I could have had the loadContent method and call that instead. My main page is a little messy. You have the hangman and letterholder classes being declared. To me that could have been avoided.

Me not pleasing my professor didn’t sit well for me. I felt a a little sad, and somewhat embarrassed. Just that morning, I talked to my friends how he changed his attitude and overall teaching style. This new professor, was not the old professor I know. He was a little more easy going, and a lot more friendly. With that being said, his instructions need to be made clearer. If he wanted the animations to be part of t he game, then he should have said so. From the quotation above, he makes it sound that any type of animation, as long as its on the screen, would have been fine. “Animation for the game”  doesn’t count. To me that says as long as its on the screen while playing the game. For example, if he said, I want the rotations and transforms for the letters, then that’s specific, and I get it. The best I can do now is improve it on my own and leave it on my portable hard drive for future use.