-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBattleGame.java~
131 lines (122 loc) · 5 KB
/
BattleGame.java~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.util.Random;
import java.util.Scanner;
import java.util.ArrayList;
public class BattleGame {
private static Random randomGenerator=new Random();
//playgame takes a file for the player, the monster and the spell
public static void playGame(String filePlayer, String fileMonster, String fileSpell){
//reads the files for the player and the spell
Character player=FileIO.readCharacter(filePlayer);
Character monster=FileIO.readCharacter(fileMonster);
ArrayList<Spell> spell= FileIO.readSpells(fileSpell);
if(spell==null){
System.out.println("The game will be played without spells.\n");
}
//if either the player or the monster file doesn't read out a correct character, the game cannot be played
if(player==null||monster==null){
System.out.println("The game cannot be played.\n");
return;
}else{
//print out the characteristics of the player and the monster
printInfo(player, monster);
player.setSpells(spell);
System.out.println("\nHere are the available spells:");
player.displaySpells();
//scanner for the input of the command
Scanner read=new Scanner(System.in);
while(player.getCurrHealth()>0&&monster.getCurrHealth()>0){
System.out.println("\nEnter a command: ");
String command;
//takes the next line of input in command
command=read.nextLine();
//for all the commands, checks after each one attacks if the health of the other is 0
//if it is, it prints out a message and takes you out of the while loop.
if(command.equals("attack")){
oneAttackOther(player, monster);
if(monster.getCurrHealth()<=0){
System.out.println("\nFantastic! You killed the monster");
player.increaseWins();
break;
}
System.out.println();
oneAttackOther(monster, player);
if(player.getCurrHealth()<=0){
System.out.println("\nOh no! You lost!");
monster.increaseWins();
break;
}
}
else if(command.equals("quit")){
System.out.println("Goodbye!");
return;
}
else{
playerSpellMonster(player, monster, spell, command);
if(monster.getCurrHealth()<=0){
System.out.println("\nFantastic! You killed the monster!");
player.increaseWins();
break;
}
System.out.println();
oneAttackOther(monster, player);
if(player.getCurrHealth()<=0){
System.out.println("\nOh no! You lost!");
monster.increaseWins();
break;
}
}
}
if(monster.getCurrHealth()<=0){
FileIO.writeCharacter(player,"player.txt");
System.out.println(player.getName()+" has won: "+player.getNumWins()+" times.");
}else{
FileIO.writeCharacter(monster,"monster.txt");
System.out.println(monster.getName()+" has won: "+monster.getNumWins()+" times.");
}
}
}
private static void printInfo(Character player, Character monster){
System.out.println("Name: "+player.getName());
System.out.println("Health: "+player.getMaxHealth());
System.out.println("Attack: "+player.getAttackValue());
System.out.println("Number of wins: "+player.getNumWins());
System.out.println("\nName: "+monster.getName());
System.out.println("Health: "+monster.getMaxHealth());
System.out.println("Attack: "+monster.getAttackValue());
System.out.println("Number of wins: "+monster.getNumWins());
}
private static void oneAttackOther(Character player, Character monster){
double damage=player.getAttackDamage(randomGenerator.nextInt());
String damageStr=String.format("%1$.2f", damage);
System.out.println("\n"+player.getName()+" attacks for "+damageStr+" damage!");
double damagedHealth=monster.takeDamage(damage);
if(damagedHealth>0){
System.out.println(monster.toString());
}else{
System.out.println(monster.getName()+" was knocked out!");
return;
}
}
private static void playerSpellMonster(Character player, Character monster, ArrayList<Spell> spell, String command){
double damage=player.castSpell(command,(randomGenerator.nextInt()));
if(damage<0){
System.out.println("\n"+player.getName()+" tried to cast "+command+" but they dont' know that spell.");
return;
}else if(damage==0){
System.out.println("\n"+player.getName()+" tried to cast "+command+", but they failed.");
}else{
String dmgCast=String.format("%1$.2f", damage);
double damagedHealth=monster.takeDamage(damage);
System.out.println("\n"+player.getName()+" casts "+command+" dealing "+dmgCast+" damage!");
if(damagedHealth>0){
System.out.println(monster.toString());
}else{
System.out.println(monster.getName()+" was knocked out!");
return;
}
}
}
public static void main(String args[]){
playGame("player.txt", "monster.txt", "spells.txt");
}
}