
% Rock-paper-scissors-lizard-spock
%    played against a random ai
% To play a round enter the query
%    play.

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

% message(Msg) or message(Msg, Value)
% -----------------------------------
% display the message (and value if supplied) and a newline
message(Msg) :- name(Atom,Msg), write(Atom), nl.
message(Msg,Value) :- name(Atom,Msg), write(Atom), write(Value), nl.


% makeChoice(Choice)
% ------------------
% prompt the user until they give a valid response
makeChoice(Choice) :- repeat,
    message("Enter rock. paper. scissors. lizard. or spock."),
    read(C), playerChoice(C), Choice = C.
playerChoice(rock).
playerChoice(paper).
playerChoice(scissors).
playerChoice(lizard).
playerChoice(spock).


% aiChoice(C)
% -----------
% randomly choose rock, paper, scissors, lizard, or spock
aiChoice(C) :- random(0,5,M), selected(M,C).
selected(0,rock).
selected(1,paper).
selected(2,scissors).
selected(3,lizard).
selected(4,spock).

% result(PlayerChoice,AIChoice,Result)
% ------------------------------------
% given the player and ai choices, 
%    determines result as win, loss, draw (from player perspective)
% fails if invalid choices are given
  
% handle ties
result(Choice,Choice,draw).

% handle losing choices
result(rock,paper,loss).
result(rock,spock,loss).
result(paper,lizard,loss).
result(paper,scissors,loss).
result(scissors,rock,loss).
result(scissors,spock,loss).
result(lizard,scissors,loss).
result(lizard,rock,loss).
result(spock,lizard,loss).
result(spock,paper,loss).

% handle winning choices
result(rock,scissors,win).
result(rock,lizard,win).
result(paper,rock,win).
result(paper,spock,win).
result(scissors,paper,win).
result(scissors,lizard,win).
result(lizard,spock,win).
result(lizard,paper,win).
result(spock,rock,win).
result(spock,scissors,win).


% play
% ----
% play one round
play :- makeChoice(Plyr), aiChoice(Ai), result(Plyr,Ai,Res),
        message("Player chose: ", Plyr),
        message("AI chose: ", Ai),
        message("Result: ", Res).

