samedi 17 octobre 2015

Calling a class: Retrieving unknown error

I am writing a hangman code at the moment and I am attempting to retrieve my words from a different class file. I am getting one error when i try to import the first random word. It starts to run but then immediately crashes. Any help would be appreciated. This first one is my Hangman.cs

    private WordList wl;

    //guesses
    public static int lives = 6;

    //Words for the running game

    public Hangman()
    {


        Console.WriteLine("SUPER AWESOME HANGMAN!\n");



        //Main Menu
        int response;

        Console.WriteLine("-Press 1 to play-");
        response = Int32.Parse(Console.ReadLine());

        if (response == 1)
        {
            runGame();
        }
        else
        {
            Console.Clear();
        }
    }

    //prompts user for guesses 
    static void Lives()
    {

        int guess = 6;

        if (guess == 6)
        {

            lives = guess;
        }
        else
        {
            Lives();
        }

    }

    //GameState
    public void runGame()
    {


        Random random = new Random((int)DateTime.Now.Ticks);

        string wordToGuess = wl.getRandomWord();
        string wordToGuessUppercase = wordToGuess.ToUpper();

        StringBuilder displayToPlayer = new StringBuilder(wordToGuess.Length);
        for (int i = 0; i < wordToGuess.Length; i++)
            displayToPlayer.Append('-');

        List<char> correctGuesses = new List<char>();
        List<char> incorrectGuesses = new List<char>();

        bool won = false;
        int letters = 0;

        string input;
        char guess;

        Lives();

        while (!won && lives > 0)
        {
            Console.WriteLine("Current word: " + displayToPlayer);
            Console.Write("Guess a letter: ");

            input = Console.ReadLine().ToUpper();
            guess = input[0];

            if (correctGuesses.Contains(guess))
            {
                Console.WriteLine("{0} was correctly chosen already", guess);
                continue;
            }
            else if (incorrectGuesses.Contains(guess))
            {
                Console.WriteLine("{0} was incorrectly chosen already", guess);
                continue;
            }

            if (wordToGuessUppercase.Contains(guess))
            {
                correctGuesses.Add(guess);

                for (int i = 0; i < wordToGuess.Length; i++)
                {
                    if (wordToGuessUppercase[i] == guess)
                    {
                        displayToPlayer[i] = wordToGuess[i];
                        letters++;
                    }
                }

                if (letters == wordToGuess.Length)
                    won = true;
            }
            else
            {
                incorrectGuesses.Add(guess);

                Console.WriteLine("There is not a {0} in the word", guess);
                lives--;
            }

            if (lives == 5)
            {
                Console.WriteLine(" \t\t\t\t\t\tO");
            }
            else if (lives == 4)
            {
                Console.WriteLine(" \t\t\t\t\t\tO" + "\n\t\t\t\t\t\t|");
            }
            else if (lives == 3)
            {
                Console.WriteLine(" \t\t\t\t\t\t O" + "\n \t\t\t\t\t\t/|");
            }
            else if (lives == 2)
            {
                Console.WriteLine(" \t\t\t\t\t\t O" + "\n \t\t\t\t\t\t/|" + "\\");
            }
            else if (lives == 1)
            {
                Console.WriteLine(" \t\t\t\t\t\t O" + "\n \t\t\t\t\t\t/|" + "\\" + " \n\t\t\t\t\t\t/");
            }
            else if (lives == 0)
            {
                Console.WriteLine(" \t\t\t\t\t\t O" + "\n \t\t\t\t\t\t/|" + "\\" + " \n\t\t\t\t\t\t/ \\");
            }

            Console.WriteLine(displayToPlayer.ToString());
        }

        if (won)
            MessageBox.Show("You Won!", "CONGRATS");

        else
            MessageBox.Show("You lost! The word was " + "'" + wordToGuess + "'", "Better Luck Next Time");

        MessageBox.Show("Press OK to replay", "Replay?");
        runGame();

    }
} }

The .cs file I am calling is below.

    private Dictionary<int, string> wlDictionary;

    public WordList()
    {

        // Create memory for our Dictionary
        wlDictionary = new Dictionary<int, string>();

        using (var sr = new StreamReader("../../dictionary_list_all_caps.txt"))
        {
            int i = 0;
            string line = null;

            // while it reads a key
            while ((line = sr.ReadLine()) != null)
            {
                // add the key and whatever it 
                // can read next as the value
                wlDictionary.Add(i, line);
                i++;
            }

        } // Streamreader gets disposed at the end of a using statement

    }


    // Returns a word from the dictionary word list
    public string getRandomWord()
    {

        // Declare our random and index variables
        Random rnd = new Random();
        int index = rnd.Next(0, (wlDictionary.Count - 1));

        // Return the found word
        return wlDictionary[index];

    }

} }

Lastly this is where my main is.

        Hangman hm = new Hangman();

        // Run the game until the user quits via a MessageBox
        hm.runGame();
        Console.WriteLine("o");
        // Pause the program
        Console.ReadKey();
    }
} }




Aucun commentaire:

Enregistrer un commentaire