forked from maxole/experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWizzardAndWarriors.cs
257 lines (213 loc) · 6.26 KB
/
WizzardAndWarriors.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
/*
http://ericlippert.com/2015/04/27/wizards-and-warriors-part-one/
*/
namespace WizzardAndWarriors.Test
{//*/
[TestClass]
public class WizzardAndWarriors
{
[TestMethod]
public void TestMethod1()
{
IPlayer player = new Warrior(new CommonBag());
player.GiveHim(new Axe());
}
[TestMethod]
public void CalcWeigthTest()
{
IPlayer player = new Warrior(new CommonBag());
player.GiveHim(new Axe());
var weigth = player.Compute(new ComputeWeigth());
Assert.AreEqual(0.4f, weigth);
}
[TestMethod]
public void RidedHorse()
{
var warrior = new Warrior(new CommonBag());
var horse = new Horse(new CommonBag());
var ride = new EquipHorse(new HorsePower(), new ComputeWeigth());
ride.EquipBy(horse, warrior);
Assert.AreEqual(Math.Round(10.0f, 2), Math.Round(horse.Power, 2));
Assert.AreEqual(Math.Round(10.0f, 2), Math.Round(horse.Speed, 2));
}
[TestMethod]
public void HorseWithBaggage()
{
var horse = new Horse(new CommonBag());
var equip = new EquipHorse(new HorsePower(), new ComputeWeigth());
equip
// todo horse !
.EquipBy(horse, new Axe())
.EquipBy(horse, new Axe());
Assert.AreEqual(Math.Round(9.92f, 2), Math.Round(horse.Power, 2));
Assert.AreEqual(Math.Round(9.92f, 2), Math.Round(horse.Speed, 2));
}
}
public interface IWeightiness
{
T Compute<T>(IComputeWeigth<T> compute);
}
public interface IWeapon : IWeightiness
{
}
public interface IPlayer : IWeightiness
{
void GiveHim<T>(T item);
}
public interface IHorse : IWeightiness
{
IPlayer Rider { get; set; }
IBag Bag { get; }
}
public interface IBag : IWeightiness
{
IBag Put<T>(T item);
IEnumerable<T> GetAll<T>();
}
public interface IComputeWeigth<out T>
{
T Compute(Warrior warrior);
T Compute(Horse horse);
T Compute(Axe axe);
T Compute(CommonBag bag);
}
public interface IHorseEquip<in T>
{
IHorseEquip<T> EquipBy(Horse horse, T item);
}
public interface IHorsePower
{
void Compute(float power, float speed, float weight);
float Speed { get; }
float Power { get; }
}
public class ComputeWeigth : IComputeWeigth<float>
{
public float Compute(Warrior warrior)
{
return warrior.Bag.Compute(this);
}
public float Compute(Horse horse)
{
var riderWeigth = horse.Rider == null ? 0.0f : horse.Rider.Compute(this);
var horseWeigth = horse.Bag.Compute(this);
return riderWeigth + horseWeigth;
}
public float Compute(Axe axe)
{
return axe.Weight;
}
public float Compute(CommonBag bag)
{
// todo bag items is generic !
return bag.GetAll<IWeapon>().Sum(s => s.Compute(this));
}
}
public class EquipHorse: IHorseEquip<IPlayer>, IHorseEquip<IWeapon>
{
private readonly IHorsePower _horsePower;
private readonly IComputeWeigth<float> _computeWeigth;
public EquipHorse(IHorsePower horsePower, IComputeWeigth<float> computeWeigth)
{
_horsePower = horsePower;
_computeWeigth = computeWeigth;
}
public IHorseEquip<IPlayer> EquipBy(Horse horse, IPlayer player)
{
horse.Rider = player;
ComputeHorsePower(horse, player);
return this;
}
public IHorseEquip<IWeapon> EquipBy(Horse horse, IWeapon weapon)
{
horse.Bag.Put(weapon);
ComputeHorsePower(horse, weapon);
return this;
}
private void ComputeHorsePower(Horse horse, IWeightiness weightiness)
{
_horsePower.Compute(horse.Power, horse.Speed, weightiness.Compute(_computeWeigth));
horse.Power = _horsePower.Power;
horse.Speed = _horsePower.Speed;
}
}
public class CommonBag : IBag
{
private readonly HashSet<object> _hashSet = new HashSet<object>();
public IBag Put<T>(T item)
{
_hashSet.Add(item);
return this;
}
public IEnumerable<T> GetAll<T>()
{
return _hashSet.Cast<T>();
}
public T Compute<T>(IComputeWeigth<T> compute)
{
return compute.Compute(this);
}
}
public class Warrior : IPlayer
{
private readonly IBag _bag;
public Warrior(IBag bag)
{
_bag = bag;
}
public void GiveHim<T>(T item)
{
_bag.Put(item);
}
public IBag Bag
{
get { return _bag; }
}
public T Compute<T>(IComputeWeigth<T> compute)
{
return compute.Compute(this);
}
}
public class Axe : IWeapon
{
public T Compute<T>(IComputeWeigth<T> compute)
{
return compute.Compute(this);
}
public float Weight
{
get { return 0.4f; }
}
}
public class Horse : IHorse
{
public IPlayer Rider { get; set; }
public IBag Bag { get; private set; }
public float Power { get; set; }
public float Speed { get; set; }
public Horse(IBag bag)
{
Bag = bag;
Power = 10;
Speed = 10;
}
public T Compute<T>(IComputeWeigth<T> compute)
{
return compute.Compute(this);
}
}
public class HorsePower : IHorsePower
{
public void Compute(float power, float speed, float weight)
{
Power = power - weight / power;
Speed = speed - weight / power;
}
public float Speed { get; private set; }
public float Power { get; private set; }
}
}