Solved

Using random 2

Posted on 2003-11-12
7
244 Views
Last Modified: 2010-04-16
program Generator;
uses crt;
var nin,t,x,y,z,a,b,c,d,e,f,g,h,i,head,body,weapon,str,def,agi,mag,level,exp,hp:integer;
    cha,critical,att,strrat,defrats,defrat,defdog,defdogs,strdog,strth,defth,defths:integer;
    menu1,menu2,levelpoints:integer;
    Chance:boolean;

Okay, This time i have made a procedure that simulates a fight with you and a rat. Everything works fine until the boolean critical, Because in every moment where you use critical it uses a new number, so the attack can BOTH success and FAIL at the same time.. is there a way to choose a random number, and then keep it in a variable? , Im very grateful for answers

Procedure ratfight;
begin
clrscr;
strrat:=3;
defrats:=35;
textcolor(15);
writeln('Fight with rat started');
write('Rat has ');
textcolor(2);
write(defrats);
textcolor(15);
writeln(' Healthpoints');
defrat:=defrats;
readln;
repeat
writeln('What is your command?');
textcolor(7);
writeln(' ');
writeln('1. Fight');
writeln('2. Furious Attack');
writeln('3. Flee');
textcolor(15);
readln(att);

If att=1 then begin
defrat:= defrat - random(str);
write('The rat has ');
textcolor(2);
write(defrat);
textcolor(15);
writeln(' Healthpoints left');
If defrat=0 then
writeln('You striked him first');
end;

If att=2 then begin
writeln('You plunge your weapon against your opponent');
critical:=random(11);
If critical<6 then
chance:= true
if Critical>5 then
chance:=false

If chance=false then begin
writeln('Hit: ');
textcolor(2);
writeln('SUCESS!');
defrat:= defrat - random(str)*2;
write('The rat has ');
textcolor(2);
write(defrat);
textcolor(15);
writeln(' Healthpoints left');
cha:=1;
end;


if chance=false then begin
writeln('Hit: ');
textcolor(2);
writeln('FAILED!');
write('The rat has ');
textcolor(2);
write(defrat);
textcolor(15);
writeln(' Healthpoints left');
end;


hp:=hp-random(strrat);
write('The rat attacks you, you have ');
textcolor(8);
write(hp);
textcolor(15);
writeln(' Healthpoints left');
until (defrat=0) or (defrat<0) or (hp=0) or (hp<0);

if hp>0 then begin
writeln('You have won, congratulations!');
write('You have earned: ');
textcolor(14);
write('50');
textcolor(15);
write(' gold and ');
textcolor(5);
write('26 exp');
textcolor(15);
exp:=exp+26;
a:=a+50;
readln;

Just ask if you need more information about the problem.
0
Comment
Question by:Grandstream
  • 2
  • 2
  • 2
7 Comments
 
LVL 100

Accepted Solution

by:
mlmcc earned 53 total points
ID: 9737050
Just like you did here

critical:=random(11);


WHere do you need this?

mlmcc
0
 

Author Comment

by:Grandstream
ID: 9739482
If critical is between 1-5 , the chance becomes true, if it is between 6 and 10, the chance becomes false... in simple: if the random is 3, the attack misses,
if the random is 6, the attack hits, but.. something in that code messes it up, so the attack BOTH misses and hits, and I can't see any other solution that the random picks different numbers for both success and failed
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9739768
Both if statements test if critical = false. Thus they both succeed or they both fail.

It would be easier to read if you just used critical rather than critical = somevalue. Even better, as there are only two outcomes, why use two if statements: use else.

if (critical) then
  { do critical hit stuff }
else
  { do not critical hit stuff }

Hope this helps, -bcl
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Grandstream
ID: 9740076
I Had tried that before too, but it atleast helped me out this time, thx!, because what i did was:
example:

If x:=3 then
write('CRITICAL SUCCESSFUL);
else
write('NOT SUCCESSFUL);

It said that else was wrong.. so i dumped that idea... and then i remembered, when -bcl wrote that to me, that you MUSTNT use ; at the end of the write('CRITICAL SUCCESSFUL);-line..
so now it works, tankyou!, just tell me if you want the program .. its nearly finnished.. if youre into RPG that is =)
0
 
LVL 11

Assisted Solution

by:bcladd
bcladd earned 52 total points
ID: 9740667
   If x:=3 then
         write('CRITICAL SUCCESSFUL);
    else
        write('NOT SUCCESSFUL);

Note that this has two major problems: ':=' is an assignment, NOT a comparison operator. In Pascal use '=' (one equal sign) operator for comparison. Different in other languages; the other problem as you noted is the ; before else.

Another note on your code: You should indent it to show what portions of the code are controlled by the various control structures (if/while/for/procedure). Makes the code much easier to read.

-bcl
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 10189565
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

recommend - split bcladd & mlmcc

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

mlmcc
Pascal Page Editor
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
A customer recently asked me about anti-malware and the different deployment options available for his business. Daily news about cyberattacks, zero-day vulnerabilities, and companies that suffered a security breach made him wonder if the endpoint a…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now