Sense around: Extended format describing agents explicitly

This commit is contained in:
Petr Baudis 2011-11-27 03:43:45 +01:00
parent e22c57c97f
commit f0a603352f
4 changed files with 31 additions and 11 deletions

9
README
View file

@ -28,9 +28,14 @@ The following inputs (in no particular order) are supported:
energy <points> energy <points>
number of agent's energy points; disregard number of agent's energy points; disregard
in case of dead agents in case of dead agents
around <chars> around <desc> <desc>...
<chars> describe tiles, clockwise from top, <desc> describe tiles, clockwise from top,
in the immediate vicinity of the agent in the immediate vicinity of the agent
<desc> format is two-character, <type><agent>
<type>: . for ground
<agent>: - no agent
a dead agent
A alive agent
The following outputs are supported: The following outputs are supported:

View file

@ -18,19 +18,19 @@ connection::senses(int tick_id, class agent &a)
"tick %d\r\n" "tick %d\r\n"
"%s" "%s"
"energy %d\r\n" "energy %d\r\n"
"around %c%c%c%c%c%c%c%c\r\n" "around %s %s %s %s %s %s %s %s\r\n"
"\r\n", "\r\n",
tick_id, tick_id,
a.dead ? "DEAD\r\n" : "", a.dead ? "DEAD\r\n" : "",
a.energy, a.energy,
a.tile->tile_in_dir(0, -1).symbol(), a.tile->tile_in_dir(0, -1).str(),
a.tile->tile_in_dir(1, -1).symbol(), a.tile->tile_in_dir(1, -1).str(),
a.tile->tile_in_dir(1, 0).symbol(), a.tile->tile_in_dir(1, 0).str(),
a.tile->tile_in_dir(1, 1).symbol(), a.tile->tile_in_dir(1, 1).str(),
a.tile->tile_in_dir(0, 1).symbol(), a.tile->tile_in_dir(0, 1).str(),
a.tile->tile_in_dir(-1, 1).symbol(), a.tile->tile_in_dir(-1, 1).str(),
a.tile->tile_in_dir(-1, 0).symbol(), a.tile->tile_in_dir(-1, 0).str(),
a.tile->tile_in_dir(-1, -1).symbol() a.tile->tile_in_dir(-1, -1).str()
); );
pthread_mutex_lock(&buf_lock); pthread_mutex_lock(&buf_lock);

11
map.cc
View file

@ -1,5 +1,6 @@
#include <cassert> #include <cassert>
#include <cstdlib> #include <cstdlib>
#include <cstdio>
#include <iostream> #include <iostream>
#include "agent.h" #include "agent.h"
@ -39,6 +40,16 @@ tile_ground::type_symbol(void)
return '.'; return '.';
} }
char *
tile::str(void)
{
snprintf(str_buf, sizeof(str_buf),
"%c%c",
type_symbol(),
agent ? (!agent->dead ? 'A' : 'a') : '-');
return str_buf;
}
class tile & class tile &
tile::tile_in_dir(int dir_x, int dir_y) tile::tile_in_dir(int dir_x, int dir_y)
{ {

4
map.h
View file

@ -16,6 +16,7 @@ public:
char symbol(void); char symbol(void);
virtual char type_symbol(void) = 0; virtual char type_symbol(void) = 0;
char *str(void);
class tile &tile_in_dir(int dir_x, int dir_y); class tile &tile_in_dir(int dir_x, int dir_y);
@ -23,6 +24,9 @@ public:
virtual void on_agent_leave(class agent &); virtual void on_agent_leave(class agent &);
virtual void on_tick(void); virtual void on_tick(void);
private:
char str_buf[3];
}; };
class tile_ground : public tile { class tile_ground : public tile {