-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.cpp
126 lines (108 loc) · 3.21 KB
/
View.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
119
120
121
122
123
124
125
126
#include "View.h"
#include "Presenter.h"
#include <iostream>
#include <sstream>
#define SECTION "---------------------------------------"
#define INT_MAX 2147483647
#define ERROR "Invalid input, please try again following the instructions..."
View::View()
{
m_Presenter = new Presenter(this);
showMainMenu();
}
View::~View()
{
}
void View::header(std::string actualSection)
{
system("CLS");
showTextWithSection("\nMINIGAMES - " + actualSection);
if (actualSection == "Main Menu")
{
}
else
{
showText(SECTION);
showTextWithSection("Press 4 to return to Main Menu");
}
}
void View::showMainMenu()
{
header("Main Menu");
showTextWithSection("\nPLEASE SELECT THE GAME YOU WANT TO PLAY:");
showText("1) Tic-Tac-Toe");
showText("2) Dices");
showText("3) Poker");
showText("4) Show players balance");
showTextWithSection("5) Exit");
int choice = 1;
selectOption(choice, 1, 5);
if (choice >= 1 && choice < 3) // When other games are finished, change this condition for choice >= 1 && choice < 4
{
system("CLS");
beginPlay(choice);
}
else if (choice == 3)
{
showTextWithSection("Not avaible... We are working to bring you Poker in short time.");
std::cin.get();
std::cin.get();
}
else if (choice == 4)
{
showText("Player 1 wins --> " + parseNum(m_Presenter->showPlayersBalance(1)));
showTextWithSection("Player 2 wins --> " + parseNum(m_Presenter->showPlayersBalance(2)));
std::cin.get();
std::cin.get();
}
else if (choice == 5)
exit(0);
showMainMenu(); // Return to the menu when the user exits the current game (instance of the game is deleted)
}
void View::selectOption(int& input, int min, int max) // Allows the user to enter an input and check if its valid.
{
bool mainMenu = false;
if (input == 1) // If the input is initialized with 1, this means that is being called from the mainMenu.
mainMenu = true;
do
{
std::cin >> input;
std::cin.clear();
std::cin.ignore(INT_MAX, '\n'); // This prevents the user from enter any other type of value that is not int, but to work needs to enter the value 2 times.
if (input < min || input > max)
showTextWithSection(ERROR);
} while (input < min || input > max && max != 0 || std::cin.fail());
if (input == max && !mainMenu) // This way the max value of the main menu allows to quit the app.
showMainMenu();
}
void View::beginPlay(int gameSelected)
{
showTextWithConfirmation(m_Presenter->showInstructions(gameSelected));
system("CLS");
m_Presenter->sendGameSelected(gameSelected);
}
void View::showText(const std::string& text)
{
std::cout << text << std::endl;
}
void View::showTextWithSection(const std::string& text)
{
std::cout << text << std::endl;
std::cout << SECTION << std::endl;
}
void View::showTextWithConfirmation(const std::string& text)
{
int confirm = 0;
std::cout << text << std::endl;
std::cout << SECTION << std::endl;
std::cout << "1) Start playing" << std::endl;
std::cout << "2) Go back to Main Menu" << std::endl;
std::cout << SECTION << std::endl;
selectOption(confirm, 1, 2);
}
std::string View::parseNum(double value)
{
std::ostringstream aux;
aux << value; // Take the values of variable value and it change them into a char array
return aux.str(); // Transforms the char array into a string and returns it
}