Lua breaking out of a function at random

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
tdc5013
Prole
Posts: 38
Joined: Wed Feb 08, 2012 4:24 pm

Lua breaking out of a function at random

Post by tdc5013 »

Hi Guys,

I've been looking at this for a few hours now so I thought I'd bring it to you, as I've made no headway. The error occurring is that the game will simply jump out of the user's turn and jump straight into the code that allows the A.I to start determining what cards it can play (this happens in a function called Opponent_AI.OppPlayCards). It doesn't even start the function that does this, it simply jumps into it. I've included an image illustrating where this happens (white line). What should happen is after the AI has finished the process of playing a card the code changes turns, this part is appears fine as the

Code: Select all

It's Player 1's Turn
printout indicates. From here it should wait for a card to be selected by the user before starting up again. What actually happens is that it jumps out of this straight into looking unplayable cards,

Code: Select all

OppPlayCards: UnPlayable: 1

QC
and runs repeatedly for several turns until either stopping or throwing up a separate error in the Opponent_AI class (which I'm sure are being caused by this error). I haven't even been able to find an exact reproduction method, it just happens after a handful of turns.

Also it's probably worth knowing the rules of the game, otherwise you might not get too far in it. Ace is high, and the aim is to get rid of all your cards before the AI does. You can play one at a time, or multiples if the ranks are the same. You can only play a card of equal or higher rank to the last one played, except in exception circumstances. These being when a 2 (resets the deck) or an 8 (a mirror of the card beneath it) are played. If a 7 is played then you have to play a card equal or lower than that, if a Queen is played then 2,7,8,10 cannot be played. Oh, and 10 'burns' the played card deck. If you can't play a card then you have to 'pickup' - there's a button for that.

The .love, and a zip of the Lua files have been attached. I wish I could give more information on this, but I have no idea what could be the cause so the best I can really do is provide you with the code and answer any questions you have about it.

Any help will be appreciated, this one is really driving me crazy!

Thanks.
Attachments
prints.png
prints.png (50.65 KiB) Viewed 4227 times
CardCoder.zip
(90.23 KiB) Downloaded 135 times
CardCoder.love
(83.24 KiB) Downloaded 192 times
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Lua breaking out of a function at random

Post by miko »

tdc5013 wrote:The .love, and a zip of the Lua files have been attached.
It has .love extension, but in fact it is a rar archive. Anyways, I have 2 suggestions:
1. make a test version with known initial state instead of randomly generated (so it could be easily tested)
2. make a way to record and replay user (and AI) actions.

Then you could attach a scenario which leads to error condition, so we could check that out.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
tdc5013
Prole
Posts: 38
Joined: Wed Feb 08, 2012 4:24 pm

Re: Lua breaking out of a function at random

Post by tdc5013 »

Hi thanks for the reply. When you say 'known initial state' do you mean simply having the same cards played out every time? That should be doable. As for recording and replaying actions I'm really not sure how I'd do that; I have everything printing out in the console window during the game which basically allows you to read through and see what's going on.

As for the .love file, I'll have another look at how to make them, I thought I'd done it right.

EDIT: I've attached the new love file below, this one works.
Attachments
CardCoder.love
NEW LOVE FILE, THE LAST ONE WAS SCREWY
(81.58 KiB) Downloaded 128 times
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Lua breaking out of a function at random

Post by Boolsheet »

I can't say anything yet about your specific problem with the player's turn getting ignored, but there seems to be an issue with the way you remove table entries during loops.

For example, call math.randomseed(455225) at the beginning of love.load. If we're lucky (and you're on Windows and have the same rng), the player should now have a Queen of Clubs in the deck. Start the game and play the queen. The turn changes and the AI tries to go through the cards it can play. In Opponent_AI.lua:231, the loop marks all the unplayable cards. Line 240 calls a function that will remove them. This is a problem because the for-loop starting on line 222 is unaware that the table has been changed and iterates into the nil.

I'm assuming this isn't the only place where this issue comes up.
Last edited by Boolsheet on Fri Mar 15, 2013 6:31 pm, edited 1 time in total.
Shallow indentations.
tdc5013
Prole
Posts: 38
Joined: Wed Feb 08, 2012 4:24 pm

Re: Lua breaking out of a function at random

Post by tdc5013 »

Boolsheet wrote:I can't say anything yet about your specific problem about the player's turn getting ignored, but there seems to be an issue with the way you remove table entries during loops.

For example, call math.randomseed(455225) at the beginning of love.load. If we're lucky (and you're on Windows and have the same rng), the player should now have a Queen of Clubs in the deck. Start the game and play the queen. The turn changes and the AI tries to go through the cards it can play. In Opponent_AI.lua:231, the loop marks all the unplayable cards. Line 240 calls a function that will remove them. This is a problem because the for-loop starting on line 222 is unaware that the table has been changed and iterates into the nil.

I'm assuming this isn't the only place where this issue comes up.
Thank you for pointing that out. Can this be sorted by simply ending that for loop before I remove the unplayableCards?

Also, I've figured out the problem occurring, and unfortunately I may need some more help planning the fix. The problem is that once inside OppPlayCards the program doesn't seem to leave - this is due to poor coding on my part. To fix it I think I'm going to basically have to restructure the Opponent_AI class. I'm going to need to break it all up so that one job is handled at a time, from finding unplayable cards to checking to multiples. In terms of breaking out of this loop I run myself into though, I'm not sure how I can do that. If I had Opponent_AI function calls from GamePlay() in main.lua, with those AI functions returning a whittled down table of cards each time - then modify the PlayCards() function to take in a table of cards to play and have that being called afterwards - could that work? Let me illustrate in pseudo:

Code: Select all

function GamePlay()

--If it's the AI's turn
Opponent_AI.OppPlayCards() -- modified to just determine the 'personality' of the AI.
local table x = Opponent_AI.Unplayables() 

-- if x = nil then it'll call RuleBook.PickUp()
--else
local table y = Opponent_AI.MultipleCheck(x)
local table z = Opponent_AI.LastPickupCheck(y)

RuleBook.CardsToPlay(z) -- a new function for placing cards in the cardsToBePlayed table, so I won't have to change a lot of extra code
RuleBook.PlayCards(cardsToBePlayed)
RuleBook.TurnChange()


Alternatively, and this isn't being suggested as good code but as a method of circumventing large rewrites, could I just take TurnChange out of PlayCards and place it in the existing gamePlay loop? Or will I still have the existing problem of the program running itself into a corner? My thinking is that this will bring the program out of the loop it's getting itself stuck in.

Thanks for your help so far guys.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Lua breaking out of a function at random

Post by Boolsheet »

tdc5013 wrote:Can this be sorted by simply ending that for loop before I remove the unplayableCards?
I don't know your code or the game well enough to give you any advice on that. I do believe that taking some things out of that huge function and putting them into smaller ones is a good idea.
Shallow indentations.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests