Advertisement
Diar99

Untitled

Mar 18th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.Media;
  5. using System.Windows.Forms;
  6. using GuillotineGameProject.Properties;
  7.  
  8. namespace GuillotineGameProject
  9. {
  10.     public partial class Game : Form
  11.     {
  12.         private Reader reader;
  13.         private Random random;
  14.         private string word;
  15.         private int wrongGuesses;
  16.         private int amountOfLettersRevealed;
  17.         private bool wordRevealed;
  18.         private const int MAX_WRONG_GUESSES = 9;
  19.  
  20.         public Game()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         private void Game_Load(object sender, EventArgs e)
  26.         {
  27.             LoadWords("words");
  28.             SelectRandomWord();
  29.         }
  30.  
  31.         private void LoadWords(string fileLocation)
  32.         {
  33.             reader = new Reader(fileLocation);
  34.             reader.Read();
  35.         }
  36.  
  37.         private void SelectRandomWord()
  38.         {
  39.             random = new Random();
  40.  
  41.             string[] words = reader.lines;
  42.             word = words[random.Next(words.Length)];
  43.         }
  44.  
  45.         private void letter_Click(object sender, EventArgs e)
  46.         {
  47.             if (wordRevealed)
  48.                 return;
  49.  
  50.             Button button = (Button)sender;
  51.  
  52.             button.Enabled = false;
  53.             UpdateButtonBackground(button);
  54.  
  55.             char letter = char.Parse(button.Name.Substring(6).ToLower());
  56.  
  57.             GuessLetter(letter);
  58.             CheckIfWonOrLost();
  59.         }
  60.  
  61.         private void UpdateButtonBackground(Button button)
  62.         {
  63.             Image image = LoadImage(button.Name.Substring(6) + "_HOV");
  64.             button.BackgroundImage = image;
  65.         }
  66.  
  67.         private void GuessLetter(char letter)
  68.         {
  69.             if (!word.Contains(letter))
  70.             {
  71.                 // wrong guess
  72.                 wrongGuesses++;
  73.             }
  74.             else
  75.             {
  76.                 // right guess
  77.                 for (int index = 0; index < word.Length; index++)
  78.                 {
  79.                     if (letter == word[index])
  80.                     {
  81.                         RevealLetter(index, letter);
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.  
  87.         private void CheckIfWonOrLost()
  88.         {
  89.             if (amountOfLettersRevealed == word.Length)
  90.             {
  91.                 wordRevealed = true;
  92.                 GameWon();
  93.             }
  94.             else if (wrongGuesses == MAX_WRONG_GUESSES)
  95.             {
  96.                 GameOver();
  97.             }
  98.         }
  99.  
  100.         private void RevealLetter(int index, char letter)
  101.         {
  102.             amountOfLettersRevealed++;
  103.         }
  104.  
  105.         private void GameWon()
  106.         {
  107.             PlaySound("youwin");
  108.         }
  109.  
  110.         private void GameOver()
  111.         {        
  112.             PlaySound("gameover");
  113.         }
  114.  
  115.         private void Reset()
  116.         {
  117.            
  118.         }
  119.  
  120.         private Image LoadImage(string location)
  121.         {
  122.             return (Image)Resources.ResourceManager.GetObject(location);
  123.         }
  124.  
  125.         private void PlaySound(string sound)
  126.         {
  127.             SoundPlayer sp = new SoundPlayer(@"C:\Users\99DIAGHA\Desktop\Guillotine\Sound effects\" + sound + ".wav");
  128.            sp.Play();
  129.        }
  130.    }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement