***USE PYTHON*** Part III: horizontal winner (board) (4 points) This function sc
ID: 3826795 • Letter: #
Question
***USE PYTHON***
Part III: horizontal winner (board) (4 points) 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 Part IV: vertical winner (board) (4 points) 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 column somewhere in the board 2 if player 2 has placed four consecutive O's in a single column somewhere in the board 0 (zero) if neither player has placed four consecutive pieces in a single column somewhere in the board Under no circumstances is the function permitted to make a change to board.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
# PART IV
# Determines if a player has won the game by putting four pieces on top of each other in a single column.
# Returns 1 if player 1 has won the game by placing four X's contiguously in a single column.
# Returns 2 if player 2 has won the game by placing four O's contiguously in a single column.
# 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 vertical_winner(board):
for j in range(0, board._num_cols):
count1 = 0
count2 = 0
for i in range(0, board._num_rows):
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
# 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
# PART VI
# Drops a bomb into a given column.
# If the column number is outside the range 1 through num_cols, return -1 and do not change the state of the board.
# Otherwise, remove all the pieces from the column, replacing them with '.', and then return col_num.
# Note that col_num is given as a value in the range 1 through board._num_cols, NOT 0 through board._num_cols-1.
def drop_bomb(board, col_num):
if col_num < 1 or col_num > board._num_cols:
return -1
for i in range(0, board._num_rows):
board._slots[i][col_num-1] = '.'
return col_num
# codelink : https://pastebin.com/97R0dDsK
Please rate positively.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.