Ejemplo n.º 1
0
    def draw(self, world):
        assert isinstance(world, World)

        alived = world.get_cells()
        alived = set(alived)
        user_grid_size = 10
        shift_x = self.shift_x
        shift_y = self.shift_y
        max_x = self.max_x
        max_y = self.max_y

        assert(max_x > 0)
        assert(max_y > 0)
        assert(isinstance(shift_x, int))
        assert(isinstance(shift_y, int))
        assert(isinstance(max_x, int))
        assert(isinstance(max_y, int))

        self.__grid_size = user_grid_size
        self.__height = self.__grid_size * max_y
        self.__width = self.__grid_size * max_x
        field = np.ones(
            (self.__height, self.__width, 3), dtype=np.uint8) * 255

        print(self.__height)
        for y in range(0, self.__height, self.__grid_size):
            for x in range(0, self.__width, self.__grid_size):
                x += shift_x
                y += shift_y

                field = cv.line(field, (x, 0), (x, self.__height), 0)
                color = (0, 0, 0)
                if Cell(x/self.__grid_size+self.root_x, y/self.__grid_size+self.root_y) in alived:
                    for tmp_cell in alived:
                        if tmp_cell == Cell(x/self.__grid_size+self.root_x, y/self.__grid_size+self.root_y):
                            print(tmp_cell.sort)
                            if tmp_cell.sort == 'A':
                                color = (255, 0, 0)
                                break
                            if tmp_cell.sort == 'B':
                                color = (0, 0, 255)
                                break
                            if tmp_cell.sort == 'C':
                                color = (0, 255, 0)
                                break
                            if tmp_cell.sort == 'D':
                                color = (0, 0, 0)
                                break
                    field = cv.rectangle(
                        field,
                        (x-self.root_x*self.__grid_size, y-self.root_y*self.__grid_size),
                        (x+self.__grid_size-self.root_x*self.__grid_size, y+self.__grid_size - self.__grid_size*self.root_y),
                        color, cv.FILLED)

                field = cv.line(field, (0, y), (self.__width, y), 0)

        cv.imshow('field', field)
        key = cv.waitKey(100)
        return key != ord('q')
Ejemplo n.º 2
0
 def test_cell_get_neighbours(self):
     'Test if cell could get correct position of its neighbours'
     zero_cell = Cell(0, 0)
     self.assertEqual(len(zero_cell.get_neighbours()), 8)
     self.assertSequenceEqual(zero_cell.get_neighbours(), [
         Cell(i[0], i[1]) for i in ((-1, -1), (0, -1), (1, -1), (-1, 0),
                                    (1, 0), (-1, 1), (0, 1), (1, 1))
     ])
Ejemplo n.º 3
0
 def test_draw_world_in_gui(self):
     world = World({
         Cell(0, 0, 'A'),
         Cell(0, 0, 'B'),
         Cell(0, 0, 'C'),
         Cell(0, 0, 'D'),
     })
     gui = Gui()
     gui.set_args(0, 0, 10, 10)
     gui.draw(world)
Ejemplo n.º 4
0
    def test_cell_magic(self):
        'Test if magic methods works correctly'
        zero_cell = Cell(0, 0)
        another_zero_cell = Cell(0, 0)
        non_zero_cell = Cell(0, 10)
        another_non_zero_cell = Cell(10, 0)

        self.assertEqual(zero_cell, another_zero_cell)
        self.assertNotEqual(zero_cell, non_zero_cell)
        self.assertNotEqual(zero_cell, 0)
        self.assertNotEqual(zero_cell, another_non_zero_cell)
        self.assertNotEqual(another_zero_cell, non_zero_cell)
        self.assertNotEqual(another_zero_cell, another_non_zero_cell)

        self.assertEqual(set([zero_cell]), set([another_zero_cell]))
        self.assertNotEqual(set([zero_cell]), set([non_zero_cell]))
Ejemplo n.º 5
0
 def get_neighbours(self):
     'List of neighbours getter'
     x, y = self.__x, self.__y
     return [
         Cell(i[0], i[1])
         for i in ((x - 1, y - 1), (x, y - 1), (x + 1, y - 1), (x - 1, y),
                   (x + 1, y), (x - 1, y + 1), (x, y + 1), (x + 1, y + 1))
     ]
Ejemplo n.º 6
0
        print('Current state: {}\n'.format(self.__cells))


#!/usr/bin/python3

from modules.cell import Cell
from modules.world import World
from modules.gui import Gui
import numpy as np
import tkinter as tk
import PIL
import PIL.Image, PIL.ImageTk
import cv2 as cv

world = World({
    Cell(0, 0, 'B'),
    Cell(1, 1, 'B'),
    Cell(1, 2, 'B'),
    Cell(0, 2, 'B'),
    Cell(-1, 2, 'B'),
    Cell(4, 5, 'C'),
    Cell(4, 4, 'C'),
    Cell(2, 4, 'C'),
    Cell(3, 4, 'C'),
    Cell(-1, -1, 'A'),
    Cell(-2, -1, 'A'),
    Cell(0, -1, 'A'),
    Cell(-3, -1, 'D'),
    Cell(-4, -1, 'D'),
    Cell(-5, -1, 'D'),
    Cell(-6, -1, 'D'),
Ejemplo n.º 7
0
    def test_cell_get_coords(self):
        'Test if cell could return correct coordinates'
        zero_cell = Cell(0, 0)

        self.assertEqual(zero_cell.get_coords(), (0, 0))