| | |
To create a game of Heads or Tails against the computer.
Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2009
Posts: 93
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
•
•
Join Date: Feb 2009
Posts: 93
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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.
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.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
I have an idea,see it 

pascal Syntax (Toggle Plain Text)
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 }
Last edited by FlamingClaw; Mar 15th, 2009 at 8:24 am.
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.
![]() |
Other Threads in the Pascal and Delphi Forum
- Previous Thread: Access made to undefined variable?
- Next Thread: Password program
| Thread Tools | Search this Thread |






