created Field class and fieldsFactory

This commit is contained in:
piowyg 2021-03-27 18:27:45 +01:00
parent b8d1370438
commit f860b1ce45
5 changed files with 34 additions and 6 deletions

View File

@ -1,11 +1,15 @@
from typing import List
from random import choices
from fieldsFactory import FieldsFactory
from field import Field
def generate_field() -> List[List[int]]:
return [choices(range(11), weights=[10, 10, 4, 4, 4, 4, 4, 4, 4, 4, 4], k=10) for _ in range(10)]
def generate_field () -> List[List[Field]]:
fieldsFactory = FieldsFactory()
fieldsList = fieldsFactory.getFieldsList()
return [choices (fieldsList, weights=[10, 10, 4, 4, 4, 4, 4, 4, 4, 4, 4], k=10) for _ in range (10)]
class Environment:
def __init__(self, field: List[List[int]] = None):
def __init__(self, field: List[List[Field]] = None):
self.field = field if field else generate_field()

4
src/field.py Normal file
View File

@ -0,0 +1,4 @@
class Field:
def __init__(self, number, isMined):
self.number = number
self.isMined = isMined

16
src/fieldsFactory.py Normal file
View File

@ -0,0 +1,16 @@
from typing import List
from field import Field
class FieldsFactory:
def __init__(self):
pass
def createField(self, number) -> Field:
if number > 3:
return Field(number, True)
else:
return Field(number, False)
def getFieldsList(self) -> List[Field]:
return [self.createField(i) for i in range(11)]

View File

@ -29,6 +29,7 @@ class GameUi:
def update(self):
for x in range(10):
for y in range(10):
self.screen.blit(IMAGES[self.env.field[y][x]], (x * 80, y * 80))
print(IMAGES[self.env.field[y][x].number])
self.screen.blit(IMAGES[self.env.field[y][x].number], (x * 80, y * 80))
self.screen.blit(self.agent.img, (self.agent.x * 80, self.agent.y * 80))
pg.display.update()

View File

@ -4,6 +4,7 @@ from agent import Agent
from game_ui import GameUi
from environment import Environment
from const import ICON, IMAGES_MAPPING
from fieldsFactory import FieldsFactory
def main():
@ -15,6 +16,8 @@ def main():
agent = Agent()
game_ui = GameUi(agent, env)
game_ui.update()
factory = FieldsFactory()
running = True
@ -32,8 +35,8 @@ def main():
elif event.key == pg.K_w and agent.y*80 > 0:
game_ui.move('y', -1)
elif event.key == pg.K_SPACE:
if env.field[agent.y][agent.x] > 3:
env.field[agent.y][agent.x] = IMAGES_MAPPING[env.field[agent.y][agent.x]]
if env.field[agent.y][agent.x].number > 3:
env.field[agent.y][agent.x] = factory.createField(IMAGES_MAPPING[env.field[agent.y][agent.x].number])
game_ui.update()