Noob BASIC programmer

By faxerpelle

Expert (98)

Аватар пользователя faxerpelle

01-05-2012, 21:06

Just started programming in BASIC trying to recall old knowledge and to help my self for university exams. So the first tries are on real simple coding.

One of the simple programs I made is a kind of quiz where you have to find out a random generated number between 1 to 100. So I set up a routine to help the player to understand how close he is to the hidden number. This routine is made with several ifs that check the absolute value of the difference between the hidden number and the input number by the player and give back a printed answer on how close you are.

Problem is I get the answers of only 2 of the ifs, even if the absolute value is correctly within range check of another of the ifs.

Routine is like:

100 If abs(x-a)>=40 then goto relevant print instruction else goto 110
110 if abs(x-a)<40 or abs(x-a)>20 goto relevant print instruction else goto 120
120 if abs(x-a)<19 or abs(x-a)>5 goto relevant print instruction else goto 130
130 if abs(x-a)<=5 goto relevant print instruction

where x is random generated number and a is input number by the player

Print instruction is like print something depending on the distance from the number and the goes back to input phase until the player wins and the program ends.

For some reason instructions 110 and 120 are not considerd.

Any hints?

Для того, чтобы оставить комментарий, необходимо регистрация или !login

By hap

Paragon (1889)

Аватар пользователя hap

01-05-2012, 21:21

this statement is always true:
if abs(x-a)<40 or abs(x-a)>20

same goes for the next one, even though it never reaches that point

*edit* I think what you're looking for is an AND instead of OR

By faxerpelle

Expert (98)

Аватар пользователя faxerpelle

01-05-2012, 21:24

You are right. That's the point. I should use and instead of or for statements 110 and 120. Then it won't be' always true.

Thanks a lot. Smile

By faxerpelle

Expert (98)

Аватар пользователя faxerpelle

02-05-2012, 21:37

Now it works!

The only strange thing left is that everytime I run the program, and I mean every single time, the "random" generated number is always 59.

By Manuel

Ascended (9460)

Аватар пользователя Manuel

02-05-2012, 22:09

initialize the RND function with: r=rnd(-time)
Even better is to do that after a user pressed a key.

By msxholder

Master (231)

Аватар пользователя msxholder

02-05-2012, 23:11

Random doesn't exist on an MSX its Random can be calculated.

By wolf_

Ambassador_ (9468)

Аватар пользователя wolf_

02-05-2012, 23:38

Does true random exist on any digital computer anyway?

By Wolverine_nl

Champion (310)

Аватар пользователя Wolverine_nl

03-05-2012, 10:18

True is even the question if we, humans, can randomly choose something 'really'. There are several unconcussional things which make us choose.
What Manual says, use a time ref in RND, which is never the same, if on msx1, make ur own counter possibly.


Prophet (3317)

Аватар пользователя NYYRIKKI

03-05-2012, 11:14

msxholder wrote:

Random doesn't exist on an MSX its Random can be calculated.

10 ' Here we have to split these numbers, elseway BASIC is
20 ' going to lose some important desimals :
30 '
40 M1= 21132480000000#:  A1= .1438982#
50        M2= 6540519#:            A2=4.20821E-09
60 '
70 R=RND(-TIME) ' Here we give a new seed for RND function. Seed is allways
80 '              reseted, when we start runnig a BASIC program.
90 '
100 CLS
110 PRINT "If you are going to use positive numbers for RND(X) function,"
120 PRINT "then here is next 10 values, that you are going to get :"
130 PRINT STRING$(65,"-")
140 '
150 FOR I = 0 TO 9
160 '
170 L1 = 1E-07*INT(R*10000000!)
180 L2 = R-L1
190 HE = 10000000!*(M2*L2+A2)
200 H1 = (M1*L2)-INT(M1*L2)
210 H2 = (M2*L1)-INT(M2*L1)
220 H3 = H1 + H2 + A1 + 1E-07 * INT(HE)
230 H4 = H3 - INT(H3)
240 H5 = 1E-07*(HE - INT(HE))
250 R  = H4 + H5
260 PRINT I;"|";R
270 '
280 NEXT I


My MSX profile