Worm Game: move() and grow(), please help.

235 views
Skip to first unread message

Diego Ciccia

unread,
Dec 28, 2015, 8:16:09 PM12/28/15
to mooc.fi
Hi,

I'm having problems with week 12 exercise 49 WormGame.

I don't understand how grow() and move() are supposed to talk to each other, or how grow() is supposed to grow the worm by one piece but not add the piece to the List until move() is called.  I created a variable to store a new piece when grow() is called and then add it to the list when move() is called but I don't think that is right.  The example shows that nothing is supposed to happen when grow() is called until move() is called but how can that be?  The example also shows that the first piece of the game doesn't get deleted after grow() and move() are called:

[(5,5)] // new Worm(5,5, RIGHT)
[(5,5), (6,5)] // move
[(5,5), (6,5), (7,5)] // move
[(6,5), (7,5), (8,5)] // move
[(6,5), (7,5), (8,5)] // grow
[(6,5), (7,5), (8,5), (9,5)] // move

How come (6,5) stays in the worm? Shouldn't the worm be (7, 5), (8, 5), (9, 5), (10, 5) if the first piece should be deleted?

Please help, I don't think I am understanding how these methods should be working.  Thank you!

The link to my work is here: PASTE TMC PASTEBIN LINK HERE


Best regards


Supported languages are English and Finnish

Please use our build in TMC Pastebin when asking questions related to your code  
 
TMC Plugin has built-in code sharing feature. 
In NetBeans, select TMC -> Send code to TMC pastebin. Copy the link you receive and paste it to your message.  

You may use our web interface to share code snippets: http://paste.mooc.fi

Diego Ciccia

unread,
Dec 29, 2015, 8:20:25 PM12/29/15
to mooc.fi
I believe I have solved this myself.  However, is it okay for us to create new private variables in the class Worm? I know in the past, some tests have failed because we couldn't create extra variables.  In my example, I used a variable as a flag when grow() is called.

ljleppan

unread,
Jan 4, 2016, 4:24:18 AM1/4/16
to mooc.fi
Hi Diego,

As far as I recall, the tests allow you to create new variables in the worm if you feel that you need that.

As for your original question, consider it like this: when ever we move, we only want the head to move by one space on the board. For that reason, we want to add the new piece to the end so the tail "stays still". The other option would be for the head to jump two spaces ahead which would look weird and be somewhat unpredictable for the player.

Hopefully that made some sense. If you want me to clear up some other things, please don't hesitate to ask :)

-Leo
Reply all
Reply to author
Forward
0 new messages