Solved

Using random 2

Posted on 2003-11-12
7
248 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WSAAdressToString example Delphi code needed 3 1,784
Unsigned int64 to unsigned int32 10 1,709
Display image from a link 4 507
Intraweb submit form as a POST request 4 327
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

831 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