Part 2: Programming Contest Players

Download

As with previous CS 105 MPs, we have provided a base set of files for you inside of a ZIP file that you must extract:

Part 2.1: Understanding the Files

For the project, there is one file and then several directories inside of the ZIP file:

Part 2.2: Meeting Hue the Hunter

In the players directory, you will find hueTheHunter.js contains the following code:

var player = {
  // Enter the name of this player in CS 105's Hunger Games
  // (When submitted, this `name` WILL appear on the contest page.  You can
  // use whatever name you would like, it should probably not be your real name,
  // but it should be clean and not disrespectful.)
  name: "Hue the Hunter",

  // Program the makeDecision function to play in the contest!
  makeDecision: function(me, partner, capital) {
    return "h";
  }
};

This is the code for Hue the Hunter. The player variable is a JavaScript object that contains two keys: name (a String) and makeDecision (a function). The makeDecision will be called every time a hunting decision needs to be made:

Since Hue the Hunter always hunts, the makeDecision returns "h".

Part 2.3: Creating Sam the Slacker

We need at least two players to play CS 105's Hunger Games. Let's create Sam the Slacker who will always slack off:

  1. In the players directory, copy hueTheHunter.js, paste it, and rename the new version samTheSlacker.js.
  2. Open samTheSlacker.js in a text editor like Atom.
  3. Change the value of name to "Sam the Slacker"
  4. Change the reutrn value in makeDecision to "s"
  5. Save the file

Part 2.4: Adding Sam the Slacker to CS 105's Hunger Games

The last thing we must do is add Sam the Slacker to CS 105's Hunger Games. We do this by adding an HTML script tag to tell the web browser where to find the code for the player:

  1. Open up cs105HungerGames.html in a text editor.
  2. Find Line 11, which contains the HTML comment <!-- Players in the CS 105 Hunger Games Goes Here -->
  3. After Line 11 and before Line 12, add the following line of HTML:
    <script type="text/javascript" src="players/samTheSlacker.js"></script>
    
  4. Save the file

Part 2.5: Testing Everything so Far

If you completed everything correctly so far, you can run your very first 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, Hue the Hunter should always be hunting and Sam the Slacker should always be slacking:

Part 3