Programming using Activity Diagrams

In aid of  continuing to learn to code using C# script within Unity, we were introduced to Activity Diagrams and how they could be used much like flow charts, to help simplify the code down to a step-by-step process.

Example

blog1

The example we were given was a simple dice roll which logged the number rolled between 1 and 6, whilst also congratulating them in the console command if they rolled a 6.

blog2.png

We then transferred the process into code, starting with a public class named “Dice”. Furthermore, in the “Start” function we created a integer variable called “number” which calculated a random number between 1 and 6 using the “Random.Range” command.

Note: When returning a random integer using the Random.Range command, the minimum number is inclusive whilst the maximum number  is exclusive.

For example, when returning a random integer between 1 and 6, the code would be displayed as follows:

“int number = Random.Range(1, 7);”

However, when returning a random float, both the maximum and minimum range are inclusive.

Next, we added an if statement which logged “Nice Roll” to the console command if the random integer returned was equal to 6 (number == 6).  Lastly, following it up with an else statement which logged the number rolled if the random integer was not equal to 6 (number =! 6).

As the above script was all placed within the “Start” function, the code would only run once when the application is first loaded.

My own Activity Diagram – Black Jack!

blog3.png

Adam and I then went on to create our own activity diagram, albeit being slightly more complicated than the example.  We wanted to re-create the card game “Black Jack”, also known as “21”.

The minimum card amount being 1 and the maximum 11, both being the “Ace”. The other cards go between 2 and 9, with the remaining cards of 10, Jack, Queen and King all equaling 10.

Note :As you will have noticed, we would have to determine if the Ace drawn was 1 or 11, however for this experiment we would think of them as two separate amounts (cards) and amend the code at a later time.

The game begins with the player drawing 2 cards between 1 and 11. If the result of those numbers equals 21 the players score is logged and “Black Jack!” is displayed within the console.

Else, if the result does not equal 21, the player then will be prompted with a choice to “hit” or “stick”.  If “stick” is chosen, the players score is logged.  However, if the player chooses to “hit”, an additional random integer is generated and added the the players current score.

The result now splits into three paths.

If the result is equal to 21, the players score is logged and “Black Jack!” is displayed in the console.

If the result is greater than 21, the players score is logged and “Bust!” is displayed in the console.  The player automatically loses.

If the result is less than 21, the player then returns to the choice of stick or hit, where this process loops / repeats until the player decides to stick, go bust or achieve “Black Jack”.

This score would then need to be compared to a score generated by AI (Computer Score), to determine whether the player has won, drawn or lost.

Conclusion

After being introduced to activity diagrams, I feel that it helps organize the way in which I am going to code a process as breaks it down into more manageable steps.  My next step is to translate the black jack activity diagram into working code and provide an update in the coming days.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s