public
Last active — forked from greghaynes/gist:300209

  • Download Gist
gistfile1.txt
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
// This software is released under the DBAD (Dont be a duche) license.
// If you improve this code in any way, you must release your modifications publicly.
 
#include "Map.h"
#include <string>
#include <vector>
#include <iostream>
 
#define DEPTH 1
 
const char *moves[] = {
"SOUTH",
"NORTH",
"EAST",
"WEST"
};
 
bool is_blocked(int x, int y, const Map &map,
std::vector<std::vector<bool> > &blocked_grid)
{
return map.IsWall(x, y) || blocked_grid[x][y];
}
 
 
int flood_fill_grid(const Map &map,
std::vector<std::vector<bool> > &blocked_grid,
std::vector<std::vector<bool> > &grid,
int x, int y)
{
int count = 1;
if (is_blocked(x, y, map, blocked_grid) || grid[x][y])
return 0;
grid[x][y] = true;
count += flood_fill_grid(map, blocked_grid, grid, x, y+1);
count += flood_fill_grid(map, blocked_grid, grid, x, y-1);
count += flood_fill_grid(map, blocked_grid, grid, x+1, y);
count += flood_fill_grid(map, blocked_grid, grid, x-1, y);
return count;
}
 
std::vector<std::vector<bool> > *create_map_grid(const Map &map)
{
return new std::vector<std::vector<bool> >(map.Width(),
std::vector<bool>(map.Height(), false));
}
 
bool can_move_from(int x, int y, const Map &map, std::vector<std::vector<bool> > &blocked_grid)
{
if(!is_blocked(x, y+1, map, blocked_grid))
return true;
if(!is_blocked(x, y-1, map ,blocked_grid))
return true;
if(!is_blocked(x+1, y, map, blocked_grid))
return true;
if(!is_blocked(x-1, y, map, blocked_grid))
return true;
return false;
}
 
bool is_same_location(int x1, int y1, int x2, int y2)
{
return x1 == x2 && y1 == y2;
}
 
int max(std::vector<int> &vals)
{
unsigned int i;
int highest;
highest = vals[0];
for(i=1;i<vals.size();i++)
{
if(vals[i] > highest)
highest = vals[i];
}
return highest;
}
 
int score_my_move(const Map &map,
std::vector<std::vector<bool> > &blocked_grid,
int my_x, int my_y,
int their_x, int their_y,
int max_depth);
 
int move_count(const Map &map,
std::vector<std::vector<bool> > &blocked_grid,
int x, int y)
{
int count = 0;
if(!is_blocked(x, y+1, map, blocked_grid))
count++;
if(!is_blocked(x, y-1, map, blocked_grid))
count++;
if(!is_blocked(x+1, y, map, blocked_grid))
count++;
if(!is_blocked(x-1, y, map, blocked_grid))
count++;
return count;
}
 
 
int abs(int val)
{
if(val >= 0)
return val;
return -val;
}
 
int score_their_move(const Map &map,
std::vector<std::vector<bool> > &blocked_grid,
int my_x, int my_y,
int their_x, int their_y,
int max_depth)
{
int my_area, their_area;
bool they_have_moves, we_have_moves;
std::vector<int> scores(4, -100);
 
if(is_same_location(my_x, my_y, their_x, their_y))
return 50;
 
they_have_moves = can_move_from(their_x, their_y, map, blocked_grid);
we_have_moves = can_move_from(my_x, my_y, map, blocked_grid);
if(!they_have_moves)
{
if(!we_have_moves)
return 50;
return -100;
}
if(!we_have_moves)
return 100;
 
if(max_depth > 0)
{
max_depth--;
blocked_grid[my_x][my_y] = true;
blocked_grid[their_x][their_y] = true;
 
if(!is_blocked(my_x, my_y+1, map, blocked_grid))
scores[0] = -score_my_move(map, blocked_grid, my_x, my_y+1, their_x, their_y, max_depth);
if(!is_blocked(my_x, my_y-1, map, blocked_grid))
scores[1] = -score_my_move(map, blocked_grid, my_x, my_y-1, their_x, their_y, max_depth);
if(!is_blocked(my_x+1, my_y, map, blocked_grid))
scores[2] = -score_my_move(map, blocked_grid, my_x+1, my_y, their_x, their_y, max_depth);
if(!is_blocked(my_x-1, my_y, map, blocked_grid))
scores[3] = -score_my_move(map, blocked_grid, my_x-1, my_y, their_x, their_y, max_depth);
 
blocked_grid[my_x][my_y] = false;
blocked_grid[their_x][their_y] = false;
return max(scores);
}
 
std::vector<std::vector<bool> > *fg = create_map_grid(map);
my_area = flood_fill_grid(map, blocked_grid, *fg, my_x, my_y);
if(!(*fg)[their_x][their_y])
{
their_area = flood_fill_grid(map, blocked_grid, *fg, their_x, their_y);
delete fg;
fg = 0;
if(their_area > my_area)
return 75;
if(their_area < my_area)
return -75;
}
if(fg)
delete fg;
 
float area = my_area;
return -20 + (area / (map.Width() * map.Height())) * 40.0f;
}
 
 
int score_my_move(const Map &map,
std::vector<std::vector<bool> > &blocked_grid,
int my_x, int my_y,
int their_x, int their_y,
int max_depth)
{
if(is_blocked(my_x, my_y, map, blocked_grid))
return -100;
std::vector<int> scores(4, -100);
 
if(!is_blocked(their_x, their_y+1, map, blocked_grid))
scores[0] = -score_their_move(map, blocked_grid, my_x, my_y, their_x, their_y+1, max_depth);
if(!is_blocked(their_x, their_y-1, map, blocked_grid))
scores[1] = -score_their_move(map, blocked_grid, my_x, my_y, their_x, their_y-1, max_depth);
if(!is_blocked(their_x+1, their_y, map, blocked_grid))
scores[2] = -score_their_move(map, blocked_grid, my_x, my_y, their_x+1, their_y, max_depth);
if(!is_blocked(their_x-1, their_y, map, blocked_grid))
scores[3] = -score_their_move(map, blocked_grid, my_x, my_y, their_x-1, their_y, max_depth);
 
return max(scores);
}
 
std::string MakeMove(const Map& map) {
int x = map.MyX();
int y = map.MyY();
int their_x = map.OpponentX();
int their_y = map.OpponentY();
int best_score = -101;
int best_move = 0;
int score;
 
std::vector<std::vector<bool> > *grid = create_map_grid(map);
 
#define try_move(my_x, my_y, n)\
if (!map.IsWall(my_x, my_y)) {\
score = score_my_move(map, *grid, my_x, my_y, their_x, their_y, DEPTH);\
if(score > best_score)\
{\
best_score = score;\
best_move = n;\
}\
}\
 
try_move(x, y+1, 0);
try_move(x, y-1, 1);
try_move(x+1, y, 2);
try_move(x-1, y, 3);
 
#undef try_move
 
return moves[best_move];
}
 
// Ignore this function. It is just handling boring stuff for you, like
// communicating with the Tron tournament engine.
int main() {
while (true) {
Map map;
Map::MakeMove(MakeMove(map));
}
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.