Demo entry 3298937

robot in a plane

   

Submitted by anonymous on Dec 08, 2015 at 03:09
Language: Prolog. Code size: 5.3 kB.

% MEMBROS DO GRUPO:
% Fabio Hecktheuer, Pedro Gonçalves, Pedro Halmenschlager

% ----------------------------------------------------------------------------------- - - - - - - - -

% Há 1 bug que não encontramos a causa, mas que, no entanto, é bem específico e não
% impede o prosseguimento da execução:
% Quando há a primeira instrução que mande o robô ir para o sul, ele efetivamente vai.
% No entanto, o avizo "HIT THE WALL!" tende a ser impresso nesse caso específico, apesar de a 
% posição ser atualizada. (outras idas para o sul ou qualquer outro movimento que não bata numa
% parede não gera esse avizo).

% ----------------------------------------------------------------------------------- - - - - - - - -

%Set robot's position to use dynamic positioning
:- dynamic(position/3).

% set the robot's starting position
position(50, 50, leste).

% assert new position
setPosition(X, Y, F) :- assertz(position(X, Y, F)).

% setting the limits for any of the plane's axis
axis(X) :- (X > 0), (X < 101).

% setting positions to be taken as "black positions" (a.k.a. walls)
blackPositions(X, Y) :- 
(
	% limits
	((X = 1), axis(Y), write('\033[7;31mHIT THE WALL!\033[00m'), nl, !, fail); 
	((X = 100), axis(Y), write('\033[7;31mHIT THE WALL!\033[00m'), nl, !, fail);
	((Y = 1), axis(X), write('\033[7;31mHIT THE WALL!\033[00m'), nl, !, fail); 
	((Y = 100), axis(X), write('\033[7;31mHIT THE WALL!\033[00m'), nl, !, fail);
	% other walls
	((X = 50), (Y > 51), write('\033[7;31mHIT THE WALL!\033[00m'), nl, !, fail ); 
	((X = 50), (Y < 25), write('\033[7;31mHIT THE WALL!\033[00m'), nl, !, fail ); 
	true
).
	
% printing
printAll([]) :- nl.
printAll([Head | Tail]) :- write(Head), write(' '), printAll(Tail).

% face to given direction
faceTo(Face)  :- faceVer(Face); faceHor(Face).
faceVer(Face) :- (Face=north; Face=south; Face=norte; Face=sul).
faceHor(Face) :- (Face=west; Face=east; Face=leste; Face=oeste).

% full turn command
turnTo([Turn, In, Face | [] ]) :- (Turn=turn; Turn=vire), (In=to; In=ao), faceTo(Face), true.

% move to position
move(X, Y) :- axis(X), axis(Y).

% full move command
moveTo(W, Num, Cells, In, Face) :-	(W=walk; W=ande; W=move; W=mover), number(Num), 
					(Cells=cells; Cells=celulas; Cells=positions; Cells=posições), 
					(In=ao; In=to), faceTo(Face), 
					true.

add(X, Y, Result) :- Result is X + Y.
sub(X, Y, Result) :- Result is X - Y.

% register movement
regMove(Num, Face) :-
robotPosition(H, V, _),
% get current position to evaluate the new one
% resolutions to the axis and correct face
(
	(sub(H, Num, NH1), blackPositions(NH1, V), axis(NH1), (Face=oeste; Face=west),  setPosition(NH1, V, Face));
	(add(H, Num, NH2), blackPositions(NH2, V), axis(NH2), (Face=leste; Face=east),  setPosition(NH2, V, Face));
	(add(V, Num, NV1), blackPositions(H, NV1), axis(NV1), (Face=north; Face=norte), setPosition(H, NV1, Face));
	(sub(V, Num, NV2), blackPositions(H, NV2), axis(NV2), (Face=south; Face=sul),   setPosition(H, NV2, Face))
),
% verticals/horizontals calcs.
% after that position(H, V, F) to update the values
currentPosition(_).

% auxiliar functors to get all robot positions and faces 
% (H, V and F are out params.)
robotPosition(H, V, F) :-	findall(X, position(X, _, _), HT), last(HT, H), 
				findall(X, position(_, X, _), VT), last(VT, V), 
				findall(X, position(_, _, X), FT), last(FT, F).

% robot current possition
currentPosition(_) :- robotPosition(H, V, F), write('Position: ('), write(H), write(', '), write(V), write(')'), nl.

% robot takes a movement instruction 
% (formated as in the example at the top of this file)
robot(Arg1, Arg2, Arg3, Arg4, Arg5) :-
(
	printAll([Arg1, Arg2, Arg3, Arg4, Arg5]),
	(moveTo(Arg1, Arg2, Arg3, Arg4, Arg5), 
	regMove(Arg2, Arg5))
); 
currentPosition(_).

% draw a mini-map of the 100x100 plane where the robot is at
% give informations about the beginning status
:- nl, 
   write('                  START           '), nl,
   write('      ┏━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓ where:'), nl, 
   write('      ┃             ┃             ┃ \'\033[0;34m▸\033[00m\' is the robot, which is at'), nl,
   write('      ┃             ┃             ┃ coordinates (50, 50, east)'), nl, 
   write('      ┃             ┃             ┃'), nl, 
   write('      ┃             ┃             ┃ there are walls on all the limits'), nl,
   write('      ┃             ┃             ┃ and also at:'), nl, 
   write('    \033[0;30mY\033[00m ┃             \033[0;34m▸\033[00m             ┃'),
   write(' coordinates (50, 52) to (50, 100)'), nl,
   write('      ┃                           ┃ coordinates (50, 75) to (50, 100)'), nl, 
   write('      ┃                           ┃'), nl, 
   write('      ┃             ┳             ┃ space = white positions'), nl,
   write('      ┃             ┃             ┃ walls = black positions'), nl, 
   write('      ┃             ┃             ┃'), nl, 
   write('      ┃             ┃             ┃'), nl, 
   write('      ┗━━━━━━━━━━━━━┻━━━━━━━━━━━━━┛'), nl,
   write('                    \033[0;30mX\033[00m               '), nl,
   nl, 
   write('-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - - - '), nl.

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).