Computer Sciences Department logo

CS 368 (Summer 2009) — Day 12 Homework

Due Monday, August 3rd, at the start of class.

Description

Write a simple vampire fighting game similar to a Facebook application.

Inspiration

Facebook allows third parties, potentially even you, to write applications that run on the Facebook site. These applications are written in the scripting language PHP. We're not going to be writing a Facebook application, that would take too much time. Also, we're not going to ask you to suddenly start writting PHP. However we will take the general idea as inspiration. The techniques learned here are the foundation for a Facebook application.

Facebook have a glut of very similar games in which you control a monster: vampire, zombie, or similar. Depending on the game there are a variety of options, but the core is the same: your vampire fights the vampires of your friends. If you win, you get some points which makes your vampire stronger. On the theory that if companies keep reimplementing the same game over and over it must be a good idea, we'll be implementing this sort of game.

Details

Players are identified by a single work name, (eg "Alan" "Nick", or "Tim"). Each player has a vampire. A vampire a number of "points" which must be tracked. Points start at 0. A vampire gets more points when they win a fight. A vampire also has a "level." A vampire's level is calculated based on their current points: int(points/5)+1.

When vampires fight, each one "rolls" a normal 6-sided die and adds their level to the result. The vampire with the larger number wins. The winner points equal to the loser's level. So if Tim, a level 5 vampire, fights Nick, a level 7 vampire, and the Tim wins, Tim would receive 7 points. Sadly, Nick doesn't get any points.

Your program should store player information on disk, one file per player. Use the player's name as the file name. You may assume that all player names are valid file names. You may also assume that any two different players have unique names. Because the player name is the file name, that leaves only one piece of information to store: the vampire's points. You can store it as a string representation of the number.

Your program should take exactly two arguments on the command line: the names of the two players whose vampires should fight. It should load the vampires points. It should print the starting status of the two vampires. Your program should then simulate a fight, award the winner some points, and save the updated points. The program should also print final results of the fight, including the updated status of the winner. If a player doesn't have any data on disk, they should be treated as a new vampire with 0 points, and thus be level 1.

You should create and use a new object to store a single vampire's information. "Vampire" might be a good name for it. All of a vampire's information should be stored in this object. In addition, the logic for loading a vampire, saving a vampire, and returning the vampire's level should be implemented in the "Vampire" package. Feel free to include any other logic you feel best fits in the Vampire package.

Because most of the logic for a Vampire is in the Vampire package, the non-Vampire portion (the "main" package) should be relatively straightforward.

Use a hard-coded directory for storing the player files.

The output of rand is good enough for simulating rolling a die.

You may ignore the risk of running multiple copies of the program simultaneously. Obviously in the case of a real web application this is a serious concern.

You may ignore the risk of encountering corrupt player files.

You may assume that you are the only person who will be running this program. As a result you don't need worry about file permissions for anyone else.

Why use a Vampire object?

This application could have been written without bothering with a new object. However, this is a common use for an object oriented design. Everything relevant to a single vampire, both the data and algorithms, is collected in a single place, making it easier to debug, update, and extend. It also means that if we wanted to turn this into a web game, we could take the existing Vampire module and write a new front end, even allowing people to play from the web or the command line. We could then move from using files to store points to using a database just by changing the Vampire object, and both the web and command line versions would continue to work without needing to change.

Example output

This output is representative, your output does not need to exactly match. This output started with the data files found here.

A line beginning with "%" represents the command line.

% ./vampiregame Tim Alan
Tim: Level 2, 7 points
    versus
Alan: Level 2, 8 points
Tim rolls 7, Alan rolls 8: Alan wins and earns 2 points!
Alan: Level 3, 10 points
% ./vampiregame Tim Alan
Tim: Level 2, 7 points
    versus
Alan: Level 3, 10 points
Tim rolls 8, Alan rolls 9: Alan wins and earns 2 points!
Alan: Level 3, 12 points
% ./vampiregame Tim Alan
Tim: Level 2, 7 points
    versus
Alan: Level 3, 12 points
Tim rolls 8, Alan rolls 8: Tie!
% ./vampiregame Tim Nick
Tim: Level 2, 7 points
    versus
Nick: Level 1, 0 points
Tim rolls 8, Nick rolls 7: Tim wins and earns 1 points!
Tim: Level 2, 8 points
% ./vampiregame Tim Nick
Tim: Level 2, 8 points
    versus
Nick: Level 1, 0 points
Tim rolls 8, Nick rolls 6: Tim wins and earns 1 points!
Tim: Level 2, 9 points
% ./vampiregame Tim Nick
Tim: Level 2, 9 points
    versus
Nick: Level 1, 0 points
Tim rolls 3, Nick rolls 4: Nick wins and earns 2 points!
Nick: Level 1, 2 points
% ./vampiregame Tim Nick
Tim: Level 2, 9 points
    versus
Nick: Level 1, 2 points
Tim rolls 4, Nick rolls 2: Tim wins and earns 1 points!
Tim: Level 3, 10 points
% ./vampiregame Tim Nick
Tim: Level 3, 10 points
    versus
Nick: Level 1, 2 points
Tim rolls 7, Nick rolls 7: Tie!
% ./vampiregame Tim Nick
Tim: Level 3, 10 points
    versus
Nick: Level 1, 2 points
Tim rolls 7, Nick rolls 2: Tim wins and earns 1 points!
Tim: Level 3, 11 points

Reminders

Do the work yourself, consulting reasonable reference materials as needed; any reference material that gives you a complete or nearly complete solution to this problem or a similar one is not OK to use. Asking the instructors for help is OK, asking other students for help is not.

Hand In

A printout of your script on a single sheet of paper. At the top of the printout, please include “CS 368 Summer 2009”, your name, and “Homework 12, July 31, 2009”. Identifying your work is important, or you may not receive appropriate credit.