-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
100 lines (85 loc) · 2.29 KB
/
index.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
<?php
// Constants
const MODE = 'life';
const ITERATIONS = 10;
const STRAT_COUNT = 10;
const STRAT_FACTOR = null; // integer or null
const STRATS = [
'TitTat',
'TitForTat',
'AlwaysCoop',
'AlwaysDefect',
'Grudger',
'ReverseTitTat',
'Random',
];
const STRAT_RAND = false;
// =============================================================================
echo '<h3>Prisoners Dillemma</h3><hr>';
set_include_path(
$_SERVER['DOCUMENT_ROOT'] . '/'
. PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] . '/prisoners-dillemma/'
. PATH_SEPARATOR . get_include_path()
);
// 2 minutes max
set_time_limit(60 * 2);
require_once('game/Strategy.php');
require_once('game/Loader.php');
require_once('game/Game.php');
require_once('game/Move.php');
require_once('game/Score.php');
require_once('game/World.php');
try {
// Load?
if (isset($_GET['load_id'])) {
$id = addslashes(trim(strip_tags($_GET['load_id'])));
$f = $id .'.run';
$game = Game::load($f);
if (!$game instanceof Game) {
throw new Exception('Cannot load '. $id);
}
$game->showWorld();
}
// Load strats
$strategies = Loader::load();
if (!is_array($strategies) || !count($strategies)) {
throw new Exception('No strategies found');
}
// Setup game
$game = new Game($strategies);
// Build strategy pool
$pool = [];
foreach (STRATS as $strat) {
$f = is_null(STRAT_FACTOR) ? 1 : (float) (rand(1, STRAT_FACTOR) / 10);
$pool[$strat] = (int) round(STRAT_COUNT * $f);
}
$game->buildPool($pool, STRAT_RAND);
// Set game mode
$game->setMode(MODE);
// Start game
$game->run(ITERATIONS);
// Display latest iteration
$game->showWorld();
// Display all :)
/*
for ($i=0; $i < $game->getIteration(); $i++) {
$game->showWorld($i);
}
*/
// Store game in runs dir
$game->store();
} catch (Exception $e) {
die('<hr>ERROR: ' . (string) $e);
}
?>
<style type="text/css">
.world {
position:absolute;left:600px;width:400px;height:auto;
padding:5px;
}
.strat {
position:absolute;border:1px solid grey;margin:1px;vertical-align:middle;overflow:hidden;
}
.score {
}
</style>