Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Ranges in-between numbers

Author
Message
Donald Smith
21
Years of Service
User Offline
Joined: 15th Nov 2002
Location:
Posted: 31st Oct 2022 05:08
Hello everyone!!!

I'm having a tough trying to figure the math and the programming behind what I want to do, so I decided to ask for some help.

If you have never found the game "The Money Vault" on the Internet, the premise is to fill 10 slots with dollar amounts from lowest to highest by choosing from 50 panels of various dollar amounts ranging from $1,000 to $100,000 (even numbers only).

Now, I store the money amounts in an array called slot(10), with slot(10) at the top, etc.

On the first go round, all slots are empty, so for example, I can place $16,000 (let's call this value Money) in slot(4). Also, the program displays all the useable slots.
With the second try, say I choose $40,000 and I put that in slot(8), skipping the amount in slot(4). Here's where my issue starts.

Third try, say the value is $32, 000 which falls between slot(4) and slot(8), so the useable slots are from slot(5) to slot(7). How do I get the useable slots to show? Let's say that I put the $32,000 in slot(6).
Four try, say the value chosen is $37,000, which falls between slot(6) of 32,000 and slot(8) of 40,000. How do I only show slot(7) as useable?

Now, remember that I choose a number lower than $16,000, slot(1) through slot(3) should be useable.


Thank you for your help, Donald
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 31st Oct 2022 23:04 Edited at: 31st Oct 2022 23:14
It's been a while since I've seen new stuff or written here! I like the "challenge" you've presented. I haven't programmed for some time but this seems like a fun task to maybe move a little rust off my gears.

One way you could approach this is to make the array that you store the value in the slots, a 2 dimensional array. The second dimension for each value could be a flag set to 1 or 0. 1 for "this slot is already set. no you can't put anything in this box anymore" and 0 "This box will happily accept a new figure.

You also would do a check when trying to place a figure in a box: you'd loop forward from the slot you intend to drop the new value in to see if the next "already set" slot is actually higher in value, and you'd loop back to check if the value is actually lower. If the intended slot to drop the value in can't fit, then you branch to some routine to handle this - maybe you've lost the game at this point or whatever.

I knocked up some pretty quick code. It you point and LEFT CLICK on the 10 money slots with 0 in them they will tell you whether or not you can put money here. This is controlled by subroutine _fillslot:

The serices of squares at the bottom are the cards you can choose from. When you LEFT CLICK on one, the value will stick to the mouse. You can then go up to the slots to place the value. If you RIGHT CLICK, the value will be placed in that slot. The squares are controlled with the subroutine _cards: . If you examine the code, you'll see how it all works.

Basically, once you click on a card, the curcell variable is populated with that card number. The card number is the index to the array choosemoney(curcell,1) that holds the random values of the cards that were set when the program begins. When you choose a card and go back up to the slots to change the value, the program checks curcell to make sure it is set, it looks for a LEFT CLICK in the slot and will tell you if that is a slot you can choose, the program looks for a RIGHT CLICK on a slot to try and put the array value choosemoney(curcell) into slot(x,1). Once money is put in a slot, slot(x,2) is set to 1. If this is set to 1, money cannot be put here anymore. That's how the code below functions. I didn't put a check in it however to see if the value to be placed is "in between" the values above or below it. I'm sure you could handle that logic. Just step forward and backward the the slots(x,1) array from the current slot you are trying to choose and see if theres a higher and lower value. You'd only look for lower on slot 10 and only look for higher on slot 1.

If you are graphically adapting to show the visibility of available slots, then use the flag slots(x,2) to control that code.


Enjoy your day.
Donald Smith
21
Years of Service
User Offline
Joined: 15th Nov 2002
Location:
Posted: 1st Nov 2022 01:37
So Latch, thanks for the help but let's add a visual to the mix: https://www.youtube.com/watch?v=abnYIt2juTM&list=PL2Xcx-Ev_HEroJhpZkco6u9vviQygrmCw

So, if you look at the video at the 6:18 mark, $85,000 is in slot(7) and $67,000 is in slot(4) and the dollar amount randomly chosen was $81,000, which can fit in slot(5) or slot(6) *** which both equal zero*** and both of the these slots light up.
This is the math piece I can't figure out, to calculate the "lowend" and the "highend" values, let's say that, in this case, LowEnd = 5 and HighEnd = 6 and the program makes those two slots light up.

So using the video as a reference, if a dollar amount chosen (Money) equaled $35,000, slot(1),slot(2) and slot(3) would light up.

Thanks for your help, Latch

Donald


Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 1st Nov 2022 18:32 Edited at: 1st Nov 2022 18:51
Hi,

So if you looked at my code, the basic premise is, set a flag that identifies which slots are available. Since this is stored in an array (the same array as the dollars contained in slots) you can loop through the array and find the flags that aren't set.

once you find an unset flag, you will have also obtained the slot number (the index). Let's say we loop through slot(x,2) search for a value of 0 and when x=5 we've found an empty slot. Now do a loop backwards from 5 to 1 and do a loop forward 5 to 10.

When going backward, check the dollar value currently stored in each slot once you hit a dollar value not equal to zero, you have to check if it is less than the target. If it is greater, than nothing above that found slot can be high lighted and you should set this to a variable called highslot. Continue to check below in the same fashion until you reach slot 1. If you find a value that is less than the target, set a variable maybe named lowslot to this slot number. Now do the same thing starting from your originally found empty slot and loop upwards towards 10. if you find a number greater than your target, then this is highslot, if you find a number lower then of course this is lowslot. Now that you have the first lowest and last highest slot numbers below and above your target, loop through lowslot to highslot, check for the flag ( slot(x,2) )that indicates the slot is available and for each slot available within the range run you graphics routine to highlight those slots as available. If there end up being no available slots after your tests, you've lost.

Note, if you make it to slot 1 or make it to slot 10 and you haven't found a non-zero value at all, that means those below or above the originally found empty slot that are available can all be highlighted.

Make sense?
Enjoy your day.
Donald Smith
21
Years of Service
User Offline
Joined: 15th Nov 2002
Location:
Posted: 2nd Nov 2022 06:57 Edited at: 2nd Nov 2022 07:20
Latch,
After I printed out your replies and hung them on the wall in front of me, (believe me, it was very helpful ) I wrote some code and it works the way I want it to, but can you think of an easier way to check for the LowEnd and HighEnd values?




Thanks, Donald
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 2nd Nov 2022 17:55
Hi,

I'm looking at your code. It's organized a little differently than I'm used to so I'll have to sort out what you did. I saw a few GOTOs in there within the same subroutine blocks. I think there can be some improvements there. Also Instead of drawing the boxes each loop, you can store graphics of them off screen on another bitmap and just copy them over - or assign the box to an image number.

One thing, I wasn't able to select or click on anything so I didn't understand how to select a slot to put the dollars in. I think that's because you have a do:loop at the bottom of the code that will never end.

Let me mull it over a little to think of simplified code.
Enjoy your day.
Donald Smith
21
Years of Service
User Offline
Joined: 15th Nov 2002
Location:
Posted: 2nd Nov 2022 19:44
Hello,
I just changed the value of Money in the code just to see how the program would react. I haven't copied and pasted the new code I wrote into the existing program where I can actually click on slots and the 50 panels yet...no time like the present.

Donald
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 3rd Nov 2022 17:02 Edited at: 3rd Nov 2022 17:07
Hi Donald,

Ok I had to write a complete working game to understand what was giving you trouble. I have to say, I agree: finding the available slots was a little tricky! I'll still stick to my guns though - with a slight variation. I feel you have to loop up when your looking for a high limit, and you have to loop down when looking for a low value. No difference there. I changed the original code above pretty much entirely.

From the new code I'll post only the subroutine (at the bottom of this message) that handles all of the checking. In the main code, the subroutine is activated as soon as someone chooses a card with a hidden money value. The flag "curcard" is set with the number of the card that was chosen once a card is LEFT CLICKED.

Main code jump point


1. The first thing the subroutine (SR) does is to DEACTIVATE ALL of the slots whether they are empty or not. This ensures a possible losing status and will only display available slots in the end.
2. Next the SR loops from slot 10 to slot 1 to find the low value. What was important here was to check each slot for a non-zero value that was lower than the amount chosen and if it was found, to immediately exit the loop. The logic being since you are moving down in slots starting from the highest slot, the first money value that is lower than the amount you chose is you floor (lowest possible value).
3. Since we have our low value, we loop up from that slot to slot 10. As soon as we find a non-zero value that is higher than the amount we chose, we get out of the loop with that slot number. That would be our ceiling. Otherwise we just keep increasing the high slot value until we reach 10
4. Last, we know the range where possible available slots exist. We loop between the low and high. If any of these slots have a 0 dollar value in them, we show them as available (green) and we set slot(x,2)=0. Remember when we set all flags to 1? well that's because we want to show NOW only the available slots within our range that have zero dollars by unsetting that flag.

It probably seems weird to have initially set all of the flags to 1. That actually doesn't matter because in the main program, I can show all of the slots anyway I choose because unless I've chosen a card, I don't need to know which slots are available since I can't put anything in a slot unless I've chosen a card. The SR is only active when I've chosen a card and that's when it greys out the unavailable slots and opens up the available ones if there are any.

It's pretty straight forward I think when you look at the code. I haven't found a simpler way at this point.

Enjoy your day.
Donald Smith
21
Years of Service
User Offline
Joined: 15th Nov 2002
Location:
Posted: 9th Nov 2022 03:21
Latch,
In your code, if slots 1 - 7 are filled and the money amount chosen is a valid pick for slots 8 - 10. It seem that the lowend value is correct but the highend value isn't changing.

Donald
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 11th Nov 2022 17:59 Edited at: 11th Nov 2022 18:18
No. It works, though I didn't account for a case where an existing dollar amount is chosen. In the attached screenshot using the code, the next card chosen is 8000. Slots 1-7 are filled, so 8 9 and 10 are available (they are green which means available in my version). Note: I display them 1 at the top 10 at the bottom.

The low end value in this case would be slot 7. The code would start to count down the variable "av" from 10 to 1. "av" checks to see if the money in the current "av" slot is NOT 0 and lower than the chosen amount. If those two criteria are met, we've found the floor value and the loop is exited. In the screen shot example:

New value chosen = 8000

pass 1 av=10 slot value = 0 : move on to the next one
pass 2 av=9 slot value = 0 : move on
pass 3 av=8 slot value = 0 : move on
pass 4 av=7 slot value = 7000 : Is it less than 8000 ? Yes. Is it not = 0? Yes.
lowvalue = av = 7 exit loop

The high value is then looked for by looping UP from the lowvalue . It loops 7 to 10. It will look in the slots until it finds a higher dollar value than 8000. If none is found, then the ceiling is set to 10.

The final loop goes from lowvalue to highvalue (7 to 10 in this case). If the slot dollar values are 0, then these slots are made available to put money in.
Enjoy your day.

Attachments

Login to view attachments
Donald Smith
21
Years of Service
User Offline
Joined: 15th Nov 2002
Location:
Posted: 14th Nov 2022 04:41
Latch,
So I had to tweak your subroutine to make it work for my program



Otherwise, I think I can move to the Pass and Swap portions now. I'll keep you in the loop.

Donald
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 17th Nov 2022 06:58
I just discovered there is an error in my logic. I haven't figured out what it is but I'm able to force slots to remain open when they shouldn't. Maybe you bypassed it.
Enjoy your day.

Login to post a reply

Server time is: 2024-04-19 18:28:12
Your offset time is: 2024-04-19 18:28:12