Sunday, November 30, 2014

0007 - Prolog Adventure Game

I hate the Prolog language. Just going to get that out there to start this.

Anyway, I've finally finished the assignment given to my class a few weeks ago: take a text-based adventure/maze game that is written in Prolog, and modify the code so that different configurations can be used in the game. Also, this rewrite should include the capability of having a list of commands passed to the game for an "autopilot" type of deal.

Simple, right? No, because I hate Prolog because it is stupid and sucks and I'll probably play with it later and find out I secretly like it.

Anyway, I decided to add a few of the more interesting bits of code on here in case anyone ever wants to see it:

The "go" and "fileManager" clauses. The first "go" clause simply opens up the configuration file (a comma-delimited plaintext file), reads it into the List labeled "Data," and passes Data to the fileManager clause. The fileManager clause opens up a new stream for writing the results of the game, creates an atom whose value is set to the next file name, and calls the next go clause. This go clause reads in the commands from the file, then runs through them iteratively for each configuration. Once all of that ends, everything kicks back to fileManager, which sends the next file.

go :-
  csv_read_file('/home/jonathan/Desktop/Prolog/configurations.txt',Data),
  fileManager(Data).
 
fileManager(Data) :-
  open('/home/jonathan/Desktop/Prolog/commands_v1Results.txt',write,Stream1),
  File1='/home/jonathan/Desktop/Prolog/commands_v1',
  go(Data,Stream1,0,0,File1),
  open('/home/jonathan/Desktop/Prolog/commands_v2Results.txt',write,Stream2),
  File2='/home/jonathan/Desktop/Prolog/commands_v2',
  go(Data,Stream2,0,0,File2),
  open('/home/jonathan/Desktop/Prolog/commands_v3Results.txt',write,Stream3),
  File3='/home/jonathan/Desktop/Prolog/commands_v3',
  go(Data,Stream3,0,0,File3),
  open('/home/jonathan/Desktop/Prolog/commands_v4Results.txt',write,Stream4),
  File4='/home/jonathan/Desktop/Prolog/commands_v4',
  go(Data,Stream4,0,0,File4),
  open('/home/jonathan/Desktop/Prolog/commands_v5Results.txt',write,Stream5),
  File5='/home/jonathan/Desktop/Prolog/commands_v5',
  go(Data,Stream5,0,0,File5).
   
go([],_,_,_,_).

go([row(X,Y)|Data],Stream,M,S,File) :-
  retractall(at(_,_)), % clean up from previous runs
  area(X,Loc1),
  area(Y,Loc2),
  assert(at(you,valley)),
  assert(at(ogre,Loc1)),
  assert(at(treasure,Loc2)),
  open(File, read, Str),
  read_commands(Str, Commands),
  close(Str),
  report,
  S1 is S+1,
  main(Commands,Data,Stream,M,S1,File).
 
read_commands(Stream, []) :-
  at_end_of_stream(Stream).
   
read_commands(Stream, [X|L]) :-
  \+ at_end_of_stream(Stream),
  read(Stream, X),
  read_commands(Stream, L).


That's really the trickiest part of the code. Every other change mainly had to deal with writing to the Stream rather than the console (which may not have actually been a requirement for the assignment...).

Enjoy!

No comments:

Post a Comment