-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
249 lines (216 loc) · 10.1 KB
/
index.php
File metadata and controls
249 lines (216 loc) · 10.1 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
class Room {
public static $coordinates_let = [[5,23],[6,23],[4,23],[6,22],[5,22],[4,22],[6,21],[5,21],[4,21],[5,20]];
public static $coordinates_jewel = [[16,22],[15,22],[16,21],[15,21],[16,20],[15,20],[14,22],[14,21],[14,20],[16,19],[15,19],[14,19],[16,18],[15,18],[14,18]];
private $room = [
0 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#|', ' ', ' ', ' ', ' ', ' ', ' '],
1 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#|', ' ', ' ', ' ', ' ', ' ', ' '],
2 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', ' ', ' ', '^', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#|', ' ', ' ', ' ', ' ', ' ', ' '],
3 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', ' ', '(', '@', ')', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#|', ' ', ' ', ' ', ' ', ' ', ' '],
4 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', ' ', ' ', '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
5 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', ' ', '/', '|', '\\', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
6 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', ' ', ' ', '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
7 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', ' ', '/', ' ', '\\ ', '', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#|', ' ', ' ', ' ', ' ', ' ', ' '],
8 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#|', ' ', ' ', ' ', ' ', ' ', ' '],
9 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#|', ' ', ' ', ' ', ' ', ' ', ' '],
10 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#|', ' ', ' ', ' ', ' ', ' ', ' '],
11 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#|', ' ', ' ', ' ', ' ', ' ', ' '],
12 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#|', ' ', ' ', ' ', ' ', ' ', ' '],
13 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#|', ' ', ' ', ' ', ' ', ' ', ' '],
14 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#|', ' ', ' ', ' ', ' ', ' ', ' '],
15 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#|', ' ', ' ', ' ', ' ', ' ', ' '],
16 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#|', ' ', ' ', ' ', ' ', ' ', ' '],
17 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#|', ' ', ' ', ' ', ' ', ' ', ' '],
18 => [' ', ' ', ' ', ' ', ' ', ' ', '|#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#|', ' ', ' ', ' ', ' ', ' ', ' '],
];
// дополнительные предметы в комнате, например, сокровища или препятствия
public function setRoom($coordinates, $count, $cell) {
for ($i = 0; $i < $count; $i++){
$position = self::${$coordinates}[$i];
$this->room[$position[0]][$position[1]] = $cell;
}
}
public function getRoom() {
return $this->room;
}
}
abstract class Builder {
protected $room;
protected $count;
public function __construct($count = '')
{
$this->count = $count;
}
final public function getRoom() {
return $this->room;
}
public function buildRoom() {
$this->room = new Room();
}
}
class EmptyRoom extends Builder {
public function buildRoom() {
parent::buildRoom();
}
}
class JewelRoom extends Builder {
private $cell = '+';
public function buildRoom() {
parent::buildRoom();
$this->room->setRoom('coordinates_jewel', $this->count, $this->cell);
}
}
class LetRoom extends Builder {
private $cell = '=';
public function buildRoom() {
parent::buildRoom();
$this->room->setRoom('coordinates_let', $this->count, $this->cell);
}
}
class Factory {
private $builder;
public function __construct(Builder $builder) {
$this->builder = $builder;
$this->builder->buildRoom();
}
public function getRoom() {
return $this->builder->getRoom();
}
}
class Game {
private static $room = 0; // вид комнаты
private static $count = 0; // величина сокровища или препятствия
private static $gameField = []; // координаты для построения
private static $mark = 0; // количество баллов
private static $time = 0; // время между выстрелами
public static function init() {
self::$mark = 50;
self::$time = 0;
self::runGame();
}
private static function showTitle() {
self::clearScreen();
echo <<<EOD
=========================================================
Для окончания игры нажмите *.
=========================================================
EOD;
}
private static function runGame() {
self::startNewGame();
self::drawField();
self::makeMove(true);
}
private static function continueGame() {
self::drawField();
self::makeMove(true);
}
private static function startNewGame() {
self::$room = rand(0,2);
if(self::$room == 1){
// комната с сокровищем
self::$count = rand(5,15);
$room = new Factory(new JewelRoom(self::$count));
}else if(self::$room == 2){
// комната с препятствием
self::$count = rand(2,10);
$room = new Factory(new LetRoom(self::$count));
}else{
$room = new Factory(new EmptyRoom());
}
self::$gameField = $room->getRoom()->getRoom();
}
private static function makeMove($isUserMove) {
// допустимые команды
if(in_array($isUserMove, ['*',1,2,3])){
$commands = ['1 - перейти дальше', '1 - перейти дальше, 2 - взять сокровище', '3 - стрелять'];
$index = self::$room == 2 && self::$count == 0 ? 0 : self::$room;
echo "\nКоличество очков: " . self::$mark. " Выберите действие: {$commands[$index]} ";
$userChoice = $isUserMove === true ? substr(fgets(STDIN), 0, 1) : '';
switch ($userChoice) {
case '*':
self::gameOver();
case 1:
// перейти дальше
if(self::$room !== 2 || self::$count == 0) {
self::runGame();
}else{
self::continueGame();
}
break;
case 2:
//получить сокровище
if(self::$room == 1){
self::$mark += self::$count;
self::runGame();
}else{
self::continueGame();
}
break;
case 3:
// стрелять
if(self::$room == 2 && self::$count > 0){
if(self::$time > 0){
self::$mark -= time() - self::$time;
}
self::$time = time();
$position = Room::$coordinates_let[self::$count - 1];
self::$gameField[$position[0]][$position[1]] = ' ';
self::$count--;
}
if(self::$count == 0){
self::$time = 0;
}
if(self::$mark <= 0){
self::gameOver(0);
}
self::continueGame();
break;
}
self::makeMove(!$isUserMove);
}else{
self::continueGame();
}
}
private static function drawField() {
self::clearScreen();
self::showTitle();
echo "\n";
foreach (self::$gameField as $line) {
foreach ($line as $cell) {
echo "\e[102;82;1m $cell"; // белый на зелёном фоне
}
echo "\n";
}
}
private static function gameOver($whoWin = -1) {
sleep(1);
self::clearScreen();
if ($whoWin == -1) {
echo <<<EOD
=========================================================
игра окончена
=========================================================
EOD;
}
if ($whoWin == 0) {
echo <<<EOD
=========================================================
вы проиграли
=========================================================
EOD;
}
echo "\n Счёт : " . self::$mark;
echo "\n Хотите играть ещё (Y/N)? : ";
$userChoice = strtolower(substr(fgets(STDIN), 0, 1));
$userChoice == 'y' ? self::init(): exit();
}
private static function clearScreen() {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
popen('cls', 'w');
} else {
system("clear");
}
}
}
Game::init();