Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
285 lines (226 loc) · 8.12 KB
/
Copy pathProgram.cs
File metadata and controls
285 lines (226 loc) · 8.12 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
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
276
277
278
279
280
281
282
283
284
285
using System;
using System.Linq;
using System.Threading;
using System.Collections.Generic;
namespace AdvancedInheritance
{
class Program
{
static List<Animal> AnimalPen = new List<Animal>();
static List<Item> Backpack = new List<Item>();
public static bool ContainsWeapon()
{
bool result = false;
foreach (var item in Backpack)
{
if (item is IUsable)
{
result = true;
}
}
return result;
}
public static void PresentAnimals()
{
int i = 1;
foreach (var animal in AnimalPen)
{
Type type = animal.GetType();
string printThis = $"{type}";
Console.WriteLine($"{i}. {printThis.Remove(0, 20)}");
i++;
}
}
public static void PresentItems()
{
int i = 1;
foreach (var item in Backpack)
{
Type type = item.GetType();
string printThis = $"{type}";
Console.WriteLine($"{i}. {printThis.Remove(0,20)}");
i++;
}
}
public static Animal ChoosePet()
{
Animal result = null;
PresentAnimals();
Console.WriteLine("Choose your animal. Enter the corresponding number from the list above.");
result = AnimalPen[Input(1, AnimalPen.Count) - 1];
return result;
}
public static void Greet()
{
Console.WriteLine("Who do you want to greet?");
Console.WriteLine(ChoosePet().Reply());
}
public static void Explore()
{
Random rand = new Random();
int randomNum = rand.Next(15);
switch (randomNum)
{
case 1:
Console.WriteLine("You stumbled across a horse. Do you want to keep it? 1. Yes. 2. No");
if (Input(1, 2) == 1) { AnimalPen.Add(new Horse()); }
break;
case 2:
Console.WriteLine("You stumbled across a mouse. Do you want to keep it? 1. Yes. 2. No");
if (Input(1, 2) == 1) { AnimalPen.Add(new Mouse()); }
break;
case 3:
Console.WriteLine("You found a loaf of bread. Do you want to keep it? 1. Yes. 2. No");
if (Input(1, 2) == 1) { Backpack.Add(new Bread()); }
break;
case 4:
Console.WriteLine("You found a sword. Do you want to keep it? 1. Yes. 2. No");
if (Input(1, 2) == 1) { Backpack.Add(new Sword()); }
break;
case 5:
Console.WriteLine("You found a axe. Do you want to keep it? 1. Yes. 2. No");
if (Input(1, 2) == 1) { Backpack.Add(new Axe()); }
break;
default:
Console.WriteLine("You found nothing, keep exploring.");
break;
}
}
public static IUsable ChooseWeapon()
{
var choice = new Item(); // Kan ej skriva null på implicita variabeltyper, därav "new Item()".
Type type = null;
bool isWeapon = false;
do
{
PresentItems();
Console.WriteLine("Choose your weapon. Enter the corresponding number from the list above.");
choice = Backpack[Input(1, Backpack.Count) - 1];
type = choice.GetType();
if (choice is IUsable) { isWeapon = true; } else { Console.WriteLine("You cant hurt anyone with that."); };
} while (!isWeapon);
return choice as IUsable;
}
public static IEdible ChooseFood()
{
var choice = new Item(); // Kan ej skriva null på implicita variabeltyper, därav "new Item()".
Type type = null;
bool isFood = false;
PresentItems();
Console.WriteLine("Choose your food. Enter the corresponding number from the list above.");
do
{
choice = Backpack[Input(1, Backpack.Count) - 1];
type = choice.GetType();
if (choice is IEdible) { isFood = true; } else { Console.WriteLine("Do you want to kill yourself?!?"); };
} while (!isFood);
return choice as IEdible;
}
public static void Fight()
{
Animal animal = ChoosePet();
while (animal.Health > 0)
{
int damage = ChooseWeapon().Damage;
animal.Health -= damage;
Console.WriteLine($"You hit the animal with {damage}, the animal has {animal.Health} left.");
}
AnimalPen.Remove(animal);
Console.WriteLine("The animal has died, it will be your slave in Valhalla.");
}
public static int Input(int minVal, int maxVal)
{
int result = 0;
bool loop = false;
do
{
bool isInt = int.TryParse(Console.ReadLine().Trim(), out result);
if (isInt && result >= minVal && result <= maxVal)
{
loop = false;
}
else
{
Console.Beep(900, 100);
Thread.Sleep(70);
Console.Beep(400, 650);
loop = true;
}
} while (loop);
return result;
}
public static void StartMeny()
{
string option1 = "\0";
string option2 = "\n2. Explore the world";
string option3 = "\0";
string option4 = "\0";
string option5 = "\0";
int minVal = 2;
int maxVal = 2;
while (1+ 1 == 2)
{
if (AnimalPen.Count > 0)
{
option1 = "\0";
option3 = "\n3. Check your animals.";
option4 = "\n4. Greet an animal.";
minVal = 2;
maxVal = 4;
}
if (Backpack.Count > 0)
{
option1 = "\n1. Present Items";
option3 = "\0";
option4 = "\0";
option5 = "\0";
minVal = 1;
maxVal = 2;
}
if (Backpack.Count > 0 && AnimalPen.Count > 0 && !ContainsWeapon())
{
option1 = "\n1. Present Items";
option3 = "\n3. Check your animals.";
option4 = "\n4. Greet an animal.";
minVal = 1;
maxVal = 4;
}
if (AnimalPen.Count > 0 && ContainsWeapon())
{
option3 = "\n3. Check your animals.";
option4 = "\n4. Greet an animal.";
option5 = "\n5. Fight an animal.";
maxVal = 5;
}
Console.WriteLine("What do you want to do?\n" +
$"{option1}" +
$"{option2}" +
$"{option3}" +
$"{option4}" +
$"{option5}");
switch (Input(minVal, maxVal))
{
case 1:
PresentItems();
break;
case 2:
Explore();
break;
case 3:
PresentAnimals();
break;
case 4:
Greet();
break;
case 5:
Fight();
break;
}
}
}
static void Main(string[] args)
{
StartMeny();
}
}
}