From 358cb76b61960d8b8763c1889225c0f865082247 Mon Sep 17 00:00:00 2001 From: acarlier Date: Wed, 27 Mar 2019 14:56:33 +0100 Subject: [PATCH] - Tile switched from game to map - state_from_player added in map --- tron/game.py | 34 +------------------------------ tron/map.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 33 deletions(-) diff --git a/tron/game.py b/tron/game.py index 7f54fd7..93477a2 100644 --- a/tron/game.py +++ b/tron/game.py @@ -5,44 +5,12 @@ This module contains everything related to the game. from time import sleep from enum import Enum -from tron.map import Map +from tron.map import Map, Tile class Winner(Enum): PLAYER_ONE = 1 PLAYER_TWO = 2 - -class Tile(Enum): - """ - The different type of elements that can be in a map. - """ - EMPTY = 0 - WALL = -1 - PLAYER_ONE_BODY = 1 - PLAYER_ONE_HEAD = 2 - PLAYER_TWO_BODY = 3 - PLAYER_TWO_HEAD = 4 - - def color(self): - """ - Converts an element of a map to a nice color. - """ - if self == Tile.EMPTY: - return (255, 255, 255) - elif self == Tile.WALL: - return (0, 0, 0) - elif self == Tile.PLAYER_ONE_BODY: - return (128, 0, 0) - elif self == Tile.PLAYER_ONE_HEAD: - return (255, 0, 0) - elif self == Tile.PLAYER_TWO_BODY: - return (0, 128, 0) - elif self == Tile.PLAYER_TWO_HEAD: - return (0, 255, 0) - else: - return None - - class PositionPlayer: """ A container to store a player with is id, position and boolean indicating diff --git a/tron/map.py b/tron/map.py index 66544c9..0b0a828 100644 --- a/tron/map.py +++ b/tron/map.py @@ -3,10 +3,41 @@ This module contains the Map class. """ import numpy as np +from enum import Enum def is_on_border(i, j, w ,h): return i == 0 or i == w - 1 or j == 0 or j == h - 1 +class Tile(Enum): + """ + The different type of elements that can be in a map. + """ + EMPTY = 0 + WALL = -1 + PLAYER_ONE_BODY = 1 + PLAYER_ONE_HEAD = 2 + PLAYER_TWO_BODY = 3 + PLAYER_TWO_HEAD = 4 + + def color(self): + """ + Converts an element of a map to a nice color. + """ + if self == Tile.EMPTY: + return (255, 255, 255) + elif self == Tile.WALL: + return (0, 0, 0) + elif self == Tile.PLAYER_ONE_BODY: + return (128, 0, 0) + elif self == Tile.PLAYER_ONE_HEAD: + return (255, 0, 0) + elif self == Tile.PLAYER_TWO_BODY: + return (0, 128, 0) + elif self == Tile.PLAYER_TWO_HEAD: + return (0, 255, 0) + else: + return None + class Map: """ The map of the game. @@ -56,6 +87,31 @@ class Map: clone_map = self.clone() return clone_map._data + def color(self, t, p): + """ + Converts an element of a map into an element to be perceived by player p. + """ + if t == Tile.EMPTY: + return 1 + elif t == Tile.WALL: + return -1 + elif t == Tile.PLAYER_ONE_BODY: + return -1 + elif t == Tile.PLAYER_ONE_HEAD: + return 10 if p == 1 else -10 + elif t == Tile.PLAYER_TWO_BODY: + return -1 + elif t == Tile.PLAYER_TWO_HEAD: + return 10 if p == 2 else -10 + else: + return None + + def state_for_player(self, p): + """ + Returns an image representing the current perception of the environment from player p. + """ + return self.apply(lambda tile: self.color(tile, p)).array() + def __getitem__(self, index): (i, j) = index return self._data[i+1][j+1]