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
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
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.
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.
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.