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
| import requests import json from heapq import heappop, heappush
current_direction = "RIGHT" last_direction = "RIGHT"
direction_map = { "UP": "DOWN", "DOWN": "UP", "LEFT": "RIGHT", "RIGHT": "LEFT" }
def heuristic(a, b): return abs(a[0] - b[0]) + abs(a[1] - b[1])
def a_star_search(start, goal, grid_size, snake): open_set = [] heappush(open_set, (0, start)) came_from = {} g_score = {start: 0} f_score = {start: heuristic(start, goal)}
while open_set: _, current = heappop(open_set)
if current == goal: path = [] while current in came_from: path.append(current) current = came_from[current] path.reverse() return path
for dx, dy in [(0, -1), (0, 1), (-1, 0), (1, 0)]: neighbor = (current[0] + dx, current[1] + dy) if 0 <= neighbor[0] < grid_size and 0 <= neighbor[1] < grid_size and neighbor not in snake: tentative_g_score = g_score[current] + 1 if neighbor not in g_score or tentative_g_score < g_score[neighbor]: came_from[neighbor] = current g_score[neighbor] = tentative_g_score f_score[neighbor] = tentative_g_score + heuristic(neighbor, goal) heappush(open_set, (f_score[neighbor], neighbor))
return []
def get_next_direction(snake, food, grid_size): global current_direction, last_direction
head_x, head_y = snake[0]['x'], snake[0]['y'] food_x, food_y = food[0], food[1]
path = a_star_search((head_x, head_y), (food_x, food_y), grid_size, [(seg['x'], seg['y']) for seg in snake])
if path: next_x, next_y = path[0] if next_x > head_x: return "RIGHT" elif next_x < head_x: return "LEFT" elif next_y > head_y: return "DOWN" elif next_y < head_y: return "UP" else: possible_directions = ["UP", "DOWN", "LEFT", "RIGHT"] possible_directions.remove(direction_map[current_direction]) return possible_directions[0]
url = "http://eci-2zeg9nmyrzhwzgcfhwik.cloudeci1.ichunqiu.com:5000/move" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0", "Accept": "*/*", "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2", "Accept-Encoding": "gzip, deflate", "Referer": "http://eci-2zeg9nmyrzhwzgcfhwik.cloudeci1.ichunqiu.com:5000/", "Content-Type": "application/json", "Origin": "http://eci-2zeg9nmyrzhwzgcfhwik.cloudeci1.ichunqiu.com:5000/", "Connection": "close", "Cookie": "session=eyJ1c2VybmFtZSI6IjEifQ.ZydYYQ.y4ThT_rofnb2wUqbhAdL-57DLLU", "Priority": "u=4" } proxies = { "http": "http://127.0.0.1:8080", "https": "http://127.0.0.1:8080", }
def main(): global current_direction, last_direction
while True: try: response = requests.post(url, headers=headers, data=json.dumps({"direction": current_direction}), proxies=proxies)
if response.status_code == 200: game_data = json.loads(response.text)
if 'status' in game_data: if game_data['status'] == 'game_over': print("Game Over!") print("Final Score:", game_data.get('score', 'Score not provided')) break elif game_data['status'] == 'win': print("Congratulations! You win!") print("Final Score:", game_data.get('score', 'Score not provided'))
score = game_data.get('score', 0) if score >= 50: burp_url = "http://127.0.0.1:8080/some_endpoint" burp_response = requests.post(burp_url, json=game_data) print("Data sent to Burp:", burp_response.status_code)
food = game_data.get('food', []) snake = [{'x': segment[0], 'y': segment[1]} for segment in game_data.get('snake', [])] grid_size = game_data.get('grid_size', 20)
print("Food:", food) print("Snake:", snake) print("Current Score:", score)
if food and snake: next_direction = get_next_direction(snake, food, grid_size) current_direction = next_direction else: print(f"Request failed with status code: {response.status_code}") print("Response Body:", response.text) break except requests.exceptions.RequestException as e: print(f"Request error: {e}") print("Response Body:", response.text if 'response' in locals() else "No response") break except json.JSONDecodeError as e: print(f"JSON decode error: {e}") print("Response Body:", response.text if 'response' in locals() else "No response") break except KeyError as e: print(f"Key error: {e}") print("Response Body:", response.text if 'response' in locals() else "No response") break except Exception as e: print(f"Unexpected error: {e}") print("Response Body:", response.text if 'response' in locals() else "No response") break
if __name__ == "__main__": main()
|