C++ deque impl


  • 0
    T
    class SnakeGame {
        unordered_set<int> mapSnake; // stores snake body pos
        deque<pair<int,int>> Snake;  // snake 
        vector<pair<int, int>> _food;
        int indx_food, w, h; // indx_food: current food pos 
    public:
        /** Initialize your data structure here.
            @param width - screen width
            @param height - screen height 
            @param food - A list of food positions
            E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. */
        SnakeGame(int width, int height, vector<pair<int, int>> food):indx_food(0),w(width),h(height),_food(food) {
            Snake.push_front(make_pair(0, 0));
            mapSnake.insert(0);
        }
        bool hasEatFood(int sh, int sw) {
            if (indx_food == _food.size()) { // eat all food
                return false;
            }
            if (_food[indx_food].first == sh && _food[indx_food].second == sw) {
                ++indx_food;
                return true;
            }
            return false;
        }
        void printMap(string dir) {
            cout << "----" << dir << "----" << endl;
            for (int i = 0; i < h; ++i) {
                for (int j = 0; j < w; ++j) {
                    if (mapSnake.find(i*w+j) == mapSnake.end())
                        cout << "0";
                    else {
                        i == Snake.front().first && j == Snake.front().second ? cout << "H" : cout << ".";
                    }
                }
                cout << endl;
            }
            
        }
        void updateSnake(int sh, int sw) { // move snake 1 step ahead
            if (!hasEatFood(sh, sw)) { // update tail
                mapSnake.erase(Snake.back().first*w+Snake.back().second);
                Snake.pop_back();
            }
            Snake.push_front(make_pair(sh, sw));
            mapSnake.insert(sh*w+sw);
        }
        bool isCollideOrEatSelf(int sh, int sw) { // check validity 
            return !(sh >= 0 && sh < h && sw >= 0 && sw < w &&
                (mapSnake.find(sh*w+sw) == mapSnake.end() || sh == Snake.back().first && sw == Snake.back().second));
        }
        int getScore() {
            return indx_food;
        }
        /** Moves the snake.
            @param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down 
            @return The game's score after the move. Return -1 if game over. 
            Game over when snake crosses the screen boundary or bites its body. */
        int move(string direction) {
            // get snake head pos
            int sh = Snake.front().first, sw = Snake.front().second;
            
            switch(direction[0]){
                case 'U':
                    --sh; break;
                case 'D':
                    ++sh; break;
                case 'L':
                    --sw; break;
                case 'R':
                    ++sw; break;
            }
            //printMap(direction);
            if (isCollideOrEatSelf(sh, sw)) {
                return -1;
            }
            updateSnake(sh, sw);
            return getScore();
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.