-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStupidRandomBot.php
186 lines (138 loc) · 4.42 KB
/
StupidRandomBot.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
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
<?php
namespace Prokki\Warlight2BotTemplate\Examples\StupidRandomBot;
use Prokki\Warlight2BotTemplate\AIBot;
use Prokki\Warlight2BotTemplate\Examples\StupidRandomBot\Game\EnvironmentFactory;
use Prokki\Warlight2BotTemplate\Examples\StupidRandomBot\Game\RegionState;
use Prokki\Warlight2BotTemplate\Game\Move\AttackMove;
use Prokki\Warlight2BotTemplate\Game\Move\PickMove;
use Prokki\Warlight2BotTemplate\Game\Move\PlaceMove;
use Prokki\Warlight2BotTemplate\Game\Move\TransferMove;
use Prokki\Warlight2BotTemplate\Game\Region;
use Prokki\Warlight2BotTemplate\Game\RegionArray;
/**
* StupidRandomBot is a simple bot used to describe how to use the Warlight2BotTemplate.
*
* @package Prokki\Warlight2BotTemplate\StupidRandomBot
*/
class StupidRandomBot extends AIBot
{
/**
* The constructor of your bot is not necessary, but advisable.
*
* Especially if you your bot uses overridden classes you have to *initialize
* your custom `EnvironmentFactory`*.
*
* @inheritdoc
*/
public function __construct($init_inventory = true)
{
if( $init_inventory )
{
EnvironmentFactory::Init();
}
parent::__construct();
}
/**
* @inheritdoc
*/
public function getPickMove($region_ids)
{
mt_srand((double) microtime() * 1234567);
$index = mt_rand(0, count($region_ids) - 1);
$region = $this->_environment->getMap()->getRegions()->get($region_ids[ $index ]);
$region->»getState()->setOwner(RegionState::OWNER_ME);
return new PickMove($region->getId());
}
protected static function _ChooseIndexes($length)
{
if( $length < 1 )
{
return array();
}
mt_srand((double) microtime() * 9876543);
$moves = mt_rand(
max(1, floor(log($length, M_E))),
max(1, ceil(log($length, 2)))
);
$moves = min($moves, $length);
$chosen_indexes = array();
do
{
do
{
$_chosen_index = rand(0, $length - 1);
}
while( in_array($_chosen_index, $chosen_indexes) );
array_push($chosen_indexes, $_chosen_index);
}
while( count($chosen_indexes) < $moves );
return $chosen_indexes;
}
/**
* @inheritdoc
*/
public function getAttackTransferMoves()
{
$source_regions = $this->_environment->getMap()->getRegions()->filterOwner(RegionState::OWNER_ME);
$chosen_source_seq = self::_ChooseIndexes(count($source_regions));
$seq_region = 0;
$moves = array();
foreach( $source_regions as $_source_region )
{
/** @var Region $_source_region */
if( $_source_region->getArmies() <= 1 || !in_array($seq_region, $chosen_source_seq) )
{
++$seq_region;
continue;
}
/** @var RegionArray $_destination_regions */
$_destination_regions = $_source_region->getNeighbors();
$_chosen_destination_seq = self::_ChooseIndexes(count($_destination_regions));
$_seq_destination = 0;
foreach( $_destination_regions as $__destination_region )
{
/** @var Region $__destination_region */
if( !in_array($_seq_destination, $_chosen_destination_seq) )
{
continue;
}
array_push($moves, ( $__destination_region->getOwner() === RegionState::OWNER_ME ) ?
new TransferMove($_source_region->getId(), $__destination_region->getId(), $_source_region->getArmies() - 1) :
new AttackMove($_source_region->getId(), $__destination_region->getId(), $_source_region->getArmies() - 1)
);
++$_seq_destination;
}
++$seq_region;
}
return $moves;
}
/**
* @inheritdoc
*/
public function getPlaceMoves()
{
mt_srand((double) microtime() * 10101010);
$armies_to_dispense = $this->_environment->getPlayer()->getStartingArmies();
$my_region_ids = $this->_environment->getMap()->getRegions()->filterOwner(RegionState::OWNER_ME)->getIds();
$armies_to_place = array();
$circa_placements = min(count($my_region_ids), (int) mt_rand(1, (int) ceil(log($armies_to_dispense, 1.75))));
$placement_round_no = 1;
do
{
mt_srand((double) microtime() * 10101010);
$_already_placed_armies = array_sum($armies_to_place);
$_max_armies_to_place_this_round = $armies_to_dispense - $_already_placed_armies;
$_armies_this_round = (int) mt_rand(1, $_max_armies_to_place_this_round - $circa_placements + $placement_round_no);
array_push($armies_to_place, $_armies_this_round);
++$placement_round_no;
}
while( array_sum($armies_to_place) < $armies_to_dispense );
$moves = array();
foreach( $armies_to_place as $_armies )
{
shuffle($my_region_ids);
array_push($moves, new PlaceMove(array_pop($my_region_ids), $_armies));
}
return $moves;
}
}