def __init__(self, model, memory_size, iterations_num, batch_size, game_board_size=20, random_start=True): self.model = model self.game_engine = Engine(game_board_size, random_start) self.replay_memory = Memory(memory_size) self.dataset = RLDataset(self.replay_memory, iterations_num, batch_size) self.game_state = self.get_game_state()
def __init__(self, **kwargs): self.score_label = kwargs.pop('score_label', None) super(SnakeGame, self).__init__(**kwargs) self._keyboard = Window.request_keyboard(self._keyboard_closed, self) self._keyboard.bind(on_key_down=self._on_keyboard_down) self.engine = Engine(board_size=20) self.round_time = .05 self.model_path = './models/model.ptl' self.block_size = 10 self.board_length = (self.block_size + 1) * self.engine.board_size self.game_direction = self.engine.direction self.game_next_direction = self.game_direction
import shutil import numpy as np from collections import namedtuple from itertools import count from copy import deepcopy import torch import torch.nn as nn import torch.optim as optim import torch.nn.functional as F from torch.autograd import Variable from game import Engine width, height = 10, 20 engine = Engine(width, height) FloatTensor = torch.FloatTensor LongTensor = torch.LongTensor ByteTensor = torch.ByteTensor Transition = namedtuple('Transition', ('state', 'action', 'next_state', 'reward')) """ Replay Memory Transition - a named tuple representing a single transition in our environment ReplayMemory - a cyclic buffer of bounded size that holds the transitions observed recently. It also implements a ``.sample()`` method for selecting a random batch of transitions for training. """
f = tkinter.LabelFrame(top, height=200, width=200) f.grid(row=r, column=c) l = tkinter.Label(f, textvariable=labels[r][c], width=10, height=5, bg='snow3') l.pack() frames[r][c] = l start = time.time() printed = False while True: new_state = engine.get_next() if new_state != None: state = new_state else: if not printed: print(time.time() - start) printed = True for r in range(4): for c in range(4): labels[r][c].set(str(state[r][c]) if state[r][c] != 0 else "") frames[r][c].configure(bg=color_from_num(state[r][c])) top.update_idletasks() top.update() run(Engine())
import pygame pygame.init() size = (900, 600) from game import Engine Engine(pygame.display.set_mode(size)).run() pygame.quit()
def say(self): if not self.reusable: self.active = False return self.script() def script(self): pass class ConvoEngine: """ interprets conversation graph, ConvoNode attributes, conditions and scripts """ engine = Engine() d = defaultdict(set) d['0'].add('A') d['A'].add('B') d['A'].add('C') d['B'].add('A') d['C'] convos = dict(A=ConvoNode("Hello", True, True, True), B=ConvoNode("Hi", True, False, False), C=ConvoNode("Bye", True, True, True)) # TODO: show conversation option based on value of variable # TODO: after conversation tree of side conversation is finished, go back to main branch def convo():