PDA

View Full Version : Need Urgent Help!



eJay
25-09-2006, 09:54 PM
Hi people,

I'm a student teacher in training studying program languages in order to teach it one day, and i have been assigned to learn Pascal. I also need to construct a Heads or Tales game for studnets, where they enter their selection and the program generates a head or a tale to determin if the user made the right choice. So anyways a very basic heads or tales program..

Is there anybody out there that can help me, i have no experience with programing languages whatsoever. :(

dmantione
25-09-2006, 10:08 PM
You need to teach programming and can't program?? :shock:

Download Free Pascal right away:

http://www.freepascal.org

Here is a good tutorial to learn programming: :read:

http://www.taoyue.com/tutorials/pascal/

Make sure the tutorial has no secrets for you. As soon that is done, you you can start to think about the problem you are proposing.

eJay
26-09-2006, 05:58 AM
i would need to teach programming in the future, bot just yet though. At the moment i just need to creat a program for an assignment, which can be used once i start teaching.

eJay
26-09-2006, 06:21 AM
hi out there,

this is what i have so far for the heads or tales programe, i'm getting an error and don't know how to fix it...

program heads_tales
var
HoT,
RandomNumber : interger;
begin (* heads or tales *)
writeln ('This is a game of heads or tales');
writeln ('Press 1 for heads or 2 for tales, and then ENTER');
write ('Enter 1 or 2');
writeln ('Thanks for your choice.');
Read (HoT);
RandomNumber = random(1)
if RandomNumber = HoT then
begin
writeln ('Looks like your choice won!');
end;
else
begin
writeln ('Sorry! Try again.')
end;
end.

Any suggestions to fixing the coding

(EDIT by WILL: Fixed for the visitor's viewing pleasure. ;))

dmantione
26-09-2006, 06:32 AM
Yes:
* Place a ; after the program statement.
* Place a ; after random(1)
* Remove the ; before else
* It is "integer, not "interger".
* Use := in assigments, not =

Further, random(1) returns 0 or 1, not 1 or 2, so you will want to use random(1)+1

Many of these errors, escpecially the ; ones can be very easily solved by looking at the compiler error messages.

tux
26-09-2006, 06:34 AM
well firstly you cant have an ; before an else statement


writeln ('Looks like your choice won!');
end;
else

(damn, he beat me to it)

tanffn
26-09-2006, 06:46 AM
same here, was busy in my baby. 5 post :)

Only suggestion I’m left to give is, use the Pascal tag for posting code. and it will be nice to get the compiler's error/warning messages.

eJay
26-09-2006, 07:06 AM
here's the new and improved code, thanks to your help

program heads_tails;
var
HoT,
RandomNumber : integer;
begin
writeln ('This is a game of heads or tales');
writeln ('Press 1 for heads or 2 for tales');
write ('Enter 1 or 2');
writeln ('Thanks for your choice.');
Read (HoT);
RandomNumber := Random(1)+1;
if RandomNumber = HoT then
writeln ('Looks like your choice won!')
else
writeln ('Sorry! Try again.')
end.

I think it works, i just can't really see what's going on,
even when i when i select the user screen from the debug menu of the compiler.

Any suggestions on how to make it better than what it is?
I would appreciate to hear back from you.

Thanks

Traveler
26-09-2006, 07:37 AM
What kind of compiler are you using?

One way to improve this program is to ask for the user if he wants to play again, instead of ending it after his first go.
You could also ask the player for his name at the start of the program and make the resulting comment a bit more personal.

eJay
26-09-2006, 07:40 AM
i am using free pascal...
i haven't looked at loop construction yet

dmantione
26-09-2006, 08:44 AM
You can add a readln at the end to make the program wait or run it from a Dos prompt.

cairnswm
26-09-2006, 09:44 AM
Should this not be a console app?

{$CONSOLE}

I think needs to be added before the Vars statement - I'm not sure this is correct though so please check in the help first.

eJay
26-09-2006, 09:49 AM
thanks for all the help so far, it has been valuable.
i've applied the readln statement, and played around with the code.

one problem i have though is that when the user presses 1 (head) they win, if they press 2 (tail) they lose. How can i make so that the user doesn't always win with head and lose with tail???

cairnswm
26-09-2006, 11:42 AM
From Delphi Help:
function Random [ ( Range: Integer) ];

Description

In Delphi code, Random returns a random number within the range 0 <= X < Range. If Range is not specified, the result is a real-type random number within the range

0 <= X < 1.

To initialize the random number generator, add a single call Randomize or assign a value to the RandSeed variable before making any calls to Random.

So the random number returned from Random(1) is always 0.

Change to

RandomNumber := Trunc(Random * 2)+1;


To get a 1 or 2 result.

Random * 2 - will return a number between 0 and 1.999999.

Trunc drops the digits after the '.' - So a number betwenn 0 and 1


Does anyone ealse feel we are doing someones homework....

K4Z
26-09-2006, 12:08 PM
to make your program not quit straight away and keep playing, start off with something simple like this:


program youprog;
var
quit : boolean;
quitStr: string;
begin
quit := false;
repeat



//your code here



writeln('Whould you like to play again? y/n');
ReadLn(quitStr);

if LowerCase(quitStr) = 'y' then //still quit if they have CAPs on
quit := true;

until quit = true;
writeLn('Good bye!')
end.


---

I think {$CONSOLE} is a Delphi only thing. (I been caught out by this in Delphi a few times though, lol)

dmantione
26-09-2006, 12:27 PM
Should this not be a console app?
{$CONSOLE}
I think needs to be added before the Vars statement - I'm not sure this is correct though so please check in the help first.

The (documented) default in Free Pascal is a console app, so there is no need to force this.



Change to

RandomNumber := Trunc(Random * 2)+1;



Huh, why? There is no need to teach this man floating point yet.




one problem i have though is that when the user presses 1 (head) they win, if they press 2 (tail) they lose. How can i make so that the user doesn't always win with head and lose with tail???


This is because you haven't randomized the random generator yet. Computers are highly predictable devices, each time you run your program, the random number it starts with is the same.

To counter this, call "randomize" at the begin of your program, i.e.:


begin
randomize;


A call to randomize will put the random generator in a ruly random state, so each time you run your program the result will be different.

JernejL
26-09-2006, 12:44 PM
Random * 2 - will return a number between 0 and 1.999999.

Trunc drops the digits after the '.' - So a number betwenn 0 and 1

and round would round the number up to between 0 and 2.

WILL
26-09-2006, 03:09 PM
I think {$CONSOLE} is a Delphi only thing. (I been caught out by this in Delphi a few times though, lol)

{$APPTYPE CONSOLE} // for Delphi
{$APPTYPE GUI} // for FreePascal/Lazarus

Don't ask my why it's 'GUI', but it is. :)

WILL
26-09-2006, 03:25 PM
Here is a quick little guide for using Random. ;)


For Integer values: Random(X);

Result: 0 <= Random < X


For Real values: ie. Random

Result: 0 <= Random < 1