-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
118 lines (90 loc) · 4.24 KB
/
main.cpp
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
#include <iostream>
#include "Game.h"
#include "DEFS.h"
void displayWelcomeMessage(); // Displays the welcome message
int requestValidInput(int, int); // Keeps asking the user for an input until a valid one is given
GameMode getUserGameModeChoice(); // Asks the user to choose the game mode and returns the choice
std::string getUserInputFileChoice(); // Asks the user to choose the input file and returns the choice
std::string getUserOutputFileChoice(); // Asks the user to choose the output file and returns the choice
int main()
{
// Seed the random number generator
srand((unsigned) time(NULL));
// Display welcome messages
displayWelcomeMessage();
// Get user's choice for game mode
GameMode gameMode = getUserGameModeChoice();
// Get user's choice for input file
std::string inputFileName = getUserInputFileChoice();
// Get user's choice for output file
std::string outputFileName = getUserOutputFileChoice();
// Start the game (made on the heap to avoid stack overflow since the game object is large)
Game* game = new Game;
game->run(gameMode, "InputFiles/" + inputFileName + ".txt", outputFileName + ".txt");
// Delete the game object
delete game;
return 0;
}
void displayWelcomeMessage()
{
std::cout << std::endl << "==================== ALIEN INVASION ===========================" << std::endl;
std::cout << "================= Let the battle begin! =======================" << std::endl;
}
int requestValidInput(int minChoice, int maxChoice)
{
int userInput = 0;
// Generate the options string
std::string options = "(";
for (int i = minChoice; i < maxChoice; i++)
options += std::to_string(i) + "/";
options += std::to_string(maxChoice) + ")";
// Ask the user for input
std::cout << "Enter your choice " << options << ": ";
// Keep asking the user for input until a valid one is given
while (!(std::cin >> userInput) || userInput < minChoice || userInput > maxChoice)
{
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "Invalid input. Please enter a number from " << options << ": ";
}
// Clear the input buffer
std::cin.ignore();
return userInput;
}
GameMode getUserGameModeChoice()
{
std::cout << std::endl;
std::cout << "How do you want to witness humanity's last stand?" << std::endl;
std::cout << "[1] Interactive Mode --> Do you want the aliens to know you're watching? Press enter for a timestep by step detailed results" << std::endl;
std::cout << "[2] Silent Mode --> Silent stealth mission, only output file is generated" << std::endl;
// Get user choice
int userChoice = requestValidInput(1, 2);
return static_cast<GameMode>(userChoice - 1);
}
std::string getUserInputFileChoice()
{
const int numInputFiles = 6;
std::string inputFiles[] = { "strong_earth_strong_aliens", "strong_earth_moderate_aliens", "strong_earth_weak_aliens",
"weak_earth_weak_aliens", "weak_earth_moderate_aliens", "weak_earth_strong_aliens" };
std::cout << std::endl;
std::cout << "Choose the input file:" << std::endl;
std::cout << "[1] Strong Earth and Aliens --> Earth and Aliens have a stronger army in front of each other" << std::endl;
std::cout << "[2] Strong Earth and Moderate Aliens --> Earth has a stronger army in front of moderate Aliens" << std::endl;
std::cout << "[3] Strong Earth and Weak Aliens --> Earth has a stronger army in front of weak Aliens" << std::endl;
std::cout << "[4] Weak Earth and Aliens --> Earth and Aliens have a weaker army in front of each other" << std::endl;
std::cout << "[5] Weak Earth and Moderate Aliens --> Earth has a weaker army in front of moderate Aliens" << std::endl;
std::cout << "[6] Weak Earth and Strong Aliens --> Earth has a weaker army in front of strong Aliens" << std::endl;
// Get user choice
int userChoice = requestValidInput(1, numInputFiles);
return inputFiles[userChoice - 1];
}
std::string getUserOutputFileChoice()
{
std::string outputFileName;
std::cout << std::endl;
std::cout << "Enter the name of the output file (e.g. BattleResults): ";
std::cin >> outputFileName;
// Clear the input buffer
std::cin.ignore();
return outputFileName;
}