ISM001 Introduction to Programming (2007-08)

Homework 1: The Simple Text Fighter


Instructions:

Develop a Player class for the Simple Text Fighter, which is a simplified text-based version of the Little Fighter.

When you finish, upload your work to your Programming Portfolio:

  1. Write a few paragraphs in your personal Programming Portfolio to describe your work.
  2. Show a sample output of your program.
  3. Upload the complete source code to an online service (e.g. Box.net, pastebin) so that I can download the source code and run it on my computer. Of course also link to the source code from your programming portfolio.
  4. Send me the URL of your website by the due date.

Remember that anything in your programming portfolio is potentially open to everyone on the Internet, so make sure that everything is in good shape. These include comments inside the source code, robustness of the program, and a good presentation. I would visit your sites to grade the assignments.

Due date: 18 October 2007


Rules of the game:

There are two players, each with an initial power of 100. One player attacks the other. The winner, determined by random, receives the difference between the power of the players before the attack. The loser, on the other hand, loses the same amount. For example, consider that Ho Yin and Justin have 40 and 60 points of power respectively before the attack. If Justin wins the attack, then Ho Yin's power would become 20, and Justin's power would become 80:

Ho Yin: 40 -> 20
Justin: 60 -> 80

If the difference is zero before the attack (i.e. both players have the same power), then use 1 as the difference.

The process is repeated until one player is dead, which occurs when the power becomes negative. End the game and display the power of all the users.

Technical specifications:

More specifically, the class should contain the following (you may add more if needed):

Instance variables:

  1. A String variable that represents the name of the player.
  2. An int variable that represents the power.

Constructor:

  1. A constructor that takes in the name of the player.

Methods:

  1. A get-method that returns the name of the player.
  2. A get-method that returns the remaining power of the player.
  3. A method that updates the power of the player.
  4. A method called isDead() that checks if the player is dead or not. It should return true if the player is dead, and false otherwise. (Hint: Use boolean type for this method.)
  5. A method attack( Player victim) that attacks the victim. It should display a text showing who is attacking whom. Each player has a probability of 1/2 to win the attack. Determine which player wins by using the random number generator in Java (examples here). Check and say if somebody gets killed after the attack.

Main program:

The main program should do the following:

  1. Create two players.
  2. Use a do-while loop to repeat the game.
  3. In each round, let one player attack the other. Tell the result and update the power of each player after the attack.
  4. The loop should continue until any one of the players is dead.
  5. Finally, display the remaining power of the players and end the game.

Example output:

I have created an example output for your reference:

Java Simple Text Fighter! v20070928

== Round 1 ==
Player Ho Yin's power = 100
Player Justin's power = 100
Justin attacks Ho Yin!
Justin wins
== Round 2 ==
Player Ho Yin's power = 99
Player Justin's power = 101
Justin attacks Ho Yin!
Justin wins
== Round 3 ==
Player Ho Yin's power = 97
Player Justin's power = 103
Justin attacks Ho Yin!
Justin loses
== Round 4 ==
Player Ho Yin's power = 103
Player Justin's power = 97
Justin attacks Ho Yin!
Justin wins
== Round 5 ==
Player Ho Yin's power = 97
Player Justin's power = 103
Justin attacks Ho Yin!
Justin loses
== Round 6 ==
Player Ho Yin's power = 103
Player Justin's power = 97
Justin attacks Ho Yin!
Justin loses
== Round 7 ==
Player Ho Yin's power = 109
Player Justin's power = 91
Justin attacks Ho Yin!
Justin wins
== Round 8 ==
Player Ho Yin's power = 91
Player Justin's power = 109
Justin attacks Ho Yin!
Justin loses
== Round 9 ==
Player Ho Yin's power = 109
Player Justin's power = 91
Justin attacks Ho Yin!
Justin loses
== Round 10 ==
Player Ho Yin's power = 127
Player Justin's power = 73
Justin attacks Ho Yin!
Justin loses
== Round 11 ==
Player Ho Yin's power = 181
Player Justin's power = 19
Justin attacks Ho Yin!
Justin loses
Justin is killed!
== Game over ==
Player Ho Yin's power = 343
Player Justin's power = -143


Challenge:

For those who want to make the game more interesting, you can build another version to allow for human interaction with the computer during the game. One idea is to include more than 2 players, one played by human and the others played by the computer. The game ends until the human player is dead (in which case the human player loses), or all the other computer players are dead (in which case the human player wins). An example output is shown
here for your reference.

Note that you will not receive any bonus points for the homework if you complete the challenge. However, you can upload your work to your programming portfolio or use the code in your project. I would also show the most interesting games in the class.

Come to me if your have questions.

- Cheung Ho Yin, 20070929