Randomtest infinite loop

I’m having trouble with randomtest, it loops around because of this code:

My code looks like this:

*label cardtest
*temp goldbet 0
"How much will you bet"
*input_number goldbet 1 10
*if gold < goldbet
    You don't have enough gold
    *goto cardtest
*else 
    *set gold - goldbet
    OK you have bet ${goldbet} gold
*return

you reach it from here

#Bet
                                *gosub cardtest
                                You are dealt a 10 of hearts and a 7 of clubs. The dealer has an ace of spades and the other card is face down. The fat man starts at 14, takes another card and is busted. The blonde man has 16.
                                *choice
                                    #Stick

when I run randomtest, it will produce this over and over , never ending:
club *rand goldbet 10
club *rand goldbet 1 etc etc

Try setting the maximum the player can type as the value of the gold variable.

*input_number goldbet 1 gold

This will assure that the player cannot type more than they have so you can remove the gold check below. Maybe this will help with the randomtest.

1 Like

Many thanks, do you mean change this:

*temp goldbet 0

to this:
*temp goldbet 10

The character has 20 gold at this stage as defined in startup and it is impossible for them to have another amount at this stage

No, I mean this:

*label cardtest
*temp goldbet 0
"How much will you bet"
*input_number goldbet 1 gold
*set gold - goldbet
OK you have bet ${goldbet} gold
*return

When you use *input_number you select the lowest and highest possible value the player can type. By setting it to 1 and the gold variable (which is the amount the player is carrying now) they can never type more then they have. If they have 25 gold right now, then they can only type 1 to 25.

With this you can remove the check to see if they typed more then they have below. Maybe this will also help with randomtest because now it will assure that randomtest can’t choose more they it is carrying at the moment it reaches here.

1 Like

I see and thanks! I want them to be able to bet a maximum of 10 though, not all they have

Fair enough. You could still try that and see if it breaks the randomtest loop though.

I don’t see what else could be causing it, might be something else in the code which we would need to see it to be sure.

Many thanks, I tried and it still goes into a loop

*goto cardtest is going back up to *label cardtest each time so if gold < goldbet it’s going round in circles and repeating. If you’re using implicit control flow, try deleting *goto cardtest. If not, try replacing it with *return.

2 Likes

I am now getting errors on all the lines because of indenting, I think this is because I wrote the code in notepad++ and CSIDE is changing the formatting
I don’t know how to get back to the way it was before I can test

I have now changed the code to the following, it still generates an endless stream of *rand though

I can no longer use the notepad ++ files but I can use CSIDE
If I delete *goto cardtest then I get fall out error
*return also doesn’t work I’m afraid

Then you should try placing that *return inside the *else. I don’t know if randomtest tries changing the number in each try of the *input_number otherwise it would be stuck if it kept repeating a value that wouldn’t work.

Still, it would be best to see the rest of the code because all of it looks ok to me. Are you calling *gosub cardtest anywhere else besides this moment?

1 Like

You’re getting those errors because you’re not using implicit control flow. Without it enabled, you have to have a *goto at the end of every *if and *else. What I believe the issue to be is that randomtest is inputting the same number each time, and when that number happens to be less than the amount of gold the player has, it can’t get out.

Here’s my suggestion, and it might be a little hamfisted/dirty coding/etc. If what you said above:

is true, then is there really any need to do this check at this point? If the player always has 20 gold, then you can just use:

*input_number goldbet 1 10

And if this is something you return to after one round, where the player may run out of gold, then I would suggest making a workaround that gives the player an option to either stop playing or try a new bet, so that you don’t accidentally create an endless loop and can give randomtest a chance to break out and continue on the rest of the game. You can even implement a check on the continue playing option to prevent the player/randomtest from continuing if gold = 0.

*temp goldbet 0

*label cardtest
*set goldbet 0
How much do you want to bet?
*input_number goldbet 1 10
*if gold < goldbet
        *goto cantbet
*else
        *goto playgame

*label cantbet
You don't have enough money to bet. What do you want to do?
*choice
        #Stop playing.
                *goto stopplaying
        *if (gold != 0) #Place a new bet.
                *goto cardtest

*label playgame
blah blah blah...

*label stopplaying
You decide to stop playing.
*goto continue

*label continue
blah blah blah...

At some point, gold will equal 0, so randomtest will have to stop playing and continue on with the rest of the game.

A side note. CSIDE’s indent fix feature causes more problems than solutions. I would recommend just rewriting what you need into CSIDE so it does the indentation correctly automatically.

3 Likes

Thank you all! I am going to try now but wanted to say thank you for all your help and time. Occasionaly it must seem as if I don’t try things for myself before I reach out for help but I wanted to assure all you helpful people that I am trying all I can think of before asking

Cheers!

It’s all fixed now! As was noted in the replies, the loop was being caused when the player didn’t have enough gold and was sent back round in circles
Annoyingly for me I used this code in three places in the scene and was horrified when the error kept returning - I had forgotten it was elsewhere too (doh!)

It has now passed a random test of 10,000 iterations (which took ages so I hope my laptop is up to testing the final version)

Thank you all, appreciated

2 Likes

This topic was automatically closed 24 hours after the last reply. If you want to reopen your WiP, contact the @moderators.