943,667 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Feb 27th, 2009
0

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

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Junior Poster
turbomen is offline Offline
113 posts
since Feb 2009
Feb 28th, 2009
0

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

What is the problem ? The pseudo code is as it should be. You only have to replace it with actual statements.
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 728
Bite my shiny metal ass!
pritaeas is offline Offline
4,163 posts
since Jul 2006
Feb 28th, 2009
0

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

Thank you for your kind reply. But my problem is to convert it to Delphi. Please help me to solve this problem.
Reputation Points: 10
Solved Threads: 0
Junior Poster
turbomen is offline Offline
113 posts
since Feb 2009
Mar 1st, 2009
0

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

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.
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 728
Bite my shiny metal ass!
pritaeas is offline Offline
4,163 posts
since Jul 2006
Mar 1st, 2009
0

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

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
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Mar 2nd, 2009
0

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

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
turbomen is offline Offline
113 posts
since Feb 2009
Mar 2nd, 2009
0

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

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.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Mar 2nd, 2009
0

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

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.
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
jsosnowski is offline Offline
68 posts
since Nov 2007
Mar 15th, 2009
0

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

I have an idea,see it
pascal Syntax (Toggle Plain Text)
  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.
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009
Mar 15th, 2009
0

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

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.
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
jsosnowski is offline Offline
68 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: Access made to undefined variable?
Next Thread in Pascal and Delphi Forum Timeline: Operate with binary digits





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC