- Tile switched from game to map
- state_from_player added in map
This commit is contained in:
parent
b2504f49f9
commit
358cb76b61
34
tron/game.py
34
tron/game.py
|
@ -5,44 +5,12 @@ This module contains everything related to the game.
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
from tron.map import Map
|
from tron.map import Map, Tile
|
||||||
|
|
||||||
class Winner(Enum):
|
class Winner(Enum):
|
||||||
PLAYER_ONE = 1
|
PLAYER_ONE = 1
|
||||||
PLAYER_TWO = 2
|
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:
|
class PositionPlayer:
|
||||||
"""
|
"""
|
||||||
A container to store a player with is id, position and boolean indicating
|
A container to store a player with is id, position and boolean indicating
|
||||||
|
|
56
tron/map.py
56
tron/map.py
|
@ -3,10 +3,41 @@ This module contains the Map class.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
def is_on_border(i, j, w ,h):
|
def is_on_border(i, j, w ,h):
|
||||||
return i == 0 or i == w - 1 or j == 0 or j == h - 1
|
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:
|
class Map:
|
||||||
"""
|
"""
|
||||||
The map of the game.
|
The map of the game.
|
||||||
|
@ -56,6 +87,31 @@ class Map:
|
||||||
clone_map = self.clone()
|
clone_map = self.clone()
|
||||||
return clone_map._data
|
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):
|
def __getitem__(self, index):
|
||||||
(i, j) = index
|
(i, j) = index
|
||||||
return self._data[i+1][j+1]
|
return self._data[i+1][j+1]
|
||||||
|
|
Loading…
Reference in New Issue