-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.php
160 lines (136 loc) · 3.69 KB
/
model.php
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
<?php
class Point {
public $x, $y;
public function __construct(int $x, int $y) {
$this->x = $x;
$this->y = $y;
}
public function __toString() {
return $this->x . "," . $this->y;
}
}
class Ship {
public $name;
public $x, $y, $size;
public $vertical;
public $hits = array();
public function __construct(string $name, int $x, int $y, int $size, bool $vertical) {
$this->name = $name;
$this->x = $x;
$this->y = $y;
$this->size = $size;
$this->vertical = $vertical;
}
public function get_all_points(): array {
$points = array();
$currentX = $this->x;
$currentY = $this->y;
for ($i = 0; $i < $this->size; $i++) {
$points[] = new Point($currentX, $currentY);
if ($this->vertical) {
$currentY++;
} else {
$currentX++;
}
}
return $points;
}
public function get_hits(): array {
return $this->hits;
}
public function is_hit(Point $point): bool {
$hit = in_array($point, $this->get_all_points());
if ($hit && !in_array($point, $this->hits)) $this->hits[] = $point;
return $hit;
}
public function is_sunk(): bool {
return count($this->hits) >= $this->size;
}
public function intersects(Ship $ship) {
$common_points = array_intersect($this->get_all_points(), $ship->get_all_points());
return count($common_points) > 0;
}
}
class Board {
public $name;
public $my_turn = false;
public $ships = array();
public $misses = array();
public $last_strike;
public $size;
public function __construct(int $size) {
$this->size = $size;
$this->place_ship("Carrier", 5);
$this->place_ship("Battleship", 4);
$this->place_ship("Cruiser", 3);
$this->place_ship("Submarine", 3);
$this->place_ship("Destroyer", 2);
}
public function can_place(Ship $to_place_ship): bool {
foreach ($this->ships as $ship) {
if ($ship->intersects($to_place_ship)) return false;
}
return true;
}
public function place_ship($name, $size) {
do {
$vertical = rand(0, 1) > 0;
$x = rand(0, $this->size - ($vertical ? 1 : $size));
$y = rand(0, $this->size - ($vertical ? $size : 1));
$ship = new Ship($name, $x, $y, $size, $vertical);
} while (!$this->can_place($ship));
$this->ships[] = $ship;
}
public function get_hits(): array {
$hits = array();
foreach ($this->ships as $ship) {
$hits = array_merge($hits, $ship->get_hits());
}
return $hits;
}
public function get_misses(): array {
return $this->misses;
}
public function strike(Point $point): bool {
foreach ($this->ships as $ship) {
if ($ship->is_hit($point)) {
$this->last_strike = array(
"point" => $point,
"name" => $ship->name
);
return true;
}
}
if (!in_array($point, $this->misses)) $this->misses[] = $point;
$this->last_strike = array(
"point" => $point,
"name" => null
);
return false;
}
public function is_alive(): bool {
foreach ($this->ships as $ship) {
if (!$ship->is_sunk()) return true;
}
return false;
}
}
function fail_with($error_msg) {
echo json_encode(array("error" => $error_msg));
exit();
}
function end_session($delete = true) {
// load session
session_start();
// delete session data from server
if ($delete) $_SESSION = array();
// delete session id from client
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"]);
}
// also delete player cookie
setcookie("player", '', time() - 42000);
// unload
if ($delete) session_destroy();
}