• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Sep 22nd, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from AlbotOnline.Snake.SnakeGame import SnakeGame
2. import random
3. #Remove non possible hands
4.
5. game = SnakeGame()
6.
7. # check if the position in the grid is empty
8. def empty(grid, yPos, xPos):
9.   if yPos < 0 or yPos >= 10 or xPos < 0 or xPos >= 10:
10.     return False
11.   return grid[yPos][xPos] == "0"
12.
13. def nonCollidingMoves(possibleMoves, grid, pos):
14.   possibleNonCollidingMoves = []
15.   if "right" in possibleMoves:
16.     nextX = pos[0] + 1
17.     nextY = pos[1]
18.     if empty(grid, nextY, nextX):
19.       possibleNonCollidingMoves.append("right")
20.
21.   if "down" in possibleMoves:
22.     nextX = pos[0]
23.     nextY = pos[1] + 1
24.     if empty(grid, nextX, nextY):
25.       possibleNonCollidingMoves.append("down")
26.
27.   if "left" in possibleMoves:
28.     nextX = pos[0] - 1
29.     nextY = pos[1]
30.     if empty(grid, nextX, nextY):
31.       possibleNonCollidingMoves.append("left")
32.
33.   if "up" in possibleMoves:
34.     nextX = pos[0]
35.     nextY = pos[1] - 1
36.     if empty(grid, nextX, nextY):
37.       possibleNonCollidingMoves.append("up")
38.
39.   return possibleNonCollidingMoves
40.
41. def countStepsInDirection(grid, pos):
42.   freeStepsRight = 0
43.   freeStepsLeft = 0
44.   freeStepsDown = 0
45.   freeStepsUp = 0
46.
47.   iterator = 1
48.   while(empty(grid,pos[0] + iterator, pos[1])):
49.     freeStepsRight += 1
50.     iterator += 1
51.   print("right: " + str(freeStepsRight))
52.
53.   iterator = 1
54.   while(empty(grid,pos[0] - iterator, pos[1])):
55.     freeStepsLeft += 1
56.     iterator += 1
57.   print("left: " + str(freeStepsLeft))
58.
59.   iterator = 1
60.   while(empty(grid,pos[0], pos[1] - iterator)):
61.     freeStepsUp += 1
62.     iterator += 1
63.   print("up: " + str(freeStepsDown))
64.
65.   iterator = 1
66.   while(empty(grid,pos[0], pos[1] + iterator)):
67.     freeStepsDown += 1
68.     iterator += 1
69.   print("Down: " + str(freeStepsUp))
70.
71.   dictionary = {
72.     "left" : freeStepsLeft,
73.     "right" : freeStepsRight,
74.     "down" : freeStepsDown,
75.     "up" : freeStepsUp
76.   }
77.   return dictionary
78.
79. while(game.awaitNextGameState() == "ongoing"):
80.   print("\n-----------------------------------------")
81.   board = game.currentBoard
82.   playerPosition = (board.player.x, board.player.y)
83.   playerMoves, enemyMoves = game.getPossibleMoves(board)
84.   board.printBoard("current board")
85.
86.   #Remove all moves that will make you collide on the next turn
87.   possibleNonCollidingMoves = nonCollidingMoves(playerMoves, board.grid, playerPosition)
88.
89.   #Count how many free tiles you have in each direction
90.   blockedDistances = countStepsInDirection(board.grid, playerPosition)
91.   maxDistance = max(blockedDistances, key=blockedDistances.get)
92.   print("The max distance is in direction: " + str(maxDistance))
93.
94.   if maxDistance in possibleNonCollidingMoves:
95.     game.makeMove(maxDistance)
96.   elif True:
97.     game.makeMove(random.choice(possibleNonCollidingMoves))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top