-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path_13_OO_Interfaces.cs
199 lines (180 loc) · 4.35 KB
/
_13_OO_Interfaces.cs
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
using System;
/*
# An interface is a contract that specifies a set of methods
and properties that a class must implement.
# Interfaces are used to achieve abstraction by allowing to write
code that is independent of any specific implementation.
# Interfaces are declared using the interface keyword,
followed by the name of the interface and a set of
method and property signatures.
# A class can implement one or more interfaces by providing
an implementation for each method and property in the interface.
# A class can inherit from a base class and implement one or
more interfaces at the same time.
# Interfaces can be used to define common functionality across
multiple classes, which can help reduce code duplication and
improve maintainability.
# Interfaces can be used as Types
# Class Content
# Theory
# Explain the classes and interfaces
# Show the entire program
# Run the game
# Change the game a little to execute multiple times and
count victories
*/
public class InterfacesApp
{
public static void Main(string[] args)
{
int countWins = 0;
for (int i = 0; i < 1000; i++)
{
if (GameTurn())
{
countWins++;
}
}
Console.WriteLine($"Wins: {countWins}");
Console.WriteLine($"Loses: {100 - countWins}");
}
private static bool GameTurn()
{
GameEntity[] entities = new GameEntity[5];
entities[0] = new Player("Jose", 100, 10);
entities[1] = new Enemy("Bat", 10, 2);
entities[2] = new Enemy("Vampire", 30, 15);
entities[3] = new Enemy("Mummy", 50, 5);
entities[4] = new Enemy("Werewolf", 35, 10);
Random random = new Random();
int turn = 0;
Player player = (Player)entities[0];
int aliveEnemiesCount = entities.Length - 1;
while (player.IsAlive && aliveEnemiesCount > 0)
{
turn++;
Console.WriteLine("".PadLeft(100, '-'));
Console.WriteLine($"Turn: {turn}");
aliveEnemiesCount = entities.Length - 1;
for (int i = 1; i < entities.Length; i++)
{
Enemy enemy = (Enemy)entities[i];
if (!enemy.IsAlive)
{
aliveEnemiesCount--;
}
else
{
enemy.Log();
enemy.TakeDamage(random.Next(0, player.Damage + 1));
if (enemy.IsAlive)
{
player.Log();
player.TakeDamage(random.Next(0, enemy.Damage + 1));
if (!player.IsAlive)
{
break;
}
}
}
}
}
Console.WriteLine($"Game turns: {turn}");
if (player.HP > 0)
{
Console.WriteLine("Player won!");
}
else
{
Console.WriteLine("Player lost!");
}
player.Log();
Console.WriteLine("".PadLeft(100, '='));
Console.WriteLine("Final Log");
for (int i = 0; i < entities.Length; i++)
{
ILogger logger = entities[i];
logger.Log();
}
return player.IsAlive;
}
}
interface ILogger
{
void Log();
}
interface IDamageable
{
void TakeDamage(int value);
}
public class GameEntity : ILogger, IDamageable
{
public int HP { get; set; }
public int Damage { get; set; }
public bool IsAlive { get => HP > 0; }
public GameEntity(int hp, int damage)
{
HP = hp;
Damage = damage;
}
public virtual void TakeDamage(int damage)
{
HP -= damage;
if (HP < 0)
{
HP = 0;
}
}
public virtual void Log()
{
Console.Write($"[HP:{HP}]");
if (IsAlive)
{
Console.WriteLine("[Alive]");
}
else
{
Console.WriteLine("[Dead]");
}
}
}
public class Player : GameEntity
{
public string Name { get; set; }
public Player(string name, int hp, int damage)
: base(hp, damage)
{
Name = name;
}
public override void TakeDamage(int damage)
{
base.TakeDamage(damage);
Console.WriteLine($"[Combat] Player: {Name}; took {damage} damage; HP: {HP}");
Log();
}
public override void Log()
{
Console.Write($"Player: {Name}; ");
base.Log();
}
}
public class Enemy : GameEntity
{
public string Type { get; set; }
public Enemy(string type, int hp, int damage)
: base(hp, damage)
{
Type = type;
}
public override void TakeDamage(int damage)
{
base.TakeDamage(damage);
Console.WriteLine($"[Combat] Enemy: {Type}; took {damage} damage; HP: {HP}");
Log();
}
public override void Log()
{
Console.Write($"Enemy: {Type}; ");
base.Log();
}
}