Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Linq;
- using System.Media;
- using System.Windows.Forms;
- using GuillotineGameProject.Properties;
- namespace GuillotineGameProject
- {
- public partial class Game : Form
- {
- private Reader reader;
- 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)
- {
- reader = new Reader(fileLocation);
- reader.Read();
- }
- private void SelectRandomWord()
- {
- random = new Random();
- string[] words = reader.lines;
- word = words[random.Next(words.Length)];
- }
- private void letter_Click(object sender, EventArgs e)
- {
- if (wordRevealed)
- return;
- Button button = (Button)sender;
- button.Enabled = false;
- UpdateButtonBackground(button);
- char letter = char.Parse(button.Name.Substring(6).ToLower());
- GuessLetter(letter);
- CheckIfWonOrLost();
- }
- private void UpdateButtonBackground(Button button)
- {
- Image image = LoadImage(button.Name.Substring(6) + "_HOV");
- button.BackgroundImage = image;
- }
- 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);
- }
- }
- }
- }
- private void CheckIfWonOrLost()
- {
- if (amountOfLettersRevealed == word.Length)
- {
- wordRevealed = true;
- GameWon();
- }
- else if (wrongGuesses == MAX_WRONG_GUESSES)
- {
- GameOver();
- }
- }
- private void RevealLetter(int index, char letter)
- {
- amountOfLettersRevealed++;
- }
- private void GameWon()
- {
- PlaySound("youwin");
- }
- private void GameOver()
- {
- PlaySound("gameover");
- }
- private void Reset()
- {
- }
- private Image LoadImage(string location)
- {
- return (Image)Resources.ResourceManager.GetObject(location);
- }
- private void PlaySound(string sound)
- {
- SoundPlayer sp = new SoundPlayer(@"C:\Users\99DIAGHA\Desktop\Guillotine\Sound effects\" + sound + ".wav");
- sp.Play();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement