/
moskito_game.py
60 lines (49 loc) · 1.66 KB
/
moskito_game.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
#! /usr/bin/env python
from random import random, sample, randint
from game import BoardGame
from board import Board
from figure import Figure, FigureStrategy
from logger import log
class MoskitoGame(BoardGame):
def __init__(self, height, width, amount):
self.board = Board(height, width)
self.amount = amount
def setup(self):
for n in range(self.amount):
figure = Figure(self.board)
figure.bindStrategy(self.figureStrategyFactory())
figure.strategy.placeIt()
self.board.figures[0].color = 1
def figureStrategyFactory(self):
return MoskitoStrategy()
class MoskitoStrategy(FigureStrategy):
symbol = "."
deltaX, deltaY = (0, 0)
def modify(self):
self.deltaX = randint(-2, 2)
self.deltaY = randint(-2, 2)
def placeIt(self):
x = sample(range(0, self.board.width), 1)[0]
y = sample(range(0, self.board.height), 1)[0]
self.modify()
try:
self.figure.add(y, x)
except:
self.placeIt()
def planMovement(self):
if random() < 0.01: self.modify()
def step(self):
self.planMovement()
try:
self.figure.move(self.deltaY, self.deltaX, relative = True)
except self.board.AboveWidthException:
self.deltaX = -1
except self.board.AboveHeightException:
self.deltaY = -1
except self.board.BelowWidthException:
self.deltaX = 1
except self.board.BelowHeightException:
self.deltaY = 1
except self.board.TakenException:
self.deltaX = -self.deltaX
self.deltaY = -self.deltaY