잠토의 잠망경

[python] 딥러닝과 바둑 본문

Book/0006.딥러닝과 바둑

[python] 딥러닝과 바둑

잠수함토끼 2020. 6. 13. 07:23

gotypes.py

github

import enum


#tag::color[]
class Player(enum.Enum):

    black = 1
    white = 2

    @property
    def other(self):
        return Player.black if self == Player.white else Player.white

#end::color[]



#tag::points[]
from collections import namedtuple

class Point(namedtuple('Point', 'row col')):

    def neighbors(self):

        return[
            Point(self.row-1,   self.col),      # A         A
            Point(self.row+1,   self.col),      # B     C   X   D
            Point(self.row,     self.col-1),    # C         B
            Point(self.row,     self.col+1)     # D
        ]

#end::points[]

goboard_slow.py

github


import copy
from dlgo.gotypes import Player

class Move():

    def __init__(self, point=None, is_pass=False, is_resign=False):

        assert(point is not None) ^ is_pass ^ is_resign

        self.point     = point
        self.is_play   = (self.point is not None)
        self.is_pass   = is_pass
        self.is_resign = is_resign

    # 바둑판에 돌을 놓는다.
    @classmethod
    def play(cls, point):
        return Move(point=point)


    # 차례를 넘긴다.
    @classmethod
    def pass_turn(cls):
        return Move(is_pass=True)


    # 현재 대국을 포기한다.
    @classmethod
    def resign(cls):
        return Move(is_resign=True)

github


# tag::strings[]
class GoString():

    def __init__(self, color, stones, liberties):
        self.color      = color
        self.stones     = set(stones)
        self.liberties  = set(liberties)

    def remove_liberty(self, point):
        self.liberties.remove(point)

    def add_liberty(self, point):
        self.liberties.add(point)

    def merged_with(self, go_string):

        assert go_string.color == self.color

        combined_stones = self.stones | go_string.stones

        return GoString(self.color, combined_stones, (self.liberties | go_string.liberties) - combined_stones)

    @property
    def num_liberties(self):
        return len(self.liberties)

    def __eq__(self, other):
        return isinstance(other, GoString) and \
                self.color == other.color and \
                self.stones == other.stones and \
                self.liberties == other.liberties

# end::strings[]

대표

github

Comments