-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuessingGame.java
275 lines (215 loc) · 9.2 KB
/
GuessingGame.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import java.util.Scanner;
//This is the main abrastract class that acts as the blueprint for the other classes e.g DOG
abstract class Animal {
protected String name;
protected int legs;
protected boolean isPet;
protected boolean isFlexible;
public Animal(String name, int legs, boolean isPet, boolean isFlexible) {
this.name = name;
this.legs = legs;
this.isPet = isPet;
this.isFlexible = isFlexible;
}
public String getName() {
return name;
}
public int getLegs() {
return legs;
}
public boolean isPet() {
return isPet;
}
public boolean isFlexible() {
return isFlexible;
}
// This is the method we used so that when someone enters the attributes an ascii art of the animal is displayed
public abstract void displayAsciiArt();
}
// Dog class extending Animal class
class Dog extends Animal {
public Dog() {
super("Dog", 4, true, false);
}
//Mr Benard , you will see these type of art in the code but we obtained them from asciiart.com to make the game more fun.
@Override
public void displayAsciiArt() {
System.out.println(" ,_-~~~-, _-~~-_");
System.out.println(" / ^-_/ \\_ _-~-.");
System.out.println("| /\\ , `-_/ \\");
System.out.println("| /~^\\ '/ /~\\ /~\\ / \\_ \\");
System.out.println(" \\_/ }/ / \\ \\ ,_\\ }");
System.out.println(" Y / /~ /~ | Y \\ |");
System.out.println(" / | {Q) {Q) | | \\_/");
System.out.println(" | \\ _===_ / |");
System.out.println(" / >--{ }--< \\");
System.out.println(" /~ \\_._/ ~\\");
System.out.println(" / * * Y * \\");
System.out.println(" | * .: | :.* * |");
System.out.println(" \\ )--__==#==__-- /");
System.out.println(" \\_ \\ \\ \\ ,/");
System.out.println(" '~_ | | } ,~'");
System.out.println(" \\ {___/ /");
System.out.println(" \\ ~~~ /");
System.out.println(" BORNE /\\._._._./\\");
System.out.println(" { ^^^ }");
System.out.println(" ~-_______-~");
System.out.println(" / \\");
}
}
// Cat class extending Animal class
class Cat extends Animal {
public Cat() {
super("Cat", 4, true, true);
}
@Override
public void displayAsciiArt() {
System.out.println(" ,_ _");
System.out.println(" |\\_,-~/");
System.out.println(" / _ _ | ,--.");
System.out.println("( @ @ ) / ,-'");
System.out.println(" \\ _T_/-._( (");
System.out.println(" / `. \\");
System.out.println("| _ \\ |");
System.out.println(" \\ \\ , / | Borne");
System.out.println(" || |-\\__ /");
System.out.println(" ((_/`(____,-'");
}
}
// Chicken class extending Animal class
class Chicken extends Animal {
public Chicken() {
super("Chicken", 2, false, false);
}
@Override
public void displayAsciiArt() {
System.out.println(" ,~.");
System.out.println(" ,-'__ `-,");
System.out.println(" {,-' `. } ,')");
System.out.println(" ,( a ) `-.__ ,',')~,");
System.out.println("<=.) ( `-.__,==' ' ' '}");
System.out.println(" ( ) /");
System.out.println(" `-'\\ , )");
System.out.println(" | \\ `~. /");
System.out.println(" \\ `._ \\ /");
System.out.println(" \\ `._____,' /");
System.out.println(" `-. ,'");
System.out.println(" `-. ,-");
System.out.println(" `~~~~'");
System.out.println(" //_||");
System.out.println(" __//--'/` Borne");
System.out.println(" ,--'/` '");
System.out.println(" '");
}
}
// Cow class extending Animal
class Cow extends Animal {
public Cow() {
super("Cow", 4, false, false);
}
@Override
public void displayAsciiArt() {
System.out.println(" .= , =.");
System.out.println(" _ _ /'/ )\\,/,/(_ \\ \\");
System.out.println(" `//-.| ( ,\\\\)\\//\\)\\/\\ ) |");
System.out.println(" //___\\ `\\\\/\\/\\/\\\\///' /");
System.out.println(",-\"~`-._ `\"--'_ `\"\"\"` _ \\`'\"~-.,_");
System.out.println("\\ `-. '_`. .'_` \\ ,-\"~`/");
System.out.println(" `.__.-'`/ (-\\ /-) |-.__,'");
System.out.println(" || | \\O) /^\\ (O/ |");
System.out.println(" `\\\\ | / `\\ /");
System.out.println(" \\\\ \\ / `\\ /");
System.out.println(" `\\\\ `-. /' .---.--.\\");
System.out.println(" `\\/`~(, '() ('");
System.out.println(" /(O) \\\\ _,.-.,_)");
System.out.println(" // \\\\ `\\'` /");
System.out.println("borne / | || `\"\"\"\"~\"`");
System.out.println(" /' |__||");
System.out.println(" `o");
}
}
// Snake class extending Animal class
class Snake extends Animal {
public Snake() {
super("Snake", 0, false, false);
}
@Override
public void displayAsciiArt() {
System.out.println(" /^\\/^\\");
System.out.println(" _|__| O|");
System.out.println(" \\/ /~ \\_/ \\");
System.out.println(" \\____|__________/ \\");
System.out.println(" \\_______ \\");
System.out.println(" `\\ \\ \\");
System.out.println(" Borne | | \\");
System.out.println(" / / \\");
System.out.println(" / / \\\\");
System.out.println(" / / \\ \\");
System.out.println(" / / \\ \\");
System.out.println(" / / _----_ \\ \\");
System.out.println(" / / _-~ ~-_ | |");
System.out.println(" ( ( _-~ _--_ ~-_ _/ |");
System.out.println(" \\ ~-____-~ _-~ ~-_ ~-_-~ /");
System.out.println(" ~-_ _-~ ~-_ _-~");
System.out.println(" ~--______-~ ~-___-~");
}
}
// AnimalFactory class to create animals
class AnimalFactory {
public static Animal createAnimal(String type) {
switch (type) {
case "Dog": return new Dog();
case "Cat": return new Cat();
case "Chicken": return new Chicken();
case "Cow": return new Cow();
case "Snake": return new Snake();
default: return null;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class GuessingGame {
public void start() {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Animal Guessing Game ");
System.out.println("Answer the following questions to guess the animal.");
System.out.print("How many legs does the animal have? ");
int legs = scanner.nextInt();
System.out.print("Is the animal kept as a pet? (true/false) ");
boolean isPet = scanner.nextBoolean();
Animal guessedAnimal = guessAnimal(legs, isPet, scanner);
if (guessedAnimal != null) {
System.out.println("I guess the animal is a " + guessedAnimal.getName() + ".");
guessedAnimal.displayAsciiArt();
} else {
System.out.println("Animal Does not exist try again.");
}
scanner.close();
}
private Animal guessAnimal(int legs, boolean isPet, Scanner scanner) {
switch (legs) {
case 4:
if (isPet) {
System.out.print("Is the animal very flexible? (true/false) ");
boolean isFlexible = scanner.nextBoolean();
if (isFlexible) {
return AnimalFactory.createAnimal("Cat");
} else {
return AnimalFactory.createAnimal("Dog");
}
} else {
return AnimalFactory.createAnimal("Cow");
}
case 2:
return AnimalFactory.createAnimal("Chicken");
case 0:
return AnimalFactory.createAnimal("Snake");
default:
return null;
}
}
public static void main(String[] args) {
GuessingGame game = new GuessingGame();
game.start();
}
}