Advertisement
Guest User

Hangman V1

a guest
Feb 8th, 2022
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. /*
  2.  * Created by SharpDevelop.
  3.  * User: Admin
  4.  * Date: 21.1.2022 г.
  5.  * Time: 9:15
  6.  *
  7.  * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8.  */
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Drawing;
  12. using System.Windows.Forms;
  13. using System.IO;
  14.  
  15. namespace besenica
  16. {
  17.     /// <summary>
  18.     /// Description of MainForm.
  19.     /// </summary>
  20.     public partial class MainForm : Form
  21.     {
  22.         public string[] words;
  23.         public string pcWord = "";
  24.         List<string> activeWord = new List<string>();
  25.         List<string> wrongLetters = new List<string>();
  26.         string[] pictures = {"hang1.png", "hang2.png",
  27.             "hang3.png", "hang4.png", "hang5.png", "hang6.png",
  28.             "hang7.png", "hang8.png", "hang9.png", "hang10.png"};
  29.         int pictureIndex = 0;
  30.        
  31.         public MainForm()
  32.         {
  33.             //
  34.             // The InitializeComponent() call is required for Windows Forms designer support.
  35.             //
  36.             InitializeComponent();
  37.            
  38.             //
  39.             // TODO: Add constructor code after the InitializeComponent() call.
  40.             //
  41.         }
  42.         void MainFormLoad(object sender, EventArgs e)
  43.         {
  44.             words = File.ReadAllLines("words.txt");
  45.            
  46.         }
  47.         void ButtonNewGameClick(object sender, EventArgs e)
  48.         {
  49.             activeWord.Clear();
  50.             wrongLetters.Clear();
  51.             labelWrongLetters.Text = "";
  52.             pictureBox1.Image = null;
  53.             pictureIndex = 0;
  54.             textBoxLetter.Enabled = true;
  55.             buttonCheck.Enabled = true;
  56.            
  57.             Random r = new Random();
  58.             int index = r.Next(0, words.Length);
  59.             pcWord = words[index];
  60.            
  61.             for (int i = 0; i < pcWord.Length; i++)
  62.             {
  63.                 activeWord.Add(" _ ");
  64.             }
  65.            
  66.             labelWord.Text = string.Join("", activeWord);
  67.         }
  68.        
  69.         void ButtonCheckClick(object sender, EventArgs e)
  70.         {
  71.             string letter = textBoxLetter.Text.ToLower();
  72.            
  73.             if (letter.Length != 1)
  74.             {
  75.                 MessageBox.Show("Въведи буква");
  76.                 textBoxLetter.Text = "";
  77.                 return;
  78.             }
  79.            
  80.             if (char.Parse(letter) < 'а'|| char.Parse(letter) > 'я')
  81.             {
  82.                 MessageBox.Show("Буквата трябва да е между А и Я");
  83.                 textBoxLetter.Text = "";
  84.                 return;
  85.             }
  86.            
  87.             textBoxLetter.Text = "";
  88.            
  89.             bool isPresent = false;
  90.            
  91.             for (int i = 0; i < pcWord.Length; i++)
  92.             {
  93.                 if (char.Parse(letter) == pcWord[i])
  94.                 {
  95.                     activeWord[i] = " " + letter + " ";
  96.                     isPresent = true;
  97.                 }
  98.             }
  99.            
  100.             labelWord.Text = string.Join("", activeWord);
  101.            
  102.             if (!isPresent)
  103.             {
  104.                 if (!wrongLetters.Contains(letter))
  105.                 {
  106.                     wrongLetters.Add(letter);
  107.                     labelWrongLetters.Text = string.Join(", ", wrongLetters);      
  108.                     pictureBox1.Image = Image.FromFile(pictures[pictureIndex]);
  109.                     pictureIndex++;
  110.                     if (pictureIndex >= 10)
  111.                     {
  112.                         textBoxLetter.Enabled = false;
  113.                         buttonCheck.Enabled = false;
  114.                         MessageBox.Show("Губите");
  115.                         return;
  116.                     }
  117.                 }
  118.             }
  119.         }
  120.     }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement