[ create a new paste ] login | about

Link: http://codepad.org/zlwvnPTX    [ raw code | output | fork ]

C, pasted on Nov 24:
#include <stdio.h>
#include "Info.h"

#define _START 5
#define _GOAL 250

#define _ABS(x) (x < 0 ? x * -1 : x)

int _manhattanDist(int x1, int y1, int x2, int y2)
{
	int dx, dy;

	dx = _ABS(x1 - x2);
	dy = _ABS(y1 - y2);

	return dx + dy;
}

POSITION _getPosition(int x, int y)
{
	POSITION pos;

	pos.x = x;
	pos.y = y;

	return pos;
}

void _getPositionAround(int x, int y, POSITION pos[4])
{
	pos[0] = _getPosition(x, y - 1);
	pos[1] = _getPosition(x - 1, y);
	pos[2] = _getPosition(x + 1, y);
	pos[3] = _getPosition(x, y + 1);
}

int _calcPath(int cost, MAPDATA map, POSITION tar)
{
	POSITION pos[4];

	int num;
	int x, y;
	int i, j, k;

	for (i = 0; i < MAP_HEIGHT; i++)
	{
		for (j = 0; j < MAP_WIDTH; j++)
		{
			num = cost + _manhattanDist(j, i, tar.x, tar.y);

			if (map[i][j] == num)
			{
				_getPositionAround(j, i, pos);

				for (k = 0; k < 4; k++)
				{
					x = pos[k].x;
					y = pos[k].y;

					if (map[y][x] == _GOAL)
						return k;

					switch(map[y][x])
					{
					case 0:
					case 1:
						map[y][x] = (cost + 1) + _manhattanDist(x, y, tar.x, tar.y);
						break;

					case 2:
					default:
						break;
					}
				}
			}
		}
	}

	return _calcPath(cost + 1, map, tar);
}

MOVEMENT redAI(MAPDATA map, POSITION p, POSITION e)
{
	map[p.y][p.x] = _START;
	map[e.y][e.x] = _GOAL;

	if (map[p.y][p.x] == _GOAL)
		return STAY;

	map[p.y][p.x] += _manhattanDist(p.x, p.y, e.x, e.y);

	switch (_calcPath(_START, map, e))
	{
	case 0: return DOWN; break;
	case 1: return RIGHT; break;
	case 2: return LEFT; break;
	case 3: return UP; break;

	default:
		break;
	}

	return STAY;
}


Output:
Line 17: error: Info.h: No such file or directory
Line 19: error: expected '=', ',', ';', 'asm' or '__attribute__' before '_getPosition'
Line 29: error: expected declaration specifiers or '...' before 'POSITION'
In function '_getPositionAround':
Line 31: error: 'pos' undeclared (first use in this function)
Line 31: error: (Each undeclared identifier is reported only once
Line 31: error: for each function it appears in.)
t.c: At top level:
Line 37: error: expected declaration specifiers or '...' before 'MAPDATA'
Line 37: error: expected declaration specifiers or '...' before 'POSITION'
In function '_calcPath':
Line 39: error: 'POSITION' undeclared (first use in this function)
Line 39: error: expected ';' before 'pos'
Line 45: error: 'MAP_HEIGHT' undeclared (first use in this function)
Line 47: error: 'MAP_WIDTH' undeclared (first use in this function)
Line 49: error: 'tar' undeclared (first use in this function)
Line 51: error: 'map' undeclared (first use in this function)
Line 53: error: 'pos' undeclared (first use in this function)
Line 53: error: too many arguments to function '_getPositionAround'
Line 79: error: too many arguments to function '_calcPath'
t.c: At top level:
Line 82: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'redAI'


Create a new paste based on this one


Comments: