Beispiel #1
0
        //Ја валидира буквата дали постои во зборот и извришува дополнителни функции во зависност од тоа дали е валидна буквата или не
        private void ValidateLetter(char letter)
        {
            bool letterGuessIsCorrect = false;

            //Ибриши ги празните места од зборот.. за да дојде во форма како: u_i_e_s_ty
            string word = new string(WordToGuessLabel.Text.Where(c => !Char.IsWhiteSpace(c)).ToArray()).ToLower();

            for (int i = 0; i < Word.Name.Length; i++)
            {
                //Ако буквата кликната ја има во зборот, избриши го "_" цртичето и стави ја буквата.
                if (Word.Name[i] == letter)
                {
                    letterGuessIsCorrect = true;
                    word = word.Remove(i, 1);
                    word = word.Insert(i, letter.ToString());
                }
            }

            if (Word.Name == word)
            {
                Game.Player.HighScore += 1;
                Words.Remove(Word);
                Timer.Stop();
                UpdateDisplayWord(word);
                MessageBox.Show(LanguageSettings.WordGuessCorrect, LanguageSettings.WellDoneMessage);
                GenerateDisplayWord();
                return;
            }

            if (letterGuessIsCorrect == false)
            {
                ++Mistakes;
                string letters = letter + ", ";
                WrongoLettersLabel.Text += letters.ToUpper();
                switch (Mistakes)
                {
                case 1:
                    hangmanHead.Visible = true;
                    break;

                case 2:
                    hangmanBody.Visible = true;
                    break;

                case 3:
                    hangmanLeftArm.Visible = true;
                    break;

                case 4:
                    hangmanRightArm.Visible = true;
                    break;

                case 5:
                    hangmanLeftLeg.Visible = true;
                    break;

                case 6:
                    hangmanRightLeg.Visible = true;
                    break;

                default:
                    break;
                }

                if (Mistakes >= 6)
                {
                    Timer.Stop();
                    MessageBox.Show($"{LanguageSettings.WordGuessIncorrect}{Word.Name.ToUpper()}", LanguageSettings.YouGotHangedMessage);
                    SaveHighscore();
                    GenerateDisplayWord();
                    return;
                }
            }
            else
            {
                UpdateDisplayWord(word);
            }
        }
        public static void AddWords()
        {
            Console.Clear();

            OpenFileDialog ofd = new OpenFileDialog();

            string wordsPath = "";
            var    exited    = false;
            var    t         = new Thread((ThreadStart)(() =>
            {
                OpenFileDialog fbd = new OpenFileDialog();
                ofd.Filter = "TXT|*.txt";

                if (ofd.ShowDialog() == DialogResult.Cancel)
                {
                    exited = true;
                }
                else
                {
                    wordsPath = ofd.FileName;
                }
            }));

            t.SetApartmentState(ApartmentState.STA);
            t.Start();
            t.Join();

            if (!exited)
            {
                if (!File.Exists(wordsPath))
                {
                    File.Create(wordsPath);
                }

                int level = 0;
                var words = File.ReadAllLines(wordsPath).Where(w => w != "").ToList();

                var wordsToAdd = new List <Words>();

                //determine level of hardness for each word
                foreach (var word in words)
                {
                    if (word.Length <= 5)
                    {
                        level = 1;
                    }
                    else if (word.Length > 5 && word.Length <= 10)
                    {
                        level = 2;
                    }
                    else if (word.Length > 10 && word.Length <= 15)
                    {
                        level = 3;
                    }
                    else if (word.Length > 15 && word.Length <= 20)
                    {
                        level = 4;
                    }
                    else if (word.Length > 20)
                    {
                        level = 5;
                    }

                    var wordToAdd = new Words()
                    {
                        Name  = word,
                        Level = level
                    };

                    var isInDB = dbContext.Words.Any(x => x.Name.ToLower() == word);

                    if (isInDB)
                    {
                        Console.Write(word, Color.LightPink);
                        Console.Write(" is already available\n", Color.Red);
                    }
                    else
                    {
                        wordsToAdd.Add(wordToAdd);
                    }
                }

                dbContext.Words.AddOrUpdate(wordsToAdd.ToArray());
                try
                {
                    dbContext.SaveChanges();

                    foreach (var word in wordsToAdd)
                    {
                        Console.Write(word.Name, Color.LightPink);
                        Console.Write(" added successfully\n", Color.LimeGreen);
                    }

                    Thread.Sleep(1500);
                }
                catch (Exception e)
                {
                    System.Console.WriteLine(e);
                    throw;
                }
            }

            Menu.Initialize();
        }
Beispiel #3
0
        //Генерира random збор од листата од зборови, пушта тајмер за зборот
        private void GenerateDisplayWord()
        {
            Timer.Stop();
            TimeElapsed = 0;
            Timer.Start();

            if (Words.Count != 0)
            {
                int index = new Random().Next(0, Words.Count);
                Word = Words.ElementAt(index);
            }
            else
            {
                if (Game.Categories.Count >= 5)
                {
                    MessageBox.Show($"{LanguageSettings.YouWonTheGameMessage}{Game.Difficulty}!", LanguageSettings.CongratulationsMessage);
                }
                else
                {
                    string categories = string.Empty;
                    foreach (var category in Game.Categories)
                    {
                        categories += category.ToString() + " ";
                    }

                    DialogResult result = MessageBox.Show(LanguageSettings.CategoriesFinishedMessage, LanguageSettings.WellDoneMessage, MessageBoxButtons.YesNo);

                    if (result == DialogResult.Yes)
                    {
                        LoadWords(true);
                        GenerateDisplayWord();
                    }
                    else
                    {
                        NewGameForm newGameForm = new NewGameForm();
                        newGameForm.Show();
                        this.Close();
                    }
                }
            }

            string ctg = string.Empty;

            if (Word.Category == Category.General)
            {
                ctg = LanguageSettings.CategoriesGeneral;
            }
            else if (Word.Category == Category.Sport)
            {
                ctg = LanguageSettings.CategoriesSport;
            }
            else if (Word.Category == Category.Science)
            {
                ctg = LanguageSettings.CategoriesScience;
            }
            else if (Word.Category == Category.Movies)
            {
                ctg = LanguageSettings.CategoriesMovies;
            }
            else
            {
                ctg = LanguageSettings.CategoriesGeography;
            }

            CategoryLabel.Text = $"{LanguageSettings.CategoryLabel}: {ctg}";

            Mistakes = 0;
            WrongoLettersLabel.Text = string.Empty;

            var word = Word.Name.ToUpper()[0].ToString();

            for (var i = 0; i < Word.Name.Length - 1; i++)
            {
                word += " _";
            }
            WordToGuessLabel.Text = word;

            foreach (Button c in EnglishKeyboard.Controls)
            {
                c.Enabled = true;
            }

            foreach (Button c in MacedonianKeyboard.Controls)
            {
                c.Enabled = true;
            }

            hangmanHead.Visible     = false;
            hangmanBody.Visible     = false;
            hangmanLeftArm.Visible  = false;
            hangmanRightArm.Visible = false;
            hangmanLeftLeg.Visible  = false;
            hangmanRightLeg.Visible = false;
        }