Advertisement
electricmaster

rockPaperScissors.dpr

Jan 19th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.63 KB | None | 0 0
  1. program rockPaperScissors;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. const
  9.   MOVES = 20;
  10. var
  11.   username: string;
  12.   outfile: textfile;
  13.   i, m, c: integer;
  14.   choice: char;
  15.   e: boolean;
  16.   score: array[0..1] of integer;
  17.  
  18. procedure construct;
  19. begin
  20.   writeln;
  21.   writeln('           / * * * * * * * * * * * * * * * * * * * * * \');
  22.   writeln('           / * * * * * * * * * * * * * * * * * * * * * \');
  23.   writeln('           / * *                                   * * \');
  24.   writeln('           / * *                                   * * \');
  25.   writeln('           / * *       ROCK, PAPER, SCISSORS       * * \');
  26.   writeln('           / * *                                   * * \');
  27.   writeln('           / * *                                   * * \');
  28.   writeln('           / * * * * * * * * * * * * * * * * * * * * * \');
  29.   writeln('           / * * * * * * * * * * * * * * * * * * * * * \');
  30.   writeln;
  31.   write('Enter your name: ');
  32.   readln(username); username := uppercase(username);
  33.   assignfile(outfile, 'rps.out');
  34.   rewrite(outfile);
  35.   writeln(outfile, 'PLAYER ONE: ', username);
  36.   writeln(outfile);
  37. end;
  38.  
  39. function pickMove(): integer;
  40. begin
  41.   randomize;
  42.   result := random(3);
  43. end;
  44.  
  45. function compare(player, computer: integer): integer;
  46. begin
  47.        if (player = 0) AND (computer = 1) then compare := 0 // R v. P: loss
  48.   else if (player = 0) AND (computer = 2) then compare := 1 // R v. S: win
  49.   else if (player = 0) AND (computer = 0) then compare := 2 // R v. R: draw
  50.   else if (player = 1) AND (computer = 0) then compare := 1 // P v. R: win
  51.   else if (player = 1) AND (computer = 2) then compare := 0 // P v. S: loss
  52.   else if (player = 1) AND (computer = 1) then compare := 2 // P v. P: draw
  53.   else if (player = 2) AND (computer = 0) then compare := 0 // S v. R: loss
  54.   else if (player = 2) AND (computer = 1) then compare := 1 // S v. P: win
  55.   else if (player = 2) AND (computer = 2) then compare := 2 // P v. P: draw
  56.   else compare := -1;
  57. end;
  58.  
  59. function move(name: integer): string;
  60. begin
  61.   case name of
  62.     0: move := 'ROCK';
  63.     1: move := 'PAPER';
  64.     2: move := 'SCISSORS';
  65.     else move := 'NULL';
  66.   end;
  67. end;
  68.  
  69. function action(x, y: integer): string;
  70. begin
  71.        if (x = 0) AND (y = 1) then action := 'COVERS'
  72.   else if (x = 0) AND (y = 2) then action := 'BEATS'
  73.   else if (x = 1) AND (y = 0) then action := 'COVERS'
  74.   else if (x = 1) AND (y = 2) then action := 'CUTS'
  75.   else if (x = 2) AND (y = 0) then action := 'BEATS'
  76.   else if (x = 2) AND (y = 1) then action := 'CUTS'
  77.   else action := 'NULL';
  78. end;
  79.  
  80. procedure destruct();
  81. begin
  82.   writeln(outfile);
  83.   writeln(outfile, 'Made by Michael MacLean 1/6/15');
  84.   close(outfile);
  85.   readln;
  86. end;
  87.  
  88. begin
  89.   construct();
  90.  
  91.   for i := 1 to MOVES do begin
  92.     e := true;
  93.     while e = true do begin
  94.       if(i = 1) then write('Type ''R'' for ROCK, ''P'' for PAPER, or ''S'' for SCISSORS. ')
  95.      else write('Please make a selection ');
  96.      readln(choice); choice := upcase(choice);
  97.      case choice of
  98.         'R': begin m := 0; e := false; end;
  99.         'P': begin m := 1; e := false; end;
  100.         'S': begin m := 2; e := false; end;
  101.        else begin writeln('[ERROR] Invalid selection. '); writeln; e := true; end;
  102.      end; // case
  103.     end; // while
  104.  
  105.     writeln(username, ' CHOOSES ', move(m));
  106.     writeln(outfile, username, ': ', move(m));
  107.     c := pickMove();
  108.     writeln('COMPUTER CHOOSES ', move(c));
  109.     writeln(outfile, 'COMPUTER: ', move(c));
  110.     if compare(m, c) = 2 then begin  // DRAW
  111.       writeln('IT''S A DRAW!');
  112.       writeln(outfile, 'IT''S A DRAW!');
  113.     end
  114.     else if compare(m, c) = 1 then begin  // PLAYER ONE'S WIN
  115.       writeln(move(m), ' ', action(m, c), ' ', move(c), ': ', username,'''S WIN');
  116.       writeln(outfile, move(m), ' ', action(m, c), ' ', move(c), ': ', username, '''S WIN');
  117.       score[0] := score[0]+1;
  118.     end
  119.     else if compare(m, c) = 0 then begin // COMPUTER'S WIN
  120.       writeln(move(c), ' ', action(c, m), ' ', move(m), ': COMPUTER''S WIN');
  121.       writeln(outfile, move(c), ' ', action(c, m), ' ', move(m), ': COMPUTER''S WIN');
  122.       score[1] := score[1]+1;
  123.     end;
  124.     writeln; writeln(outfile);
  125.   end; // for
  126.  
  127.   writeln(outfile, 'FINAL SCORE:');
  128.   writeln(outfile, username, ': ', score[0]);
  129.   writeln(outfile, 'COMPUTER: ', score[1]);
  130.  
  131.        if score[0] > score[1] then writeln(username, ' WINS WITH A SCORE OF ', score[0], ' OUT OF ', MOVES, '!')
  132.   else if score[1] > score[0] then writeln('COMPUTER WINS WITH A SCORE OF ', score[1], ' OUT OF ', MOVES, '!')
  133.   else if score[0] = score[1] then writeln(username, ' TIED WITH A SCORE OF ', score[0], ' OUT OF ', MOVES, '!');
  134.  
  135.   destruct();
  136. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement