In Python, please comment What can I use? You may only use the following things.
ID: 3590249 • Letter: I
Question
In Python, please comment
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 won't get points 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 (ifelse, 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! 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 does Connect FourExplanation / Answer
def init_board(num_rows, num_cols):
board = []
for i in range(num_rows):
row = ['.']*num_cols
board.append(row)
return board
# print(init_board(2, 3))
def show_board(board):
boardstr = ''
for row in board:
boardstr += ''.join(row) + ' '
return boardstr
# print(show_board([['.', '.', '.'], ['A', 'B', 'A']]))
def read_board(s):
if not s:
return None
lines = []
tempLines = s.split(' ')
for line in tempLines:
if line:
lines.append(line)
col = len(lines[0])
row = 0
for i in range(len(lines)):
if len(lines[i]) != col:
return None
if lines[i] == ' ':
continue
row += 1
for c in lines[i]:
if not (c == '.' or c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
return None
board = init_board(row, col)
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):
if not board:
return None
row = len(board)
if row == 0:
return None
col = len(board[0])
return (row, col)
# print(get_size([['.', '.', '.'], ['A', 'B', 'A']]))
def is_valid_coord(board, r, c):
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):
color_coord = []
for i in range(len(board)):
for j in range(len(board[i])):
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 = []
for i in range(len(board)):
for j in range(len(board[i])):
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
for i in range(len(board)):
for j in range(len(board[i])):
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)
for j in range(col):
color_found = False
for i in range(row):
if board[i][j] != '.':
color_found = True
elif board[i][j] == '.' and color_found:
return True
return False
def is_column_full(board, c):
(row, col) = get_size(board)
if c < 0 or c >= col:
return False
for i in range(row):
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
for i in range(row-1, -1, -1):
if board[i][c] == '.':
board[i][c] = color
return True
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
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
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
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
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
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
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 board21(): # one move for 'G', four in a diagonal
return [['.','.','.','.','.','.','.'],
['.','.','.','.','B','B','.'],
['.','.','B','G','G','G','.'],
['.','.','G','G','G','B','.'],
['.','G','B','B','G','B','.'],
['G','B','B','G','B','G','B']]
print(check_minor_diagonal(board21(),5,0))
def check_winner(board):
(row, col) = get_size(board)
winner_color = ''
won = False
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
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):
return "draw"
else:
return "pending"
# completed
# code link: https://paste.ee/p/fe7gh
Sample run
Running required definitions:
True
True
..........................................................................................
----------------------------------------------------------------------
Ran 90 tests in 0.006s
OK
90 / 90 Required test cases passed (worth 1 each)
0 / 5 Extra credit test cases passed (worth 1 each)
Score based on test cases: 90.00 / 90 ( 90 * 1 + 0 * 1)
$ python3 tester.py game.py read_board
Running required definitions:
True
True
.....
----------------------------------------------------------------------
Ran 5 tests in 0.000s
OK
5/5 Required test cases passed (worth 1 each)
Score based on test cases: 5.00/90 (5.00*1)
$ python3 tester.py game.py check_vertical
Running required definitions:
True
True
.....
----------------------------------------------------------------------
Ran 5 tests in 0.000s
OK
5/5 Required test cases passed (worth 1 each)
Score based on test cases: 5.00/90 (5.00*1)
$ python3 tester.py game.py check_major_diagonal
Running required definitions:
True
True
.....
----------------------------------------------------------------------
Ran 5 tests in 0.000s
OK
5/5 Required test cases passed (worth 1 each)
Score based on test cases: 5.00/90 (5.00*1)
$ python3 tester.py game.py check_horizontal
Running required definitions:
True
True
.....
----------------------------------------------------------------------
Ran 5 tests in 0.000s
OK
5/5 Required test cases passed (worth 1 each)
Score based on test cases: 5.00/90 (5.00*1)
$ python3 tester.py game.py check_minor_diagonal
Running required definitions:
True
True
.....
----------------------------------------------------------------------
Ran 5 tests in 0.000s
OK
5/5 Required test cases passed (worth 1 each)
Score based on test cases: 5.00/90 (5.00*1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.