示例#1
0
def test_add_tile():
    """ Test if add tile properly"""
    t = Tiles(4)
    t.add_tile(1, 0, 0)
    assert t.t_total[1][0].get_color() == 0
    assert t.t_total[1][1].get_color() == 255
    assert t.t_total[1][2].get_color() == 0
    # trying to put it at a spot that is already filled a tile
    t.add_tile(1, 0, 255)
    assert t.t_total[1][0].get_color() == 0
示例#2
0
def test_counting_tile():
    """ Test counting tiles method """
    t = Tiles(4)
    t.counting_tile()
    assert len(t.blacks) == 2
    assert len(t.whites) == 2
    t.add_tile(1, 0, 0)
    t.counting_tile()
    assert len(t.blacks) == 3
    assert len(t.whites) == 2
示例#3
0
class Board:
    def __init__(self, WIDTH, HEIGHT, SPOT):
        """Create board for othello game"""
        self.WIDTH = WIDTH
        self.HEIGHT = HEIGHT
        self.SPACING = 100
        self.SPOT = SPOT
        self.tiles = Tiles(self.SPOT)
        self.STROKE = 3
        self.legal = []
        self.black = 0
        self.white = 255
        self.dir = [[0, -1], [0, 1], [-1, 0], [1, 0], [-1, -1], [1, 1],
                    [-1, 1], [1, -1]]

    def display(self):
        """Display the othello board"""
        strokeWeight(self.STROKE)
        for i in range(self.SPACING, self.WIDTH, self.SPACING):
            line(i, 0, i, self.WIDTH)
        for i in range(self.SPACING, self.HEIGHT, self.SPACING):
            line(0, i, self.HEIGHT, i)
        self.tiles.display()

    def flip(self, x, y, color):
        """ flip the tile of opponent color """
        black_white = []
        if (color == self.black):
            black_white = self.tiles.blacks
            other_color = self.white
        elif (color == self.white):
            black_white = self.tiles.whites
            other_color = self.black
        for direction in self.dir:
            ver, hor = x, y
            count = 0
            dir_x, dir_y = direction[0], direction[1]
            ver += dir_x
            hor += dir_y
            while (self.in_board(ver, hor) is True):
                if ((count == 0 and (self.tiles.t_total[ver][hor] == 0 or
                                     (ver, hor) in black_white))
                        or (count > 0 and self.tiles.t_total[ver][hor] == 0)):
                    break
                elif (self.tiles.t_total[ver][hor] != 0 and
                      self.tiles.t_total[ver][hor].get_color() == other_color):
                    count += 1
                    ver += dir_x
                    hor += dir_y
                elif (count > 0 and (ver, hor) in black_white):
                    for i in range(count):
                        ver -= dir_x
                        hor -= dir_y
                        self.tiles.t_total[ver][hor].change_color(color)
                    break

    def is_valid(self, x, y, color):
        """ Determine it is a valid move and add tile """
        if ((x, y) in self.legal):
            self.tiles.add_tile(x, y, color)
            return True
        return False

    def legal_move(self, color):
        """ Find all possible legal move of current player"""
        self.legal = []
        black_white = []
        if (color == self.black):
            black_white = self.tiles.blacks
            other_color = self.white
        if (color == self.white):
            black_white = self.tiles.whites
            other_color = self.black
        for pos in black_white:
            x = pos[0]
            y = pos[1]
            i = 0
            count = 0
            while (i < len(self.dir)):
                if (x == pos[0] and y == pos[1]):
                    x += self.dir[i][0]
                    y += self.dir[i][1]
                elif (self.tiles.t_total[x][y] == 0
                      or self.tiles.t_total[x][y].get_color() == color):
                    if (count > 0 and self.tiles.t_total[x][y] == 0):
                        if ((x, y) not in self.legal):
                            self.legal.append((x, y))
                    x = pos[0]
                    y = pos[1]
                    i += 1
                    count = 0
                elif (self.tiles.t_total[x][y].get_color() == other_color):
                    x += self.dir[i][0]
                    y += self.dir[i][1]
                    count += 1
                if (x < 0 or x > self.SPOT - 1 or y < 0 or y > self.SPOT - 1):
                    x = pos[0]
                    y = pos[1]
                    i += 1
                    count = 0
        return self.legal

    def in_board(self, x, y):
        return x >= 0 and x <= self.SPOT - 1 and y >= 0 and y <= self.SPOT - 1