-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.cpp
More file actions
88 lines (81 loc) · 2.77 KB
/
Menu.cpp
File metadata and controls
88 lines (81 loc) · 2.77 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
87
88
/*********************************************************************
** Program name: Menu.cpp
** Author: Sarah Flower
** Date: October 2, 2018
** Description: This is the class implementation file for Menu. This
** allows for inputting menu choices and checks that user input
** is valid (integers).
*********************************************************************/
#include "Menu.hpp"
#include "Board.hpp"
#include <algorithm>
#include <climits>
/*********************************************************************
** Default constructor for Menu takes no arguments.
*********************************************************************/
Menu::Menu()
{
}
/*********************************************************************
** displayMenu() prints the Menu to the console.
*********************************************************************/
void Menu::displayMenu()
{
for (unsigned int i = 0; i < menuVector.size(); i++)
{
std::cout << menuVector[i] << std::endl;
}
std::cout << "\n";
std::cout << "Steps: ";
}
/*********************************************************************
** addOption(string) allows the user to add options to the Menu
** vector, which holds all menu options.
*********************************************************************/
void Menu::addOption(std::string option)
{
menuVector.push_back(option);
}
/*********************************************************************
** getChoice() takes the user input and verifies it is valid using
** std::stoi, which ignores whitespace and takes the first valid
** integer input as its value.
*********************************************************************/
int Menu::getChoice()
{
while(1)
{
std::getline(std::cin,input);
// Checks all of input string to identify whether it is an integer
if ((std::all_of(input.begin(), input.end(), ::isdigit)) && !input.empty())
{
return std::stoi(input);
}
// If something other than integer is entered, give user an error
else
{
std::cout << "Invalid entry. Re-enter your choice." << std::endl;
}
}
}
void Menu::run()
{
Menu menu;
menu.addOption("Predator-Prey Simulation");
menu.addOption("By Group 10: Jake Anderson, Sarah Flower, Jordan Hamilton, John Quilty, Dan Tonthat ");
menu.addOption("");
menu.addOption("Enter the number of steps to run the Doodlebug/Ant simulation.");
menu.displayMenu();
int numSteps = menu.getChoice();
for (int i = 0; i < numSteps; i++)
{
Board board;
std::cout << "Steps: "<<i + 1 << std::endl;
board.printBoard();
board.moveAnts();
board.moveDoodlebugs();
board.starve();
board.breedAnts();
board.breedDoodlebugs();
}
}