Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

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. 

Hello World

I just created this blog! I know it sounds that I am a little late to the blogging world. However, this isn’t my first time blogging. I’ve had other blogs before, but it didn’t work it. I started this blog to share my thoughts, musings, and to clear my head. I’m writing this blog from the room I grew up in. It’s full of my programming books and a bed. I have my old portable hard drive laying around. 250GB doesn’t seem like a lot, but at the time I bought it, it was. 

What can I really say about myself? I am a programmer, my favorite languages are C#,Java, and PHP. I’m also a linux user. I currently use xubuntu on a virtual machine, but I’m hoping to purchase a new laptop and have linux as the only operating system. I used to be a gamer but I my PC became older and I don’t own any recent console. I still follow gaming websites, and listen to the podcasts but I don’t play games anymore. 

I should really come up with a posting schedule. I’m not sure how I want to do this. Maybe update 2 times a week. One segment that I want to really do, is Trance of Friday Nights. I want to share 5 trance songs that I found. I also want to have some post about programming and Linux. Who Knows? I’ll work this out, and come up with a schedule that I'm comfortable with.  

It’s currently a beautiful day outside. Nice day to go for a walk. Currently I am in my second to last semester in college. I’m taking software engineering, and I had a blast doing it. I met some new friends, lost some on the way(blog post for another day). Soon it will be time to look for a job. 

That seems like it for me. I know it isn’t much about me, but I’m a simple person, friendly,kind and helpful.  

Feel free to introduce yourself in the comments.