Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use PYTHON only and follow all the following instructions carefully. Than

ID: 3596387 • Letter: P

Question

Please use PYTHON only and follow all the following instructions carefully. Thank you

Requirements

Background info (for your reference):

Problem statements:

Please make sure that it can pass all the test cases as follows: https://repl.it/Mvr6

Restrictions no modules may be imported. Allowed basic statements, variables, operators, del, indexing, slicing, in, are all allowed any form of control flow we've covered is allowed (if/else, loops, etc) only these built-in functions: range(), len(), int), str), list), abs () only these built-in methods: s.split), s.join(), s.pop(), xs.append), xs.extend), xs.insert(), s.format() calling other functions of the project (and your own helper functions). Please do this! Hint In our solution, we only used range, len, in, abs, .append(), .join(), .split(), and list() Remember: your grade is significantly based on passing test cases- try to completely finish individual functions before moving on. The easiest way to implement many functions is to call the earlier/easier functions, so it'll pay off to complete functions before moving on. Don't let yourself be "almost done" with a function, but miss all the tests!

Explanation / Answer


def check_horizontal(board, r, c):
(row, col) = get_size(board)
if not is_valid_coord(board, r, c):
return False
color = board[r][c]
if color == '.':
return False
count = 1
# from given position check next 3 position if they are same oclor if yes then winner else not
for j in range(c+1, col):
if count == 4:
break
if board[r][j] == color:
count += 1
else:
return False
return count == 4
# print(check_horizontal([['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B']], 0, 4))

def check_vertical(board, r, c):
(row, col) = get_size(board)
if not is_valid_coord(board, r, c):
return False
color = board[r][c]
if color == '.':
return False
count = 1
# from given cell check below 3 cell, if they are same as cell color then winner else not
for i in range(r+1, row):
if count == 4:
break
if board[i][c] == color:
count += 1
else:
return False
return count == 4
# print(check_vertical([['A'], ['A'], ['A'], ['A'], ['A']], -1, -1))

def check_major_diagonal(board, r, c):
(row, col) = get_size(board)
if not is_valid_coord(board, r, c):
return False
color = board[r][c]
if color == '.':
return False
count = 1
j = c
# traverse major diagonal and see if next 3 entry is same as given cell, if yes winner, else not
for i in range(r+1, row):
j = j + 1
if j >= col:
break
if count == 4:
break
if board[i][j] == color:
count += 1
else:
return False
return count == 4

def check_minor_diagonal(board, r, c):
(row, col) = get_size(board)
if not is_valid_coord(board, r, c):
return False
color = board[r][c]
if color == '.':
return False
count = 1
j = c
# traverse minor diagonal and see if next 3 entry is same as given cell, if yes winner, else not
for i in range(r-1, -1, -1):
j = j + 1
if j >= col:
break
if count == 4:
break
if board[i][j] == color:
count += 1
else:
return False
return count == 4

# method to check if board is complete full
def is_board_full(board):
(row, col) = get_size(board)
for i in range(row):
for j in range(col):
if board[i][j] == '.':
return False
return True

def check_winner(board):
(row, col) = get_size(board)

winner_color = ''
won = False
# for each cell check if there is any horizonatl, vertcal, or diagonal winner
# if yes check if there is winner for other color as well
# if two winner are possible then tie
# if one winner then that is winner
for i in range(row):
for j in range(col):
if check_horizontal(board, i, j) or check_vertical(board, i, j) or check_major_diagonal(board, i, j) or check_minor_diagonal(board, i, j):
won = True
# check if winner is set and there is a new different winenr
if winner_color and winner_color != board[i][j]:
return "tie!"
else:
winner_color = board[i][j]
if won:
return winner_color
elif is_board_full(board): # if no winner is found and board is full then its a draw
return "draw"
else: # if board is not full and no winner then game is still pending
return "pending"

# copy pastable code link: https://paste.ee/p/hepcm