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

Can I get the simplist logic for the questions? I just want to see the differenc

ID: 3595484 • Letter: C

Question

Can I get the simplist logic for the questions? I just want to see the difference between my code and a simple logic... Thanks!!

Chrome File Edit View History Bookmarks People window Help 73% Wed 6:09 PM E . O Microsoft Word-p4.docx CSecure https://cs.gmu.edu/-marks/112/projects/p4.pdf Microsoft Word -p4.docx You have more time on this project because it is larger. Use all time wisely! Background: The purpose of this assignment is to practice building, inspecting, and modifying 2D lists effectively. This often requires nested for-loops, but not always. It involves thinking of the structure like an N x M matrix of labeled spots, each with a row- and column-index. As before, we are restricting ourselves to the most basic functionalitics, and any others that you want, you should implement yourself. Two-dimensional lists arent conceptually more difficult than single-dimension lists, but in practice the nested loops and more complex traversals and interactions merit some extra practice. Project Basics document (part of assignment):httpcs.gmy sdu-marks 112proiests/proiest basics,pdf Project Four tester file Grading Code passes shared tests: 98 TOTAL: 180 +5 extra credit What can I use? You may only use the following things. You may ask about adding things to a list, but we are unlikely to add anything. If you use something disallowed, it's not an honor code violation, you just wont gct points. Restrictions no modules may be imported. Allowed basic statements, variables, operators, de1, indexing, slicing. in, are all allowed any form of control flow we've covered is allowed (ifelse, loops, etc) only these built-in functions: range)en),int), str), list), abs() only these built-in methods: s.split), s.join), s.pop(), xs.append). xs.extend) xs.insert). s.fornat() 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 carlier 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! Connect Four! Connect Four is a game where players win by placing four of their pieces in a row, either horizontally, vertically, or diagonally. The board is a vertical plane with a limited number of columns; playing a piece in a column will drop to the lowest available space. Players take turns dropping pieces into columns, trying to get four in a row before their opponent docs. Connect Four

Explanation / Answer

def init_board(num_rows, num_cols):
# create a new board
board = []
  
# add given number of rows
for i in range(num_rows):
# initialise each row with empty
row = ['.']*num_cols
board.append(row)
return board
# print(init_board(2, 3))

def show_board(board):
# create an empty string which will hold string representation of board
boardstr = ''
# for each row in board, craete its string representation
for row in board:
# string representation is just putting each cell one after another in row and then separating rows with new line
boardstr += ''.join(row) + ' '
# return string repreentation of board
return boardstr
# print(show_board([['.', '.', '.'], ['A', 'B', 'A']]))

def read_board(s):
# check if string is empty or None return None
if not s:
return None
  
# create an empty list which will hold all valid lines (non empty lines) in s
lines = []
  
# get all lines as a list by splitting lines on new line charatcer
tempLines = s.split(' ')
  
# for each line b=obtained by splitting keep only non empty lines
for line in tempLines:
# check if line is not None/empty
if line:
# append line to lines if it is non empty
lines.append(line)
  
