RockPaperScissors

by Hasaan Ausat: Y12 Age ~16

Introduction

The user plays against the computer (which uses random numbers to make its choice) at rock-paper-scissors. The program keeps track of the score and, when the user decides to quit, gives the thumbs-up, thumbs-down or thumbs up and down depending on the final score. A nice touch. This web version is now available.

You can compare the code below with a RemObjects C# version.

The Program

program RockPaperScissors;
{$APPTYPE CONSOLE}

{
    Copyright (c) 2010 Hasaan Ausat
    
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/
}

uses
  SysUtils;
var
   playcount:integer;
   playercount:integer;
   compcount:integer;
   tiecount:integer;

procedure Game;

var
   number:integer;
   answer:integer;
   choice:char;

begin
   randomize;
   writeln('|-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*|');
   writeln('|                                |');
   writeln('| WELCOME TO ROCK PAPER SCISSORS |');
   writeln('|                                |');
   writeln('|*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-|');
   writeln;
   writeln('Press enter to continue');
   readln;
   playcount:=0;
   compcount:=0;
   playercount:=0;
   tiecount:=0;
   repeat
      writeln('Rock is 0, Paper is 1, Scissors is 2');
      writeln;
      readln(number);
      if number > 2 then
      begin
         repeat
            writeln('Rock is 0, Paper is 1, Scissors is 2');
            readln(number);
         until number < 3;
      end;
      answer:=random(3);
      writeln;
      writeln('I have picked ', answer);
      writeln;
      if (answer=0) and (number=0) then
      begin
         writeln('|-------|');
         writeln('| Tie!! |');
         writeln('|-------|');
         writeln;
         playcount:= playcount+1;
         tiecount:= tiecount+1;
      end;
      if (answer=0) and (number=1) then
      begin
         writeln('|------------|');
         writeln('| You win :) |');
         writeln('|------------|');
         writeln;
         playcount:= playcount+1;
         playercount:= playercount+1;
      end;
      if (answer=1) and (number=0) then
      begin
         writeln('|--------------|');
         writeln('| You  lose :( |');
         writeln('|--------------|');
         writeln;
         playcount:= playcount+1;
         compcount:= compcount+1;
      end;
      if (answer=1) and (number=1) then
      begin
         writeln('|-------|');
         writeln('| Tie!! |');
         writeln('|-------|');
         writeln;
         playcount:= playcount+1;
         tiecount:= tiecount+1;
      end;
      if (answer=1) and (number=2) then
      begin
         writeln('|------------|');
         writeln('| You win :) |');
         writeln('|------------|');
         writeln;
         playercount:= playercount+1;
         playcount:= playcount+1;
      end;
      if (answer=2) and (number=1) then
      begin
         writeln('|--------------|');
         writeln('| You  lose :( |');
         writeln('|--------------|');
         writeln;
         playcount:= playcount+1;
         compcount:= compcount+1;
      end;
      if (answer=2) and (number=2) then
      begin
         writeln('|-------|');
         writeln('| Tie!! |');
         writeln('|-------|');
         writeln;
         playcount:= playcount+1;
         tiecount:= tiecount+1;
      end;
      if (answer=2) and (number=0) then
      begin
         writeln('|------------|');
         writeln('| You win :) |');
         writeln('|------------|');
         writeln;
         playercount:= playercount+1;
         playcount:= playcount+1;
      end;
      if (answer=0) and (number=2) then
      begin
         writeln('|--------------|');
         writeln('| You  lose :( |');
         writeln('|--------------|');
         writeln;
         playcount:= playcount+1;
         compcount:= compcount+1;
      end;
      writeln('Would you like to play again? y/n');
      readln(choice);
      writeln;
   until choice='n';
   writeln('You have played ',playcount,' times');
   writeln;
   writeln('You won ',playercount,' times');
   writeln;
   writeln('You lost ',compcount,' times');
   writeln;
   writeln('We drew ',tiecount,' times');
   writeln;
   readln;
   if compcount < playercount then
   begin
      writeln('   _');
      writeln(' ( ((');
      writeln('  \ =\');
      writeln(' __\_ `-\');
      writeln('(____))(  \----');
      writeln('(____)) _');
      writeln('(____))');
      writeln('(____))____/----');
      writeln;
   end;
   if compcount > playercount then
   begin
      writeln('  _____');
      writeln('((____ \----');
      writeln('((____');
      writeln('((____');
      writeln('((____   ----');
      writeln('     /  /');
      writeln('    (_((');
      writeln;
   end;
   if compcount = playercount then
   begin
      writeln('   _');
      writeln(' ( ((');
      writeln('  \ =\');
      writeln(' __\_ `-\');
      writeln('(____))(  \----');
      writeln('(____)) _');
      writeln('(____))');
      writeln('(____))____/----');
      writeln('  _____');
      writeln('((____ \----');
      writeln('((____');
      writeln('((____');
      writeln('((____   ----');
      writeln('     /  /');
      writeln('    / =/');
      writeln('   (_((');
      writeln;
   end;
   writeln('Goodbye, Hope you had fun!!!!!!!!!');
   writeln;
   readln;
   writeln('RockPaperScissors (c) 2009 - Huskimo');
   readln;
end;

begin
   Game;
   readln;
end.

Remarks

Can you think of a program like this that you could write?

Programming - a skill for life!

Student programs to inspire you!