0

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.
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by FlamingClaw
1
{
 1,DISPLAY a title for the program
 2,Issue the statement that stops your random numbers always coming out as zero
 3,Ask the user for Heads or Tails
 4,INPUT guess
 5,Generate a random number less than 2 call this computer
 6,IF computer = 0 THEN
 7,SET coin = h
 8,OUTPUT Computer tossed a Head
 9,ELSE
10,SET coin = t
11,OUTPUT Computer tossed a Tail
12,ENDIF
13,IF coin = guess THEN
14,OUTPUT You won
15,ELSE
16,OUTPUT You lost
17,ENDIF
18,Delay the program so that you can see the results
}


Program HeadOrTailGame;

{$APPTYPE CONSOLE}

Uses
  SysUtils;


Var guess,     {choice of the user,one character}
    coin:Char;{the coin,cause it will contain a character 'H' or 'T'}
    computer:Integer;{cause this var stores a number between 0 or 1}

Begin {the main program}
     {
     1,DISPLAY a title for the program
     }
     WriteLn('Welcome to the Head or Tail Game!');
     WriteLn('---------------------------------');
     {
     2,Issue the statement that stops your random
     numbers always coming out as zero
     }
     Randomize;
     {
     3,Ask the user for Heads or Tails
     }
     WriteLn('(h)ead or (t)ail ?');
     Write('Please select h or t : ');
     {
     4,INPUT guess
     }
     ReadLn(guess);
     {
     5,Generate a random number less than 2 call this computer
     }
     computer:=Random(2);
     {
     6,IF computer = 0 THEN
     }
     If (computer = 0) Then Begin
        {
        7,SET coin = h
        }
        coin:='h';
        {
        8,OUTPUT Computer tossed a Head
        }
        WriteLn('Computer tossed a Head!');
     End {first part of IF}
     {
     9,ELSE
     }
     Else Begin
             {
             10,SET coin = t
             }
             coin:='t';
             {
             11,OUTPUT Computer tossed a Tail
             }
             WriteLn('Computer tossed a Tail!');
     {
     12,ENDIF
     }
     End;
     {
     13,IF coin = guess THEN
     }
     If (coin = guess) Then
        {
        14,OUTPUT You won
        }
        WriteLn('You won!!')
     {
     15,ELSE
     16,OUTPUT You lost
     17,ENDIF
     }
     Else WriteLn('You lost!!');
     {
     18,Delay the program so that you can see the results
     }
     ReadLn;
End.{of main.}

{
Created by FlamingClaw 2009.07.30.
Tested in delphi 7!!!
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.