brmlife: Initial version

Can draw 10x10 field of dots.
This commit is contained in:
Petr Baudis 2011-11-26 19:20:51 +01:00
commit ce6f46ef1c
5 changed files with 163 additions and 0 deletions

28
Makefile Normal file
View file

@ -0,0 +1,28 @@
CFLAGS=-Wall -O3 -g
OBJS=main.o map.o
brmlife: main.o map.o
$(CXX) -o $@ $^
clean:
rm -f $(OBJS) brmlife
DEP_FILES_1 = $(foreach src,$(OBJS),.deps/$(src))
DEP_FILES = $(DEP_FILES_1:%.o=%.P)
DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :)
ifdef DEP_FILES
-include $(DEP_FILES)
endif
%.o: %.cc
$(CXX) $(CFLAGS) -Wp,-MD,.deps/$(*F).pp -c $<
@-cp .deps/$(*F).pp .deps/$(*F).P; \
tr ' ' '\012' < .deps/$(*F).pp \
| sed -e 's/^\\$$//' -e '/^$$/ d' -e '/:$$/ d' -e 's/$$/ :/' \
>> .deps/$(*F).P; \
rm .deps/$(*F).pp

12
agent.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef BRMLIFE__AGENT_H
#define BRMLIFE__AGENT_H
class position;
class agent {
public:
int id;
class position *pos;
};
#endif

12
main.cc Normal file
View file

@ -0,0 +1,12 @@
#include <iostream>
#include "agent.h"
#include "map.h"
int
main(int argc, char *argv[])
{
class map map(10, 10);
map.print_map();
return 0;
}

49
map.cc Normal file
View file

@ -0,0 +1,49 @@
#include <iostream>
#include <cassert>
#include "map.h"
void
tile::on_agent_enter(class agent *a)
{
agent = a;
}
void
tile::on_agent_leave(class agent *a)
{
assert(a == agent);
agent = NULL;
}
void
tile::on_tick(void)
{
}
char
tile_ground::symbol(void)
{
return '.';
}
class position *
position::next_in(int dir_x, int dir_y)
{
int x2 = (map->w + x + dir_x) % map->w;
int y2 = (map->h + y + dir_y) % map->h;
return new position(x2, y2, map);
}
void
map::print_map(void)
{
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
class position p = position(x, y, this);
std::cout << tile_at(p).symbol();
}
std::cout << '\n';
}
}

62
map.h Normal file
View file

@ -0,0 +1,62 @@
#ifndef BRMLIFE__MAP_H
#define BRMLIFE__MAP_H
class agent;
class map;
class tile {
public:
class agent *agent;
virtual void on_agent_enter(class agent *);
virtual void on_agent_leave(class agent *);
virtual void on_tick(void);
virtual char symbol(void) = 0;
};
class tile_ground : public tile {
char symbol(void);
};
class position {
public:
int x, y;
class map *map;
position(int x_, int y_, class map *map_)
{
x = x_;
y = y_;
map = map_;
};
class position *next_in(int dir_x, int dir_y);
};
class map {
public:
class tile **tiles;
int w, h;
map(int w_, int h_)
{
w = w_;
h = h_;
tiles = new class tile * [w * h];
for (int i = 0; i < w * h; i++) {
tiles[i] = new tile_ground;
}
};
class tile &tile_at(class position &pos)
{
return *tiles[pos.y * h + pos.x];
};
void print_map(void);
};
#endif