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: turbomen is an unknown quantity at this point 
Solved Threads: 0
turbomen turbomen is offline Offline
Junior Poster in Training

To create a game of Heads or Tails against the computer.

 
0
  #1
Feb 27th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 849
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 138
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: To create a game of Heads or Tails against the computer.

 
0
  #2
Feb 28th, 2009
What is the problem ? The pseudo code is as it should be. You only have to replace it with actual statements.
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 93
Reputation: turbomen is an unknown quantity at this point 
Solved Threads: 0
turbomen turbomen is offline Offline
Junior Poster in Training

Re: To create a game of Heads or Tails against the computer.

 
0
  #3
Feb 28th, 2009
Thank you for your kind reply. But my problem is to convert it to Delphi. Please help me to solve this problem.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 849
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 138
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: To create a game of Heads or Tails against the computer.

 
0
  #4
Mar 1st, 2009
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.
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: To create a game of Heads or Tails against the computer.

 
0
  #5
Mar 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 93
Reputation: turbomen is an unknown quantity at this point 
Solved Threads: 0
turbomen turbomen is offline Offline
Junior Poster in Training

Re: To create a game of Heads or Tails against the computer.

 
0
  #6
Mar 2nd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: To create a game of Heads or Tails against the computer.

 
0
  #7
Mar 2nd, 2009
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 68
Reputation: jsosnowski is an unknown quantity at this point 
Solved Threads: 11
jsosnowski's Avatar
jsosnowski jsosnowski is offline Offline
Junior Poster in Training

Re: To create a game of Heads or Tails against the computer.

 
0
  #8
Mar 2nd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 447
Reputation: FlamingClaw will become famous soon enough FlamingClaw will become famous soon enough 
Solved Threads: 106
FlamingClaw's Avatar
FlamingClaw FlamingClaw is offline Offline
Posting Pro in Training

Re: To create a game of Heads or Tails against the computer.

 
0
  #9
Mar 15th, 2009
I have an idea,see it
  1. Program coins;
  2. Uses crt;
  3. Var
  4. pick,c:Char; {from ASCII characters}
  5. Head,Tail,Flip:Byte; {0..255}
  6.  
  7. Begin
  8. Head:=0;
  9. Tail:=1;
  10. Repeat
  11. Write ('choose the outcome of the toss of the coin H or T: ');
  12. ReadLn (pick);
  13. Case (pick) of
  14. 'H','h':Begin
  15. WriteLn('You choose: Head: ',Head);
  16. Randomize; {shuffling..}
  17. Flip:=Random(2); {generate}
  18. WriteLn('Computer: ',Flip);
  19. If (Flip = Head) Then {analize it}
  20. Begin
  21. WriteLn('You Win!!!'); {notify the user}
  22. End
  23. Else WriteLn('You Lose...');
  24. End;{of begin}
  25. 'T','t':Begin
  26. WriteLn('You choose: Tail:',Tail);
  27. Randomize;
  28. Flip:=Random(2);
  29. WriteLn('Computer: ',Flip);
  30. If (Flip= Tail) Then
  31. Begin
  32. WriteLn('You Win!!!');
  33. End
  34. Else WriteLn('You Lose...');
  35. End{of begin}
  36. Else Begin {of case else}
  37. Write('Wrong answer...');
  38. ReadLn;
  39. Halt;
  40. End;
  41. End;{of case}
  42. WriteLn('Do you want to continue?Press ESC for quit.');
  43. c:=ReadKey;
  44. Until c=#27;
  45. ReadLn;
  46. End.{main}
  47.  
  48. {
  49. -=Note By FlamingClaw=-
  50.  
  51. You have to choose between 'H' or 'T'.
  52. This little program run again and again while you do not press
  53. 'Esc' button. The '#27' is the code of the Esc button from ASCII table.
  54. Sorry for rewrite your program...
  55.  
  56. -=Created By FlamingClaw=-
  57. 2009.03.15
  58. }
Last edited by FlamingClaw; Mar 15th, 2009 at 8:24 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 68
Reputation: jsosnowski is an unknown quantity at this point 
Solved Threads: 11
jsosnowski's Avatar
jsosnowski jsosnowski is offline Offline
Junior Poster in Training

Re: To create a game of Heads or Tails against the computer.

 
0
  #10
Mar 15th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Pascal and Delphi Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC