Exemplo n.º 1
0
 def spawn(
     self,
     brains: (Brain
              for _ in range(sample_size)) = (None
                                              for _ in range(sample_size))):
     if use_memorized:
         past_brains = import_brains()
         if past_brains:
             brains = (brain for brain in past_brains)
     self.germs = [
         Germ.from_random(self, brains.__next__())
         for _ in range(sample_size)
     ]
     self.planks = [
         Border(self, (5, 10), 0,
                self.canvas.winfo_width() - 10, "blue"),
         Border(self, (5, self.canvas.winfo_height() - 10), 0,
                self.canvas.winfo_width() - 10, "green"),
         Border(self, (5, 10), 90,
                self.canvas.winfo_height() - 10, "red"),
         Border(self, (self.canvas.winfo_width() - 10, 10), 90,
                self.canvas.winfo_width() - 10, "yellow")
     ]
     self.killers = [
     ]  #Killer.from_random(get_state, set_state, canvas) for killer_count in range(4)]
     self.death_log = []
Exemplo n.º 2
0
 def spawn(self, brains: (Brain for _ in range(sample_size))=(None for _ in range(sample_size))):
     if use_memorized:
         past_brains = import_brains()
         if past_brains:
             brains = (brain for brain in past_brains)
     self.germs = [Germ.from_random(self, brains.__next__()) for _ in range(sample_size)]
     self.planks = [Border(self, (5, 10), 0, self.canvas.winfo_width() - 10, "blue"),
                    Border(self, (5, self.canvas.winfo_height() - 10), 0, self.canvas.winfo_width() - 10, "green"),
                    Border(self, (5, 10), 90, self.canvas.winfo_height() - 10, "red"),
                    Border(self, (self.canvas.winfo_width() - 10, 10), 90, self.canvas.winfo_width() - 10, "yellow")]
     self.killers = []#Killer.from_random(get_state, set_state, canvas) for killer_count in range(4)]
     self.death_log = []
Exemplo n.º 3
0
sample_size = 50       # The number of initial germs in each generation.
use_memorized = False   # Whether to pull brains from file, if such a file exists.
save_last = (True, 5)   # Whether to save the brains, and how many generations to go through before a save.

from tkinter import *
import time, random
import pickle
from germ import Germ
from plank import Plank, Border
from brain import Brain

root = Tk()
root.title("The Hunger Planks")

canvas = Canvas(root, width=500, height=500)
canvas.bind("<Button-1>", lambda event: state.germs.append(Germ.from_random(state)))
canvas.bind("<Button-3>", lambda event: state.planks.append(Plank.from_random(state)))
canvas.pack(fill=BOTH, expand=YES)

root.update()

class State():
    def __init__(self, canvas):
        self.canvas = canvas
        self.spawn()
        self.current_gen = 0

    def spawn(self, brains: (Brain for _ in range(sample_size))=(None for _ in range(sample_size))):
        if use_memorized:
            past_brains = import_brains()
            if past_brains:
Exemplo n.º 4
0
def test_germ_class():
    """
       Test Germ class.

    """
    try:
        germ_1 = Germ(52, 10)
        assert germ_1.get_location() == 52
        assert germ_1.get_health() == 10

        germ_1.set_location(103)
        germ_1.set_health(2)
        assert germ_1.get_location() == 103
        assert germ_1.get_health() == 2

        germ_1.increment_health()
        assert germ_1.get_health() == 3

        germ_1.decrement_health()
        germ_1.decrement_health()
        germ_1.decrement_health()
        assert germ_1.get_health() == 0

        germ_1.decrement_health()
        assert germ_1.get_health() == 0

        print("All Germ class tests pass!")
    except:
        message_1 = sys.exc_info()[0]
        message_2 = sys.exc_info()[2]
        print("Caught an error: {}\n                 {}".format(
            message_1, message_2))
Exemplo n.º 5
0
    True, 5
)  # Whether to save the brains, and how many generations to go through before a save.

from tkinter import *
import time, random
import pickle
from germ import Germ
from plank import Plank, Border
from brain import Brain

root = Tk()
root.title("The Hunger Planks")

canvas = Canvas(root, width=500, height=500)
canvas.bind("<Button-1>",
            lambda event: state.germs.append(Germ.from_random(state)))
canvas.bind("<Button-3>",
            lambda event: state.planks.append(Plank.from_random(state)))
canvas.pack(fill=BOTH, expand=YES)

root.update()


class State():
    def __init__(self, canvas):
        self.canvas = canvas
        self.spawn()
        self.current_gen = 0

    def spawn(
        self,
Exemplo n.º 6
0
            for germ in local.grid.germs[id_]:
                try:
                    germ.update(local.data[id_][0])
                    germ.name = local.data[id_][2]

                    for event in local.data[id_][1]:
                        if event == 'space':
                            local.grid.cells.append(germ.eject())
                        elif event == 's':
                            splitter = germ.split()
                            if splitter:
                                local.grid.germs[id_].append(splitter)
                except:
                    pass

        local.grid.update_cells()
        local.grid.update_germ_pvp()

start_new_thread(update_grid, ())
while True:
    conn, addr = s.accept()
    print("Connection from:",addr)
    print("Added germ for", addr)

    germ = Germ(local.grid)
    germ.id = addr[1]
    local.grid.add_germ(germ, addr[1])
    local.once = addr[1]

    start_new_thread(threaded_client, (conn,))
Exemplo n.º 7
0
def test_germ_class():
    """
       Test Germ class.

    """
    try:
        germ_1 = Germ(52, 10)
        assert germ_1.get_location() == 52
        assert germ_1.get_health() == 10
        
        germ_1.set_location(103)
        germ_1.set_health(2)
        assert germ_1.get_location() == 103
        assert germ_1.get_health() == 2

        germ_1.increment_health()
        assert germ_1.get_health() == 3

        germ_1.decrement_health()
        germ_1.decrement_health()
        germ_1.decrement_health()
        assert germ_1.get_health() == 0

        germ_1.decrement_health()
        assert germ_1.get_health() == 0

        print("All Germ class tests pass!")
    except:
        message_1 = sys.exc_info()[0]
        message_2 = sys.exc_info()[2]
        print("Caught an error: {}\n                 {}".format(message_1, message_2))