PLEASE CODE IN PYTHON Part V: diagonal winner(board) This function scans the ent
ID: 3830542 • Letter: P
Question
PLEASE CODE IN PYTHON
Part V: diagonal winner(board)
This function scans the entire game board passed as an argument and returns one of three values:
• 1 if player 1 has placed four consecutive X’s in a diagonal somewhere in the board
• 2 if player 2 has placed four consecutive O’s in a diagonal somewhere in the board
• 0 (zero) if neither player has placed four consecutive pieces in a diagonal somewhere in the board
Under no circumstances is the function permitted to make a change to board.
Note that diagonals can run lower-left-to-upper-right and upper-left-to-lower-right, as shown in the examples below:
...... : ......
...... : ......
....X. : ......
...XO. : .X.O..
..XOX. : .XXO..
.XOXO. : .OXX..
OOXOOX : OOOXXO
PROVIDED BELOW IS THE BOARD CLASS
Explanation / Answer
# PART V
# Determines if a player has won the game by placing four pieces in a diagonal.
# Returns 1 if player 1 has won the game by placing four X's diagonally.
# Returns 2 if player 2 has won the game by placing four O's diagonally.
# Returns 0 if no one has won yet by placing pieces in this manner.
# This function must not modify the contents of the game board.
def diagonal_winner(board):
for i in range(3, board._num_rows):
count1 = 0
count2 = 0
for j in range(0, i+1):
if i == j and board._slots[j][j] == 'X':
count1 += 1
count2 = 0
elif i == j and board._slots[j][j] == 'O':
count2 += 1
count1 = 0
if count1 == 4 and count2 == 0:
return 1
elif count2 == 4 and count1 == 0:
return 2
n = board._num_rows
for i in range(3, board._num_rows):
count1 = 0
count2 = 0
for j in range(0, i+1):
if (n-i-1) == j and board._slots[j][j] == 'X':
count1 += 1
count2 = 0
elif (n-1-1) == j and board._slots[j][j] == 'O':
count2 += 1
count1 = 0
if count1 == 4 and count2 == 0:
return 1
elif count2 == 4 and count1 == 0:
return 2
return 0
# pastebin code link: https://pastebin.com/1PfnjKTD
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.