Advertisement
Guest User

Hangman V2

a guest
Feb 10th, 2022
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. /*
  2.  * Created by SharpDevelop.
  3.  * User: Admin
  4.  * Date: 8.2.2022 г.
  5.  * Time: 8:11
  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. using System.Linq;
  15.  
  16. namespace besene_12a
  17. {
  18.     /// <summary>
  19.     /// Description of MainForm.
  20.     /// </summary>
  21.     public partial class MainForm : Form
  22.     {
  23.         string[] words;
  24.         string pcWord = "";
  25.         List<string> activeWord = new List<string>();
  26.         List<string> wrongLetters = new List<string>();
  27.         string[] pictures = {"hang1.png", "hang2.png",
  28.             "hang3.png", "hang4.png","hang5.png","hang6.png",
  29.             "hang7.png","hang8.png","hang9.png", "hang10.png"};
  30.         int index = 0;
  31.        
  32.         public MainForm()
  33.         {
  34.             //
  35.             // The InitializeComponent() call is required for Windows Forms designer support.
  36.             //
  37.             InitializeComponent();
  38.            
  39.             //
  40.             // TODO: Add constructor code after the InitializeComponent() call.
  41.             //
  42.         }
  43.         void MainFormLoad(object sender, EventArgs e)
  44.         {
  45.             words = File.ReadAllLines("words.txt");
  46.                    
  47.         }
  48.         void ButtonNewGameClick(object sender, EventArgs e)
  49.         {
  50.             labelLetter.Visible = true;
  51.             textBoxLetter.Visible = true;
  52.             buttonCheck.Visible = true;
  53.             labelResult.Visible = false;
  54.             labelWord.ForeColor = Color.Black;
  55.            
  56.             wrongLetters.Clear();          
  57.             activeWord.Clear();
  58.             index = 0;
  59.            
  60.             Random r = new Random();
  61.            
  62.             int wordIndex = r.Next(0, words.Length);
  63.            
  64.             pcWord = words[wordIndex];
  65.            
  66.             for (int i = 0; i < pcWord.Length; i++)
  67.             {
  68.                 activeWord.Add("_");
  69.             }
  70.            
  71.             labelWord.Text = string.Join("  ", activeWord);
  72.            
  73.         }
  74.        
  75.        
  76.         void ButtonCheckClick(object sender, EventArgs e)
  77.         {
  78.             string userLetter = textBoxLetter.Text.ToLower();
  79.            
  80.             if (userLetter.Length != 1)
  81.             {
  82.                 MessageBox.Show("Въведи буква");
  83.                 return;
  84.             }
  85.            
  86.             if (char.Parse(userLetter) < 'а'
  87.                 ||  char.Parse(userLetter) > 'я')
  88.             {
  89.                 MessageBox.Show("Въведи буква между 'а' и 'я'");
  90.                 return;
  91.             }
  92.            
  93.             textBoxLetter.Text = "";
  94.                        
  95.             bool isWrong = true;
  96.            
  97.             for (int i = 0; i < pcWord.Length; i++)
  98.             {
  99.                 if (char.Parse(userLetter) == pcWord[i])
  100.                 {
  101.                     isWrong = false;
  102.                     activeWord[i] = userLetter;
  103.                 }
  104.             }
  105.            
  106.             labelWord.Text = string.Join("  ", activeWord);
  107.            
  108.             if (!activeWord.Any(x => x == "_"))
  109.             {
  110.                 labelLetter.Visible = false;
  111.                 textBoxLetter.Visible = false;
  112.                 buttonCheck.Visible = false;
  113.                 labelWord.ForeColor = Color.Green;
  114.                
  115.                 labelWord.Text = string.Join("  ", activeWord);
  116.                
  117.                 labelResult.Visible = true;
  118.                 labelResult.ForeColor = Color.Green;
  119.                 labelResult.Text = "ПЕЧЕЛИШ !!!";
  120.             }
  121.            
  122.            
  123.             if (isWrong)
  124.             {
  125.                 if (!wrongLetters.Contains(userLetter))
  126.                 {
  127.                     wrongLetters.Add(userLetter);
  128.                     labelWrongLetters.Text = string.Join(", ", wrongLetters);
  129.                     pictureBox1.Image = Image.FromFile(pictures[index]);
  130.                     index++;
  131.                    
  132.                     if (index >= pictures.Length)
  133.                     {
  134.                         labelLetter.Visible = false;
  135.                         textBoxLetter.Visible = false;
  136.                         buttonCheck.Visible = false;
  137.                         labelWord.ForeColor = Color.Red;
  138.                        
  139.                         var word = pcWord.ToCharArray();
  140.                         labelWord.Text = string.Join("  ", word);
  141.                        
  142.                         labelResult.Visible = true;
  143.                         labelResult.ForeColor = Color.Red;
  144.                         labelResult.Text = "ГУБИШ !!!";
  145.                     }
  146.                 }
  147.             }
  148.            
  149.         }
  150.     }
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement