I've created a simple Hangman game (not finished yet).
What could I improve? (This isn't intended to be object-oriented but suggestions on that are also welcome.)
What are its flaws?
Here's the code:
public partial class Game : Form
{
private ReadFromFile readFromFile;
private Random random;
private string word;
private int wrongGuesses;
private int amountOfLettersRevealed;
private bool wordRevealed;
private const int MAX_WRONG_GUESSES = 9;
public Game()
{
InitializeComponent();
}
private void Game_Load(object sender, EventArgs e)
{
loadWords("words");
selectRandomWord();
}
private void loadWords(string fileLocation)
{
readFromFile = new ReadFromFile(fileLocation);
readFromFile.read();
}
private void selectRandomWord()
{
random = new Random();
string[] words = readFromFile.lines;
word = words[random.Next(words.Length)];
}
private void letter_Click(object sender, EventArgs e)
{
if (wordRevealed)
return;
Button button = (Button)sender;
char letter = char.Parse(button.Name.Substring(6).ToLower());
guessLetter(letter);
buttonClicked(button);
}
private void guessLetter(char letter)
{
if (!word.Contains(letter))
{
// wrong guess
wrongGuesses++;
}
else
{
// right guess
for (int index = 0; index < word.Length; index++)
{
if (letter == word[index])
{
revealLetter(index, letter);
amountOfLettersRevealed++;
}
}
}
if (amountOfLettersRevealed == word.Length)
{
wordRevealed = true;
gameWon();
}
if (wrongGuesses == MAX_WRONG_GUESSES)
{
gameOver();
}
}
private void buttonClicked(Button button)
{
button.Enabled = false;
Image image = loadImage(button.Name.Substring(6) + "_HOV");
button.BackgroundImage = image;
}
private void revealLetter(int index, char letter)
{
}
private void gameWon()
{
playSound("youwin");
}
private void gameOver()
{
playSound("gameover");
}
private Image loadImage(string location)
{
object o = Resources.ResourceManager.GetObject(location);
return (Image) o;
}
private void playSound(string sound)
{
SoundPlayer sp = new SoundPlayer(sound + ".wav");
sp.Play();
}
}