-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
185 lines (166 loc) · 3.17 KB
/
game.cpp
File metadata and controls
185 lines (166 loc) · 3.17 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "game.h"
/*
int Game::*move[4]() =
{
Game::move_up,
Game::move_down,
Game::move_right,
Game::move_left,
};*/
char Game::get_input()
{
char output;
while (!_kbhit())
{
//Force keyboard hit.
}
output = _getch();
if (output == 224)
{
return _getch(); // Two calls for arrow-key input.
}
return output;
}
void Game::apply_effect(Element e)
{
char input = e.get_display();
switch (input)
{
case '*':
score.health(-10);
break;
case 1:
case 0:
default:
score.health(5);
}
}
int Game::calculate_move(int x, int y)
{
int playerx = user.get_x();
int playery = user.get_y();
if (game_board.valid_move(playerx, playery, x, y))
{
apply_effect(game_board.get_element(playerx + x, playery + y));
game_board.destroy_board_at(playerx + x, playery + y);
game_board.move_object_to(playerx, playery, playerx + x, playery + y);
user.move(x, y);
score.piece();
return 1;
}
return 0;
}
int Game::move_up()
{
return calculate_move(-1, 0);
}
int Game::move_down()
{
return calculate_move(1, 0);
}
int Game::move_left()
{
return calculate_move(0,-1);
}
int Game::move_right()
{
return calculate_move(0,1);
}
void Game::game_init()
{
/* Player Location Set */
int user_x = user.get_x(), user_y = user.get_y();
Player p(user_x, user_y);
game_board.destroy_board_at(user_x, user_y);
game_board.add_object(user_x, user_y, p);
//Deprecated Movement
/* Movement Array Initialization*/
/*
move_player[0] = &Game::move_up;
move_player[1] = &Game::move_down;
move_player[2] = &Game::move_right;
move_player[3] = &Game::move_left;
move_player[4] = NULL;*/
}
Game::Game()
{
game_init();
/* Initialize Score */
// Do this last to start the game clock.
//score = Score(game_board.get_width(), game_board.get_height());
bool moved;
/*while (true)
{
std::cout << (int)get_input();
}*/
while (!over)
{
moved = 0;
std::cout << game_board << std::endl << "HP: " << score.get_health() << std::endl;
input = get_input();
switch (input)
{
case 'w':
case 'H':
(moved = move_up()); // move_player[0];
break;
case 's':
case 'P':
(moved = move_down()); // move_player[1];
break;
case 'd':
case 'M':
(moved = move_right()); // move_player[2];
break;
case 'a':
case 'K':
(moved = move_left()); // move_player[3];
break;
case 'p':
std::cout << score;
system("PAUSE");
break;
default:
break;
}
over = apply_score(moved);
system("CLS");
}
std::cout << game_board << std::endl << "\nThe Game has ended. . .\n\n";
get_input();
system("CLS");
std::cout << score;
get_input();
std::cout << "Please enter any value to continue . . .";
std::cin >> over;
}
Game::~Game()
{
}
int Game::apply_score(int moved)
{
score.turn();
if (moved)
{
score.moved();
if (score.get_remaining() == 0 || score.get_health() <= 0)
{
return 1;
}
return in_corner();
}
return 0;
}
int Game::in_corner()
{
int playerx = user.get_x();
int playery = user.get_y();
if (!(game_board.valid_move(playerx, playery, 1, 0) ||
game_board.valid_move(playerx, playery, -1, 0) ||
game_board.valid_move(playerx, playery, 0, 1) ||
game_board.valid_move(playerx, playery, 0, -1)))
{
return 1;
}
return 0;
}