[英]Connect four game- making a grid
我正在嘗試制作四連冠游戲。 在這一點上,我試圖使游戲僅用於控制台交互,並且使網格看起來像這種格式時遇到麻煩:
創建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
您將需要將NONE
設置為'.'
,不是空格。 然后,您可以為電路板提供這樣的打印功能:
def printBoard (b):
print(' '.join(map(lambda x: str(x + 1), range(BOARD_COLUMNS))))
for y in range(BOARD_ROWS):
print(' '.join(b[x][y] for x in range(BOARD_COLUMNS)))
像這樣使用:
>>> x = _new_game_board()
>>> printBoard(x)
1 2 3 4 5 6 7
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
並在重構示例狀態時:
>>> x[1][-1] = RED
>>> x[1][-2] = RED
>>> x[1][-3] = YELLOW
>>> x[2][-1] = RED
>>> x[2][-2] = YELLOW
>>> x[2][-3] = RED
>>> x[2][-4] = YELLOW
>>> printBoard(x)
1 2 3 4 5 6 7
. . . . . . .
. . . . . . .
. . Y . . . .
. Y R . . . .
. R Y . . . .
. R R . . . .
如果您有興趣,我會根據這個想法對整個游戲進行簡單的實現。 你可以在這里看到它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.