離蛇身體最遠(TLE)

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
8.2 kB
5
Indexable
Never
#ifndef DSAP_CUSTOM_CONTROLLER_H
#define DSAP_CUSTOM_CONTROLLER_H

#include "ISnakeController.h"
#include "DirectionType.h"
#include "Snake.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <vector>
#include <algorithm>

bool addDanger = true;


class CustomController : public ISnakeController {
public:
    DirectionType NextDirection(const Game& game, size_t id);
    const Position& SnakeHead(const Game& game, const size_t id){return game.Snakes().find(id)->second.Head();};
    const std::list<Position> SnakeBodyList(const Game& game, const size_t id){return game.Snakes().find(id)->second.Body();};
    const Position& MyHead(const Game& game){return game.Center();};
    const Snake Me(const Game& game){return game.Snakes().find(1)->second;};
    
};

long left = 999999;
long right = 999999;
long forward = 9999999;


DirectionType CustomController::NextDirection(const Game& game, size_t id){
    const int radius = game.kSnakeRadius;
    
    // judge snake is in front left or right
    
    
    
    
    
    std::vector<Position> danger_ = {};
    
// /////////// tips
//    std::cout << SnakeHead(game, 1).x;
//    std::list<Position> bodyLst = SnakeBodyList(game, 2);
    
//    for (auto it = bodyLst.begin(); it != bodyLst.end(); it++) {
//        std::cout << it->x << ", " << it->y << std::endl;
//    }
//    std::cout << MyHead(game).x << " " << MyHead(game).y << std::endl;
    
    // add danger
    

    Position pos = MyHead(game);
    for(size_t i = 1; i < game.Snakes().size(); ++i){
        if(i == id || (pos.x - SnakeHead(game, i).x) > 200 || (pos.y - SnakeHead(game, i).y) > 200){
            continue;
        }
        std::list<Position> bodyLst = SnakeBodyList(game, i);
        
//       for (auto it = bodyLst.begin(); it != bodyLst.end(); it++) {
//            long other_x = it->x;
//            long other_y = it->y;
//            long new_other_x = std::cos(Me(game).Direction()) * other_x + std::sin(Me(game).Direction()) * other_y;
//            long new_other_y = -1 * std::sin(Me(game).Direction()) * other_x + std::cos(Me(game).Direction()) * other_y;
//            if(new_other_y > pos.y - radius){  // if in my first or second quadrant
//                if((pos.x + radius) > (new_other_x - radius) || (pos.x - radius) < (new_other_x + radius)){  // snake in my forward
//                    long temp_forward = std::abs((new_other_y - radius) - (pos.y + radius));
//                    if(temp_forward < forward){
//                        forward = temp_forward;
//                    }
//                }
//                else if (new_other_x > pos.x){  // right
//                    if((pos.y + radius) > (new_other_y - radius) || (pos.y - radius) < (new_other_y + radius)){  // snake in my right
//                        long temp_right = std::abs((new_other_x - radius) - (pos.x + radius));
//                        if(temp_right < right){
//                            right = temp_right;
//                        }
//                    }
//                }
//
//                else if (new_other_x < pos.x){  // snake in my left
//                    if((pos.y + radius) > (new_other_y - radius) || (pos.y - radius) < (new_other_y + radius)){  // snake in my left
//                        long temp_left = std::abs((new_other_x - radius) - (pos.x + radius));
//                        if(temp_left < left){
//                            left = temp_left;
//                        }
//                    }
//                }
//            }
//
//
//        }  // end of snake i's body for loop
        
        
    }  // end of snakes for loop
    
    
    
    
    
    
    
    
    // print current status
    //////////////////////////////////////////////////////////////
//    std::cout << game.Time() << ": " << game.Center().x << " " << game.Center().y  << ", Cur Direction " << game.Snakes().find(1)->second.Direction() << std::endl;
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//    if(game.Time() == 200){
//        first = true;
//    }
    
    // first go to the bottom
//    if(first == true){
//        if(game.Snakes().find(1)->second.Direction() == 90){
//            first = false;
//            second = true;
//        }
//        return DirectionType::kRight;
//    }
    // than turn left
//    if(second == true){
//        if(game.Center().y > 2300 && game.Snakes().find(1)->second.Direction() > 0){
//            return DirectionType::kLeft;
//        }
//    }
   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
//    if(dir_sum < -360 || dir_sum > 360){
//        return DirectionType::kForward;
//    }
//    long XSAVE_DISTANCE = 200;
//    long YSAVE_DISTANCE = 50;
//    long SNAKESAVE_DISTANCE = 50;
//    // default: right edge turn left; left edge turn right
//    if(game.Center().y < 125 && game.Snakes().find(1)->second.Direction() == -180){
//        reverse = true;
//    }
//
//
//    if(reverse == true){
//
//
//        if(game.Center().x + XSAVE_DISTANCE > game.FieldWidth()){
////            std::cout << "1) Edge" << std::endl;
//            return DirectionType::kRight;
//        }
//        // if collide with left edge
//        if(game.Center().x - XSAVE_DISTANCE < 0){
////            std::cout << "2) Edge" << std::endl;
//            return DirectionType::kLeft;
//        }
////                for(size_t i = 1; i < game.Snakes().size(); ++i){
////                    if(ymin_edges[i] < game.Center().y + YSAVE_DISTANCE  && ymax_edges[i] > game.Center().y + YSAVE_DISTANCE){
////                        if(game.Center().x + SNAKESAVE_DISTANCE > xmin_edges[i] ){
////                            std::cout << "3) Snake: " << xmin_edges[i] << std::endl;
////                            return DirectionType::kRight;
////                        }
////                        if(game.Center().x - SNAKESAVE_DISTANCE < xmax_edges[i]){
////                            std::cout << "4) Snake: " << xmax_edges[i]<< std::endl;
////                            return DirectionType::kLeft;
////                        }
////                    }
////                }
//        return DirectionType::kForward;
//
//    }
//    else if(reverse == false){
//
////
//        // if collide with right edge
//        if(game.Center().x + XSAVE_DISTANCE > game.FieldWidth()){
////            std::cout << "5) Edge!" << std::endl;
//            return DirectionType::kLeft;
//        }
//        // if collide with left edge
//        if(game.Center().x - XSAVE_DISTANCE < 0){
////            std::cout << "6) Edge!" << std::endl;
//            return DirectionType::kRight;
//        }
//
//                for(size_t i = 1; i < game.Snakes().size(); ++i){
//                    if(ymin_edges[i] < game.Center().y + YSAVE_DISTANCE  && ymax_edges[i] > game.Center().y + YSAVE_DISTANCE){
//                        if(game.Center().x + SNAKESAVE_DISTANCE > xmin_edges[i]){
////                            std::cout << "7) Snake: " << xmin_edges[i] << std::endl;
//                            return DirectionType::kLeft;
//                        }
//                        if(game.Center().x - SNAKESAVE_DISTANCE < xmax_edges[i]){
////                            std::cout << "8) Snake: " << xmax_edges[i] << std::endl;
//                            return DirectionType::kRight;
//                        }
//                    }
//                }
////        std::cout << "9)Forward!" << std::endl;
    
    
    
    if(left > right && left > forward){
        return DirectionType::kLeft;
    }
    else if(right > left && right > forward){
        return DirectionType::kRight;
    }
    else{
        return DirectionType::kForward;
    }
        
}



#endif // DSAP_CUSTOM_CONTROLLER_H