PROGRAM RockPaperScissors (Input,Output);

(* Plays "Rocks, scissors, paper" game.
   David Wills
   CMIS 150  SHAPE  Term 5
   Turbo Pascal 5.5   *)

TYPE
   PlayType = (Rock,Paper,Scissors);
   Outcome = (Draw,Player1,Player2);

VAR
   Mine, Yours : PlayType;
   Winner : Outcome;


(****************************************************)
PROCEDURE GetHands (VAR One, Two : Playtype);
Var
   Ch : Char;
Begin
   REPEAT
     Write('Enter player 1s hand: ');
     Readln(Ch);
     CASE Ch OF
       'r', 'R' : One := Rock;
       'p', 'P' : One := Paper;
       's', 'S' : One := Scissors;
       ELSE     Writeln('Not valid input. Please try again.');
     End;
   UNTIL Ch IN ['r','R','p','P','s','S'];

   REPEAT
     Write('Enter player 2s hand: ');
     Readln(Ch);
     CASE Ch OF
       'r', 'R' : Two:= Rock;
       'p', 'P' : Two := Paper;
       's', 'S' : Two := Scissors;
       ELSE      Writeln('Not valid input. Please try again.');
     End;
   UNTIL Ch IN ['r','R','p','P','s','S'];
End;



(****************************************************)
(* Determine which hand wins.
   Rock beats scissors,
   scissors beats paper,
   paper beats rock.     *)
PROCEDURE PlayHands (Hand1, Hand2 : PlayType;
                     VAR Winner : Outcome);
Begin
   IF Hand1 = Hand2 THEN
      Winner := Draw
   ELSE IF ((Hand1=Rock) AND (Hand2=Scissors)) OR
           ((Hand1=Paper) AND (Hand2=Rock)) OR
           ((Hand1=Scissors) AND (Hand2=Paper)) THEN
              Winner := Player1
   ELSE
      Winner := Player2;
End;



(***************************************************)
FUNCTION Continue : Boolean;
Var
   Answer : Char;
Begin
     Writeln;
     Write('Play again? (Y or N) ');
     Readln(Answer);
     IF (Answer='Y') OR (Answer='y') THEN
        Continue := True
     ELSE
        Continue := False;
End;




(**************************************************)
BEGIN
   REPEAT
     GetHands(Mine,Yours);
     PlayHands(Mine,Yours,Winner);
     CASE Winner OF
      Draw : Writeln('Game is a tie.');
      Player1 : Writeln('Player 1 wins');
      Player2 : Writeln('Player 2 wins');
     End;
   UNTIL NOT Continue;
END.
