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.

DISPLAY a title for the program
Issue the statement that stops the 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


Please help me to solve the above problem.
ENDIF

What is the problem ? The pseudo code is as it should be. You only have to replace it with actual statements.

Thank you for your kind reply. But my problem is to convert it to Delphi. Please help me to solve this problem.

What have you got so far ? A normal windows application or a console application ?

If you have the former, then you can put a label, button and editbox on a form and start from there.

Turbomen, do what you can, paste the code in, tell us what doesnt work or what error you get and we'll try and help you. Doing homework for you isnt going to happen

Could you tell me what's wrong of my following work??

Var
pick: char;
head, tail, flip, flip1: integer;

begin
writeln ('Please choose the outcome of the toss of the coin');
readln (pick);
flip:=random(2);
If flip1='0' then
begin
flip1:=head;
end
else
begin
flip1:=tail;
end;
if pick='flip1' then
begin
writeln ('You win');
end
else
begin
writeln ('You lose');
end;
sleep(5000);
end.

No code tags?
Whats the error? symptom?


other than youve said pick is a char, and then asked if its 'flip1' as a string, so i guess it always says you lose.

What is the purpose of the integers Head and tail? Are they needed? Look at how you are using them and follow the value of Flip1 using debug monitors.

I have an idea,see it :)

Program coins;
Uses crt;
Var
pick,c:Char;                 {from ASCII characters}
Head,Tail,Flip:Byte;     {0..255}

Begin
   Head:=0;
   Tail:=1;
   Repeat
      Write ('choose the outcome of the toss of the coin H or T: ');
      ReadLn (pick);
      Case (pick) of
         'H','h':Begin
                    WriteLn('You choose: Head: ',Head);
                    Randomize;  {shuffling..}
                    Flip:=Random(2);  {generate}
                    WriteLn('Computer: ',Flip);
                    If (Flip = Head) Then  {analize it}
                       Begin
                          WriteLn('You Win!!!'); {notify the user}
                       End
                    Else WriteLn('You Lose...');
                 End;{of begin}
         'T','t':Begin
                    WriteLn('You choose: Tail:',Tail);
                    Randomize;
                    Flip:=Random(2);
                    WriteLn('Computer: ',Flip);
                    If (Flip= Tail) Then
                       Begin
                          WriteLn('You Win!!!');
                       End
                    Else WriteLn('You Lose...');
                 End{of begin}
      Else Begin {of case else}
              Write('Wrong answer...');
              ReadLn;
              Halt;
           End;
      End;{of case}
   WriteLn('Do you want to continue?Press ESC for quit.');
   c:=ReadKey;
   Until c=#27;
   ReadLn;
End.{main}

{
-=Note By FlamingClaw=-

You have to choose between 'H' or 'T'.
This little program run again and again while you do not press
'Esc' button. The '#27' is the code of the Esc button from ASCII table.
Sorry for rewrite your program...

-=Created By FlamingClaw=-
2009.03.15
}

Now that you have a program that works, consider this: One goal for writing code is to avoid duplication. Note that you have the same code repeating for each case (H or T). You might want to look for a way to eliminate the duplication. Consider moving some of the duplicate code above the case statement.

Ok guys,I rewrote my little application...
I hope it is now good :)

Program Coins02;

{used moduls}
Uses Crt;

{Declare variables}
Var Pick,Ch:Char;{from ascii}
    Flip:Byte;

{procedure for exiting}
Procedure WrongAnswer;
   Begin
      WriteLn; {empty line}
      WriteLn('Wrong Answer Press Any Key To Quit...');
      ReadKey;{waiting for press a key}
      Halt;   {exit the main program}
   End;

Function Turbinate(T01:Byte):Byte;{returns byte typed value}
   Begin
      Randomize; {agitate it}
      T01:=Random(2); {generate a number between 0 and 1}
      Turbinate:=T01; {return value}
   End;

{this function wait for char and return a byte value}
Function Evaluate(E01:Char):Byte;
   Begin
      Case (E01) Of {analize the answer}
         'H','h':Evaluate:=1; {returns 1}
         'T','t':Evaluate:=0  {returns 0}
      Else WrongAnswer;       {call our procedure to quit}
      End;
   End;

Begin {main}
     Repeat
        ClrScr; {ClearScreen}
        {question}
        WriteLn('choose head or tail.H or T: ');
        ReadLn(Pick);

        If (Evaluate(Pick)=Turbinate(Flip))Then  {if true}
           WriteLn('You Win :) ')       {then you win}
        Else
           WriteLn('You Lose :( ');     {else you lose}

        WriteLn('Do you want to continue?Press ESC for quit.');
        Ch:=ReadKey;
     Until Ch=#27;{and only quit when you press the esc button}
     ReadKey;
End. {main}
{
-=Note By FlamingClaw=-
I want to make it easyly...and the result is good...
For those who not clear something,alert me....
-=Created By FlamingClaw=-
2009.03.16.
}
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.