locked
count the sheeps

    Question

  • Hi, i am developing an app for windows 8.1 using visual studio 2013 in c#.my app is to count the sheeps .generate random number of sheeps ,first you will count the number of sheeps and click on the right answer then a sound produce when wrong answer then no sound produce  but my app is not working well .any one tell me whats wrong with my coding.

      private async Task DisplayImageFileAsync(StorageFile file)
            {
                // Display the image in the UI.
                BitmapImage src = new BitmapImage();
                src.SetSource(await file.OpenAsync(FileAccessMode.Read));
                Image1.Source = src;
                AutomationProperties.SetName(Image1, file.Name);

                // Use BitmapDecoder to attempt to read EXIF orientation and image dimensions.
                await GetImageInformationAsync(file);

            }

          

      private async Task GetImageInformationAsync(StorageFile file)
            {
                using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
                {
                    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                }
            }
            IPropertySet m_localSettings = Windows.Storage.ApplicationData.Current.LocalSettings.Values;
            private Random randomclass = new Random();
            private int randomnumber;

      protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                randomnumber = randomclass.Next(1, 9);
                spin(randomnumber);
                StorageItemAccessList m_futureAccess = StorageApplicationPermissions.FutureAccessList;

            }
            private void spin(int rand)
            {
           //    rand = randomnumber;
                switch (rand)
                {
                    case 1:

                        Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/CountSheep/Sheep1.png"));
                        break;
                    case 2:

                        Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/CountSheep/Sheep2.png"));
                        break;
                    case 3:

                        Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/CountSheep/Sheep3.png"));
                        break;
                    case 4:

                        Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/CountSheep/Sheep4.png"));
                        break;
                    case 5:

                        Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/CountSheep/Sheep5.png"));
                        break;
                    case 6:

                        Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/CountSheep/Sheep6.png"));
                        break;
                    case 7:

                        Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/CountSheep/Sheep7.png"));
                        break;
                    case 8:

                        Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/CountSheep/Sheep8.png"));
                        break;
                    case 9:

                        Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/CountSheep/Sheep9.png"));
                        break;
                //    case 10:

                //        Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/CountSheep/Sheep10.png"));
                //        break;
                }
            }
            private void CorrDisplay(int rand)
            {
                //rand = randomnumber;
                switch (rand)
                {
                    case 1:
                   //    Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/OneWrong.png"));
                   playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
                 
                   break;
                    case 2:

                    //    Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/TwoWrong.png"));
                   playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
               
                    break;
                    case 3:

                     //   Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/ThreeWrong.png"));
                   playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
               
                     break;
                    case 4:
                          playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
               
                      //  Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/FourWrong.png"));
                        break;
                    case 5:
                          playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
               
                       // Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/FiveWrong.png"));
                        break;
                    case 6:
                          playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
               
                       // Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/SixWrong.png"));
                        break;
                    case 7:
                          playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
               
                       // Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/SevenWrong.png"));
                        break;
                    case 8:
                          playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
               
                        //Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/EightWrong.png"));
                        break;
                    case 9:
                          playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
               
                       // Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/NinWrong.png"));
                        break;
                    //case 10:
                    //      playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    //playaudio.AutoPlay = true;
               
                    //   // Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/TenWrong.png"));
                    //    break;


                }
            }
            private void One_Click(object sender, RoutedEventArgs e)
            {
              int num = randomnumber;
                if (num == '1')
                {
                    
                    playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
                }
                else
                {
                    //    Display.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/blank.png"));
                //    CorrDisplay(randomnumber);

                    randomnumber = randomclass.Next(1, 10);
                    spin(randomnumber);
                    // CorrDisplay(randomnumber);
                    StorageItemAccessList m_futureAccess = StorageApplicationPermissions.FutureAccessList;

                }
           
            }
            
            private void Two_Click(object sender, RoutedEventArgs e)
            { 
                int num = randomnumber;
                if (num == '2')
                {

                    playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
                }
                else
                {
                    //    Display.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/blank.png"));
                    CorrDisplay(randomnumber);

                    randomnumber = randomclass.Next(1, 10);
                    spin(randomnumber);
                    // CorrDisplay(randomnumber);
                    StorageItemAccessList m_futureAccess = StorageApplicationPermissions.FutureAccessList;

                }
            }

            private void Three_Click(object sender, RoutedEventArgs e)
            {
                //  randomnumber = randomclass.Next(1, 10);
                int num = randomnumber;
                if (num == '3')
                {
                    //  Display.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/OneSheep.png"));
                    playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
                }
                else
                {
                    CorrDisplay(randomnumber);
                    randomnumber = randomclass.Next(1, 10);
                    spin(randomnumber);
                    StorageItemAccessList m_futureAccess = StorageApplicationPermissions.FutureAccessList;

                }   
            }

            private void Four_Click(object sender, RoutedEventArgs e)
            {
                //  randomnumber = randomclass.Next(1, 10);
                int num = randomnumber;
                if (num == '4')
                {
                    //  Display.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/OneSheep.png"));
                    playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
                }
                else
                {
                    CorrDisplay(randomnumber);
                    randomnumber = randomclass.Next(1, 10);
                    spin(randomnumber);
                    StorageItemAccessList m_futureAccess = StorageApplicationPermissions.FutureAccessList;

                }
            }

            private void Five_Click(object sender, RoutedEventArgs e)
            {   //  randomnumber = randomclass.Next(1, 10);
                int num = randomnumber;
                if (num == '5')
                {
                    //  Display.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/OneSheep.png"));
                    playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
                }
                else
                {
                    CorrDisplay(randomnumber);
                    randomnumber = randomclass.Next(1, 10);
                    spin(randomnumber);
                    StorageItemAccessList m_futureAccess = StorageApplicationPermissions.FutureAccessList;

                }
            }

            private void Six_Click(object sender, RoutedEventArgs e)
            {
                //  randomnumber = randomclass.Next(1, 10);
                int num = randomnumber;
                if (num == '6')
                {
                    //  Display.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/OneSheep.png"));
                    playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
                }
                else
                {
                  //  CorrDisplay(randomnumber);
                    randomnumber = randomclass.Next(1, 10);
                    spin(randomnumber);
                    StorageItemAccessList m_futureAccess = StorageApplicationPermissions.FutureAccessList;

                }
            }

            private void Seven_Click(object sender, RoutedEventArgs e)
            {   //  randomnumber = randomclass.Next(1, 10);
                int num = randomnumber;
                if (num == '7')
                {
                    //  Display.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/OneSheep.png"));
                    playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
                }
                else
                {
                    CorrDisplay(randomnumber);
                    randomnumber = randomclass.Next(1, 10);
                    spin(randomnumber);
                    StorageItemAccessList m_futureAccess = StorageApplicationPermissions.FutureAccessList;

                }
            }

            private void Eight_Click(object sender, RoutedEventArgs e)
            {   //  randomnumber = randomclass.Next(1, 10);
                int num = randomnumber;
                if (num == '8')
                {
                    //  Display.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/OneSheep.png"));
                    playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
                }
                else
                {
                    CorrDisplay(randomnumber);
                    randomnumber = randomclass.Next(1, 10);
                    spin(randomnumber);
                    StorageItemAccessList m_futureAccess = StorageApplicationPermissions.FutureAccessList;

                }
            }

            private void Nine_Click(object sender, RoutedEventArgs e)
            {
                //  randomnumber = randomclass.Next(1, 10);
                int num = randomnumber;
                if (num == '9')
                {
                    //  Display.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/OneSheep.png"));
                    playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
                }
                else
                {
                    CorrDisplay(randomnumber);
                    randomnumber = randomclass.Next(1, 10);
                    spin(randomnumber);
                    StorageItemAccessList m_futureAccess = StorageApplicationPermissions.FutureAccessList;

                }
            }


    Rabia (Microsoft Student Partner)

    Friday, March 07, 2014 5:42 PM

All replies

  • In what way is it not working well? What specifically do you need help with?

    Rather than repeating essentially the same code in several functions (one_click, two_click, etc.) you should refactor that into one function that can handle the variable case.

    --Rob

    Friday, March 07, 2014 7:21 PM
    Owner
  • Hi, i am developing an app in vs 2013 c#. actually i have buttons from 1-9 .i have number of sheeps .i want to count the number of sheeps.and the sheeps are generating randomly ,suppose one sheep occured and i count it is 1 and i will click on the button1 then a sheep sound produce that tells us that you have counted right but if 2 sheeps occured and you select the button 1 thats mean you have not counted right then it should display a wrong message occured that will be looks like (wrong, 1 sheep) and if 1 sheep occur and u select button 2then a wrong message should be display but in my app its working fine for right answer but wrong message are not displaying.anyone can tell me how can i do this?  

    private void One_Click(object sender, RoutedEventArgs e)

            {
                int num = 1;
                if (num == randomnumber)
                {
                    playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
                    randomnumber = randomclass.Next(1, 9);
                    spin(randomnumber);
                    StorageItemAccessList m_futureAccess = StorageApplicationPermissions.FutureAccessList;
                }
                else
                {
                    randomnumber = randomclass.Next(1, 9);
                    spin(randomnumber);
                    StorageItemAccessList m_futureAccess = StorageApplicationPermissions.FutureAccessList;         

      }   
            }

                            

     private void Two_Click(object sender, RoutedEventArgs e)
            { 
                int num = 2;
                if (num == randomnumber)
                {
                    playaudio.Source = new Uri(@"ms-appx:///Assets/CountSheep/baa.mp3");
                    playaudio.AutoPlay = true;
                    randomnumber = randomclass.Next(1, 9);
                    spin(randomnumber);
                    StorageItemAccessList m_futureAccess = StorageApplicationPermissions.FutureAccessList;
                }
                else 
                {
                    Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/Message/OneWrong.png"));

                    randomnumber = randomclass.Next(1, 9);
                        spin(randomnumber);
                        StorageItemAccessList m_futureAccess = StorageApplicationPermissions.FutureAccessList;

                    }
           }

    }


    Rabia (Microsoft Student Partner)

    Saturday, March 08, 2014 10:45 AM
  • Hello Rabia

    Can you please elaborate as how do you want to display your error message? Are you able to get into the false condition because from your current code, I am not able to dry run.

    I think it can be re-written in a better and simpler way.

    Regards

    Arafat

    Sunday, March 09, 2014 6:16 AM