amarek

OOP LV6 - Analiza 2

Jan 8th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | None | 0 0
  1. /* 2. Napravite jednostavnu igru vješala. Pojmovi se učitavaju u listu iz datoteke, i u
  2.    svakoj partiji se odabire nasumični pojam iz liste. Omogućiti svu
  3.    funkcionalnost koju biste očekivali od takve igre. Nije nužno crtati vješala,
  4.    dovoljno je na labeli ispisati koliko je pokušaja za odabir slova preostalo.*/
  5.  
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15.  
  16. namespace LV6_2
  17. {
  18.     public partial class form_vjesala : Form
  19.     {
  20.         public form_vjesala()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         int pogodak = 0;
  26.         int pokusaji = 10;
  27.         string rijec;
  28.         static Random random = new Random();
  29.  
  30.         List<Label> labels = new List<Label>();
  31.         List<string> rijeci = new List<string>();
  32.         string path = "C:\\Users\\Ana\\Documents\\Visual Studio 2017\\Projects\\LV6\\LV6 Zad2\\rijeci.txt";
  33.  
  34.         private void Form1_Load(object sender, EventArgs e)
  35.         {
  36.             using (System.IO.StreamReader reader = new System.IO.StreamReader(@path))
  37.             {
  38.                 string line;
  39.                 while ((line = reader.ReadLine()) != null)
  40.                 {
  41.                     rijeci.Add(line);
  42.                 }
  43.             }
  44.  
  45.             int r = random.Next(rijeci.Count);
  46.             rijec = (string)rijeci[r];
  47.  
  48.         private void button_pokusaj_Click(object sender, EventArgs e)
  49.         {
  50.             if (textBox_pokusaj.Text.Length == 0 || textBox_pokusaj.Text.Length > 1)
  51.             {
  52.                 textBox_pokusaj.Text = "";
  53.                 MessageBox.Show("Greška!");
  54.             }
  55.             else
  56.             {
  57.                 if (rijec.Contains(textBox_pokusaj.Text))
  58.                 {
  59.                     for (int i = 0; i < rijec.Length; i++)
  60.                     {
  61.                         if (rijec.IndexOf(textBox_pokusaj.Text) == rijec.IndexOf(rijec[i]))
  62.                         {
  63.                             if (labels[i].Text == "_")
  64.                             {
  65.                                 labels[i].Text = textBox_pokusaj.Text;
  66.                                 pogodak++;
  67.                             }
  68.                             else
  69.                             {
  70.                                 MessageBox.Show("Slovo već postoji!");
  71.                                 break;
  72.                             }
  73.                         }
  74.                     }
  75.                     textBox_pokusaj.Text = "";
  76.                     if (pogodak == rijec.Length)
  77.                     {
  78.                         MessageBox.Show("Pobjeda!");
  79.                         Application.Exit();
  80.                     }
  81.                 }
  82.                 else
  83.                 {
  84.                     if (pokusaji == 1)
  85.                     {
  86.                         textBox_pokusaj.Text = "";
  87.                         pokusaji--;
  88.                         label_pokusaji.Text = "Preostalo pokusaja: " + pokusaji;
  89.                         MessageBox.Show("Izgubio/la si.");
  90.                         Application.Exit();
  91.                     }
  92.                     else
  93.                     {
  94.                         textBox_pokusaj.Text = "";
  95.                         pokusaji--;
  96.                         label_pokusaji.Text = "Preostali pokusaji: " + pokusaji;
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.     }
  102. }
Add Comment
Please, Sign In to add comment