-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameIO.java
More file actions
156 lines (151 loc) · 5.68 KB
/
GameIO.java
File metadata and controls
156 lines (151 loc) · 5.68 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.io.*;
/**
* Provide a command line interface for the CODE game.
*
* @author A.A.Marczyk
* @version 02/02/14
*/
public class GameIO
{
private static Game gp ;
private static BufferedReader myIn = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args)
{
int choice;
String playerName;
try
{
System.out.println("Enter player's name");
String s = myIn.readLine();
gp = new Player(s); // create Player
choice = -1;
while (choice != 0 && !gp.hasLost())
{
choice = getMenuItem();
if (choice == 1)
{
System.out.println(gp.getAllChampionsForHire());
}
else if (choice == 2)
{
System.out.println(gp.getArmy());
}
else if (choice == 3)
{
System.out.println("Enter Champion's name");
String nme = (myIn.readLine()).trim();
if(!gp.hasLost())
{
System.out.println(gp.hireChampion(nme));
}
else
{
System.out.println("You have lost the game - quit ");
}
}
else if (choice == 4)
{
System.out.println("Enter Champion's name");
String nme = (myIn.readLine()).trim();
if(gp.getChampion(nme) != null)
{
boolean result = gp.dismissChampion(nme);
if(result)
{
System.out.println("\nChampion " + nme +
" dismissed" + "\nTreasury: " + gp.getMoney());
}
else
{
System.out.println("Dismissal not performed");
}
}
}
else if (choice == 5)
{
System.out.println("Enter Champion's name");
String nme = (myIn.readLine()).trim();
boolean result = gp.restoreChampion(nme);
if(result)
{
System.out.println("\nChampion " + nme +
" restored" + "\nTreasury: " + gp.getMoney());
}
else
{
System.out.println("Restoration not performed");
}
}
else if (choice == 6)
{
System.out.println("Enter Champion's name");
String nme = (myIn.readLine()).trim();
System.out.println(gp.getChampion(nme));
}
else if (choice ==7)
{
System.out.println("Enter number of the challenge");
String chal = myIn.readLine();
int number = Integer.parseInt(chal);
System.out.println(gp.meetChallenge(number));
}
else if (choice == 8)
{
System.out.println(gp.toString());
}
else if (choice == 9)
{
System.out.println(gp.getAllChallenges());
}
else if (choice == 10)
{
System.out.println("Treasury: " + gp.getMoney());
}
else if (choice == 11)
{
System.out.println("Write to file");
System.out.println("Enter filename");
String nme = (myIn.readLine()).trim();
gp.saveGame(nme);
}
else if (choice == 12)
{
System.out.println("Restore from file");
System.out.println("Enter filename");
String nme = (myIn.readLine()).trim();
Game gp2 = gp.restoreGame(nme);
System.out.println(gp2.toString());
}
else if ( choice == 0)
{
System.out.println("Quitting");
}
}
System.out.println(gp.toString());
}
catch (IOException e) {System.out.println (e);}
}
private static int getMenuItem()throws IOException
{ int option = -1;
System.out.println("Main Menu");
System.out.println("1. list all champions for hire");
System.out.println("2. list my army of champions");
System.out.println("3. hire a champion");
System.out.println("4. dismiss a champion");
System.out.println("5. restore a champion");
System.out.println("6. get a champion");
System.out.println("7. meet a challenge");
System.out.println("8. view my state");
System.out.println("9. list all challenges");
System.out.println("10. show state of treasury");
System.out.println("11. save this game");
System.out.println("12. restore a game");
System.out.println("0. quit");
while (option < 0|| option > 12)
{
System.out.println("Enter the number of your choice");
option = Integer.parseInt(myIn.readLine());
}
return option;
}
}