[英]matching chips in Connect Four Printing the match function (python)
[英]Connect Four Python printing problems
我已经为游戏连接四块板做了一个初始化板,现在我想跟踪每个turn的位置和比赛,如果我这样做:
b = ConnectFour()
b.play_turn(1, 3)
b.play_turn(1, 3)
b.play_turn(1, 4)
b.print_board()
木板应打印出:
-----------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
-----------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | x | | | |
| | | | x | x | | |
-----------------------------
更新:
#initializes board
class ConnectFour(object):
def __init__(self):
self.board = [[0 for i in range(7)] for j in range(8)]
def get_position(self, row, column):
""" Returns either None or an integer 1 or 2 depending
on which player is occupying the given row or column.
Row is an integer between 0 and 5 and column is an integer between 0 and 6. """
assert row >= 0 and row < 6 and column >= 0 and column < 7
return self.board[row][column]
def play_turn(self, player, column):
""" Updates the board so that player plays in the given column.
player: either 1 or 2
column: an integer between 0 and 6
"""
assert player == 1 or player == 2
assert column >= 0 and column < 7
for row in range(6):
if self.board[row][column] == None:
self.board[row][column] = player
return
def print_board(self):
print "-" * 29
print "| 0 | 1 | 2 | 3 | 4 | 5 | 6 |"
print "-" * 29
for row in range(5,-1,-1):
s = "|"
for col in range(7):
p = self.get_position(row, col)
if p == None:
s += " |"
elif p == 1:
s += " x |"
elif p == 2:
s += " o |"
else:
s += " ! |"
print s
print "-" * 29
解决:
这是当前输出:
-----------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
-----------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | x | | | |
| | | | x | x | | |
-----------------------------
我看到的问题是您的self.get_position不执行您想要的操作。
def get_position(self, row, column):
assert row >= 0 and row < 6 and column >= 0 and column < 7
if self.get_position == 1: # the type of self.get_position is a function, and it will never be equal to 1, or 2, so it always goes to None
return 1
elif self.get_position == 2:
return 2
else:
return None
另一个问题是您从未使用过开发板,也从未将数据存储在任何地方。
如果您是我,那么我将首先考虑如何存储数据。
一个明显的选择是二维数组。
self.board = [[0 for i in range(6)] for j in range(7)]
当您调用play_turn时,尝试更新该列,例如,您已经在第3列上具有[0,0,0,0,2,1],您将尝试获取不为0的第一个元素的索引。新播放器在其之前的元素上的索引,因此它将变为[0,0,0,1,2,1](如果该片段属于播放器1)。
然后,实际上要使用列号和行号来检查板上的内容。 是0还是1或2。
您最好将其称为get_player_num或类似的名称。 您没有获得位置,但获得了该位置的棋手编号。
一个例子。 如果我在一个班级有一个2 * 2数组:
class CF(object):
def __init__(self):
board = [[0, 0], [0, 0]]
这代表一个2 * 2字段,现在它们都是空的。
0 0
0 0
我可以设计一个更新方法:
def update(self, col, row, newVal):
self.board[col - 1][row - 1] = newVal
如果这样做:
game = CF()
game.update(1, 2, 1)
然后董事会变成
0 0
1 0
在阅读游戏板时,我知道第一列第二行是玩家1。
您的get_position
代码不正确。 您需要检查self.board
,而不是查看self.get_position
(这是方法本身,没有任何值)。
由于您当前不在任何地方使用self.board,因此我不确定您打算使用哪种格式(它是单个列表还是列表列表?)。
我认为您需要添加以下内容:
将板更好地初始化为__init__
。 我建议将self.board
为具有0
或None
(无论您要表示“空”为)的列表,重复7 * 6次。 或者,您也可以使其成为列表列表(可能是六个列表,每个列表七个值)。
检查电路板上的get_position
值。 如果您只列出一个列表,则应检查self.board[row*7+column]
。
play_turn
方法需要检查木板,以找出将一块放入指定列时该块落在哪一行。 该作品将进入该列为空的最低行,因此需要循环。 找到精确位置后,即可在self.board
设置值。
固定get_position
,您的print_board
函数应该可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.