Part 3: Towards a Better Player

Part 3.1: Creating Random Riko

So far the two players in the Game are rather predictable. We will now create a player who is more... random. We'll call this new player Random Riko.

  1. Follow the steps from Part 2 to create a new player. A good name for the file would be randomRiko.js.
  2. Edit the your new player's name to be "Random Riko".

Part 3.2: Coding Random Riko

To code Random Riko, we will use the JavaScript function Math.random(). Math.random() returns a randomly generated number in the range [0, 1). This means one time it might be 0.39894, another time 0.98532, and another time 0.10934.

The following code is the start of Random Riko's makeDecision function:

// Program the makeDecision function to play in the contest!
makeDecision: function(me, partner, capital) {
  var randomNumber = Math.random();


To finish this function:

  1. Add a conditional to check if randomNumber is less than 0.5. If so, return "h". Otheriwse, return "s".
  2. Once you've got it, save the file!

Part 3.3: Adding Random Riko to the Game

  1. Follow the steps from Part 2.4 to add a new player to the HTML page. Make sure to refer to the correct file name, as Random Riko is probably in players/randomRiko.js.

Part 3.4: Testing Everything so Far

If you completed everything correctly so far, you can run your second CS 105 Hunger Games!

  1. Open cs105HungerGames.html in a web browser.
  2. Press Play!

If everything works, you should be able to watch CS 105's Hunger Games unfold before your very eyes! Specifically, you should see Hue the Hunter, Sam the Slacker, and Random Riko:

Part 4