-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathController.cs
85 lines (78 loc) · 2.51 KB
/
Controller.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
using System;
namespace DepthCharge
{
/// <summary>
/// Contains functions for reading input from the user.
/// </summary>
static class Controller
{
/// <summary>
/// Retrives a dimension for the play area from the user.
/// </summary>
/// <remarks>
/// Note that the original BASIC version would allow dimension values
/// of 0 or less. We're doing a little extra validation here in order
/// to avoid strange behaviour.
/// </remarks>
public static int InputDimension()
{
View.PromptDimension();
while (true)
{
if (!Int32.TryParse(Console.ReadLine(), out var dimension))
View.ShowInvalidNumber();
else
if (dimension < 1)
View.ShowInvalidDimension();
else
return dimension;
}
}
/// <summary>
/// Retrieves a set of coordinates from the user.
/// </summary>
/// <param name="trailNumber">
/// The current trail number.
/// </param>
public static (int x, int y, int depth) InputCoordinates(int trailNumber)
{
View.PromptGuess(trailNumber);
while (true)
{
var coordinates = Console.ReadLine().Split(',');
if (coordinates.Length < 3)
View.ShowTooFewCoordinates();
else
if (coordinates.Length > 3)
View.ShowTooManyCoordinates();
else
if (!Int32.TryParse(coordinates[0], out var x) ||
!Int32.TryParse(coordinates[1], out var y) ||
!Int32.TryParse(coordinates[2], out var depth))
View.ShowInvalidNumber();
else
return (x, y, depth);
}
}
/// <summary>
/// Retrieves the user's intention to play again (or not).
/// </summary>
public static bool InputPlayAgain()
{
View.PromptPlayAgain();
while (true)
{
switch (Console.ReadLine())
{
case "Y":
return true;
case "N":
return false;
default:
View.ShowInvalidYesOrNo();
break;
}
}
}
}
}