Dear Sir,

I have got a serious problem to understanding of the answer.

The question is:
Create the first half of a game of rock, paper, scissors. Ask the user for R,P or S. Get the computer to generate 0,1 or 2. Now convert the computer's number to R, P or S using a case statement. Tell the user wht the computer picked.

The following is my answer:

program RPS5;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ourcrt;

var
        Computer : integer;
        Choice, Move : char;

begin

randomize;
   repeat
     writeln('Rock(R), Paper(P), Scissors(S)');
     readln(Choice);
   until (Choice = 'R') or (Choice = 'P') or (Choice = 'S');

case Choice of
'R': writeln('You have chosen Rock');
'P': writeln('You have chosen Paper');
'S': writeln('You have chosen Scissors');
end;

Computer:= random (2);

case Computer of
0:    begin   writeln('Computer have chosen Rock');
              Move:='R';
                if  Move=choice then begin Writeln('Draw') end
                  else
                   if Choice='S' then begin Writeln ('You lose') end
                     else writeln('You win') end;

1:    begin   writeln('Computer have chosen Paper');
              Move:='P';
                if  Choice=Move then begin Writeln('Draw') end
                 else
                  if Choice='R' then begin Writeln ('You lose') end
                   else writeln('You win') end;

2:    begin   writeln('Computer have chosen Scissors');
              Move:='S';
                if  Choice=Move then begin Writeln('Draw') end
                 else
                  if Choice='P' then begin Writeln ('You lose') end
                   else writeln('You win') end;
end;

readln;
end.

I know that user can choose R, P or S and computer generate 0,1 or 2. But could you tell me why 'if Choice='S' then begin Writeln ('You lose')', ...

Cheers,

Dani AI

Generated

posted a correct working idea but the branches need to be read as the game rules, not as magic. Rock beats Scissors, Scissors beats Paper, Paper beats Rock. In the branch where the computer chooses Rock (the case for the integer 0 in the program), the code sets the computer move to 'R' and then checks: if the moves match -> draw; else if the player picked Scissors -> player loses (because Rock > Scissors); otherwise the player must have picked Paper and so wins. That is why the test for Choice = 'S' leads to "You lose" in the Rock branch.

As pointed out, another important bug in the posted version is the random call: Random(2) only yields 0 or 1, so the Scissors branch (2) never runs. Use Random(3) so the generator can return 0, 1 or 2. Also take input case into account (convert to uppercase) and avoid naming a variable Move (it hides a common routine name); use CompMove or an enumerated type instead.

A more compact and less error-prone approach is to map the moves to integers (R=0, P=1, S=2) and use modular arithmetic to decide the result. This reduces duplicated if/else blocks:

function Outcome(user, comp: Integer): Integer;
begin
  if user = comp then Exit(0);              // 0 = draw
  if (user + 1) mod 3 = comp then Exit(-1); // -1 = user loses
  Exit(1);                                   // 1 = user wins
end;

Final checklist: call Randomize once at program start, use Random(3) for three outcomes, normalize input with UpCase, validate the first character only, and group multiple statements in case branches with begin..end.

Recommended Answers

All 3 Replies

And could you tell me why '0: begin writeln('Computer have chosen Rock')' as well??

Cheers,

Tony

Hi Turbomen.
On my land,hungary,there is a rule.
PAPER stronger then ROCK
ROCK stronger then SCISSORS
SCISSORS stronger then PAPER
think of it as precedence rule... :D
by the way, computer:=Random(2); will always 1 or 0 cause

Generates random numbers within a specified range.
Unit : System
Category: random number routines
syntax: function Random [ ( Range: Integer) ];
In Delphi code, Random returns a random number within the range 0 <= X < Range. If Range is not specified, the result is a real-type random number within the range 0 <= X < 1.
To initialize the random number generator, add a single call Randomize or assign a value to the RandSeed variable before making any calls to Random.

so change your code like
computer:=Random(3); the result will 0 or 1 or 2 :D

case skeleton is
Case var_name Of
0:command;
1:command;
2:command;
Else command;
End;
But if you want to put more than one command,then you have to put them between begin..end;
Case var_name Of
0:Begin
command1;
command2;
End;
1:command;
....
....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.