Esempio n. 1
0
// Test your vector here
int main() { 
	Vector* temp = Vector_create(); //create a vector
	
	Vector_append( temp, "a");//1
	Vector_append( temp, "b");//2
	Vector_append( temp, "c");//3
	Vector_append( temp, "d");//4
	Vector_append( temp, NULL);//5
	Vector_append( temp, "f");//6
	Vector_append( temp, "g");//7
	Vector_append( temp, NULL);//8
	Vector_append( temp, "");//9
	Vector_append( temp, "h");//10
	Vector_append( temp, "i");//11
	Vector_append( temp, NULL);//12
	
	Vector_resize( temp, 20 );
	
	if( Vector_size(temp) != 20 || strcmp( Vector_get( temp, 10 ), "i" ) || Vector_get( temp, 15 ) != NULL )
		perror( "something wrong.\n");
	
	//done for append, resize, get, size
	
	Vector_set( temp, 19, "caibi" );
	Vector_insert( temp, 20, "niubi" );
	Vector_insert( temp, 30, "wori" );
	
	if( Vector_size(temp) != 31 || strcmp( Vector_get( temp, 19 ), "caibi" ) || strcmp( Vector_get( temp, 20 ), "niubi" ) ||  Vector_get( temp, 15 ) != NULL || strcmp( Vector_get( temp, 30 ), "wori" ) )
		perror( "something wrong.\n");
	
	Vector_delete( temp, 11 );
	Vector_delete( temp, 27 );
	Vector_delete( temp, 1 );
	Vector_delete( temp, 18 );
	
	if( Vector_size(temp) != 27 || strcmp( Vector_get( temp, 4 ), "f" ) || strcmp( Vector_get( temp, 26 ), "wori") || Vector_get( temp, 18 ) !=NULL || strcmp( Vector_get( temp, 17 ), "caibi") )
		perror( "something wrong.\n");
	
	
	Vector_destroy( temp );

	return 0; 
}
Esempio n. 2
0
int main(int argc, char* argv[])
{
	Map map;
	Car cars[3] = {NULL, NULL, NULL};
	int x, y, i, rounds = 0, needPathfinding = 0, firstLoop = 1;
	List path;
	Position position;
	Vector acc;
	int fuel;

	srand(time(NULL));

	freopen("pshr4log.txt", "w+", stderr);
	// freopen("map.txt", "r", stdin);

	LOGINFO("Starting");

	initGC();

	map = Map_load(stdin);

	LOGINFO("Map loaded");

	while(!feof(stdin))
	{
		LOGINFO1I("===== Round number %d =====", rounds);

		for(i = 0; i < 3; i++)
		{
			fscanf(stdin, "%d %d", &x, &y);

			LOGINFO3I("Car number %i, position : %d %d", i, x, y);

			if(cars[i] == NULL)
				cars[i] = Car_new(x, y);
			else
				Car_updatePosition(cars[i], x, y);

			if(Car_updateArrivedStatus(cars[i], map))
			{
				recomputeDistances(map, cars);
				LOGINFO("I see you've arrived, let me see where I can park now...");
				needPathfinding = 1;
			}
		}

		if(firstLoop)
		{
			firstLoop = 0;
			path = doPathfinding(map, cars);
			List_tail(path);
			List_prev(path);
		}

		position = List_getCurrent(path);

		for(i = 1; i < 3; i++)
		{
			if(Position_equal(position, Car_getPosition(cars[i])))
			{
				needPathfinding = 1;
				LOGINFO("YOU TOOK MY SPOT JERK");
			}
		}

		if(needPathfinding)
		{
			freePath(path);
			path = doPathfinding(map, cars);
			List_tail(path);
			List_prev(path);
			needPathfinding = 0;
			position = List_getCurrent(path);
		}

		if(position == NULL)
		{
			goRandom();
		}
		else
		{
			LOGINFO2I("Going to %d %d", position->x, position->y);
			acc = Car_getAccelerationToReach(cars[0], position);
			go(acc->x, acc->y);

			if(Vector_squaredLength(acc) > 2)
			{
				Car_useBoost(cars[0]);
				LOGINFO("Using a boost");
			}

			Vector_delete(acc);
		}

		List_prev(path);

		rounds++;
	}

	LOGINFO("End");

	freePath(path);

	for(i = 0; i < 3; i++)
	{
		Car_delete(cars[i]);
	}

	Map_delete(map);
	freeGC();

	return 0;
}
Esempio n. 3
0
void OpenFilesScreen_run(OpenFilesScreen* htop_this) {
     Panel* panel = htop_this->display;
     Panel_setHeader(panel, "   FD TYPE     DEVICE       SIZE       NODE NAME");

     FunctionBar* bar = FunctionBar_new(ofsFunctions, ofsKeys, ofsEvents);
     IncSet* inc = IncSet_new(bar);

     Vector* lines = Vector_new(panel->items->type, true, DEFAULT_SIZE);

     OpenFilesScreen_scan(htop_this, lines, inc);
     OpenFilesScreen_draw(htop_this, inc);

     bool looping = true;
     while (looping) {

          Panel_draw(panel, true);

          if (inc->active)
               move(LINES-1, CRT_cursorX);
          int ch = getch();

          if (ch == KEY_MOUSE) {
               MEVENT mevent;
               int ok = getmouse(&mevent);
               if (ok == OK)
                    if (mevent.y >= panel->y && mevent.y < LINES - 1) {
                         Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV);
                         ch = 0;
                    }
               if (mevent.y == LINES - 1)
                    ch = FunctionBar_synthesizeEvent(inc->bar, mevent.x);
          }

          if (inc->active) {
               IncSet_handleKey(inc, ch, panel, IncSet_getListItemValue, lines);
               continue;
          }

          switch(ch) {
          case ERR:
               continue;
          case KEY_F(3):
          case '/':
               IncSet_activate(inc, INC_SEARCH);
               break;
          case KEY_F(4):
          case '\\':
               IncSet_activate(inc, INC_FILTER);
               break;
          case KEY_F(5):
               clear();
               OpenFilesScreen_scan(htop_this, lines, inc);
               OpenFilesScreen_draw(htop_this, inc);
               break;
          case '\014': // Ctrl+L
               clear();
               OpenFilesScreen_draw(htop_this, inc);
               break;
          case 'q':
          case 27:
          case KEY_F(10):
               looping = false;
               break;
          case KEY_RESIZE:
               Panel_resize(panel, COLS, LINES-2);
               OpenFilesScreen_draw(htop_this, inc);
               break;
          default:
               Panel_onKey(panel, ch);
          }
     }

     Vector_delete(lines);
     FunctionBar_delete((Object*)bar);
     IncSet_delete(inc);
}
Esempio n. 4
0
List doPathfinding(Map map, Car cars[3])
{
	Heap openSet = Heap_new(State_compare);
	List closedSet = List_new();
	List finalPath = List_new();
	List neighbors;
	Position neighbor;
	State state, newState;
	Vector newSpeed, acceleration;
	int end = 0, i, j, useBoost, positionTaken, distance;
	float cost;

	LOGINFO("A* doin' da werk!");

	state = State_new(Car_getPosition(cars[0]), Car_getSpeed(cars[0]), Car_getBoosts(cars[0]), map);
	Heap_insert(openSet, state);

	while(!Heap_isEmpty(openSet) && !end)
	{
		state = Heap_extractMin(openSet);

		if(Map_getTile(map, State_getPosition(state)->x, State_getPosition(state)->y) == ARRIVAL)
		{
			end = 1;
			break;
		}

		distance = Map_getDistance(map, State_getPosition(state)->x, State_getPosition(state)->y);
		neighbors = Map_getReachablePositions(map, State_getPosition(state), State_getSpeed(state), State_getBoosts(state));

		List_foreach(neighbors, neighbor, i)
		{
			if(Map_getDistance(map, neighbor->x, neighbor->y) > distance)
			{
				Position_delete(neighbor);
				continue;
			}

			cost = State_getRealCost(state) + 1;
			newSpeed = Position_findOffset(State_getPosition(state), neighbor);
			acceleration = Vector_copy(newSpeed);
			Vector_substract(acceleration, State_getSpeed(state));
			useBoost = 0;
			positionTaken = 0;

			if(Vector_squaredLength(acceleration) > 2)
			{
				useBoost = 1;
			}

			for(j = 1; j < 3; j++)
			{
				if(Position_equal(neighbor, Car_getPosition(cars[j])))
				{
					positionTaken = 1;
				}
			}

			if(!positionTaken)
			{
				newState = State_new(neighbor, newSpeed, State_getBoosts(state) - useBoost, map);
				State_setRealCost(newState, cost);
				State_setParent(newState, state);

				Heap_insert(openSet, newState);
			}

			Vector_delete(newSpeed);
			Vector_delete(acceleration);
			Position_delete(neighbor);
		}

		List_insert(closedSet, state);

		List_empty(neighbors);
		List_delete(neighbors);
	}

	while(state != NULL)
	{
		List_insert(finalPath, Position_copy(State_getPosition(state)));

		state = State_getParent(state);
	}

	List_head(closedSet);
	while(!List_isEmpty(closedSet))
	{
		state = List_getCurrent(closedSet);
		List_remove(closedSet);
		State_delete(state);
	}

	List_delete(closedSet);

	while((state = Heap_extractMin(openSet)) != NULL)
	{
		State_delete(state);
	}

	Heap_delete(openSet);

	LOGINFO("A* is done mate");

	return finalPath;
}
Esempio n. 5
0
File: vector.c Progetto: ptt/pttbbs
void
Vector_clear(struct Vector *self, const int size)
{
    Vector_delete(self);
    Vector_init(self, size);
}