Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program rockPaperScissors;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- const
- MOVES = 20;
- var
- username: string;
- outfile: textfile;
- i, m, c: integer;
- choice: char;
- e: boolean;
- score: array[0..1] of integer;
- procedure construct;
- begin
- writeln;
- writeln(' / * * * * * * * * * * * * * * * * * * * * * \');
- writeln(' / * * * * * * * * * * * * * * * * * * * * * \');
- writeln(' / * * * * \');
- writeln(' / * * * * \');
- writeln(' / * * ROCK, PAPER, SCISSORS * * \');
- writeln(' / * * * * \');
- writeln(' / * * * * \');
- writeln(' / * * * * * * * * * * * * * * * * * * * * * \');
- writeln(' / * * * * * * * * * * * * * * * * * * * * * \');
- writeln;
- write('Enter your name: ');
- readln(username); username := uppercase(username);
- assignfile(outfile, 'rps.out');
- rewrite(outfile);
- writeln(outfile, 'PLAYER ONE: ', username);
- writeln(outfile);
- end;
- function pickMove(): integer;
- begin
- randomize;
- result := random(3);
- end;
- function compare(player, computer: integer): integer;
- begin
- if (player = 0) AND (computer = 1) then compare := 0 // R v. P: loss
- else if (player = 0) AND (computer = 2) then compare := 1 // R v. S: win
- else if (player = 0) AND (computer = 0) then compare := 2 // R v. R: draw
- else if (player = 1) AND (computer = 0) then compare := 1 // P v. R: win
- else if (player = 1) AND (computer = 2) then compare := 0 // P v. S: loss
- else if (player = 1) AND (computer = 1) then compare := 2 // P v. P: draw
- else if (player = 2) AND (computer = 0) then compare := 0 // S v. R: loss
- else if (player = 2) AND (computer = 1) then compare := 1 // S v. P: win
- else if (player = 2) AND (computer = 2) then compare := 2 // P v. P: draw
- else compare := -1;
- end;
- function move(name: integer): string;
- begin
- case name of
- 0: move := 'ROCK';
- 1: move := 'PAPER';
- 2: move := 'SCISSORS';
- else move := 'NULL';
- end;
- end;
- function action(x, y: integer): string;
- begin
- if (x = 0) AND (y = 1) then action := 'COVERS'
- else if (x = 0) AND (y = 2) then action := 'BEATS'
- else if (x = 1) AND (y = 0) then action := 'COVERS'
- else if (x = 1) AND (y = 2) then action := 'CUTS'
- else if (x = 2) AND (y = 0) then action := 'BEATS'
- else if (x = 2) AND (y = 1) then action := 'CUTS'
- else action := 'NULL';
- end;
- procedure destruct();
- begin
- writeln(outfile);
- writeln(outfile, 'Made by Michael MacLean 1/6/15');
- close(outfile);
- readln;
- end;
- begin
- construct();
- for i := 1 to MOVES do begin
- e := true;
- while e = true do begin
- if(i = 1) then write('Type ''R'' for ROCK, ''P'' for PAPER, or ''S'' for SCISSORS. ')
- else write('Please make a selection ');
- readln(choice); choice := upcase(choice);
- case choice of
- 'R': begin m := 0; e := false; end;
- 'P': begin m := 1; e := false; end;
- 'S': begin m := 2; e := false; end;
- else begin writeln('[ERROR] Invalid selection. '); writeln; e := true; end;
- end; // case
- end; // while
- writeln(username, ' CHOOSES ', move(m));
- writeln(outfile, username, ': ', move(m));
- c := pickMove();
- writeln('COMPUTER CHOOSES ', move(c));
- writeln(outfile, 'COMPUTER: ', move(c));
- if compare(m, c) = 2 then begin // DRAW
- writeln('IT''S A DRAW!');
- writeln(outfile, 'IT''S A DRAW!');
- end
- else if compare(m, c) = 1 then begin // PLAYER ONE'S WIN
- writeln(move(m), ' ', action(m, c), ' ', move(c), ': ', username,'''S WIN');
- writeln(outfile, move(m), ' ', action(m, c), ' ', move(c), ': ', username, '''S WIN');
- score[0] := score[0]+1;
- end
- else if compare(m, c) = 0 then begin // COMPUTER'S WIN
- writeln(move(c), ' ', action(c, m), ' ', move(m), ': COMPUTER''S WIN');
- writeln(outfile, move(c), ' ', action(c, m), ' ', move(m), ': COMPUTER''S WIN');
- score[1] := score[1]+1;
- end;
- writeln; writeln(outfile);
- end; // for
- writeln(outfile, 'FINAL SCORE:');
- writeln(outfile, username, ': ', score[0]);
- writeln(outfile, 'COMPUTER: ', score[1]);
- if score[0] > score[1] then writeln(username, ' WINS WITH A SCORE OF ', score[0], ' OUT OF ', MOVES, '!')
- else if score[1] > score[0] then writeln('COMPUTER WINS WITH A SCORE OF ', score[1], ' OUT OF ', MOVES, '!')
- else if score[0] = score[1] then writeln(username, ' TIED WITH A SCORE OF ', score[0], ' OUT OF ', MOVES, '!');
- destruct();
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement