So I found this code online because I wanted to make my own so I like tried to understand what this person was doing by looking what all the different lines do, but i came across this line and I don't know what it does. I can't really find anything on this online, mostly because i don't know how to look it up or what it's called. It's a piece of code from the game connect 4, and this is the function for drawing the circles. I'm talking about the
if board[i][j]==0:
and
if board[i][j]==1:
code. It's in this little function:
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)
excuse all my comments on every line, I'm trying to understand the code. If someone can help me with this (or share a website or doc that explains it) that would be greatly appreciated!
If we look at the source and illustration of this game from pythonturtle.academy we can see more clearly what the code does:
The two dimensional board
matrix stores one of three values, 0 for 'white'
or unoccupied, 1 for 'black'
, the 1st player and 2 for 'red'
, the 2nd player. So the code in question is drawing circles on the board whose color is based on who occupies, (or doesn't occupy) the row and column, [i][j]
, in question:
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')
The else:
clause is an implicit elif board[i][j] == 2: