连接4游戏逻辑

时间:2014-06-25 04:10:55

标签: python tkinter

正在使用tkinter在python中开发http://en.wikipedia.org/wiki/Connect_Four游戏。

我已经拿出了棋盘和两个玩家的棋子。我现在正试图检查游戏是否结束。我已经实现了以下逻辑,但这似乎没有用。

def checkWin():
        for row in range(canvas.data.rows):
            for col in range(canvas.data.cols):
                checkWinFromCell(row, col)

def checkWinFromCell(row, col):
        if canvas.data.board[row][col] == 0:
            return False
        dirs = [[0,1], [1,0], [1,1], [1,-1], [0,-1], [-1,0], [-1,-1], [-1,1]]
        for direction in dirs:
            checkWinFromCellInDir(row, col, direction)
        return False

def checkWinFromCellInDir(row, col, direction):
        drow, dcol = direction[0], direction[1]
        for i in range(1,4):
            if row+i*drow<0 or row+i*drow>=canvas.data.rows or col+i*dcol<0 or col+i*dcol>=canvas.data.cols:
                return False
            if canvas.data.board[row][col] != canvas.data.board[row+i*drow][col+i*dcol]:
                return False
        return canvas.data.board[row][col]

我需要知道检查我的游戏是否已经完成的逻辑,即已经连接了四个点。

1 个答案:

答案 0 :(得分:0)

我对Tkinter不是很熟悉,所以这是一个半心半意的答案。然而,由于它已经差不多一个小时而且没有答案,我确实为你工作了。

class Map(list):
    def __init__(self, tiles, width, height):
        """This object functions exactly as a tile map for your connect four
        game. It is a subclass of list, so you can iterate through its rows.
        "y" increases from top to bottom and "x" increases from left to right"""
        for y in range(height):
            self.append([random.choice(tiles) for x in range(width)])
            # for ease of use, we're generating a random tile map here
    def __str__(self):
        return '\n'.join([' '.join([ch for ch in row]) for row in self])
        # this will make print(map_object) show something pretty

Vector = collections.namedtuple("Vector", ['x','y'])
# build a namedtuple to contain our directions. It's a bit easier on the eyes
# to use object access, IMO. YMMV.

def checkwin(map_):
    directions = [Vector(x, y) for (x, y) in [(1, 0), (-1, 1), (0, 1), (1, 1)]]
    # directions = E, SW, S, SE
    for y, row in enumerate(map_):
        for x, ch in enumerate(row):
            value = ch
            if value == ' ': continue # blank squares can't win
            for vector in directions:
                result = checkfour(map_, x, y, vector)
                if result:
                    return result
            return False

def checkfour(map_, x, y, vector):
    """Checks map_ for four squares from the given x and y in vector direction"""
    value = map_[y][x]
    try:
        lst = [map_[y + k*vector.y][x + k*vector.x]==value for k in range(1,4)]
        # 2 2 2 1 would return [True, True, False]
        return all(lst) and (x,y)
    except IndexError:
        return False
        # we know we'll run off the edge of the map. It's cheaper (in programmer
        # time) to simply return False here rather than check each square to make
        # sure there ARE 3 more squares in vector-direction.

map_ = Map("12 ", 8, 8)
print(checkwin(map_))
# if your randomly generated map would win in connect four, it should print
# the first (x,y) coordinate that begins a win going E, SW, S, or SE
print(map_)