-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
245 lines (212 loc) · 9.18 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WorldWarBass.Abstraction;
using WorldWarBass.State;
using WorldWarBass.CountryAbstractions;
namespace WorldWarBass.Main;
/// <summary>
/// The main class, the entry point for the game.
/// </summary>
public class WorldWarBass : Game {
/// <summary>
/// The Command Names and their descriptions.
/// </summary>
Dictionary<string, string> commandNames;
/// <summary>
/// A constructor to make a new WorldWarBass object.
/// </summary>
/// <param name="gameState">The GameState for the new game.</param>
public WorldWarBass(GameState gameState) : base(gameState) {
commandNames = new Dictionary<string, string>
{
{ "ATTACK", "Attacks a country of your choosing." },
{ "INVITE", "Invites a country of your choosing to join your alliance." },
{ "RULES", "Displays the rules of the game." },
{ "HELP", "Displays the commands you can use." },
{ "QUIT", "Quits the game." }
};
}
/// <summary>
/// Starts the game.
/// </summary>
public override void Start() {
this._isRunning = true;
while(this._isRunning) {
this.Update();
}
}
/// <summary>
/// Updates the game.
/// </summary>
public override void Update() {
// Displays the Commands to the user, then gets the command from the user as a string.
DisplayUI();
string? command = GetCommand();
// Switches on the command, and does the appropriate action.
switch (command.ToUpper()) {
case "ATTACK":
AttackInfo at = this.GetAttackInfo();
if (at._isSuccessful) {
this._gameState.GetPlayerCountry().Attack(at._countryToAttack);
} else {
Console.WriteLine("You failed to attack " + at._countryToAttack.GetName() + "!");
}
break;
case "INVITE":
Console.WriteLine("You chose to invite a country to join your alliance.");
break;
case "RULES":
Console.WriteLine("You chose to see the rules.");
break;
case "HELP":
Console.WriteLine("You chose to see the commands.");
break;
case "QUIT":
Console.WriteLine("You chose to quit the game.");
this._isRunning = false;
break;
default:
Console.WriteLine("Invalid command. Please try again.");
break;
}
}
/// <summary>
/// Displays the UI for entering commmands for the game.
/// </summary>
private void DisplayUI() {
Console.Write("Commands are as follows:\n");
foreach (KeyValuePair<string, string> commandName in commandNames) {
Console.WriteLine(commandName.Key + ": " + commandName.Value);
}
Console.Write("What would you like to do? ");
}
/// <summary>
/// Gets the command from the user.
/// </summary>
/// <returns>the command that the user entered as a string.</returns>
private string? GetCommand() {
string? command = Console.ReadLine();
if (command == null) {
Console.WriteLine("Invalid command. Please try again.");
GetCommand();
}
if (commandNames.ContainsKey(command.ToUpper())) {
return command.ToUpper();
}
return "";
}
// Gosh this is a terrible way to do this, I realized it halfway through implementing it but idk it's cool so im leaving it, very oop
// TODO: Make this not terrible
/// <summary>
/// Gets the attack info from the user.
/// </summary>
/// <returns>A new AttackInfo struct with the Country the player wishes to attack, and if the attack is successful.</returns>
private AttackInfo GetAttackInfo() {
Console.WriteLine($"Which apposing country would you like to attack?");
string? countryToAttack = Console.ReadLine();
if (countryToAttack == null) {
Console.WriteLine("Null country name. Please try again.");
GetAttackInfo();
}
return new AttackInfo(new Country(this.ConvertStringToCountryName(countryToAttack)), this._gameState.GetPlayerCountry(), this._gameState.GetPlayerCountry().CanAttack(new Country(this.ConvertStringToCountryName(countryToAttack))));
}
/// <summary>
/// Converts a string to a CountryName.
/// </summary>
/// <param name="input">a string input of a name of a country.</param>
/// <returns>a new CountryName enum based on the string input.</returns>
private CountryName ConvertStringToCountryName(string? input) {
if (input == null) {
return CountryName.NULL;
}
switch (input.ToUpper()) {
case "UNITED STATES":
return CountryName.UnitedStates;
case "UNITED KINGDOM":
return CountryName.UnitedKingdom;
case "FRANCE":
return CountryName.France;
case "GERMANY":
return CountryName.Germany;
case "RUSSIA":
return CountryName.Russia;
default:
return CountryName.NULL;
}
}
}
public static class Client {
// Goal is to literally just make WorldWarBass obj and call start on it.
public static void Main(string[] args) {
Console.WriteLine("Welcome to World War Bass!");
Console.Write("Before we get started, what country are you playing as?\nUnited States\nUnited Kingdom\nFrance\nGermany\nRussia\nType the name of the country you want to play as: ");
CountryName picked = PickCountryName();
Console.Write("One last thing before getting started, if you want to see the rules, type \"RULES\", otherwise, type \"START\" to start the game.\n[START or RULES]:");
// I should pull this into a method, however I want the actual prompt method to be all purpose, not just start and rules.
string? startOrRules = Console.ReadLine();
while (startOrRules != "START") {
if (startOrRules == "START") {
break;
}
if (startOrRules.ToUpper() == "RULES") {
Console.WriteLine("RULES:\n\nThe goal of the game is to conquer the world. You do this by conquering other countries.\nYou can conquer other countries by invading them, and removing all of their troops.");
}
if (startOrRules == null) {
Console.WriteLine("Invalid input. Please try again.");
}
Console.Write("[START or RULES] :");
startOrRules = Console.ReadLine();
}
Console.WriteLine("Starting game, good luck conquerer!");
Console.Clear();
new WorldWarBass(new GameState(picked)).Start();
}
/// <summary>
/// Recursively asks the user for a country name until they give a valid one.
/// </summary>
/// <returns>the CountryName based off of what the player typed in as a string.</returns>
public static CountryName PickCountryName() {
string? countryNameStr = Console.ReadLine();
if (countryNameStr == null) {
Console.WriteLine("Invalid country name. Please try again.");
PickCountryName();
}
CountryName countryName = ConvertStringToCountryName(countryNameStr);
if (countryName == CountryName.NULL) {
Console.WriteLine("Invalid country name. Please try again.");
PickCountryName();
}
return countryName;
}
/// <summary>
/// Converts a string to a CountryName.
/// </summary>
/// <param name="countryNameStr"></param>
/// <returns>the CountryName from the string</returns>
private static CountryName ConvertStringToCountryName(string? countryNameStr) {
if (countryNameStr == null) {
return CountryName.NULL;
}
switch (countryNameStr) {
case "united states":
case "United States":
return CountryName.UnitedStates;
case "United Kingdom":
case "united kingdom":
return CountryName.UnitedKingdom;
case "France":
case "france":
return CountryName.France;
case "Germany":
case "germany":
return CountryName.Germany;
case "Russia":
case "russia":
return CountryName.Russia;
default:
return CountryName.NULL;
}
}
}