连接四个游戏形成一个网格

2024-04-26 04:16:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我在试着做一个连接四的游戏。在这一点上,我试图使游戏只为控制台交互,并有困难使网格看起来像这样的格式:

创建7列,每个列包含“.”,直到时间被任一颜色替换(以防格式显示不正确):

1  2  3  4  5  6  7
.  .  .  .  .  .  .
.  .  .  .  .  .  .
.  .  Y  .  .  .  .
.  Y  R  .  .  .  .
.  R  Y  .  .  .  .
.  R  R  .  .  .  .

以下是我目前掌握的情况:

NONE = ' '
RED = 'R'
YELLOW = 'Y'

BOARD_COLUMNS = 7
BOARD_ROWS = 6

# board=two dimensional list of strings and
# turn=which player makes next move'''    
ConnectFourGameState = collections.namedtuple('ConnectFourGameState',
                                              ['board', 'turn'])

def new_game_state():
    '''
    Returns a ConnectFourGameState representing a brand new game
    in which no moves have been made yet.
    '''
    return ConnectFourGameState(board=_new_game_board(), turn=RED)

def _new_game_board():
    '''
    Creates a new game board.  Initially, a game board has the size
    BOARD_COLUMNS x BOARD_ROWS and is comprised only of strings with the
    value NONE
    '''
    board = []

    for col in range(BOARD_COLUMNS):
        board.append([])
        for row in range(BOARD_ROWS):
            board[-1].append(NONE)

    return board

Tags: columnsofinboardnonegame游戏new