I’ve attended amazing conference Pycon SK in Bratislava 2018. Part of the programme was a contest in a game snakepit (contests info). It was a nice game where contestants should write a behaviour of snake from the old game we all remember from childhood.
Instead of paying attention to the conference talks the contest has absorbed me and I created quick and dirty idea of how should snake play the game. There was 11 contestants and my snake artimi0 end up at the third place!
The code
You can find the code at my github page. I tried to write it as the human player would play the game and keep it as simple as possible. To understand the code it is good to start with next_direction
.
First I call get_position
to discover position of myself. Then I call find_best
to find the best place to go. Each digit on the map is scored by the manhattan distance and the score is made just by division of digit by the distance. After it finds the best spot it calls method get_to
which tries to find a way to the best scored position. If it is possible it continues, if not it calls method backup which is just randomly walk and tries not to hit any block. To be safe to move I check that given position is not some sort of block (my snake, wall, other snake) and also call method free_room
that performs autofill up to snake length to see if I can fit into given space, this is there to prevent locking myself in some small space. That is the main idea of my robot snake.
After I submitted the code I figured that there are several bugs and unintended behaviour but hey it was really only quick attempt to write some good snake. I enjoyed the contest and thinking about organizing something like that by myself at work.