Homework 6

In this assignment, you are asked to create a program that makes use of the decorator and command patterns. The scenario that you must implement is the following:

 

An orc and a knight are trapped in a ten by ten arena, forced eternally to do battle with one another. The orc is physically weaker than the knight but is armed with a pole weapon that has a longer reach than the knight's sword. The knight has 10 hit points and can attack the orc whenever he is standing next to it, delivering 1 point of damage on a successful hit. The orc has only 5 hit points but can attack the knight whenever he is within two squares of the orc, delivering 2 points of damage on a successful hit. Finally, each combatant is equipped with one defense potion and one power potion.

 

The orc and the knight each have the same interface:

  • void move(DIRECTION): move one space in the indicated direction. If the move would result in the player stepping into the boundary of the arena, then nothing happens and the player stays where it is.

  • void attack(): launch an attack on the other player. If the opponent is within reach, then generate a random number from 1-20 and if the result is higher than 10, a hit was achieved. Use the takeDamage() method to apply the appropriate amount of damage to the opponent. Note: to calculate distance between the orc and the knight, just follow the primary cardinal points of the compass (N, NE, E, SE, S, SW, W, NW) starting at the orc's position and moving two squares in each direction.

  • void takeDamage(VALUE): deduct VALUE points from the player's hit points.

  • void drinkDefensePotion(): if the player still has their defense potion, then reduce any damage applied to the player by one point for the next five rounds

  • void drinkPowerPotion(): if the player still has their power potion, then increase the damage applied to the other player on a successful hit by one point for the next five rounds

  • boolean isDead(): returns true if a player's hit points is less than or equal to zero; returns false otherwise. A player that is dead cannot perform any of the above actions.

You may, of course, add additional methods but you must support the above methods at a minimum.

 

Additional details:

  • The arena's lower left corner is position (x:0, y:0). Coordinates increase in the positive direction both up and to the right. Thus, the upper left corner is position (x:0, y:9), upper right corner is (x:9, y:9) and the lower right corner is position (x:9, y:0).

  • The orc starts in position (0, 5). The knight starts in position (9, 5).

  • The first player to act in a battle is chosen randomly. Turns then alternate between players.

  • The winner of a match is the player that has greater than zero hit points and has scored the most hits against their opponent. If both players are alive at the end of the match and have scored an equal number of hits against each other, then the match is a draw.

Your job is to implement a program that can simulate battles between the knight and the orc. There are two constraints. First, the power and defense potions must be implemented using the decorator pattern. When a player drinks a potion, they are wrapped by a decorator that ensures that the potions effects are applied correctly. At the end of five rounds, the decorator is removed from the player, thus eliminating the effects of the potion.

 

Second, the command pattern should be used to invoke commands on the two players. The input to the program are two text files, one that contains the commands for the orc and one that contains the commands for the knight. An input file might look like this:

    move(west)
    move(west)
    attack
    drink(defense)
    attack
    move(north)

Your program will parse this file and create a command object for each command. When parsing the orc command file, the commands will be configured to be applied to the orc and, similarly, when parsing the commands for the knight, the command objects will be configured to be applied to the knight.

 

Once both files have been processed, the program will randomly pick a player to go first, take its first command off the queue, and execute it. It will then take the first event off the queue of the opposing player and execute it. It will continue switching among players until all commands have been applied. The program will then determine the winner of the match and print the result. Note: if one player's command queue is shorter than the other, then that player does nothing on their turn once there queue is empty. Thus, your program should loop on applying commands for both players until both queues are empty.

 

In addition to the above, each time a command has been executed, the program will generate a textual display that shows the current state of the match. Such a display might look like this:

 

+---+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+---+
|   |   |   |   | O | K |   |   |   |   |
+---+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+---+
The Knight attacks!
He hits!
Knight: 8 HP, 2 Hits
Orc: 3 HP, 1 Hit

 

This assignment is worth up to five extra credit points. Three points can be earned by creating a high quality text-based solution to this problem. An additional two points are available for creating a GUI-based solution to this problem that shows the updates on a depiction of the arena as they happen, as opposed to generating a text-based report after each command. Most students will receive only two or three points; only the highest quality answers will receive four to five points. This assignment is due Thursday, November 20th, at 11:55 PM. Upload a single zip file to the moodle by that date containing your code and a README file describing your solution and how to run it.

 

Note: As with previous assignments, you may use any OO programming language that you want. Just be sure that your assignment follows the requirements above.. If you build a GUI-based solution to this problem that can't run on a Mac (which is the platform used by our grader) or that requires installing third-party libraries on top of a default installation of a particular language (such as Python), the grader may contact you to set up a meeting to see your code run.

 

Also Note: You are expected to follow the conventions of the Decorator and Command patterns in your implementation. A solution that provides the stated functionality but is not structured according to these patterns will receive zero points.

 

Any questions? Send them to Prof. Anderson

 

Recall that you must not work with other students on this assignment. To remind you of this, please include the following pledge at the top of your README file: "On my honor as a University of Colorado at Boulder student, I have neither given nor received unauthorized assistance on this work."

© Kenneth M. Anderson, 2008