none
Errors in my C# game app which i can't seem to solve

    Question

  • I am doing a hangman game app for windows store. I've been trying to solve this for days and been getting the same aguementOutOfRangeException. Thanks in advance.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    using System.Text;
    using Windows.Storage;
    using Windows.Storage.Streams;
    using Windows.UI.Popups;
    using System.Collections;
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace EvilHangman
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class HangmanPage : Page
        {
            /* Global list of all words that can currently be used.
         * Global current number of guesses remaining to the user
         * before they lose the game.
         * Global list of all letters that have been guessed.*/
            private List<string> wordList = new List<string>();
            private int numGuesses = -1;
            private List<string> guessedLetters = new List<string>();
            public int n;
            public string a;
            
    
    
    
            //When page is loaded, load wordList, and set numGuesses.
            /// <summary>
            /// Invoked when this page is about to be displayed in a Frame.
            /// </summary>
            /// <param name="e">Event data that describes how this page was reached.  The Parameter
            /// property is typically used to configure the page.</param>
            protected async override void OnNavigatedTo(NavigationEventArgs e)
            {
                this.InitializeComponent();
                var uri = new Uri("ms-appx:///WordsList/corncob_lowercase.txt");
                var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
    
                using (var randomAccesStream = await file.OpenReadAsync())
                {
                    using (var datareader = new DataReader(randomAccesStream))
                    {
                        await datareader.LoadAsync((uint)randomAccesStream.Size);
                        var text = datareader.ReadString((uint)randomAccesStream.Size);
                        Random r = new Random(System.DateTime.Now.Millisecond);
    
                    }
                    for (int i = 0; i < wordList.Length; i++)
                    {
                        MysteryWord.Text += "_ ";
                    }
                    ChancesLeft.Text = "Guesses Left: " + numGuesses;
                }
                }
    
                
                
                    
    
                
    
            
    
            private String displayWord()
            {
                string returnWord = "";
                String word = wordList[0];
    
                foreach (char letter in word)
                {
                    if (guessedLetters.IndexOf(Convert.ToString(letter)) == -1)
                    {
                        returnWord += "_ ";
                    }
                    else
                    {
                        returnWord += letter + " ";
                    }
                }
    
                return returnWord;
            }
    
    
            /* To check if the user has won, by guessing the correct word,
             * check the wordLabel text.  If it does not contain any "_"s
             * then the user has guessed the correct word.
             * When the user wins, display a winning message, and exit the
             * program.*/
            private void checkWin()
            {
                if (!(MysteryWord.Text.Contains("_")))
                {
    
                    var dialog = new MessageDialog("You Win!");
                }
            }
    
            /* To check if the user has lost, by running out of guesses,
             * check the number of guesses left.  If it is less than
             * 1, the user has run out of guesses, and thus lost the game.
             * When the user loses, display a message with up to 50 of the
             * possible words.*/
            private void checkLose()
            {
                if (numGuesses < 1)
                {
                    MessageDialog dialog = new MessageDialog("You Lose!");
    
                }
            }
    
    
    
            private void btnA_Click(object sender, RoutedEventArgs e)
            {
                btnA.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnB_Click(object sender, RoutedEventArgs e)
            {
                btnB.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnC_Click(object sender, RoutedEventArgs e)
            {
                btnC.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnD_Click(object sender, RoutedEventArgs e)
            {
                btnD.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnE_Click(object sender, RoutedEventArgs e)
            {
                btnE.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnF_Click(object sender, RoutedEventArgs e)
            {
                btnF.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnG_Click(object sender, RoutedEventArgs e)
            {
                btnG.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnH_Click(object sender, RoutedEventArgs e)
            {
                btnH.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnI_Click(object sender, RoutedEventArgs e)
            {
                btnI.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnJ_Click(object sender, RoutedEventArgs e)
            {
                btnJ.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnK_Click(object sender, RoutedEventArgs e)
            {
                btnK.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnL_Click(object sender, RoutedEventArgs e)
            {
                btnL.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnM_Click(object sender, RoutedEventArgs e)
            {
                btnM.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnN_Click(object sender, RoutedEventArgs e)
            {
                btnN.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnO_Click(object sender, RoutedEventArgs e)
            {
                btnO.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnP_Click(object sender, RoutedEventArgs e)
            {
                btnP.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnQ_Click(object sender, RoutedEventArgs e)
            {
                btnQ.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnR_Click(object sender, RoutedEventArgs e)
            {
                btnR.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnS_Click(object sender, RoutedEventArgs e)
            {
                btnS.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnT_Click(object sender, RoutedEventArgs e)
            {
                btnT.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnU_Click(object sender, RoutedEventArgs e)
            {
                btnU.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnV_Click(object sender, RoutedEventArgs e)
            {
                btnV.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnW_Click(object sender, RoutedEventArgs e)
            {
                btnW.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnX_Click(object sender, RoutedEventArgs e)
            {
                btnX.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnY_Click(object sender, RoutedEventArgs e)
            {
                btnY.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
            private void btnZ_Click(object sender, RoutedEventArgs e)
            {
                btnZ.IsEnabled = false;
                a = "a";
                displayWord();
            }
    
    
    
        }
    
    }
    

    Thursday, September 27, 2012 10:33 AM

All replies

  • where you get this error excatly
    Thursday, September 27, 2012 11:08 AM
  • sorry was too anxious and forget to state where my error is. I get an error on this part:

     //When page is loaded, load wordList, and set numGuesses.
            /// <summary>
            /// Invoked when this page is about to be displayed in a Frame.
            /// </summary>
            /// <param name="e">Event data that describes how this page was reached.  The Parameter
            /// property is typically used to configure the page.</param>
            protected async override void OnNavigatedTo(NavigationEventArgs e)
            {
                this.InitializeComponent();
                var uri = new Uri("ms-appx:///WordsList/corncob_lowercase.txt");
                var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
    
                using (var randomAccesStream = await file.OpenReadAsync())
                {
                    using (var datareader = new DataReader(randomAccesStream))
                    {
                        await datareader.LoadAsync((uint)randomAccesStream.Size);
                        var text = datareader.ReadString((uint)randomAccesStream.Size);
                        Random r = new Random(System.DateTime.Now.Millisecond);
    
                    }
                    for (int i = 0; i < wordList.Length; i++)
                    {
                        MysteryWord.Text += "_ ";
                    }
                    ChancesLeft.Text = "Guesses Left: " + numGuesses;
                }
                }
    

    By the way, is my algorithm for this hangman game done correctly? As i am relatively new to c# since I am more used to vb, I am not sure if i did it the right way.

    Thursday, September 27, 2012 4:47 PM
  •            

    this.InitializeComponent();
    should be only in the constructor and not in the
    OnNavigatedTo

    but can you give more details about the error. tried your code till line Random r = new Random(System.DateTime.Now.Millisecond);
     and that worked fine.

    which line number you get an exception?

    Thursday, September 27, 2012 5:35 PM
  • Where is the exception coming from?  If it's coming out of dataReader.ReadString, it's quite possible that the data reader is encountering invalid characters in the file for the encoding it was trying to use.  You can toggle that encoding by setting the Encoding property of the DataReader object to a value that matches the text file.

    What is the format of your corncob_lowercase.txt file?   If it's something along the lines of UTF-8 encoded words one per line, then an easier way for you to write that code would be:

    var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri));
    wordList = await FileIO.ReadLinesAsync(file, UnicodeEncoding.Utf8);
    
    Thursday, September 27, 2012 5:44 PM

  •    Initially i was using this code.
    //this is use for generating a random words from my wordList
    {
          for (int i = 0; i < wordList[0].Length; i++)
          {
           
    //This is a text block that will display the random word
    MysteryWord.Text += "_ ";
    
          }
    ChancesLeft.Text = "Guesses Left: " + numGuesses;
        }  
    however when i encounter the aguementOutOfRangeException, so i thought the [0] should not be added in. But when i deleted it, there will be a syntax error for Length, that say my list<string> does not contain a definition for Length.
    Thursday, September 27, 2012 7:41 PM
  • aguementOutOfRangeException you get because wordList seems to be empty.

    if you want to do the for loop for the number of items in the list

    use:

    wordList.Count

    the wordList[0] will get the first item from the list.

    Thursday, September 27, 2012 7:56 PM
  • Where is the exception coming from?  If it's coming out of dataReader.ReadString, it's quite possible that the data reader is encountering invalid characters in the file for the encoding it was trying to use.  You can toggle that encoding by setting the Encoding property of the DataReader object to a value that matches the text file.

    What is the format of your corncob_lowercase.txt file?   If it's something along the lines of UTF-8 encoded words one per line, then an easier way for you to write that code would be:

    var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri));
    wordList = await FileIO.ReadLinesAsync(file, UnicodeEncoding.Utf8);
    what is utf 8? For the corncob_lowercase.txt, the words are separated per line.
    Friday, September 28, 2012 1:20 AM
  • aguementOutOfRangeException you get because wordList seems to be empty.

    if you want to do the for loop for the number of items in the list

    use:

    wordList.Count

    the wordList[0] will get the first item from the list.

    i did think of using that but i want the word to be generated randomly so wordList. count isn't a good idea.
    Friday, September 28, 2012 1:21 AM
  • but why did you used then wordList[0].Length ? is that random?
    Friday, September 28, 2012 7:02 AM