Skip to content

Instantly share code, notes, and snippets.

@seantibor
Last active September 28, 2023 19:52
Show Gist options
  • Save seantibor/ffe4b31a0fbd3f251106d9874644980f to your computer and use it in GitHub Desktop.
Save seantibor/ffe4b31a0fbd3f251106d9874644980f to your computer and use it in GitHub Desktop.
Code for Minecraft EDU Edition to dig a mineshaft with the agent. Works with Python notebooks and the code builder
directions = ("left","right","up", "down")
ignore_bricks = ['dirt','stone', "gravel", "air"]
def dig_inspect(direction):
"""Inspects for uncommon brick types and then digs them anyway"""
block = agent.inspect(direction)
while block not in ("air", "torch"):
if block not in ignore_bricks:
say(f"Found {block} to the {direction}. Destroying anyway")
agent.destroy(direction)
block = agent.inspect(direction)
def dig_circle():
"""Dig a 3x3 area around the agent"""
for direction in directions:
dig_inspect(direction)
agent.move("up")
dig_inspect("left")
dig_inspect("right")
agent.move("down")
agent.move("down")
dig_inspect("left")
dig_inspect("right")
agent.move("up")
def mineshaft(depth=16):
"""Dig a stairstep shaft down to the specified depth.
Place torches along the way. """
while agent.position.y > depth:
dig_circle()
say(agent.position.y % 4)
if int(agent.position.y) % 4 == 0:
place_torch("right")
elif int(agent.position.y) % 4 == 2:
place_torch("left")
dig_inspect("forward")
agent.move("forward")
dig_inspect("down")
agent.move("down")
def place_torch(wall_direction="right", move_count=0):
"""Place a torch to either side up to 3 spaces away.
Uses recursion with a max depth of 3"""
max_depth = 3
move_back = "left" if wall_direction == "right" else "right"
if agent.get_item_count(1) == 0:
agent.give("torch", 64, 1)
# if we've moved over three spaces already, abort the torch placement!
if move_count > max_depth:
say("Couldn't place torch")
return
if agent.inspect(wall_direction) == "air":
agent.move(wall_direction)
#carefully use recursion to move over and place a torch if there's a solid block there
place_torch(wall_direction, move_count+1)
agent.move(move_back)
else:
agent.place(1, wall_direction)
return
def tunnel(length: int):
""" Carve a 3x3 tunnel forward for the specified length.
Place torches along the way. """
for i in range(length):
dig_circle()
if i % 4 == 0:
place_torch()
elif i % 4 == 2:
place_torch("left")
dig_inspect("forward")
agent.move("forward")
def cavern(length, width):
turn_direction = "right"
torch_direction = "left"
for j in range(int(width / 3)):
for i in range(length):
dig_circle()
if j == 0 and i % 4 == 0:
place_torch(torch_direction)
dig_inspect("forward")
agent.move("forward")
turn_direction = "left" if turn_direction == "right" else "right"
torch_direction = "left" if torch_direction == "right" else "right"
dig_circle()
agent.turn(turn_direction)
for _ in range(3):
dig_circle()
dig_inspect("forward")
agent.move("forward")
dig_circle()
dig_inspect("forward")
agent.move("forward")
dig_circle()
agent.move("back")
place_torch(torch_direction)
agent.turn(turn_direction)
""" Use these examples to cut a mineshaft down or a horizontal tunnel"""
#agent.teleport("~2 ~1 ~")
#mineshaft()
#agent.turn("right")
cavern(10, 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment