Skip to content

Latest commit

 

History

History
76 lines (56 loc) · 2.49 KB

File metadata and controls

76 lines (56 loc) · 2.49 KB
title Dices and Getting Alien Attributes
actions
checkAnswer
hints
material
editor
language startingCode answer
c#
public static uint D6 () { } public static uint D10 () { } public static uint D100 () { } public static BigInteger DN (BigInteger n) { } public static int getStrength (Alien a) { } public static int getSpeed (Alien a) { } public static int getWeight (Alien a) { }
public static uint D6 () { return (uint) RandomNumber () % 6; } public static uint D10 () { return (uint) RandomNumber () % 10; } public static uint D100 () { return (uint) RandomNumber () % 100; } public static BigInteger DN (BigInteger n) { var result = RandomNumber () % n + 1; return result; } public static int getStrength (Alien a) { return (int) (a.Xna % 1000000 / 10000); } public static int getSpeed (Alien a) { return (int) (a.Xna % 10000 / 100); } public static int getWeight (Alien a) { return (int) (a.Xna % 100); }

To lay the groundwork of game mechanics, we want to implement some methods that will come in handy later. The goal is to have D6 (), D10 (), D100 () methods that behave similarly to 6-soded, 10-sided and 100-sided dices of the same name.

  • D6 returns a random uint between 0 and 5.
  • D10 returns a random uint between 0 and 9.
  • D100 returns a random uint between 0 and 99.
  • DN(n) returns a BigInteger between 1 and n. This method will be used to randomly select an alien id.

Additionally, getStrength (), getSpeed, getWeight should return the corresponding two digits in the xna. Recall from Lesson1 that an xna has 8 digits. The 3 attributes are: Strength(digits 3-4), Speed(digits 5-6), and Weight(digits 7-8).

One thing to note is the return type of these methods. For example, since RandomNumber () returns a ulong, an explicit casting is required for methods with a return type of uint.

Instructions:

  1. Implement D6 (), D10 (), D100 () such that they return a number between 0 and n-1. Apply casting when appropriate.
  2. Implement DN (BigInteger n) such that they return a number between 1 and n.
  3. Implement getStrength (), getSpeed (), getWeight () using % and /.