summaryrefslogtreecommitdiff
path: root/src/Controllers/ConsoleController/ConsoleController.cpp
blob: bcad8735755311a125aeadaaa953ac835da847fd (plain)
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
#include "./ConsoleController.hpp"
#include <SFML/Window/Keyboard.hpp>
#include "../../Helpers/Keyboard.hpp"
#include "../../Model/Stats.hpp"

//==================== Constructor and Destructor  ====================

//Constructor
ConsoleController::ConsoleController()
{
}

//Destructor
ConsoleController::~ConsoleController()
{
}

//==================== Helpers  ====================

//Run the game.
void ConsoleController::run()
{

	//Init keyPress
	kbdh::Direction keyPress;

	//Intruction msg
	std::cout << "Use arrows to play !" << std::endl;

	//Pop a random number on the grid
	m_game.popRandomNumber();

	//First cout stats
	this->coutStats(m_game.getStats());

	//First cout grid
	m_game.coutGrid();


	//Start game
	while (!m_game.isOver())
	{
		//Get key press
		keyPress=this->waitArrowKeyPress();

		//Apply move
		bool moveDone=m_game.swipe(keyPress);

		//Cout stats
		this->coutStats(m_game.getStats());

		//Cout grid
		m_game.coutGrid();

	}

	//Last cout stats
	this->coutStats(m_game.getStats());

	//Last cout grid
	m_game.coutGrid();
}




//Wait for keypress and return the keyPress.
kbdh::Direction ConsoleController::waitArrowKeyPress()
{
	//Initialise keyPress
	kbdh::Direction keyPress;

	//White space to remove arrows print by the terminal
	std::string spaces="                                            ";

	//Wait for keypress
	while(1){
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
		{
			keyPress=kbdh::Left;
			while(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
			{
				//Wait for release and try to remove arrow printed characters
				std::cout << "\r" << spaces;
			}
			break;
		}
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
		{
			keyPress=kbdh::Right;
			while(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
			{
				//Wait for release and try to remove arrow printed characters
				std::cout << "\r" << spaces;
			}
			break;
		}
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
		{
			keyPress=kbdh::Up;
			while(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
			{
				//Wait for release and try to remove arrow printed characters
				std::cout << "\r" << spaces;
			}
			break;
		}
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
		{
			keyPress=kbdh::Down;
			while(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
			{
				//Wait for release and try to remove arrow printed characters
				std::cout << "\r" << spaces;
			}
			break;
		}
	}

	return keyPress;
}


//Cout the stats of the game
void ConsoleController::coutStats(Stats stats){

	std::cout << std::endl << "Score : " << stats.getScore() << std::endl;
	std::cout << "Nombre de coups : " << stats.getNbMove() << std::endl;
}