代码行 if board [i][j]==0 是做什么的?
What does the code line if board [i][j]==0 do?
所以我在网上找到了这段代码,因为我想制作自己的代码,所以我想通过查看所有不同行的作用来理解这个人在做什么,但我遇到了这条线,但我不知道它能做什么。我真的无法在网上找到任何东西,主要是因为我不知道如何查找它或它叫什么。这是来自游戏 connect 4 的一段代码,这是绘制圆圈的函数。我说的是
if board[i][j]==0:
和
if board[i][j]==1:
代码。它在这个小函数中:
def draw_pieces(): #draws the pieces
global board #makes 'board' global
row_gap = HEIGHT/ROWS #variable row_gap is now 771,4 / 6 = 128,6 (gap between circles)
col_gap = WIDTH/COLS #variable col_gap is now 900 / 7 = 128,6 (gap between circles)
Y = STARTY + row_gap / 2; # Y is now -385,7 + 128,6 / 2 = -321,4
for i in range(ROWS): #this next function happens 6 times (i think its first black their turn)
X = STARTX + col_gap/2 #X is now -450 + 128,6 / 2 = -385,7
for j in range(COLS): #this next function happens 7 times (then red their turn)
if board[i][j] == 0: #if board i and j are equal to 0??
draw_circle(X,Y,row_gap/3,'white') #draw a circle on X and Y, radius row_gap/3 and in color white
elif board[i][j] == 1: #if board i and j are equal to 1??
draw_circle(X,Y,row_gap/3,'black') #draw a circle on X and Y, raduis row_gap/3 and in color black
else: #if board i and j are equal to anything else??
draw_circle(X,Y,row_gap/3,'red') #draw a circle on X and Y, radius row_gao/3 and in color red
X += col_gap #for every j, X is now added with col_gap (128,6)
Y += row_gap #for every i, Y is now added with row_gap (128,6)
请原谅我对每一行的所有评论,我正在努力理解代码。如果有人可以帮助我(或分享解释它的网站或文档),将不胜感激!
如果我们查看 source and illustration of this game from pythonturtle.academy,我们可以更清楚地看到代码的作用:
二维 board
矩阵存储三个值之一,0 表示 'white'
或未占用,1 表示 'black'
,第 1 个玩家,2 表示 'red'
,第二名球员。所以有问题的代码是在板上绘制圆圈,其颜色基于谁占据(或不占据)行和列,[i][j]
,问题:
if board[i][j] == 0:
draw_circle(X,Y,row_gap/3,'white')
elif board[i][j] == 1:
draw_circle(X,Y,row_gap/3,'black')
else:
draw_circle(X,Y,row_gap/3,'red')
else:
子句是隐含的 elif board[i][j] == 2:
所以我在网上找到了这段代码,因为我想制作自己的代码,所以我想通过查看所有不同行的作用来理解这个人在做什么,但我遇到了这条线,但我不知道它能做什么。我真的无法在网上找到任何东西,主要是因为我不知道如何查找它或它叫什么。这是来自游戏 connect 4 的一段代码,这是绘制圆圈的函数。我说的是
if board[i][j]==0:
和
if board[i][j]==1:
代码。它在这个小函数中:
def draw_pieces(): #draws the pieces
global board #makes 'board' global
row_gap = HEIGHT/ROWS #variable row_gap is now 771,4 / 6 = 128,6 (gap between circles)
col_gap = WIDTH/COLS #variable col_gap is now 900 / 7 = 128,6 (gap between circles)
Y = STARTY + row_gap / 2; # Y is now -385,7 + 128,6 / 2 = -321,4
for i in range(ROWS): #this next function happens 6 times (i think its first black their turn)
X = STARTX + col_gap/2 #X is now -450 + 128,6 / 2 = -385,7
for j in range(COLS): #this next function happens 7 times (then red their turn)
if board[i][j] == 0: #if board i and j are equal to 0??
draw_circle(X,Y,row_gap/3,'white') #draw a circle on X and Y, radius row_gap/3 and in color white
elif board[i][j] == 1: #if board i and j are equal to 1??
draw_circle(X,Y,row_gap/3,'black') #draw a circle on X and Y, raduis row_gap/3 and in color black
else: #if board i and j are equal to anything else??
draw_circle(X,Y,row_gap/3,'red') #draw a circle on X and Y, radius row_gao/3 and in color red
X += col_gap #for every j, X is now added with col_gap (128,6)
Y += row_gap #for every i, Y is now added with row_gap (128,6)
请原谅我对每一行的所有评论,我正在努力理解代码。如果有人可以帮助我(或分享解释它的网站或文档),将不胜感激!
如果我们查看 source and illustration of this game from pythonturtle.academy,我们可以更清楚地看到代码的作用:
二维 board
矩阵存储三个值之一,0 表示 'white'
或未占用,1 表示 'black'
,第 1 个玩家,2 表示 'red'
,第二名球员。所以有问题的代码是在板上绘制圆圈,其颜色基于谁占据(或不占据)行和列,[i][j]
,问题:
if board[i][j] == 0:
draw_circle(X,Y,row_gap/3,'white')
elif board[i][j] == 1:
draw_circle(X,Y,row_gap/3,'black')
else:
draw_circle(X,Y,row_gap/3,'red')
else:
子句是隐含的 elif board[i][j] == 2: