TASK #1
In the domain class you will create a loop that will prompt the user to enter a value of 1, 2, or 3,
which will in turn, be translated to a floor number in the game. Make sure the user only picks a
selection you are expecting (1, 2, or 3) with a while-loop. Then you will create a switch or if-else
statement. If you are more comfortable with if-else then do switch, or if you are more
comfortable with switch then do if-else. Based on the number the user chooses you will set the
floor variable to the appropriate value. When they select 1 floor should be set to 3, when they
select 2 floor should be set to 6, and when they select 3 floor should be set to 10.
TASK #2
In the domain class you will create a constructor with 6 parameters, representing all the data
loaded from the input file that was saved from a previous adventure. This constructor receives 6
parameters: aName, anAttack, aDefense, aHealth, aCurrentFloor, & aMaxFloor.
TASK #3
In the domain class you will create a save method that will allow the user to save their progress
using a PrintWriter object. This file will overwrite whatever was there before, so no need to use
FileWriter, only PrintWriter. There are 6 attributes that you need to write to the file:
name, attack, defense, health, currentFloor, & maxFloor
public void saveFile()
{
String filename = “game.txt”;
PrintWriter pw = new PrintWriter(filename);
pw.println(name);
pw.println(attack);
pw.println(defense);
pw.println(health);
pw.println(currentFloor);
pw.println(maxFloor);
pw.close();
}
TASK #4
In the driver class you will create a load method that will allow the user to pick up from where
they left off. You will do this using a File object and a Scanner object. Remember to use the File
and Scanner classes, and to close the file object after you’re done. There are 6 attributes you will
need to load from the file to successfully continue an Adventure:
name, attack, defense, health, currentFloor, & maxFloor
After you read the record from the file, and store the data in these 6 variables, you can create a
new Adventure object called JavaQuest with those 6 variables. Remember JavaQuest is a global
variable defined at the beginning of the driver class. Then, within the load method, invoke the
startAdventure() method for the newly created Adventure object.
public static void load()
{
String filename = “load.txt”;
File myFile = new File(filename);
Scanner myScan = new Scanner(myFile);
String name;
int attack, defense, health, currentFloor, maxFloor;
name = myScan.nextLine();
attack = myScan.nextInt();
myScan.nextLine();
defense = myScan.nextInt();
myScan.nextLine();
…
javaQuest = new Adventure(name, attack, defense, health, currentFloor, maxFloor);
}
javaQuest.startAdventure();
The Files
package Adventure;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;
public class Adventure
{
int health, defense, attack;
String mons.
TASK #1In the domain class you will create a loop that will prompt.pdf
1. TASK #1
In the domain class you will create a loop that will prompt the user to enter a value of 1, 2, or 3,
which will in turn, be translated to a floor number in the game. Make sure the user only picks a
selection you are expecting (1, 2, or 3) with a while-loop. Then you will create a switch or if-else
statement. If you are more comfortable with if-else then do switch, or if you are more
comfortable with switch then do if-else. Based on the number the user chooses you will set the
floor variable to the appropriate value. When they select 1 floor should be set to 3, when they
select 2 floor should be set to 6, and when they select 3 floor should be set to 10.
TASK #2
In the domain class you will create a constructor with 6 parameters, representing all the data
loaded from the input file that was saved from a previous adventure. This constructor receives 6
parameters: aName, anAttack, aDefense, aHealth, aCurrentFloor, & aMaxFloor.
TASK #3
In the domain class you will create a save method that will allow the user to save their progress
using a PrintWriter object. This file will overwrite whatever was there before, so no need to use
FileWriter, only PrintWriter. There are 6 attributes that you need to write to the file:
name, attack, defense, health, currentFloor, & maxFloor
public void saveFile()
{
String filename = “game.txt”;
PrintWriter pw = new PrintWriter(filename);
pw.println(name);
pw.println(attack);
pw.println(defense);
pw.println(health);
pw.println(currentFloor);
pw.println(maxFloor);
pw.close();
}
TASK #4
In the driver class you will create a load method that will allow the user to pick up from where
they left off. You will do this using a File object and a Scanner object. Remember to use the File
and Scanner classes, and to close the file object after you’re done. There are 6 attributes you will
need to load from the file to successfully continue an Adventure:
name, attack, defense, health, currentFloor, & maxFloor
2. After you read the record from the file, and store the data in these 6 variables, you can create a
new Adventure object called JavaQuest with those 6 variables. Remember JavaQuest is a global
variable defined at the beginning of the driver class. Then, within the load method, invoke the
startAdventure() method for the newly created Adventure object.
public static void load()
{
String filename = “load.txt”;
File myFile = new File(filename);
Scanner myScan = new Scanner(myFile);
String name;
int attack, defense, health, currentFloor, maxFloor;
name = myScan.nextLine();
attack = myScan.nextInt();
myScan.nextLine();
defense = myScan.nextInt();
myScan.nextLine();
…
javaQuest = new Adventure(name, attack, defense, health, currentFloor, maxFloor);
}
javaQuest.startAdventure();
The Files
package Adventure;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;
public class Adventure
{
int health, defense, attack;
String monster, name;
int success;
Scanner keyboard = new Scanner(System.in);
Random myRand = new Random();
3. int enemyHP;
int enemyAtk;
int enemyDef;
int currentFloor = 1;
int userChoice = 0;
int floor = 0;
public Adventure(int playerClass)
{
System.out.println(" What is your champion's name?");
name = keyboard.nextLine();
if(playerClass == 1) //warrior
{
this.health = 10;
this.defense = 8;
this.attack = 4;
}
else if(playerClass == 2) //ranger
{
this.health = 10;
this.defense = 4;
this.attack = 8;
}
else //wizard
{
this.health = 8;
this.defense = 4;
this.attack = 10;
}
//Task #1 Goes here:
//Loop to validate the selection (1,2,or 3). After loop, create an Adventure
//using one of the floor values that correspond to the user choice ( 1 = 3, 2 = 6, & 3 = 10).
//Use either switch or if-statements.
4. System.out.println("--------------------------------");
}
//Task #2 Goes here:
//Create a constructor that receives as parameters all the values of the Adventure object:
public void increaseHealth(int number)
{
if(health + number < 10)
{
health += number;
}
else if (health + number < 11)
{
health = 10;
}
else
{
System.out.println(" ---------------------"
+ "You are already at full HP! "
+ "---------------------");
}
}
public void setHealth(int health)
{
this.health = health;
}
public int getAttack()
{
return this.attack;
}
5. public int getHealth()
{
return this.health;
}
public int getDefense()
{
return this.defense;
}
public void setEnemyHP(int enemyHP)
{
this.enemyHP = enemyHP;
}
public String toString()
{
return "-------------------------------------------------- "
+ "Your current health is " + getHealth() + ". You have " + getAttack() + " attack and " +
getDefense() + " defense. "
+ "You are currently on floor " + (currentFloor + 1) + " of " + floor +" "
+ "--------------------------------------------------";
}
public void startAdventure() throws IOException
{
int randNum = 0;
String strChoice="";
char chrChoice;
do
{
randNum = myRand.nextInt(5);
switch(randNum)
{
6. case 0:
System.out.println(" A slime draws near!");
monster = "Slime";
battle(monster);
break;
case 1:
System.out.println(" A skeleton draws near!");
monster = "Skeleton";
battle(monster);
break;
case 2:
System.out.println(" A dragon draws near!");
monster = "Dragon";
battle(monster);
break;
case 3:
System.out.println("You found a trap room! Choose your next step wisely! ");
trapRoom();
break;
case 4:
System.out.println("You found a health potion!");
if(this.health < 8)
{
increaseHealth(2);
System.out.println("You restore some health.");
}
else
{
System.out.println("Unfortunately you are already at full health! "
+ "The potion is to big to put in your pocket, so you just leave it for the next adventurer that
comes through.");
}
break;
}
7. currentFloor++;
if(health > 0)
{
System.out.println("You made it to a new floor, do you wish to continue? yes/no");
strChoice = keyboard.next();
strChoice = strChoice.toLowerCase();
chrChoice = strChoice.charAt(0);
if(chrChoice == 'n')
{
save();
break;
}
}
}
while(currentFloor < floor && health > 0);
if (health > 0)
{
System.out.println("You've survived the adventure! Congratulations!");
}
else
{
System.out.println("You made it to floor " + currentFloor + ". Better luck next time.");
}
}
public void battle(String enemy)
{
if (enemy.equals("Slime"))
{
enemyHP = 4;
enemyAtk = 5;
enemyDef = 1;
8. }
else if (enemy.equals("Skeleton"))
{
enemyHP = 6;
enemyAtk = 7;
enemyDef = 2;
}
else
{
enemyHP = 8;
enemyAtk = 9;
enemyDef = 3;
}
while (enemyHP > 0 && health > 0)
{
System.out.println("You are in battle! What is your action?");
System.out.println(" 1. Attack"
+ " 2. Defend"
+ " 3. Flee"
+ " 4. Switch Pokemon");
int battleChoice = keyboard.nextInt();
switch (battleChoice)
{
case 1:
dmgCalc(true);
if (enemyHP > 0)
{
dmgCalc(false);
}
break;
case 2:
System.out.println(" You defend and make a cautious attack!");
enemyHP -= 1;
System.out.println("1 damage!");
9. if (enemyHP > 0)
{
dmgCalc(false);
}
break;
case 3:
System.out.println(" You attempt to escape!");
int escapeCheck = myRand.nextInt(10);
if (escapeCheck > enemyHP)
{
System.out.println("Got away safely!");
enemyHP = 0;
}
else
{
dmgCalc(false);
}
break;
case 4:
System.out.println(" Professor Oak's words ring in your head..."
+ " "Now is not the time to use that!"");
}
System.out.println(toString());
if (enemyHP <= 0)
{
System.out.println("---> You are victorious! <--- ");
}
if (health <= 0)
{
System.out.println("You have been slain...");
}
}
}
public void dmgCalc(boolean userAttack)
11. System.out.println(" Quick! What do you do? 1. Run straight 2. Go left 3. Go right");
userAns = keyboard.nextInt();
if(success == userAns)
{
System.out.println("You found a way out!");
}
else
{
success = myRand.nextInt(2);
System.out.println("You don't see a way out!");
switch(success)
{
case 1:
if(currentFloor>1)
{
System.out.println("You managed to find a hole in the floor! You made it out alive! But you
went down a floor.");
currentFloor --;
}
else
System.out.println("Accepting your fate you punch the wall in rage, the old rocks crumble
away, enough so you can squeeze through."
+ " But....you broke your hand in the process.");
setHealth(health - 2);
break;
}
}
break;
case 1:
System.out.println("You walk into the room and see the way out! The only thing preventing
you from getting there is a spike pit about 10 feet across that spans the entire room.");
System.out.println(" Do you: (1) Get a running start and jump accross? (2) Realize that you
are no athlete and turn around and try to find a different way up?");
userAns = keyboard.nextInt();
switch(userAns)
{
12. case 1:
success = myRand.nextInt(2);
if(success == 1)
{
System.out.println(" You made it!");
}
else
{
System.out.println(" You fall into the pit of spikes. Unfortunately there is no happy ending.");
setHealth(health - health);
}
break;
case 2:
System.out.println("You went back where you came from to try and find a different way up.");
currentFloor --;
break;
}
break;
case 2:
System.out.println("Oh look! You found a Dragon.");
battle("Dragon");
break;
}
}
public void save() throws IOException
{
//Task #3:
//Using PrintWriter, save the values in the Adventure object's 6 fields.
}
package Adventure;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
13. public class AdventureTester
{
static Adventure JavaQuest;
public static void main(String[] args) throws IOException
{
int userChoice = 0;
Scanner kb = new Scanner(System.in);
do
{
System.out.println("Welcome to JavaQuest!"
+ "Do you want to:"
+ " 1. Start New Adventure"
+ " 2. Load Previous Adventure");
userChoice = kb.nextInt();
}
while(userChoice < 1 || userChoice > 2);
if(userChoice == 1)
{
System.out.println("Select your class:"
+ " 1. Warrior"
+ " 2. Ranger"
+ " 3. Wizard");
userChoice = kb.nextInt();
if (userChoice < 1 || userChoice > 3)
{
System.out.println("You, uh, didn't pick a valid choice. Here, have a wizard.");
userChoice = 3;
}
JavaQuest = new Adventure(userChoice);
JavaQuest.startAdventure();
}
else
{
load();
}
14. }
public static void load() throws IOException
{
String name;
int attack, defense, health;
int currentFloor, maxFloor;
//Task #4 Goes here:
//Define a File and Scanner objects
//Read from the file into 6 local variables:
//Create a new Adventure object, and invoke the startAdventure() method
}
}
Solution
Adventure.java
AdventureTest.java // driver class to test the Adventure
Note- please create a file "load.text" to use the second option first time and please enter some
random value as you want.
Sample i/o -
Welcome to JavaQuest!Do you want to:
1. Start New Adventure
2. Load Previous Adventure
1
Select your class:
1. Warrior
2. Ranger
3. Wizard
3
What is your champion's name?
15. Alex
A dragon draws near!
You are in battle! What is your action?
1. Attack
2. Defend
3. Flee
4. Switch Pokemon
1
You attack!
The monster took 7 damage!
The monster attacks!
You took 5 damage!
--------------------------------------------------
Your current health is 3.
You have 10 attack and 4 defense.
You are currently on floor 2 of 10
--------------------------------------------------
You are in battle! What is your action?
1. Attack
2. Defend
3. Flee
4. Switch Pokemon
2
You defend and make a cautious attack!
1 damage!
--------------------------------------------------
Your current health is 3.
You have 10 attack and 4 defense.
You are currently on floor 2 of 10
--------------------------------------------------
---> You are victorious! <---
You made it to a new floor, do you wish to continue? yes/no
no
You've survived the adventure! Congratulations!