1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| import requests import json
score = 0 last_direction = "RIGHT" directions = ["RIGHT", "LEFT", "UP", "DOWN"]
url = "<http://eci-2zealtn2xy2kz270tbqu.cloudeci1.ichunqiu.com:5000/move>"
score = 0 last_direction = "RIGHT" directions = ["RIGHT", "LEFT", "UP", "DOWN"]
def send_move(direction): global score headers = { "Content-Type": "application/json", "Cookie": "session=eyJ1c2VybmFtZSI6ImFhYSJ9.ZycPhA.vBT0Ik7lI5P8xk13_9sRWRSKKv0", "Origin": "<http://eci-2zealtn2xy2kz270tbqu.cloudeci1.ichunqiu.com:5000>", "Referer": "<http://eci-2zealtn2xy2kz270tbqu.cloudeci1.ichunqiu.com:5000/>" } data = json.dumps({"direction": direction}) response = requests.post(url, headers=headers, data=data)
print("Response Text:", response.text) return response.json()
def is_safe_move(snake, next_head): snake_body_set = set(tuple(segment) for segment in snake) return (0 <= next_head[0] < 20) and (0 <= next_head[1] < 20) and (tuple(next_head) not in snake_body_set)
def can_move(snake, direction): head_x, head_y = snake[0] if direction == "UP": for segment in snake[1:]: if segment[0] == head_x and segment[1] < head_y: if is_safe_move(snake, [head_x + 1, head_y]): return "RIGHT" elif is_safe_move(snake, [head_x - 1, head_y]): return "LEFT" return None return "UP" elif direction == "DOWN": for segment in snake[1:]: if segment[0] == head_x and segment[1] > head_y: if is_safe_move(snake, [head_x + 1, head_y]): return "RIGHT" elif is_safe_move(snake, [head_x - 1, head_y]): return "LEFT" return None return "DOWN" elif direction == "LEFT": for segment in snake[1:]: if segment[1] == head_y and segment[0] < head_x: if is_safe_move(snake, [head_x, head_y - 1]): return "UP" elif is_safe_move(snake, [head_x, head_y + 1]): return "DOWN" return None return "LEFT" elif direction == "RIGHT": for segment in snake[1:]: if segment[1] == head_y and segment[0] > head_x: if is_safe_move(snake, [head_x, head_y - 1]): return "UP" elif is_safe_move(snake, [head_x, head_y + 1]): return "DOWN" return None return "RIGHT"
def get_new_directions(snake, food): head_x, head_y = snake[0] food_x, food_y = food
directions = [] if head_x < food_x: directions.append("RIGHT") elif head_x > food_x: directions.append("LEFT")
if head_y < food_y: directions.append("DOWN") elif head_y > food_y: directions.append("UP")
return directions
def print_game_state(snake, food): grid_size = 20 grid = [['.' for _ in range(grid_size)] for _ in range(grid_size)]
for segment in snake: grid[segment[1]][segment[0]] = 'S'
grid[food[1]][food[0]] = 'F'
for row in grid: print(' '.join(row)) print()
response = send_move(last_direction) while score < 50: if response is None: break
if "status" in response: if response["status"] == "game_over": print("游戏结束,得分:", score) break elif "snake" not in response: print("响应缺少蛇的信息,游戏结束。") break
snake = response["snake"] food = response["food"] score = response["score"]
print_game_state(snake, food)
possible_directions = get_new_directions(snake, food)
new_direction = None for direction in possible_directions: moved_direction = can_move(snake, direction) if moved_direction: new_direction = moved_direction break
if new_direction: response = send_move(new_direction) if score > 0 and snake[0] == food: on_edge_rotation = True edge_direction = "RIGHT" continue
print("游戏结束,得分:", score)
|