Q from CL newbie: stop undefined function from breaking graph display

Dear Desmos CL folks,
I made a Desmos game with a snake eating apples. The user steers the path of the snake by defining a function, with the aim of making it eat the apples:

The main bug at the moment is that the user-created function f(x) becomes undefined whenever the user is in the middle of editing it. For example, if it is 'f(x) = x + ’ because the user hasn’t finished typing yet, then f(x) is undefined, and any part of the graph that depends on f(x) breaks.

Specifically, I need to use f(x) in order to decide whether the apples have been eaten or not, and hence whether or not to plot them. However, that stops working while f(x) is undefined, so my apples start looking as though they are getting eaten, even while there is no snake!

From looking at this forum and the Desmos help docs, it looks like there does exist an isUndefined() function in the CL. However, it’s not clear to me how to incorporate that into my activity. I confess that I find the talk of sources and sinks a bit confusing. I keep on trying mentally to convert that into Python, but it doesn’t quite match!

Any help greatly appreciated!

Raj
P.S. I’d also be very interested in accessing the last element of a list, a bit list Python’s [-1] index. My current activity hard-codes this, making it easy to forgot to update the number when the list of apple coordinates gets longer or shorter.