Example #1
0
        //Assume you are throwing darts at a 5 by 5 square board.
        //Each throw will hit randomly at any of the 25 possible squares with equal likelihood.
        //After throwing a certain number of darts, you count the total number of different squares you hit.
        //Write a program to simulate this game by throwing n darts, where n is a given input value.
        private void BtnDarts_Click(object sender, EventArgs e)
        {
            LstView.Clear();
            int darts = int.Parse(Microsoft.VisualBasic.Interaction.InputBox("Number of darts thrown", "", ""));

            int[] squareBoard = new int[24];
            int   squaresHit  = 0;

            for (int i = 0; i < darts; i++)
            {
                Random r            = new Random();
                int    squareNumber = r.Next(1, 26);

                squareBoard[squareNumber] += 1;
            }

            for (int x = 0; x < squareBoard.Length; x++)
            {
                if (squareBoard[x] != 0)
                {
                    squaresHit += 1;
                }
            }
            LstView.Items.Add(darts.ToString() + " darts have been thrown and have hit " + squaresHit.ToString() + " different squares");
        }
Example #2
0
        private async void LstView_Refreshing(object sender, EventArgs e)
        {
            var post = _contacts = await GetContact();

            _contacts           = new ObservableCollection <Contact>(post);
            LstView.ItemsSource = _contacts;
            LstView.EndRefresh();
        }
Example #3
0
        private async void LstView_Refreshing(object sender, EventArgs e)
        {
            var post = await GetPeopleAsync();

            _person             = new ObservableCollection <Person>(post);
            LstView.ItemsSource = _person;
            LstView.EndRefresh();
        }
Example #4
0
        //***************** Exercise 1 *****************//
        //Write a program which will simulate the tossing of 4 fair coins, 50 times.
        //Display the results of each toss of four on a
        //separate line.If exactly 3 heads occur on a give toss,
        //follow the line with an asterisk (*).

        private void BtnExercise1_Click(object sender, EventArgs e)
        {
            LstView.Clear();
            int    dice        = 0;
            string sumOfDice   = "";
            int    countThreeH = 0;

            RollDice(dice, sumOfDice, countThreeH);
        }
Example #5
0
        //At a casino, the following game is played between a player and the banker.The player pays the banker
        //$7.5 . The banker then pays the player $1, and spins the wheel again.Suppose the number this time is 24. The
        //banker pays another $1, and the process continues until a number is produced which has already occured.
        //Suppose the sequence of numbers is 17,24,18,31. The banker has paid the player $4.00 . At the next turn of the
        //wheel the number is 18 (a number already produced). At this point the game ends, and the player has lost
        //$3.50. Simulate 200 such games, and display the number of times the
        //player wins or loses, and the players total loss or gain.
        private void BtnPlay_Click(object sender, EventArgs e)
        {
            LstView.Clear();
            int    wins   = 0;
            int    losses = 0;
            double total  = 0;

            for (int i = 1; i <= 200; i++)
            {
                int[] numbersPlayed = new int[37];
                PlayTheRoulette(numbersPlayed, ref wins, ref losses, ref total);
            }
            LstView.Items.Add("Wins: " + wins.ToString() + " Losses: " + losses.ToString() + " Total: " + total.ToString());
        }
Example #6
0
        private void LstView_LayoutUpdated(object sender, EventArgs e)
        {
            LstView.SelectedIndex = LstView.Items.Count - 1;

            LstView.ScrollIntoView(LstView.SelectedItem);

            if (GlobalVariable.Message.Count >= 1000)
            {
                GlobalVariable.Message.Clear();
            }
            if (GlobalVariable.Message.Count > 50)
            {
                GlobalVariable.Message.RemoveAt(0);
                //
            }
        }
Example #7
0
        //A gambler has a choice of two games. The first costs $8.00 to play.
        //Two dice are rolled and the player receive the sum
        //of the numbers rolled in dollars.The second game costs $15.00 to play.
        //Two dice are rolled and the player receives the product of the numbers rolled in dollars.

        //Write a program which simulates each game being played 1000 times
        //and calculates the average winnings per game. Also determine which game the gambler should choose.

        private void BtnPlay_Click(object sender, EventArgs e)
        {
            LstView.Clear();

            int game1        = 0;
            int game2        = 0;
            int averageGame1 = 0;
            int averageGame2 = 0;

            for (int i = 0; i < 1000; i++)
            {
                Random r = new Random();

                //game 1
                int dice1 = r.Next(1, 7);
                int dice2 = r.Next(1, 7);
                game1 += dice1 + dice2 - 8;

                //game 2
                int dice3 = r.Next(1, 7);
                int dice4 = r.Next(1, 7);
                game2 += dice3 * dice4 - 15;
            }
            averageGame1 = game1 / 1000;
            averageGame2 = game2 / 1000;

            LstView.Items.Add("Average Game1 = " + averageGame1.ToString());
            LstView.Items.Add("Average Game2 = " + averageGame2.ToString());


            if (averageGame1 <= 0 && averageGame2 <= 0)
            {
                LstView.Items.Add("Better stop gambling");
            }
            else if (averageGame1 > averageGame2)
            {
                LstView.Items.Add("Game 1 offers more profit");
            }
            else if (averageGame1 < averageGame2)
            {
                LstView.Items.Add("Game 2 offers more profit");
            }
            else
            {
                LstView.Items.Add("Both games offer same profit");
            }
        }
Example #8
0
        //Write a VB program that simulates 15 rolls of a dice and then
        //prints out the number of times each number is generated.
        private void BtnExercise_Click(object sender, EventArgs e)
        {
            LstView.Clear();
            int[] dice = new int[7];

            for (int i = 0; i < 15; i++)
            {
                Random r          = new Random();
                int    diceNumber = r.Next(1, 7);
                dice[diceNumber] += 1;
            }

            for (int x = 0; x < dice.Length - 1; x++)
            {
                if (dice[x] != 0)
                {
                    LstView.Items.Add(x.ToString() + " \t " + dice[x]);
                }
            }
        }
Example #9
0
 private void LstView_GiveFeedback(object sender, GiveFeedbackEventArgs e)
 {
     LstView.SelectedIndex = LstView.Items.Count - 1;
     LstView.ScrollIntoView(LstView.SelectedItem);
 }