Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using random 2

Posted on 2003-11-12
7
Medium Priority
?
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
7 Comments
 
LVL 101

Accepted Solution

by:
mlmcc earned 212 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
Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

 

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 208 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 101

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

If you’re involved with your company’s wide area network (WAN), you’ve probably heard about SD-WANs. They’re the “boy wonder” of networking, ostensibly allowing companies to replace expensive MPLS lines with low-cost Internet access. But, are they …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

721 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