graywest Posted May 28, 2015 Share Posted May 28, 2015 Hello everyone - I need some help from a Batari Basic guru. I'm trying to put together a routine that builds a playfield randomly, with some restrictions. The playfield has two horizontal rows that will be randomly generated, while the rest is fixed. The restrictions on the randomly generated rows are as follows: For each column, the bits can both be blank, both be filled, or only the bottom bit filled. The top bit cannot be filled without the bottom. So far, so good. I figured that I would just do a "for... next" loop for each of the variables that makes up those two rows of the playfield, use some random numbers, and repeat. Something along these lines: For BitPosition = 0 to 7 TempStorage2 = rand if TempStorage2 < 26 then var27{BitPosition} = 1: var31{BitPosition} = 1: goto BuildVar27 if TempStorage2 < 51 then var31{BitPosition} = 1 BuildVar27 Next However, the problem that I am running into is that the compiler tells me that Batari Basic can't use variables with single bits. So after pulling my hair out for a bit, I rewrote the code and just did the whole thing manually, with no "for... next" loops. Something like this: TempStorage2 = rand if TempStorage2 < 26 then var27{3} = 1: var31{3} = 1: goto BuildVar27Bit3 if TempStorage2 < 51 then var31{3} = 1 BuildVar27Bit3 I did that 32 times and changed the bit position numbers each time. It works, but needless to say, it's hellacious and a pain in the ass to maintain or to make changes. It also takes up far more ROM space than it should. So my question is: can anyone help optimize this code? Is there a way to do what I'm trying to do with loops? I'm including the working (but bloated) version of the code below. Thanks in advance!! Sneaky.bas Quote Link to comment Share on other sites More sharing options...
ZackAttack Posted May 28, 2015 Share Posted May 28, 2015 Keep in mind that setting a variable to a random value is the same thing as setting each bit randomly. Also, realize that the behavior of the top row describes the behavior of a bit-wise AND. You should be able to do something like this for each byte in the rows. BottomRowByte = rand TopRowByte = rand & BottomRowByte 1 Quote Link to comment Share on other sites More sharing options...
graywest Posted May 29, 2015 Author Share Posted May 29, 2015 Keep in mind that setting a variable to a random value is the same thing as setting each bit randomly. Also, realize that the behavior of the top row describes the behavior of a bit-wise AND. You should be able to do something like this for each byte in the rows. BottomRowByte = rand TopRowByte = rand & BottomRowByte That's a good point about setting a full byte to a random value. I'm not sure it would work for my game though - I need to be able to set the probability that the individual bytes will be on or off. They represent a wall with gaps, and there should be more gaps than wall. (I'm using 25% chance on, 75% off currently, but I would like to vary those as the levels increase.) Your post just gave me a brain flash - I think I could set the bits individually by calculating the probability that the bit should be on or off, and then adding the value of the bit's position to the variable (1,2,4,8,16, etc.) I think I could do THAT in a loop. And then I could do the top row bits as you suggested, with the randomized bit-wise AND. Thanks for the help! Quote Link to comment Share on other sites More sharing options...
Mountain King Posted May 29, 2015 Share Posted May 29, 2015 Darn you. My next game was going to have a similar concept. You beat me to it. Nicely done so far. 1 Quote Link to comment Share on other sites More sharing options...
graywest Posted May 29, 2015 Author Share Posted May 29, 2015 Darn you. My next game was going to have a similar concept. You beat me to it. Nicely done so far. Great minds think alike! Quote Link to comment Share on other sites More sharing options...
bogax Posted May 30, 2015 Share Posted May 30, 2015 That's a good point about setting a full byte to a random value. I'm not sure it would work for my game though - I need to be able to set the probability that the individual bytes will be on or off. They represent a wall with gaps, and there should be more gaps than wall. (I'm using 25% chance on, 75% off currently, but I would like to vary those as the levels increase.) Your post just gave me a brain flash - I think I could set the bits individually by calculating the probability that the bit should be on or off, and then adding the value of the bit's position to the variable (1,2,4,8,16, etc.) I think I could do THAT in a loop. And then I could do the top row bits as you suggested, with the randomized bit-wise AND. Thanks for the help! (you may already know this) if you're going to add be sure the bit is zeroed first don't forget that some of the playfield bytes are reversed use rand16 sounds like you'll be calling rand a lot if you call rand some set number of times that's a factor of the cycle length it'll likely seem much less random 1 Quote Link to comment Share on other sites More sharing options...
Papa Posted May 30, 2015 Share Posted May 30, 2015 I love using rand16! I want my game to say 16-BIT in big letters in the bottom corner and under it in really tiny letters I want it to say (randomized variables...) 1 Quote Link to comment Share on other sites More sharing options...
ZackAttack Posted May 31, 2015 Share Posted May 31, 2015 If you want to control the probability you could always bitwise and multiple rand values. The more you and together, the less chance a bit has of being set. Using rand is fairly expensive since it produces a subroutine call and performs some calculations each time. For extremely sparse outputs you could just randomly choose which bit to enable. Randomly choose a byte and then randomly choose a bit in that byte. Then you only have 2 rand calls to produce a 1/32nd probability of bits being set. The major drawback to this approach is that you lose some of the randomness feel. I've created a small test program that uses the first approach to randomly set the bits in the first 10 rows of the playfield. Up and Down on joystick 0 will change the sparseness between 4 different levels. The indicator on the bottom left shows which sparseness level is currently in use. The level wraps around in both directions. This should take up less CPU time and possibly less rom space than calling rand for each bit. sparse.bas sparse.bin sparse.lst Quote Link to comment Share on other sites More sharing options...
graywest Posted May 31, 2015 Author Share Posted May 31, 2015 If you want to control the probability you could always bitwise and multiple rand values. The more you and together, the less chance a bit has of being set. Using rand is fairly expensive since it produces a subroutine call and performs some calculations each time. For extremely sparse outputs you could just randomly choose which bit to enable. Randomly choose a byte and then randomly choose a bit in that byte. Then you only have 2 rand calls to produce a 1/32nd probability of bits being set. The major drawback to this approach is that you lose some of the randomness feel. I've created a small test program that uses the first approach to randomly set the bits in the first 10 rows of the playfield. Up and Down on joystick 0 will change the sparseness between 4 different levels. The indicator on the bottom left shows which sparseness level is currently in use. The level wraps around in both directions. This should take up less CPU time and possibly less rom space than calling rand for each bit. sparse.bas sparse.bin sparse.lst Thanks ZackAttack - this is a really interesting idea. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.