-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
146 lines (116 loc) · 2.3 KB
/
Copy pathSource.cpp
File metadata and controls
146 lines (116 loc) · 2.3 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
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
constexpr int FIELD_WIDTH = 10;
constexpr int FIELD_HEIGHT = 10;
int g_treasureX;
int g_treasureY;
int g_pirateX = 0,
int g_pirateY = 0;
void Init();
void Greeting();
void MainLoop();
void MoveLeft();
void MoveRight();
void MoveUp();
void MoveDown();
bool CheckWin();
int main() {
Init();
Greeting();
MainLoop();
system("pause");
return 0;
}
void MoveLeft() {
g_pirateX--;
if (g_pirateX < 0) {
g_pirateX = FIELD_WIDTH;
}
}
void MoveRight() {
g_pirateX++;
if (g_pirateX > FIELD_WIDTH) {
g_pirateX = 0;
}
}
void MoveUp() {
g_pirateY++;
if (g_pirateY > FIELD_HEIGHT) {
g_pirateY = 0;
}
}
void MoveDown() {
g_pirateY--;
if (g_pirateY < 0) {
g_pirateY = FIELD_HEIGHT;
}
}
void MainLoop() {
char inputDirections;
bool isGameRunning = true;
while (isGameRunning)
{
cout << "Choose your direction: ";
cin >> inputDirections;
switch (inputDirections)
{
case 'w':
{
MoveUp();//pirateY = (10 + pirateY) % 10;
break;
}
case 'a':
{
MoveLeft();
break;
}
case 's':
{
MoveDown();
break;
}
case 'd':
{
MoveRight();
break;
}
case 'q':
{
cout << "Are you tired? Understand. See you." << endl;
isGameRunning = false;
continue;
}
default:
break;
}
cout << endl << ", your are at [" << g_pirateX << ", " << g_pirateY << "]" << endl;
isGameRunning = !CheckWin();
}
}
bool CheckWin() {
if (g_treasureX == g_pirateX && g_treasureY == g_pirateY) {
cout << "You've found the treasure!" << endl;
return true;
}
else {
cout << "Not here, looser" << endl;
cout << "Treasure is here: " << g_treasureX << ", " << g_treasureY << endl;
}
return false;
}
void Init() {
//srand(unsigned(std::time(0)));
g_treasureX = rand() % FIELD_WIDTH;
g_treasureY = rand() % FIELD_HEIGHT;
}
void Greeting() {
std::string pirateName;
cout << "Hello, stranger! What is your name? : ";
cin >> pirateName;
cout << "Welcome on island, " << pirateName << endl;
cout << "You can walk around using 'W', 'S', 'A', 'D' keys\n";
cout << "Treasure is here: " << g_treasureX << ", " << g_treasureY << endl;
}