Created
April 11, 2013 02:56
-
-
Save jacobgardner/5360292 to your computer and use it in GitHub Desktop.
Simple AI
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 snape | |
import argparse | |
import os | |
DIRECTION = snape.RIGHT | |
def move(b): | |
global DIRECTION | |
if b.head.x == 0 and b.head.y == b.height - 1: | |
DIRECTION = snape.DOWN | |
return b.move(snape.DOWN) | |
elif DIRECTION == snape.RIGHT: | |
if b.head.x == b.width - 1: | |
DIRECTION = snape.LEFT | |
return b.move(snape.UP) | |
else: | |
return b.move(snape.RIGHT) | |
elif DIRECTION == snape.LEFT: | |
if b.head.x == 1 and b.head.y != b.height - 1: | |
DIRECTION = snape.RIGHT | |
return b.move(snape.UP) | |
else: | |
return b.move(snape.LEFT) | |
elif DIRECTION == snape.DOWN: | |
if b.head.x == 0 and b.head.y == 0: | |
DIRECTION = snape.RIGHT | |
return b.move(snape.RIGHT) | |
else: | |
return b.move(snape.DOWN) | |
def solve(b): | |
while b.max_tail < b.width * b.height: | |
print(b) | |
move(b) | |
def main(): | |
parser = argparse.ArgumentParser(description='AI for snape.py') | |
parser.add_argument('-w', '--width', default=10, help='Width of the board. (Default 10)') | |
parser.add_argument('--height', default=10, help='Height of the board. (Default 10)') | |
args = parser.parse_args() | |
solve(snape.Board(int(args.width), int(args.height), (0, 0))) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment