Dear Sir,
I have got the answer of the following question but I do not understand why it has been done in this way:
3. Your task is to create a game of Heads or Tails against the computer.
Write a program that asks the user for Heads or Tails, then simulates a toss of a coin and then tells the user whether they have guessed correctly.
Question:-
First, draw an activity chart that matches the pseudocode provided below.
Convert the pseudocode to Delphi:
DISPLAY a title for the program
Issue the statement that stops your random numbers always coming out as zero
Ask the user for Heads or Tails
INPUT guess
Generate a random number less than 2 call this computer
IF computer = 0 THEN
SET coin = h
OUTPUT Computer tossed a Head
ELSE
SET coin = t
OUTPUT Computer tossed a Tail
ENDIF
IF coin = guess THEN
OUTPUT You won
ELSE
OUTPUT You lost
ENDIF
Delay the program so that you can see the results
Answer:-
program HeadTaili;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
guess: integer;
flip, throw: char;
begin
randomize;
writeln('Welcome to the guessing game');
writeln('Please guesses the outcome of a coin');
readln(flip);
guess:=random(2);
if (guess=0) then
begin
throw:='h'
end
else
begin
throw:='t'
end;
if (flip=throw) then
begin
writeln('You win');
end
else
begin
writeln('You lose');
end;
readln;
end.