SI_InteligentnyWozekWidlowy/test/nastepnik.py

110 lines
3.3 KiB
Python
Raw Normal View History

2022-04-14 23:25:07 +02:00
from enum import Enum
from typing import Tuple, Dict
2022-04-16 15:55:43 +02:00
from data import GameConstants, Direction
2022-04-14 23:25:07 +02:00
2022-04-16 15:55:43 +02:00
def getHotSpot(game:GameConstants) -> (int, int):
2022-04-14 23:25:07 +02:00
pass
2022-04-16 15:55:43 +02:00
def isOrderReady(game:GameConstants) -> bool:
2022-04-14 23:25:07 +02:00
pass
2022-04-16 15:55:43 +02:00
def getReadyOrderId(game:GameConstants) -> int:
2022-04-14 23:25:07 +02:00
pass
class Action():
2022-04-16 15:55:43 +02:00
def __init__(self, game: GameConstants):
self.game = GameConstants
2022-04-14 23:25:07 +02:00
2022-04-16 15:55:43 +02:00
def rotate(self, direction: Direction) -> GameConstants:
2022-04-14 23:25:07 +02:00
self.game.agentDirection = direction
pass
2022-04-16 15:55:43 +02:00
def move(self) -> GameConstants:
2022-04-14 23:25:07 +02:00
# w zaleznosci od kierunku napierdala do przodu
pass
2022-04-16 15:55:43 +02:00
def special(self) -> GameConstants:
2022-04-14 23:25:07 +02:00
# w zaleznosci od miejsca gdzie jest i czy ma cos na lapie odklada albo bierze przedmiot
pass
2022-04-16 15:55:43 +02:00
def orderOut(self, orderId: int) -> GameConstants:
2022-04-14 23:25:07 +02:00
# nalicza punkty wypierdalaorder i czysci orderStock
pass
def heuristic(a: Tuple[int, int], b: Tuple[int, int]) -> float:
(x1, y1) = a
(x2, y2) = b
return abs(x1 - x2) + abs(y1 - y2)
class PossibleMoves(Enum):
2022-04-16 14:55:25 +02:00
move = 1
2022-04-14 23:25:07 +02:00
rotateLeft = 2
rotateDown = 3
rotateRight = 4
2022-04-16 14:55:25 +02:00
rotateTop = 5
2022-04-14 23:25:07 +02:00
def getRotationEvaluation(direction: Direction):
# get evaluationForMoveAfterRotation
return 1.0
2022-04-16 15:55:43 +02:00
def evaluateMoves(game: GameConstants) -> Dict:
2022-04-14 23:25:07 +02:00
posibleMoves = Dict
currPos = game.agentPos
gameCopy = game.getCopy()
getRotationEvaluation()
posibleMoves[PossibleMoves.move] = heuristic(currPos, Action(gameCopy).move().agentPos)
posibleMoves[PossibleMoves.rotateTop] = getRotationEvaluation(Direction.Direction.top)
posibleMoves[PossibleMoves.rotateLeft] = getRotationEvaluation(Direction.Direction.left)
posibleMoves[PossibleMoves.rotateDown] = getRotationEvaluation(Direction.Direction.down)
posibleMoves[PossibleMoves.rotateRight] = getRotationEvaluation(Direction.Direction.right)
return posibleMoves
def getIndex(value: float, dict: Dict) -> PossibleMoves:
for key, val in dict:
if val == value:
return key
def getMaxFromList(list: [float]) -> float:
maxi = -10000000
for i in range(len(list)):
if list[i] > maxi:
maxi = list[i]
return maxi
2022-04-16 15:55:43 +02:00
def getBestPossibleMove(game: GameConstants) -> PossibleMoves:
2022-04-14 23:25:07 +02:00
movesDict = evaluateMoves()
bestChoice = getMaxFromList(movesDict.values())
return getIndex(bestChoice, movesDict)
2022-04-16 15:55:43 +02:00
def getBestMove(game: GameConstants) -> GameConstants:
2022-04-14 23:25:07 +02:00
gameCopy = game.getCopy()
bestPossibleMove = getBestPossibleMove(gameCopy)
if bestPossibleMove == PossibleMoves.move:
return Action(gameCopy).move()
elif bestPossibleMove == PossibleMoves.rotateTop:
return Action(gameCopy).rotate(Direction.Direction.top)
elif bestPossibleMove == PossibleMoves.rotateLeft:
return Action(gameCopy).rotate(Direction.Direction.left)
elif bestPossibleMove == PossibleMoves.rotateDown:
return Action(gameCopy).rotate(Direction.Direction.down)
else:
return Action(gameCopy).rotate(Direction.Direction.right)
2022-04-16 15:55:43 +02:00
def nastepnik(game: GameConstants) -> GameConstants:
2022-04-14 23:25:07 +02:00
if isOrderReady(game) > -1:
return Action(game.getCopy()).orderOut(getReadyOrderId(game))
elif game.agentPos == getHotSpot(game):
return Action(game.getCopy()).special()
else:
return getBestMove(game)