/
greedy.py
executable file
·95 lines (61 loc) · 2.56 KB
/
greedy.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import lib.PyratApi as api
import lib.travelHeuristics as th
import lib.utils as ut
import time
BOT_NAME = "greedy"
PATH = []
METAGRAPH = {}
BESTPATH = {}
MOVING = False
EATENCOINS = []
NB_COINS_TO_COMPUTE = 5
CURRENTCOIN = []
# This function should not return anything, but should be used for a short preprocessing
def initializationCode (mazeWidth, mazeHeight, mazeMap, timeAllowed, playerLocation, opponentLocation, coins) :
global METAGRAPH
global BESTPATHS
iniTime = time.time()
# Compute metaGraph
METAGRAPH, BESTPATHS = th.generateMetaGraph(mazeMap, playerLocation, coins)
api.debug(time.time() - iniTime)
return "Everything seems fine, let's start !"
def chooseCoin (metaGraph, playerLocation, eatenCoins):
# Determination des sommets à calculer avec l'algo naif
nodesToCompute = ut.orderNodesByDistance(metaGraph, playerLocation, eatenCoins)
return nodesToCompute[0][0]
# This is where you should write your code to determine the next direction
def determineNextMove (mazeWidth, mazeHeight, mazeMap, timeAllowed, playerLocation, opponentLocation, coins) :
global MOVING
global METAGRAPH
global BESTPATHS
global EATENCOINS
global PATH
global CURRENTCOIN
EATENCOINS = ut.updateCoins(METAGRAPH, EATENCOINS, coins)
if MOVING :
if not PATH :
MOVING = False
if opponentLocation == CURRENTCOIN and playerLocation != CURRENTCOIN:
PATH = []
PATH = th.findNearestCoin(mazeMap, playerLocation, coins)
if not MOVING :
CURRENTCOIN = chooseCoin(METAGRAPH, playerLocation, EATENCOINS)
PATH = list(BESTPATHS[playerLocation][CURRENTCOIN])
PATH.pop()
MOVING = True
nextPos = PATH.pop()
return ut.convertPosesToDir(nextPos, playerLocation, mazeMap)
####
if __name__ == "__main__" :
# We let technical stuff happen
(mazeWidth, mazeHeight, mazeMap, preparationTime, turnTime, playerLocation, opponentLocation, coins, gameIsOver) = api.initGame(BOT_NAME)
initializationCode(mazeWidth, mazeHeight, mazeMap, preparationTime, playerLocation, opponentLocation, coins)
# We decide how to move and wait for the next step
while not gameIsOver :
(playerLocation, opponentLocation, coins, gameIsOver) = api.processNextInformation()
if gameIsOver :
break
nextMove = determineNextMove(mazeWidth, mazeHeight, mazeMap, turnTime, playerLocation, opponentLocation, coins)
api.writeToPipe(nextMove)