Ejemplo n.º 1
0
 def render(self,
            outfile=sys.stdout,
            width: Optional[int] = None,
            height: Optional[int] = None,
            in_place: bool = False) -> None:
     height: int = height or self.HEIGHT
     width: int = width or self.WIDTH
     if not in_place:
         for _ in range(height):
             outfile.write("\n")
         util.print_up(height, outfile=outfile)
     for i in range(self.HEIGHT - height, self.HEIGHT):
         for j in range(width):
             empty = True
             for k in range(self.num_colors):
                 puyo = self.data[i + self.HEIGHT * k] & (1 << j)
                 if puyo:
                     util.print_puyo(k, outfile=outfile)
                     empty = False
             if self.has_garbage:
                 garbage_puyo = self.data[i + self.HEIGHT *
                                          self.num_colors] & (1 << j)
                 if garbage_puyo:
                     util.print_color(6, outfile=outfile)
                     outfile.write("\u25ce ")
                     empty = False
             if empty:
                 outfile.write("\u00b7 ")
             util.print_reset(outfile=outfile)
         util.print_down(1, outfile=outfile)
         util.print_back(2 * width, outfile=outfile)
Ejemplo n.º 2
0
Archivo: versus.py Proyecto: brnor/dipl
 def render(self, outfile=sys.stdout, in_place=False):
     if not in_place:
         for _ in range(self.height + 1):
             outfile.write("\n")
         util.print_up(self.height + 1, outfile=outfile)
     super(VersusState, self).render(outfile=outfile, in_place=True)
     status_text = "x{} c{} s{} p{} {}".format(
         self.chain_number, self.chain_score, self.step_score,
         self.pending_garbage, "!" if self.all_clear_pending else "")
     outfile.write(status_text)
     util.print_down(1)
     util.print_back(len(status_text))
Ejemplo n.º 3
0
def test_render_in_place():
    field = TallField(1)
    for i in range(16):
        field.data[i] = ((i + 4234)**3) % 256

    for i in range(20):
        print(i)
    util.print_up(16)
    print("Let's shift this a bit!", end="")
    field.render(in_place=True)
    print("hello")
    field.render(width=6, height=13)
Ejemplo n.º 4
0
def test_render_in_place(height):
    for i in range(20):
        print(i)
    util.print_up(height)
    print("Move it on up!", end="")
    state = State(height, 7, 6, 4)
    state.step(2, 3)
    state.step(4, 2)
    state.render(in_place=True)
    print("hey!")
    state.render()
    print("wohou!")
Ejemplo n.º 5
0
 def render(self, outfile=sys.stdout, in_place=False) -> bool:
     height: int = self.players[0].height
     width: int = self.players[0].width
     if not in_place:
         for _ in range(height + 1):
             outfile.write("\n")
         util.print_up(height + 1, outfile=outfile)
     for player in self.players:
         player.render(outfile=outfile, in_place=True)
         util.print_up(height + 1)
         util.print_forward(2 * width + 9)
     util.print_down(height + 1)
     util.print_back(len(self.players) * (2 * width + 9))
     outfile.flush()
Ejemplo n.º 6
0
def test_render_in_place():
    field = BottomField(2)
    for i in range(16):
        field.data[i] = ((i + 4234)**5) % 256

    for i in range(8):
        field.data[i] &= ~field.data[i + 8]

    for i in range(10):
        print(i)
    util.print_up(8)
    print("Let's shift this a bit!", end="")
    field.render(in_place=True)
    print("hello")
    field.render(width=6, height=7)
Ejemplo n.º 7
0
 def render(self, outfile=sys.stdout, in_place=False):
     if not in_place:
         for _ in range(self.height):
             outfile.write("\n")
         util.print_up(self.height, outfile=outfile)
     self.field.render(outfile, width=self.width, height=self.height, in_place=True)
     util.print_up(self.height, outfile=outfile)
     util.print_forward(2 * self.width + 2, outfile=outfile)
     remaining = self.height
     for deal in self.deals[:self.height // 2]:
         for puyo in deal:
             util.print_puyo(puyo, outfile=outfile)
         util.print_back(4, outfile=outfile)
         util.print_down(2, outfile=outfile)
         remaining -= 2
     util.print_reset(outfile=outfile)
     util.print_down(remaining, outfile=outfile)
     util.print_back(2 * self.width + 2, outfile=outfile)
     outfile.flush()
Ejemplo n.º 8
0
def test_tree_search(name):
    env = make(name)

    def deep_agent():
        root = env.unwrapped.get_root()
        best_score = 0
        best_action = np.random.randint(0, env.action_space.n)
        for action, (child, score) in enumerate(root.get_children(True)):
            if child is None:
                continue
            for grand_child, child_score in child.get_children():
                for _, grand_child_score in grand_child.get_children():
                    total = score + child_score + grand_child_score
                    if total > best_score:
                        best_action = action
                        best_score = total
        return best_action

    def agent():
        root = env.unwrapped.get_root()
        best_score = 0
        best_action = np.random.randint(0, env.action_space.n)
        for action, (child, score) in enumerate(root.get_children(True)):
            if child is None:
                continue
            for _, child_score in child.get_children():
                total = score + child_score
                if total > best_score:
                    best_action = action
                    best_score = total
        return best_action

    env.reset()
    total_reward = 0
    for _ in range(10):
        _, reward, done, _ = env.step(agent())
        if done:
            break
        total_reward += reward
        env.render(mode="human")
        print_up(1)
        print("Reward =", total_reward)
Ejemplo n.º 9
0
 def render(self,
            outfile=sys.stdout,
            width: Optional[int] = None,
            height: Optional[int] = None,
            in_place=False):
     height = height or self.HEIGHT
     width = width or self.WIDTH
     if not in_place:
         for _ in range(height):
             outfile.write("\n")
         util.print_up(height, outfile=outfile)
     for i in range(self.HEIGHT - height, self.HEIGHT):
         offset, row = divmod(i, 8)
         for j in range(width):
             empty = True
             for k in range(self.num_colors):
                 if self.data[row + 8 * k +
                              8 * self.num_layers * offset] & (1 << j):
                     if self.tsu_rules and i <= self.offset:
                         util.print_puyo(k + 7, outfile=outfile)
                     else:
                         util.print_puyo(k, outfile=outfile)
                     empty = False
             if self.has_garbage:
                 garbage_puyo = self.data[row + 8 * self.num_colors +
                                          8 * self.num_layers * offset] & (
                                              1 << j)
                 if garbage_puyo:
                     if self.tsu_rules and i <= self.offset:
                         util.print_color(4, outfile=outfile)
                     else:
                         util.print_color(6, outfile=outfile)
                     outfile.write("\u25ce ")
                     empty = False
             if empty:
                 outfile.write("\u00b7 ")
             util.print_reset(outfile=outfile)
         util.print_down(1, outfile=outfile)
         util.print_back(2 * width, outfile=outfile)