-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameEngine.cs
190 lines (177 loc) · 6.03 KB
/
GameEngine.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
using System;
using System.Linq;
namespace BullsAndCowsGame
{
public class GameEngine
{
const int MAX_COW_VARIATIONS = 10;
private string playerInput = null;
private string generatedNumber = null;
private bool isGameFinished = false;
private int attempts = 0;
private int cheats = 0;
public GameEngine()
{
}
public void Start()
{
PlayerCommand enteredCommand;
do
{
ConsolePrinter.PrintWelcomeMessage();
this.generatedNumber = NumberGenerator.GenerateNumber();
this.isGameFinished = false;
do
{
Console.Write("Enter your guess or command: ");
playerInput = Console.ReadLine();
enteredCommand = CommandReader.ReadPlayerInput(playerInput);
ExecuteCommand(enteredCommand);
}
while (enteredCommand != PlayerCommand.Exit &&
enteredCommand != PlayerCommand.Restart &&
this.isGameFinished != true);
}
while (enteredCommand != PlayerCommand.Exit);
Console.WriteLine("\nGood bye!");
}
private void ExecuteCommand(PlayerCommand command)
{
switch (command)
{
case PlayerCommand.Top:
{
ScoreBoard.Instance.Print();
break;
}
case PlayerCommand.Restart:
{
PlayerHelper.ClearHelp();
this.cheats = 0;
this.attempts = 0;
Console.WriteLine();
break;
}
case PlayerCommand.Help:
{
this.cheats = PlayerHelper.PrintHelp(cheats, generatedNumber);
break;
}
case PlayerCommand.Exit:
{
break;
}
case PlayerCommand.Other:
if (IsValidInput())
{
ProcessGame();
}
else
{
ConsolePrinter.PrintWrongCommandMessage();
}
break;
default:
{
throw new InvalidOperationException("Invalid Input Command!");
}
}
}
private void ProcessGame()
{
bool[] isBull = new bool[generatedNumber.Length];
CheckPlayerInputForBull(isBull);
int bullsCount = CallculateBullsCount(isBull);
int cowsCount = CallculateCowsCount(isBull);
this.attempts++;
if (bullsCount == generatedNumber.Length)
{
ConsolePrinter.PrintCongratulateMessage(attempts, cheats);
FinishGame();
PlayerHelper.ClearHelp();
this.isGameFinished = true;
this.cheats = 0;
this.attempts = 0;
}
else
{
Console.WriteLine("Wrong number! Bulls: {0}, Cows: {1}", bullsCount, cowsCount);
}
}
private void CheckPlayerInputForBull(bool[] isBull)
{
for (int i = 0; i < this.playerInput.Length; i++)
{
if (this.playerInput[i] == this.generatedNumber[i])
{
isBull[i] = true;
}
}
}
private int CallculateBullsCount(bool[] isBull)
{
int bullsCount = 0;
for (int i = 0; i < isBull.Length; i++)
{
if (isBull[i] == true)
{
bullsCount++;
}
}
return bullsCount;
}
private int CallculateCowsCount(bool[] isBull)
{
int cowsCount = 0;
bool[] isNumberACow = new bool[MAX_COW_VARIATIONS];
for (int i = 0; i < this.playerInput.Length; i++)
{
for (int j = 0; j < this.generatedNumber.Length; j++)
{
int checkedDigit = int.Parse(this.generatedNumber[i].ToString());
if ((this.playerInput[i] == this.generatedNumber[j]) && (!isBull[i]) && (!isNumberACow[checkedDigit]))
{
cowsCount++;
isNumberACow[checkedDigit] = true;
}
}
}
return cowsCount;
}
private bool IsValidInput()
{
if (playerInput == String.Empty || playerInput.Length != generatedNumber.Length)
{
return false;
}
for (int i = 0; i < playerInput.Length; i++)
{
char currentChar = playerInput[i];
if (!Char.IsDigit(currentChar))
{
return false;
}
}
return true;
}
private void FinishGame()
{
if (cheats == 0)
{
Console.Write("Please enter your name for the top scoreboard: ");
string playerName = Console.ReadLine();
while (playerName == null || string.IsNullOrWhiteSpace(playerName))
{
Console.WriteLine("Invalid name!\nEnter valid name");
playerName = Console.ReadLine();
}
ScoreBoard.Instance.AddPlayer(playerName, attempts);
ScoreBoard.Instance.Print();
}
else
{
Console.WriteLine("You are not allowed to enter the top scoreboard.");
}
}
}
}