Solved

Using random 2

Posted on 2003-11-12
7
242 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Moving applications to the cloud or switching services to cloud-based ones, is a stressful job.  Here's how you can make it easier.
Hyena v12.2 is now available for downloading and is available in English, French, German and Spanish versions.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

20 Experts available now in Live!

Get 1:1 Help Now