Dear Sir,

Could you tell me what is (are) wrong of the following answer:-

Question:-

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,1or 2. Now convert the computer’s number to R, P or S using a case statement. Tell the user what the computer picked. Draw an Activity Diagram first.
Can you finish the rock, paper, scissors game? One solution to working out who won is the following:
check whether it was a draw ie player=computer
else check if the player won ie (player = R and computer = S) or (player = S and computer = P) or (player = P and computer = R)
else the only other alternative is that the computer won.
You should be able to this with one nested if

program RPSii;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ourcrt;

var
        Computer : integer;
        Choice, compChar : char;

begin

randomize;
   repeat
     writeln('Welcome to play the game of 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 (3);

  case Computer of
  0:    writeln('Computer have chosen Rock');

  1:    writeln('Computer have chosen Paper');

  2:    writeln('Computer have chosen Scissors');
  end;
  if ((Choice='R') and (compChar='S')) or ((Choice='P') and (compChar='R')) or ((Choice='S') and (compChar='P')) then
  begin
    writeln('You win');
  end
  else
    if ((Choice='R') and (compChar='R')) or ((Choice='P') and (compChar='P')) or ((Choice='S') and (compChar='S'))  then
    begin
      writeln('You draw');
    end
    else
      writeln('You loss');
      readln;
end.

Could you tell me what is (are) wrong of the above answer because when I execute the program, it always give me the wrong answer 'you loss'.

Hi

You're assigning the value to "Computer" and testing "compChar"...

Regards

Hi

You're assigning the value to "Computer" and testing "compChar"...

Regards

Hi Placo,

Thank you for your kind reply but could you tell me what can I do?

Cheers,

Hi again

the compChar variable is not being used.
You should either assign 'R', 'S' or 'P' to compChar inside the "case" structure or use Computer to test game result, e.g.

((Choice='R') and (Computer=2)) //2 = 'S'

HTH

yes ...yes

...
...
  if ((Choice='R') and (Computer=2)) or ((Choice='P') and (Computer=0)) or ((Choice='S') and (Computer=1)) then
  begin
    writeln('You win');
  end
  else
    if ((Choice='R') and (Computer=0)) or ((Choice='P') and (Computer=1)) or ((Choice='S') and (Computer=2))  then
    begin
...
...

:D

Hi Pico,

Thank you for your help.

I have tried to do it in your way but it doesn't work.

whole solution :D

program RPSii;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
        Computer : integer;
        Choice : char;

begin

randomize;
   repeat
     writeln('Welcome to play the game of 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(3);

  case Computer of
  0:    writeln('Computer have chosen Rock');

  1:    writeln('Computer have chosen Paper');

  2:    writeln('Computer have chosen Scissors');
  end;
  if ((Choice='R') and (Computer=2)) or ((Choice='P') and (Computer=0)) or ((Choice='S') and (Computer=1)) then
  begin
    writeln('You win');
  end
  else
    if ((Choice='R') and (Computer=0)) or ((Choice='P') and (Computer=1)) or ((Choice='S') and (Computer=2))  then
    begin
      writeln('You draw');
    end
    else
      writeln('You loss');
      readln;
end.

Hi friend,:) Seems you've forgot to evaluate CompChar!!!
Just change the second CASE statement (for computer choice) with for example this one:
(How ever you can evaluate CompChar in different ways, even you can change some parts in your program and it works even not using CompChar itself.)

case Computer of
0:
begin
writeln('Computer have chosen Rock');
compChar:= 'R';
end;
1:
begin
writeln('Computer have chosen Paper');
compChar:= 'P';
end;
2:
begin
writeln('Computer have chosen Scissors');
compChar:= 'S';
end;
end;

Bingo!! Tested To Modifier's Computer!!! :D It works properly.

Wish You Success. ;)
Mehrdad Nekoei.

Dear Mehrdad Nekoei and all the brothers

Thank you for your help on this program. Could you tell me what is the meaning of 1-3 at the case statement? I have tried to revise the order 3,2,1 but I can get the answer.

Cheers,

Dear ALL,

I still got some of the problem on the understand of the following coding:

program RPSii;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
        Computer : integer;
        Choice, compChar : char;

begin

randomize;
   repeat
     writeln('Welcome to play the game of 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(3);

case Computer of
0:
begin
  writeln('Computer have chosen Rock');
  compChar:= 'R';
end;
1:
begin
  writeln('Computer have chosen Paper');
  compChar:= 'P';
end;
2:
begin
  writeln('Computer have chosen Scissors');
  compChar:= 'S';
end;
end;

  if ((Choice='R') and (compChar='S')) or ((Choice='P') and (compChar='R')) or ((Choice='S') and (compChar='P')) then
  begin
    writeln('You win');
  end
  else
    if ((Choice='R') and (compChar='R')) or ((Choice='P') and (compChar='P')) or ((Choice='S') and (compChar='S'))  then
    begin
      writeln('You draw');
    end
    else
      writeln('You loss');
      readln;
end.

Hope this helps.

program RPSii;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  Computer : integer;
  Choice, compChar : char;

begin

{== Assures a good random number is generated later on in the program ==}
randomize;

{== Keep prompting for input until user enters 'R' 'P' or 'S' ==}
repeat
  writeln('Welcome to play the game of Rock(R), Paper(P), Scissors(S)');
  readln(Choice);
until (Choice = 'R') or (Choice = 'P') or (Choice = 'S');

{== Show text corresponding to the users selection ==}
case Choice of
  'R': writeln('You have chosen Rock');
  'P': writeln('You have chosen Paper');
  'S': writeln('You have chosen Scissors');
end;

{== Crank out a random number between 0 and 2 inclusive ==}
{== Delphi random ints return from 0 to (arg - 1)       ==}
{== http://www.delphibasics.co.uk/RTL.asp?Name=Random   ==}
Computer:= random(3);

{== Set compChar and display info corresponding to computer "selection" ==}
case Computer of
  0:
  begin
    writeln('Computer have chosen Rock');
    compChar:= 'R';
  end;

  1:
  begin
    writeln('Computer have chosen Paper');
    compChar:= 'P';
  end;

  2:
  begin
    writeln('Computer have chosen Scissors');
    compChar:= 'S';
  end;
end;

if ((Choice='R') and (compChar='S')) or   {Rock beats Scissor}
   ((Choice='P') and (compChar='R')) or   {Paper beats Rock}
   ((Choice='S') and (compChar='P')) then {Scissor beats Paper}
  writeln('You win')
else
  if Choice=compChar then {Choices equal (draw)(easier to read)}
    writeln('You draw')
  else                    {Not draw, not win, so must be loss}
    writeln('You loss');

{== Wait for user to press enter before exiting so they can see result ==}
readln;
end.
This question has already been answered. Start a new discussion instead.