Commit 8749f8a6 authored by Adrian Kosmaczewski's avatar Adrian Kosmaczewski

Made the Python version a bit more readable, and removed unused code

parent a6a7823d
__pycache__
# Created by https://www.gitignore.io/api/pycharm
# Edit at https://www.gitignore.io/?templates=pycharm
......
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
<content url="file://$MODULE_DIR$/..">
<excludeFolder url="file://$MODULE_DIR$/../venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.7 (Conway)" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.8 (.)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
......@@ -3,5 +3,5 @@
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (Conway)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (.)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
= Python
This application is written in Python 3.7, and can be run using the `./main.py` command.
This application is written in Python 3.8, and can be run using the `./main.py` command.
== Tests
......
......@@ -11,11 +11,3 @@ class Coord:
def __ne__(self, other):
return not (self == other)
def __str__(self):
return "%d:%d" % (self.x, self.y)
@staticmethod
def parse(str):
values = list(map(int, str.split(":")))
return Coord(values[0], values[1])
from . import cell
from . import coord
from .cell import Cell
from .coord import Coord
class World:
def __init__(self, size, alive_cells):
......@@ -7,11 +7,11 @@ class World:
self.cells = {}
for a in range(0, self.size):
for b in range(0, self.size):
c = coord.Coord(a, b)
c = Coord(a, b)
if c in alive_cells:
self.cells[c] = cell.Cell.ALIVE
self.cells[c] = Cell.ALIVE
else:
self.cells[c] = cell.Cell.DEAD
self.cells[c] = Cell.DEAD
def evolve(self):
alive_cells = []
......@@ -19,10 +19,10 @@ class World:
count = 0
for a in range(-1, 2):
for b in range(-1, 2):
current_coord = coord.Coord(key.x + a, key.y + b)
if current_coord != key and current_coord in self.cells and self.cells[current_coord] == cell.Cell.ALIVE:
current_coord = Coord(key.x + a, key.y + b)
if current_coord != key and current_coord in self.cells and self.cells[current_coord] == Cell.ALIVE:
count += 1
if value == cell.Cell.ALIVE:
if value == Cell.ALIVE:
if count == 2 or count == 3:
alive_cells.append(key)
else:
......@@ -47,46 +47,46 @@ class World:
s += "\n"
s += ("%3s|" % a)
for b in range(0, self.size):
c = coord.Coord(b, a)
c = Coord(b, a)
s += ("%s" % self.cells[c])
s += "\n"
return s
@staticmethod
def blinker(c):
return [coord.Coord(c.x, c.y),
coord.Coord(c.x + 1, c.y),
coord.Coord(c.x + 2, c.y)]
return [Coord(c.x, c.y),
Coord(c.x + 1, c.y),
Coord(c.x + 2, c.y)]
@staticmethod
def beacon(c):
return [coord.Coord(c.x, c.y),
coord.Coord(c.x + 1, c.y),
coord.Coord(c.x, c.y + 1),
coord.Coord(c.x + 1, c.y + 1),
coord.Coord(c.x + 2, c.y + 2),
coord.Coord(c.x + 3, c.y + 2),
coord.Coord(c.x + 2, c.y + 3),
coord.Coord(c.x + 3, c.y + 3)]
return [Coord(c.x, c.y),
Coord(c.x + 1, c.y),
Coord(c.x, c.y + 1),
Coord(c.x + 1, c.y + 1),
Coord(c.x + 2, c.y + 2),
Coord(c.x + 3, c.y + 2),
Coord(c.x + 2, c.y + 3),
Coord(c.x + 3, c.y + 3)]
@staticmethod
def glider(c):
return [coord.Coord(c.x + 2, c.y + 2),
coord.Coord(c.x + 1, c.y + 2),
coord.Coord(c.x, c.y + 2),
coord.Coord(c.x + 2, c.y + 1),
coord.Coord(c.x + 1, c.y)]
return [Coord(c.x + 2, c.y + 2),
Coord(c.x + 1, c.y + 2),
Coord(c.x, c.y + 2),
Coord(c.x + 2, c.y + 1),
Coord(c.x + 1, c.y)]
@staticmethod
def block(c):
return [coord.Coord(c.x, c.y),
coord.Coord(c.x + 1, c.y),
coord.Coord(c.x, c.y + 1),
coord.Coord(c.x + 1, c.y + 1)]
return [Coord(c.x, c.y),
Coord(c.x + 1, c.y),
Coord(c.x, c.y + 1),
Coord(c.x + 1, c.y + 1)]
@staticmethod
def tub(c):
return [coord.Coord(c.x + 1, c.y),
coord.Coord(c.x, c.y + 1),
coord.Coord(c.x + 2, c.y + 1),
coord.Coord(c.x + 1, c.y + 2)]
return [Coord(c.x + 1, c.y),
Coord(c.x, c.y + 1),
Coord(c.x + 2, c.y + 1),
Coord(c.x + 1, c.y + 2)]
......@@ -3,30 +3,30 @@
import os
from signal import signal, SIGINT
from time import sleep
from conway import coord
from conway import world
from conway.world import World
from conway.coord import Coord
def handler(signal_received, frame):
os.system('clear')
os.system("clear")
exit(0)
blinker = world.World.blinker(coord.Coord(0, 1))
beacon = world.World.beacon(coord.Coord(10, 10))
glider = world.World.glider(coord.Coord(4, 5))
block1 = world.World.block(coord.Coord(1, 10))
block2 = world.World.block(coord.Coord(18, 3))
tub = world.World.tub(coord.Coord(6, 1))
blinker = World.blinker(Coord(0, 1))
beacon = World.beacon(Coord(10, 10))
glider = World.glider(Coord(4, 5))
block1 = World.block(Coord(1, 10))
block2 = World.block(Coord(18, 3))
tub = World.tub(Coord(6, 1))
alive_cells = blinker + beacon + glider + block1 + block2 + tub
w = world.World(30, alive_cells)
world = World(30, alive_cells)
generation = 0
if __name__ == '__main__':
if __name__ == "__main__":
signal(SIGINT, handler)
while True:
os.system('clear')
os.system("clear")
generation += 1
print(w)
print(world)
print(("Generation %d" % generation))
sleep(0.5)
w = w.evolve()
world = world.evolve()
#!/usr/bin/env python3
import unittest
from conway import world
from conway import coord
from unittest import TestCase, main
from conway.world import World
from conway.coord import Coord
class MyTestCase(unittest.TestCase):
class MyTestCase(TestCase):
def test_block(self):
alive = world.World.block(coord.Coord(0, 0))
original = world.World(5, alive)
alive = World.block(Coord(0, 0))
original = World(5, alive)
next_world = original.evolve()
self.assertEqual(next_world, original)
def test_tub(self):
alive = world.World.tub(coord.Coord(0, 0))
original = world.World(5, alive)
alive = World.tub(Coord(0, 0))
original = World(5, alive)
next_world = original.evolve()
self.assertEqual(next_world, original)
def test_blinker(self):
alive = world.World.blinker(coord.Coord(0, 1))
original = world.World(3, alive)
alive = World.blinker(Coord(0, 1))
original = World(3, alive)
gen1 = original.evolve()
expected_alive = [coord.Coord(1, 0),
coord.Coord(1, 1),
coord.Coord(1, 2)]
expected = world.World(3, expected_alive)
expected_alive = [Coord(1, 0),
Coord(1, 1),
Coord(1, 2)]
expected = World(3, expected_alive)
self.assertEqual(expected, gen1)
gen2 = gen1.evolve()
self.assertEqual(original, gen2)
if __name__ == '__main__':
unittest.main()
if __name__ == "__main__":
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment