PLEASE CODE IN PYTHON Part III: horizontal winner(board) This function scans the
ID: 3830127 • Letter: P
Question
PLEASE CODE IN PYTHON
Part III: horizontal 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 single row somewhere in the board
• 2 if player 2 has placed four consecutive O’s in a single row somewhere in the board
• 0 (zero) if neither player has placed four consecutive pieces in a single row somewhere in the board
Under no circumstances is the function permitted to make a change to board.
PROVIDED BELOW IS THE BOARD CLASS
Explanation / Answer
# PART III
# Determines if a player has won the game by putting four pieces next to each other in a single row.
# Returns 1 if player 1 has won the game by placing four X's contiguously in a single row.
# Returns 2 if player 2 has won the game by placing four O's contiguously in a single row.
# 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 horizontal_winner(board):
# scan horizontally
for i in range(0, board._num_rows):
# counters for 'X' and 'O'
count1 = 0
count2 = 0
for j in range(0, board._num_cols):
if board._slots[i][j] == 'X':
count1 += 1
count2 = 0
elif board._slots[i][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 link : https://pastebin.com/PznFHFmY
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.