Dear Sir / Madam,

The following is the game of Rock, Paper, Scissors. But I don't know why computer is always choose Rock.

My Answer:-

program RPS5;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ourcrt;

var
        Computer : integer;
        Choice, Move : char;

begin

   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;

sleep (10000);
end.

Could you tell me what is wrong of my answer,

Cheers,

Dani AI

Generated

Good compact summary and thanks to for cleaning up the formatting. The behaviour you described comes from two subtle issues that are easy to miss and one neat simplification you can use to make the win/lose logic clearer.

First, check the random-selection logic. The random call takes an upper bound N and yields integers from 0 to N-1, so to produce three distinct outcomes you need to pass 3. Also make sure the pseudo-random generator is seeded once at program start; otherwise you can get the same sequence every run. Adding a short diagnostic print of the generated integer will quickly show whether the third outcome is ever produced.

Second, handle the input robustly. Reading a single char and comparing only against uppercase letters will reject lowercase input. Convert the entered char to uppercase (or accept both cases) and validate it before proceeding. That avoids the loop hanging or re-prompting unexpectedly.

Finally, consider simplifying the result logic. Map the three moves to 0..2 (for example: Rock=0, Paper=1, Scissors=2) and compute (player - computer + 3) mod 3. Result 0 = draw, 1 = player win, 2 = player lose. That reduces nested if/else branches and makes the code easier to test. Also use named constants or an enumerated type for the moves so comparisons are self-documenting.

Quick checklist to confirm:

  • Seed the RNG once at startup.
  • Use the correct upper bound so all three outcomes can occur.
  • Normalize input to a single case and validate it.
  • Add a short diagnostic print to confirm distribution while testing.
  • Consider mapping moves to numbers and use modular arithmetic for outcome.

These steps will make the behavior deterministic during debugging and correct in production.

Here is the fixed version of your program :)

(*
Dear Sir / Madam,

The following is the game of Rock, Paper, Scissors.
But I don't know why computer is always choose Rock.

My Answer:-
 *)
Program RPS5;

{$APPTYPE CONSOLE}

Uses
SysUtils;
var Computer : integer;
    Choice, Move : char;
Begin //main
   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;//case
   Randomize;  (* <-- you forget about randomize,not? *)
   Computer:= Random(2);
   Case Computer of
      0: Begin
            WriteLn('Computer have chosen Rock');
            Move:='R';
            If (Move=choice) Then Begin
               Writeln('Draw')
            End //'if' move
            Else Begin
               If Choice='S' Then Begin
                  WriteLn ('You lose')
               End //'if' choice
               Else WriteLn('You win');
            End;//else
         End;//case 0
      1: Begin
            WriteLn('Computer have chosen Paper');
            Move:='P';
            If (Choice=Move) Then Begin
               Writeln('Draw');
            End
            Else Begin
               If (Choice='R') Then Begin
                  Writeln ('You lose');
               End
               Else writeln('You win');
            End;
         End;//case 1
      2: Begin
           WriteLn('Computer have chosen Scissors');
           Move:='S';
           If (Choice=Move) Then Begin
              Writeln('Draw');
           End
           Else Begin
              If (Choice='P') Then Begin
                 Writeln ('You lose');
              End
              Else writeln('You win');
           End;
         End;//case 2
   End;//case main

  //sleep(10000);
  ReadLn; //I put this line,waiting for press the enter button
End.//of main program

(*
-=Note By FlamingClaw=-
I dont know your 'ourcrt',so I put it out  
Sorry for formatting your program,but now already readable...to me.
You forget about randomize!!!
-=Created By FlamingClaw=-
-=2009.04.04=-
*)
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.