Exercise 1: A text based game

The Duel

NOTE: This exercise is intended for an object oriented programming language such as C# or Java. PHP or python should work fine as well, without to many adjustments.

Your task is to create a simple text based game where two characters fight each other to death. You can either let the fight progress automatically or hit a key for each round. The project will be divided into subtasks with prerequisits for each task. Make sure each piece works before you move on to the next one. You must be able to run the program before you move on.

 

Goal

At the end, you’ll have four character classes with one unique skill each as well as some skills they all have in common. Each character class must have some way to attack as well as some defence attributes. I won’t bother with class balancing for this challenge. If you want to do that yourself, feel free to go ahead. Some of the classes will have a weapon. We’ll only create two weapons for this exercise, but you, again, can create more if you like. The result of the fight will be randomly generated each time you run the program. The program flow will look like this.

  1. The program starts
  2. The player enters a name for his/her character.
  3. The player picks a class for his/her character.
  4. The player picks a weapon for the character.
  5. The computer selects an opponent with a random class and weapon.
  6. Opponent information is shown to the player.
  7. The player is prompted to press a key or click a button to start the fight.
  8. At the start of each round, the first character to make a move is selected randomly.
  9. The first character to make a move picks a skill to use.
  10. The second character gets a chance to counter the skill.
  11. The second character makes his move.
  12. The first character gets a chance to counter the move.
  13. This continues until one of the characters is out of hitpoints.
  14. The result is displayed to the player.
  15. Program ends.

This might seem overwhelming at first, but we’re going to use basic concepts to complete all these tasks.

Good luck!

Step 1: Setup

Create a new project in your IDE.

Step 2: Character name

Required knowlege: Variables, strings, text output, user input.

Make sure everything is setup correctly, and start by writing a welcome message to the screen. The welcome message should read “Welcome to THE DUEL” (or whatever you want to call it). Afther that prompt the user for a name for their character and save it in a string variable.

 

Step 3: List of opponent names (optional)

Required knowledge: Arrays, random numbers

If you want, you can create a list of names that the computer can use to pick a name. If you don’t wnat to use this, you can set a default name for each character class later on.

 

Step 4: Create the base character class

Required knowledge: Classes (basic understanding), methods, fields (variables in classes)

In this step, we create the base character class and the methods common to all characters. Once you learn inheritance we’ll use this class to class to build the other character classes.

You can use the following steps to create this class, but also feel free to add your own methods and fields if you like:

  1. Create a new class named Character
  2. Now create a fiew fields that are common to all characters, remember to use appropriate data types for your fields. The fields we need for now are only characterName and hitPoints.
  3. Create tow methods: attack() and defend(). If you know about return types, these does not have to return  anything right now.

Now it’s time to add some code so we cna test our new class.

  1. Print the text “Attacking!” in the attack method.
  2. Print the text “Defending” in the defend method.
  3. In your main file, create a new instance of the character class.
  4. Call your attack() and defend() method to make sure they work.

If everything works correctly, you’re done here. If not, you should read some more about objects and classes.

 

Step 5: Give the character a name

Required knowledge: Contructors, input parameters

This step is easy. Open up your character class and add a constructor to it. In Java/C# this will have the name Character. Add two input parameters to the constructor; characterName and hitPoints. When the constructor is called, set the fields name and hitpoints in the character class.

When done, go back to your main program and try to instanciate a character with the new constructor and print the name and hitpoints you pass to it. In C# this will look something like this, asuming you have the name stored in a variable called playerName and use 20 hitpoints:

 

Character myChar = new Character(playerName, 20);
Console.writeLine(myChar.characterName);
Console.writeLine(myChar.hitPoints);

Step 6: Create a weapon class

First we have to ask ourselves, what properties doesa weapon need for this project?

Well, I’ve come up with a sinple one. All we need is name, speed, chance to parry and damage. For this project, we may not use the speed property, but I’ll add it as a bonus if you want to implement it later. We’ll also add two methods for our weapon: parry and attack. I’ll list everything that needs to be implemented below with naming suggestions. Again, you’ll have to figure out the datatypes to use yourself.

Fields:

weaponName
weaponSpeed
weaponDamage
parryChance:

Contructor:

Weapon(name, speed, damage, parray)

Methods:

swing()
parray()

 

Step 7: Implement methods in the weapon class

Required knowledge: return types, bools, integers, if statements

We’ve almost got to the point where we can implement our fighting logic, but first we need our weapon to deal some damage.

To do this, we do the following:

  1. Set the swing() method to return the damage dealt (int will work fine)
  2. Set the parry() method to return a bool (if a parry is successful or not)
  3. In the swing() method, generate a random number between 1 and the weapon damage. NOTE: If you want more control you can adda minDamage field to your weapon but we’ll use 1 as minimum for now.
  4. In the parray() method, generate a random number between 1 and 100. If the value is smaller or equal to parrayChance, return true, otherwise, return false.

Try to add a swing and parray to your main method to make sure everything works correctly before you continue.

 

Step 8: A few more things

We need to fix a few things before we start implementing our fighting logic.

Our weapon can swing and parry, but our character can’t havea weapon yet. We also need to do some work in the attack and defend methods.

Let’s make the following changes:

  1. Open our character class
  2. Add a field called weapon of type Weapon.
  3. Add a weapon parameter to the constructor and set the weapon field.
  4. Add a field called hitChance with a value between 1 and 100.
  5. Add this new field as well to the constructor and set it.
  6. Change the attack() method to return an integer.
  7. Change the defend method to return a bool.
  8. In the attack method, generate a random number between 1 and 100 and compare it to the hitChance lit we did in the parray method for the weapon. Return true if we hit, otherwise return false.
  9. In the defend method, call the weapon parray method and just return the result.
  10. Change yooour main program to at least be able to run it and do some testing.

If everything works correctly, go on to the next step, where we’ll do some fighting.

 

Step 9: Let’s do some fighting

Required knowledge: while loops

This will be quite a long exercise, since we have a lot of work and testing to do. After this step, we should be able to at least generate and run a fight. We don’t have character classes yet, nor do we have different weapons, but we have enough to be able to run a fight to test our program.

  1. Open up our main program.
  2. Remove all the test code (if any), but keep the things we did in step 2 where we stored our character name. If you did step 3, also keep that code.
  3. Put the cursor after the code where you stored the name of the player character.
  4. create two new weapons, I suggest calling them playerWeapon and opponentWeapon. Set the name of the player weapon to “sword” and the name of the opponentWeapon to “axe”.  use whatever damage and parrayCHance you like for each weapon.
  5. Create the player character, give it some hitpoints and a hit chance that makes sense. Try to balance these values with your weapon damage. Also give it the playerWeapon. I suggest calling this variable playerCharacter
  6. Repeat the above step for the opponent character and call it opponentCharacter.
  7. Create a bool variable and call it fightIsRunning (or something else if you prefer) and set it to false.
  8. Print out a message that says something like “Press enter to start the fight”.
  9. wait for user input to continue the program (in C# use Console.readLine())
  10. Leave your main method and create a new method called fight. We still want to be in our main program. This method will be called each round.
  11. Add two input parameters to the fight method for player character and opponent character. Leave the method block empty for now.
  12. Go back to our main method to continue after the user presses enter.
  13. set fightIsRunning to true.
  14. add a while loop. It will look something like this while (fightIsRunning)
  15. Inside teh while look add a call to fight() and pass in the player and opponent characters.
  16. Still in the while loop, add an if statement to check if either player has 0 or less hitpoints.
  17. If player has below 0 hitpoints, print “The player lost!”
  18. If opponent hitpoints is below 0 print “Player won!”.
  19. If either of the above is true, set fightIsRunning to false. This will end the program.
  20. Now, let’s start working on our fight method.
  21. Later in we want to randomize who acts first, but for now we’ll always start with the player.
  22. Create an integer variable to store the result of the player attack (suggested name pAattack).
  23. Call attack on the player character and store the value in pAttack.
  24. If pAttack is greater than 0, call defend() for the opponent.
  25. if opponent defends successfully, print a message about the player missing it’s target (C# example: Console.writeLine(playerCharacter.characterName + ” trues to hit ” opponentCharacter.characterName + ” with his ” + p.weapon.weaponName + “, but misses”); )
  26. If the player hits the opponent print a similar message to the above, except this time, tell the player how much damage he dealt.
  27. Reduce the opponents hitpoints if needed.
  28. Repeat  steps 22-27 in this list for the opponent.

There is some problems with the steps above. If you check the logic, you’ll notice that both players can die in the same round. You can fix this with an if statement, but I leave this to you to figure out.

Try to run your program, if you’ve done everything correctly, you should be able to generate some fight results. I don’t expect you to get this right the first time. Make some mistakes and learn from them.

 

 

Creating character classes (Unfinished)

Required knowledge: Inheritance

Not done yet, please check back later.

 

More weapons (Unfinished)

 

Not done yet, please check back later.

Let the player pick a class (Unfinished)

Required knowledge: Polymorphism

Not done yet, please check back later.

 

Cleaning up (Unfinished)

Required knowledge: Refactoring, abstract lasses

Not done yet, please check back later.

 

Adding more features; Wizard class, mana and spells (Unfinished)

Not done yet, please check back later.

This entry was posted in Uncategorized. Bookmark the permalink.