#pragma once #include "Effect.h" #include "prototypes.h" #define SNAKE_DIR_NORTH 0 #define SNAKE_DIR_EAST 1 #define SNAKE_DIR_SOUTH 2 #define SNAKE_DIR_WEST 3 class SnakeEffect : public Effect { private: Coords _pos; Coords _apple; int8_t _dir = SNAKE_DIR_NORTH; uint8_t* _map; uint16_t _pixels; uint8_t _length; unsigned long _last_apple_at; unsigned long _last_move_at; // The following code is a handwritten "ai". Useful for testing and stuff. //int8_t _decisions[64] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,1,-1,1,-1,-1,-1,-1,0,0,0,0,-1,-1,0,0,0,1,0,0,-1,-1,0,0}; int8_t _decisions[64] = {0, 1, 1, -1, 0, 1, 1, -1, 0, -1, 1, -1, -1, 0, 1, -1, -1, 0, 0, 1, -1, -1, 0, 0, -1, 0, 0, -1, 0, -1, 0, -1, -1, 0, 0, -1, -1, 0, 1, -1, -1, 1, 1, -1, -1, 1, -1, 0, 0, 1, 0, 1, -1, -1, 0, 0, 0, -1, 0, 1, 0, -1, 0, -1}; //int8_t _decisions[64] = {1, 1, 0, 0, 1, 1, -1, -1, 0, -1, 0, -1, -1, 0, -1, 1, 1, -1, 0, 0, 1, -1, 0, 1, 0, -1, 0, -1, 0, 1, 1, 1, -1, 0, 0, 0, -1, -1, -1, 1, -1, -1, 1, 1, -1, 0, 1, 1, 1, 0, 0, -1, -1, 0, -1, 1, 0, 0, 0, -1, 0, -1, 1, 1}; uint16_t _xy2i(uint8_t x, uint8_t y); uint16_t _xy2i(Coords c); Coords _i2xy(uint16_t i); Coords _new_pos(uint8_t dir); uint8_t _dying = 0; bool _is_free(uint8_t dir); bool _to_apple(uint8_t dir); void _place_apple(); void _init(); void _decide(); int8_t _manual_decision(); void _move(); void _draw(); public: SnakeEffect(); ~SnakeEffect(); void loop(uint16_t ms); boolean can_be_shown_with_clock(); String get_name() override { return "snake"; } };