Repository navigation
Expand file tree
/
Copy pathGame.cs
More file actions
86 lines (72 loc) · 2.34 KB
/
Copy pathGame.cs
File metadata and controls
86 lines (72 loc) · 2.34 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
using BugGame.Parts;
using BugGame.Resources;
using Games.Common.IO;
using Games.Common.Randomness;
using static System.StringComparison;
namespace BugGame;
internal class Game
{
private readonly IReadWrite _io;
private readonly IRandom _random;
public Game(IReadWrite io, IRandom random)
{
_io = io;
_random = random;
}
public void Play()
{
_io.Write(Resource.Streams.Introduction);
if (!_io.ReadString("Do you want instructions").Equals("no", InvariantCultureIgnoreCase))
{
_io.Write(Resource.Streams.Instructions);
}
BuildBugs();
_io.Write(Resource.Streams.PlayAgain);
}
private void BuildBugs()
{
var yourBug = new Bug();
var myBug = new Bug();
while (true)
{
var partAdded = TryBuild(yourBug, m => m.You);
Thread.Sleep(500);
_io.WriteLine();
partAdded |= TryBuild(myBug, m => m.I);
if (partAdded)
{
if (yourBug.IsComplete) { _io.WriteLine("Your bug is finished."); }
if (myBug.IsComplete) { _io.WriteLine("My bug is finished."); }
if (!_io.ReadString("Do you want the picture").Equals("no", InvariantCultureIgnoreCase))
{
_io.Write(yourBug.ToString("Your", 'A'));
_io.WriteLine();
_io.WriteLine();
_io.WriteLine();
_io.WriteLine();
_io.Write(myBug.ToString("My", 'F'));
}
}
if (yourBug.IsComplete || myBug.IsComplete) { break; }
}
}
private bool TryBuild(Bug bug, Func<Message, string> messageTransform)
{
var roll = _random.Next(6) + 1;
_io.WriteLine(messageTransform(Message.Rolled.ForValue(roll)));
IPart part = roll switch
{
1 => new Body(),
2 => new Neck(),
3 => new Head(),
4 => new Feeler(),
5 => new Tail(),
6 => new Leg(),
_ => throw new Exception("Unexpected roll value")
};
_io.WriteLine($"{roll}={part.GetType().Name}");
var partAdded = bug.TryAdd(part, out var message);
_io.WriteLine(messageTransform.Invoke(message));
return partAdded;
}
}