Last active
July 13, 2023 07:59
-
-
Save ncchen99/a5a1c2e0ca9a7c064c287299101676aa to your computer and use it in GitHub Desktop.
Keep Off The Grass Function version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import math | |
import random | |
from typing import List, Dict, Tuple | |
width, height = [int(i) for i in input().split()] | |
grid_data, my_matter, opp_matter = [ | |
[{} for i in range(height)] for j in range(width)], 0, 0 | |
def input_data() -> None: | |
"""輸入每回合ㄉ遊戲資料""" | |
global my_matter, opp_matter, grid_data | |
my_matter, opp_matter = [int(i) for i in input().split()] | |
grid_data = [[{} for _ in range(height)] for __ in range(width)] | |
for i in range(height): | |
for j in range(width): | |
# owner: 1 = me, 0 = foe, -1 = neutral | |
# can_spawn: 1 if you can spawn a unit, else 0 | |
# can_build: 1 if you can build a factory, else 0 | |
# in_range_of_recycler: 1 if you can build a factory, else 0 | |
# units: number of units on the tile | |
# recycler: 1 if the tile is a recycler, else 0 | |
# scrap_amount: amount of scrap on the tile | |
input_data = [int(k) for k in input().split()] | |
grid_data[j][i] = { | |
"scrap_amount": input_data[0], | |
"owner": input_data[1], | |
"units": input_data[2], | |
"recycler": input_data[3], | |
"can_build": input_data[4], | |
"can_spawn": input_data[5], | |
"in_range_of_recycler": input_data[6] | |
} | |
def print_message(message) -> None: | |
"""輸出除錯(底霸格)ㄉ訊息 | |
輸入ㄉ參數: | |
message -- 要輸出ㄉ訊息 | |
""" | |
print(f"Debug messages: {message}", file=sys.stderr, flush=True) | |
def get_can_capture_tiles() -> List[Dict[str, int]]: | |
"""取得可以奪取ㄉ地板們 | |
回傳值: 一個 List 裡面有若干個 Dict,Dict 裡面有 x, y 兩個 key | |
範例: | |
[{"x": 0, "y": 0}, {"x": 1, "y": 0}, ...] | |
""" | |
get_can_capture_tiles = [] | |
for i in range(height): | |
for j in range(width): | |
if grid_data[j][i]["owner"] != 1 and grid_data[j][i]["scrap_amount"] > 0: | |
get_can_capture_tiles.append({"x": j, "y": i}) | |
return get_can_capture_tiles | |
def get_my_robots_infos() -> List[Dict[str, int]]: | |
"""取得我方所有機器人ㄉ位置和數量資訊 | |
回傳值: 一個 List 裡面有若干個 Dict,Dict 裡面有 x, y, units 三個 key | |
範例: | |
[{"x": 0, "y": 0, "units": 1}, {"x": 1, "y": 0, "units": 1}, ...] | |
""" | |
my_robots = [] | |
for i in range(height): | |
for j in range(width): | |
if grid_data[j][i]["owner"] == 1 and grid_data[j][i]["units"] > 0: | |
my_robots.append( | |
{"x": j, "y": i, "units": grid_data[j][i]["units"]}) | |
return my_robots | |
def get_spwanable_tiles() -> List[Dict[str, int]]: | |
"""取得可以放置機器人的地板 | |
回傳值: 一個 List 裡面有若干個 Dict,Dict 裡面有 x, y 兩個 key | |
範例: | |
[{"x": 0, "y": 0}, {"x": 1, "y": 0}, ...] | |
""" | |
get_spwanable_tiles = [] | |
for i in range(height): | |
for j in range(width): | |
if grid_data[j][i]["owner"] == 1 and grid_data[j][i]["can_spawn"] == 1: | |
get_spwanable_tiles.append({"x": j, "y": i}) | |
return get_spwanable_tiles | |
def get_my_recyclers_location() -> List[Dict[str, int]]: | |
"""取得我方所有回收機ㄉ位置 | |
回傳值: 一個 List 裡面有若干個 Dict,Dict 裡面有 x, y 兩個 key | |
範例: | |
[{"x": 0, "y": 0}, {"x": 1, "y": 0}, ...] | |
""" | |
my_recyclers = [] | |
for i in range(height): | |
for j in range(width): | |
if grid_data[j][i]["owner"] == 1 and grid_data[j][i]["recycler"] == 1: | |
my_recyclers.append({"x": j, "y": i}) | |
return my_recyclers | |
def get_can_build_tiles() -> List[Dict[str, int]]: | |
"""取得可以建造回收機ㄉ地板 | |
回傳值: 一個 List 裡面有若干個 Dict,Dict 裡面有 x, y 兩個 key | |
範例: | |
[{"x": 0, "y": 0}, {"x": 1, "y": 0}, ...] | |
""" | |
get_can_build_tiles = [] | |
for i in range(height): | |
for j in range(width): | |
if grid_data[j][i]["owner"] == 1 and grid_data[j][i]["can_build"] == 1: | |
get_can_build_tiles.append({"x": j, "y": i}) | |
return get_can_build_tiles | |
def move_robot(rx: int, ry: int, tx: int, ty: int, unit: int) -> None: | |
"""手動移動機器人 | |
輸入ㄉ參數: | |
rx -- 機器人ㄉ x 座標 | |
ry -- 機器人ㄉ y 座標 | |
tx -- 目標地板ㄉ x 座標 | |
ty -- 目標地板ㄉ y 座標 | |
unit -- 要移動ㄉ單位數量 | |
""" | |
if grid_data[rx][ry]["owner"] == 1 and grid_data[rx][ry]["units"] > 0: | |
if grid_data[rx][ry]["units"] >= unit: | |
print(f"MOVE {unit} {rx} {ry} {tx} {ty};", end="") | |
else: | |
print( | |
f"MOVE {grid_data[rx][ry]['units']} {rx} {ry} {tx} {ty};", end="") | |
def auto_move_robot(rx: int, ry: int, unit: int) -> None: | |
"""自動移動機器人 | |
輸入ㄉ參數: | |
rx -- 機器人ㄉ x 座標 | |
ry -- 機器人ㄉ y 座標 | |
unit -- 要移動ㄉ單位數量 | |
""" | |
available_tiles = get_can_capture_tiles() | |
target_tile = random.choice(available_tiles) | |
if grid_data[rx][ry]["units"] >= unit: | |
print( | |
f"MOVE {unit} {rx} {ry} {target_tile['x']} {target_tile['y']};", end="") | |
else: | |
print( | |
f"MOVE {grid_data[rx][ry]['units']} {rx} {ry} {target_tile['x']} {target_tile['y']};", end="") | |
def auto_spawn(unit: int) -> None: | |
"""自動生產機器人 | |
輸入ㄉ參數: | |
unit -- 要生產ㄉ單位數量 | |
""" | |
my_tiles = get_spwanable_tiles() | |
target_tile = random.choice(my_tiles) | |
if unit*10 <= my_matter: | |
print(f"SPAWN {unit} {target_tile['x']} {target_tile['y']};", end="") | |
else: | |
print( | |
f"SPAWN {my_matter//10} {target_tile['x']} {target_tile['y']};", end="") | |
def spawn(tx: int, ty: int, unit: int) -> None: | |
"""手動生產機器人 | |
輸入ㄉ參數: | |
tx -- 目標生產ㄉ地板 x 座標 | |
ty -- 目標生產ㄉ地板 y 座標 | |
unit -- 要生產ㄉ單位數量 | |
""" | |
if grid_data[tx][ty]["owner"] == 1 and grid_data[tx][ty]["can_spawn"] == 1: | |
if unit*10 <= my_matter: | |
print(f"SPAWN {unit} {tx} {ty};", end="") | |
else: | |
print(f"SPAWN {my_matter//10} {tx} {ty};", end="") | |
def auto_build() -> None: | |
"""自動建造回收機""" | |
can_build_tiles = get_can_build_tiles() | |
target_tile = random.choice(can_build_tiles) | |
print(f"BUILD {target_tile['x']} {target_tile['y']};", end="") | |
def build(tx: int, ty: int) -> None: | |
"""手動建造回收機 | |
輸入ㄉ參數: | |
tx -- 目標建造ㄉ地板 x 座標 | |
ty -- 目標建造ㄉ地板 y 座標 | |
""" | |
if grid_data[tx][ty]["owner"] == 1 and grid_data[tx][ty]["can_build"] == 1: | |
print(f"BUILD {tx} {ty};", end="") | |
# 遊戲ㄉ無窮迴圈 | |
while True: | |
input_data() | |
############## 修改下面ㄉ城市 ################## | |
print_message(grid_data) | |
my_robots = get_my_robots_infos() | |
print_message(get_my_recyclers_location) | |
for robot in my_robots: | |
auto_move_robot(robot["x"], robot["y"], robot["units"]) | |
if random.randint(0, 3) != 0: | |
auto_spawn(1) | |
auto_spawn(1) | |
else: | |
auto_build() | |
############## 修改上面ㄉ城市 ################## | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment