Laboratory Exercise – Functions and Subroutines

Dice Rolling Application

Problem Description:

Write an application that will simulate rolling dice for two players. Each player will have a set of dice. When a button is pressed, the application will simulate rolling a set of dice for each player. The application will display each die individually and output whose total was higher. If both totals are the same, then the application will output that it was a tie. The output should be in the form of a message box.

Your application should look as follows:

Problem Discussion:

The description of the application and the initial layout of the application are not overly complex. However, designing a solution without repeating code to perform the same action for each die requires some thought.

Your application will require two picture boxes to represent each set of dice. Above each set of dice should be a label indicate which player the dice belong to. In addition, you will need a button to simulate the rolling of the dice and outputting the result. Finally, you will need a label to indicate the title of the application.

You will require a variable to store the total of both dice rolled from each player. Since the total can only be a whole number and the number can be no greater than 12, you will use a Short to store each player’s total.

The only coding required for the application could be written entirely in the Click events of the buttons however this would lead to confusing and repeated code. With four individual dice to roll, the same code would be required for each die. Why repeat the same actions over and over again. With your newly acquired knowledge of functions, you should see an opportunity to improve the efficiency and readability of your code. Create two functions that will assist coding the Click event of the button in your application. The Click event will pass the picture boxes to the function, so that the function may make any changes required. Passing objects like a picture box is no different that passing other parameters. Code a RollDice to call and RollDie function for each die you need to roll. The RollDie function would calculate the roll, update the picture, and return the result of the roll.

The majority of the work would be in the RollDie function. However, since this will limit the goal of the function to only effect one die, it is more straightforward than if you tried to solve the entire problem at once. The goals of the function taken individually are:

Compute the value of the rolled die

Update the picture representing the die to its new value

return the new value for the die.

To compute the rolled die, you need to simulate a roll from 1 to 6. By using the Rnd function, you can return a value from 0 to 1. If that value is multiplied by 6 you can simulate a value from 0 to 5. Therefore, to get a value from 1 to 6, just add 1. Also, note that the final value should be converted to an Integer. The code for simulating a value from 1 to 6 is (Int(Rnd()*6+1).

Six images are required to represent the possible faces of a die. By numbering the images as Die1.jpg, Die2.jpg, Die3.jpg, Die4.jpg, Die5.jpg, and Die6.jpg, you can easily load the proper picture to the Image property of the text box. The Image property can be changed in code by calling the Image.FromFile method with the proper name of the picture. One could create a Select/Case statement to pick the proper image file, however this would be a waste of code and time. Since the parameter of the FromFile method is a String, you can build the proper String dynamically with the following statement: "Die" & shtRoll.ToString & ".jpg". By passing that to the FromFile method, you handle all six cases in one statement.

The last part is the easiest. Just return the value computed in step 1 to the RollDice function.

 

Problem Solution:

Step 1: Create an Application called DiceRolling.

Step 2: Rename the form in the Solution Explorer to frmDiceRolling.vb.

Step 3: Change the Name property of the form to frmDiceRolling.

Step 4: Change the Text property of the form to Dice Rolling Application.

Step 5: Add a label control to the form.

Step 6: Change the Name property of the label to lblTitle.

Step 7: Change the Text property of the label to Dice Rolling Application.

Step 8: Change the TextAlign property of the label to MidddleCenter.

Step 9: Change the Font Size to 16 and the Bold property to True.

 

Step 10: Add a label for player 1.

Step 11: Change the Name property of the label to lblPlayer1.

Step 12: Change the Text property of the label to Player 1.

Step 13: Change the Font Bold property to True.

 

Step 14: Add two picture boxes for each of the dice for Player 1.

                           

Step 15: Change the Name property of the label to pctPlayer1Die1 and pctPlayer2Die2.

Step 16: Set the Image property of both picture boxes to the file Die1.jpg.

Step 17: Add a label for player 2.

Step 18: Change the Name property of the label to lblPlayer2.

Step 19: Change the Text property of the label to Player 2.

Step 20: Change the Font Bold property to True.

 

Step 21: Add two picture boxes for each of the dice for Player 2.

Step 22: Change the Name property of the label to pctPlayer2Die1 and pctPlayer2Die2.

Step 23: Set the Image property of both picture boxes to the file Die1.jpg.

 

Step 24: Add a button to the form for rolling the dice.

Step 25: Change the Name property of the button to btnRollDice.

Step 26: Change the Text property of the button to Roll Dice.

 

 

Step 27: Resize the form so that the extra space on the bottom is removed.

 

 

Step 28: Add the declaration of the variables to store the total of both dice for each player.

Step 29: Add the code to the btnRollDice button’s Click event. Follow the algorithm presented in the problem discussion.

 

Step 30: Add the code for the function RollDice. Follow the algorithm presented in the problem discussion.

 

Step 31: Add the code for the function RollDie. Follow the algorithm presented in the problem discussion.