# number of column will be entry in one line and all lines should have same length
col = len(lines[0])
row = 0
# for each lines
for i in range(len(lines)):
# check if length of line is different from other line, if yes return None
if len(lines[i]) != col:
return None
# if line is just white space don't consider it
if lines[i] == ' ':
continue
row += 1
# for each char in line
for c in lines[i]:
# check if char is in upper case letter, else its an invalid data
if not (c == '.' or c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
return None
  
# create an empty board as we now know exact number of rows and column
board = init_board(row, col)
  
# fill row and column from lines
for i in range(row):
for j in range(col):
board[i][j] = lines[i][j]
return board
# print(read_board(".... NOO "))

def get_size(board):
# check if board is not defined, if yes return None
if not board:
return None
# number of rows is length of outer list
row = len(board)
if row == 0:
return None
# length of column should be same for all column so taking out from first row
col = len(board[0])
return (row, col)
# print(get_size([['.', '.', '.'], ['A', 'B', 'A']]))

def is_valid_coord(board, r, c):
# a coordinate is valid if it is greater than equal to zero but strictl less than board size
if r < 0 or c < 0:
return False
(row, col) = get_size(board)
if r >= row or c >= col:
return False
  
return True
# print(is_valid_coord([["B", ".", "R"], ["R", "R", "B"]], 0, 0))
# print(is_valid_coord([["B", ".", "R"], ["R", "R", "B"]], 2, 3))
# print(is_valid_coord([["B", ".", "R"], ["R", "R", "B"]], -1, -1))

def get_coords_by_color(board, color):
# list to hold all coordinates of a given colr in board
color_coord = []
# iterate over full board
for i in range(len(board)):
for j in range(len(board[i])):
# check if color is same as given color if yes then add coordinate as tuple in list
if board[i][j] == color:
color_coord.append((i, j))
return color_coord
# print(get_coords_by_color([['G', '.'], ['Y', 'Y']], 'Y'))
# print(get_coords_by_color([['G', '.'], ['Y', 'Y']], 'G'))
# print(get_coords_by_color([['.', 'X'], ['Y', 'X']], 'X'))

def get_colors(board):
colors = []
# iterate over full board and get all color present in board
for i in range(len(board)):
for j in range(len(board[i])):
# ignore empty cell or if color is already notes, else add it to list
if board[i][j] != '.' and (not board[i][j] in colors):
colors.append(board[i][j])
return colors
# print(get_colors([['.', 'Y'], ['Y', 'X']]))

def count_pieces_by_color(board, color):
count = 0
# iterate over board
for i in range(len(board)):
for j in range(len(board[i])):
# if color in cell as asked color, increase its count
if board[i][j] == color:
count += 1
return count
# print(count_pieces_by_color([['.', 'Y'], ['Y', 'X']], 'Y'))

def any_floating(board):
(row, col) = get_size(board)
# iterate over all column
for j in range(col):
# variable to make sure that once we found a color all below column should have color
color_found = False
# iterate over row
for i in range(row):
# if cell is not empty then it has color which means now all below cell should have color
if board[i][j] != '.':
color_found = True
elif board[i][j] == '.' and color_found: # if color is empty and above cell is filled meaning above ell was floating
return True
# return false if no cell is floating
return False

def is_column_full(board, c):
(row, col) = get_size(board)
if c < 0 or c >= col:
return False
# iterate over all row for given column
for i in range(row):
# if there is free space meaning coloumn is not full
if board[i][c] == '.':
return False
return True

# print(is_column_full([['.', 'Y'], ['Y', 'X'], ['Y', 'X']], 0))
# print(is_column_full([['.', 'Y'], ['Y', 'X'], ['Y', 'X']], 99))
# print(is_column_full([['.', 'Y'], ['Y', 'X'], ['Y', 'X']], 1))

def place_one(board, c, color):
(row, col) = get_size(board)
if c < 0 or c >= col or is_column_full(board, c):
return False
  
# iterate over all row from last for given column and see if it is empty, if it is but color at that cell
for i in range(row-1, -1, -1):
# if cell si empty put color
if board[i][c] == '.':
board[i][c] = color
return True
# return False if no cell was free
return False

def pop_out(board, c, color):
(row, col) = get_size(board)
if c < 0 or c >= col or board[row-1][c] != color:
return False
# move all element down by removing last eleemnt of given column
for i in range(row-1, 0, -1):
board[i][c] = board[i-1][c]
board[0][c] = '.';
return True
# b = [['A', 'B'], ['B', 'A']]
# print(pop_out(b, 0, 'B'))
# print(b)

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"
  

# completed

# create a copy of board
def get_copy_of_board(board):
board_copy = []
for row in board:
newRow = []
for col in row:
newRow.append(col)
board_copy.append(newRow)
return board_copy

def winning_move(board,color):
# if board is not pending then there cant be any winning move.
if check_winner(board) != "pending":
return None
  
(row, col) = get_size(board)
  
# for each column see if we add one entry and then check if we found a winner, if yes then there is winning move and hence we will return column index
for j in range(col):
board_copy = get_copy_of_board(board)
if place_one(board_copy, j, color):
if check_winner(board_copy) == color:
return j
# if no winning index return None
return None

# copy pastbale ;ink if indentation mess up: https://paste.ee/p/BqrF0

Do comment if you need help undrstanding any of the function.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote