Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
smly committed Feb 26, 2012
0 parents commit 1e88aee
Show file tree
Hide file tree
Showing 140 changed files with 13,962 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
My personal sandbox for trying out various algorithms, programming languages, etc.
24 changes: 24 additions & 0 deletions competition/aichallenge2010/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# The Makefile
#
# If you're using Windows and you don't know what this file is,
# don't worry about it. Just use Visual C++ Express Edition or
# Dev-C++ to work on your code.

MyTronBot: MyTronBot.o Map.o
g++ -o MyTronBot MyTronBot.o Map.o

MyTronBot.o: MyTronBot.cc
g++ -c -o MyTronBot.o MyTronBot.cc -O2

Map.o: Map.cc
g++ -c -o Map.o Map.cc -O2

clean:
rm MyTronBot test
rm *.o

test: Map.o
cd tests
g++ -c -o tests/test_simple.o tests/test_simple.cc -I.
g++ -o tests/test_simple tests/test_simple.o Map.o -lgtest
./tests/test_simple
149 changes: 149 additions & 0 deletions competition/aichallenge2010/Map.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Map.cc

#include "Map.h"
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>

Map::Map() {
ReadFromFile(stdin);
}

int Map::Width() const {
return map_width;
}

int Map::Height() const {
return map_height;
}

bool Map::IsWall(int x, int y) const {
if (x < 0 || y < 0 ||
x >= map_width ||
y >= map_height) {
return true;
} else {
return is_wall[x][y];
}
}

int Map::MyX() const {
return player_one_x;
}

int Map::MyY() const {
return player_one_y;
}

int Map::OpponentX() const {
return player_two_x;
}

int Map::OpponentY() const {
return player_two_y;
}

void Map::MakeMove(const std::string& move) {
if (move.length() == 0) {
fprintf(stderr, "ERROR: zero-length string passed to MakeMove(string)\n");
MakeMove(0);
} else {
int c = (int)move[0];
switch (c) {
case 'n':
case 'N':
MakeMove(1);
break;
case 'e':
case 'E':
MakeMove(2);
break;
case 's':
case 'S':
MakeMove(3);
break;
case 'w':
case 'W':
MakeMove(4);
break;
default:
fprintf(stderr, "Invalid string passed to MakeMove(string): %s\n"
"Move string must start with N, E, S, or W!", move.c_str());
MakeMove(0);
break;
}
}
}

void Map::MakeMove(int move) {
fprintf(stdout, "%d\n", move);
fflush(stdout);
}

void Map::ReadFromFile(FILE *file_handle) {
int x, y, c;
int num_items = fscanf(file_handle, "%d %d\n", &map_width, &map_height);
if (feof(file_handle) || num_items < 2) {
exit(0); // End of stream means end of game. Just exit.
}
is_wall =
std::vector<std::vector<bool> >(map_width,
std::vector<bool>(map_height, false));
x = 0;
y = 0;
while (y < map_height && (c = fgetc(file_handle)) != EOF) {
switch (c) {
case '\r':
break;
case '\n':
if (x != map_width) {
fprintf(stderr, "x != width in Board_ReadFromStream\n");
return;
}
++y;
x = 0;
break;
case '#':
if (x >= map_width) {
fprintf(stderr, "x >= width in Board_ReadFromStream\n");
return;
}
is_wall[x][y] = true;
++x;
break;
case ' ':
if (x >= map_width) {
fprintf(stderr, "x >= width in Board_ReadFromStream\n");
return;
}
is_wall[x][y] = false;
++x;
break;
case '1':
if (x >= map_width) {
fprintf(stderr, "x >= width in Board_ReadFromStream\n");
return;
}
is_wall[x][y] = false;
player_one_x = x;
player_one_y = y;
++x;
break;
case '2':
if (x >= map_width) {
fprintf(stderr, "x >= width in Board_ReadFromStream\n");
return;
}
is_wall[x][y] = false;
player_two_x = x;
player_two_y = y;
++x;
break;
default:
fprintf(stderr, "unexpected character %d in Board_ReadFromStream", c);
return;
}
}
}

109 changes: 109 additions & 0 deletions competition/aichallenge2010/Map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Map.h
//
// Handles the Tron map. Also handles communicating with the Tron game engine.
// You don't need to change anything in this file.

#include <string>
#include <vector>
#include <queue>
#include <iostream>

class Map {
public:
// Constructs a Map by reading an ASCII representation from the console
// (stdin).
Map();

// Returns the width of the Tron map.
int Width() const;

// Returns the height of the Tron map.
int Height() const;

// Returns whether or not the given cell is a wall or not. TRUE means it's
// a wall, FALSE means it's not a wall, and is passable. Any spaces that are
// not on the board are deemed to be walls.
bool IsWall(int x, int y) const;

// Get my X and Y position. These are zero-based.
int MyX() const;
int MyY() const;

// Get the opponent's X and Y position. These are zero-based.
int OpponentX() const;
int OpponentY() const;

// Sends your move to the contest engine. Only the first character of
// the string is used. It is case insensitive. The four possible moves are
// * "N" -- North. Negative Y direction.
// * "E" -- East. Positive X direction.
// * "S" -- South. Positive X direction.
// * "W" -- West. Negative X direction.
// Other strings can be valid moves, too. For example: "North", "east",
// "s", and "WwWwWest!" are all valid moves, because they start with one
// of the four allowed characters.
static void MakeMove(const std::string& move);

// Sends your move to the contest engine. The four possible moves are
// * 1 -- North. Negative Y direction.
// * 2 -- East. Positive X direction.
// * 3 -- South. Positive X direction.
// * 4 -- West. Negative X direction.
static void MakeMove(int move);

// private:
// Load a board from an open file handle. To read from the console, pass
// stdin, which is actually a (FILE*).
// file_handle -- an open file handle from which to read.
//
// If there is a problem, the function returns NULL. Otherwise, a valid
// Board structure is returned.
//
// The file should be an ascii file. The first line contains the width and
// height of the board, separated by a space. subsequent lines contain visual
// representations of the rows of the board, using '#' and space characters.
// The starting positions of the two players are indicated by '1' and '2'
// characters. There must be exactly one '1' character and one '2' character
// on the board. For example:
// 6 4
// ######
// #1# 2#
// # ##
// ######
void ReadFromFile(FILE *file_handle);

// ______________________________________________________________________
// Indicates whether or not each cell in the board is passable.
std::vector<std::vector<bool> > is_wall;

// The locations of both players.
int player_one_x, player_one_y;
int player_two_x, player_two_y;

// Map dimensions.
int map_width, map_height;
private:
};

enum MoveDirection {
WEST = 0,
EAST,
NORTH,
SOUTH
};

enum Move {
LEFT = 0,
RIGHT,
TOP,
BOTTOM
};

struct Player {
public:
int x, y;
Player() {}
Player(int x, int y) : x(x), y(y) { }
};

typedef int Direction; // if 3 block, dir is player direction
Loading

0 comments on commit 1e88aee

Please sign in to comment.