Question: def drop _ disc _ one _ square ( board , x , y ) : def can _ disc _ fall ( board ,

def drop_disc_one_square(board, x, y): def can_disc_fall(board, x, y):
""!"
Given board and x,y location.
Assume }x,y\mathrm{ is in bounds and contains a disc.
Is moving the disc down one square ok?
Return True if the disc can fall, or False otherwise.
Disc can fall if the square exactly one space downward is in bounds and empty.Out-of-bounds (O0B) testsboard =[['y']]Falseboard =[[None,'r', None],[None, None, 'y']]TrueFalseboard =[[None, None],['r','y']]False
"""
# TODO: What y-location should you check is in bounds and doesn't already have a disc?
return False def update_whole_board(board):
"""
Given board, do one round of falling discs over the whole board.
If there is no disc at a location, no need to change the board.
If there is a disc, check if it can fall, then drop the disc,
one square at a time. Call your helper functions from above!
Return the board in all cases.
(tests provided, code TBD)update_whole_board(board)
[['y','r','y'],[None, None, 'r'],[None,'y', None]][['y','r','y'],[None, None, 'r'],[None, None, 'y']]
""!for y in reversed(range(len(board[0]))):
for x in range(len(board)):pass
return board
def check_horizontal_win(board, player):
""!"
Takes in the board and a player's token ('y' for player 1
or 'r' for player 2).
Returns True if the player has 4 tokens in a row horizontally
anywhere on the board and False if otherwise.
""!"
for y in range(len(board[0])):
for x in range(len(board)-3):pass
return False def check_vertical_win(board, player):
""!
Takes in the board and a player's token ('y' for player 1
or 'r' for player 2).
Returns True if the player has 4 tokens in a row vertically
anywhere on the board and False if otherwise.
""!
for y in range(len(board[0])-3):
for x in range(len(board)):pass
return False
def check_diagonal_wins(board, player):
"*"!
PROVIDED (DO NOT EDIT)
Takes in a board and a player's token.
Check if that player wins by getting 4-in-a-row in two diagonal
directions, top-left-to-down-right and bottom-left-to-top-right.
""!for y in range(len(board[0])-3):
for x in range(len(board)-3):
if (
board[x][y]== player
and board[x+1][y+1]== player
and board[x+2][y+2]== player
and board[x+3][y+3]== player
:
return Truefor y in range(len(board[0])-3):
for x in range(3, len(board)):
if (
board[x][y]== player
and board[x-1][y+1]== player
and board[x-2][y+2]== player
and board[x-3][y+3]== player
:
return Truedef check_win(board, player):
"!"
Takes in a board and a player's token.
Check if that player wins by calling each helper boolean function.
Return True if the player wins in any of the 3 ways, False if not.
"\prime"!Hint: this is where decomposition helps with readability!
is_player_diagonal_winner = check_diagonal_wins(board, player)
return is_player_diagonal_winner
def is_full(board):
"\prime\prime\prime
PROVIDED (DO NOT EDIT)
Take in a board and return True if all the slots are filled.
Ignores the top row since that is invisible and not treated as the board.
"\prime")
for y in range(1, len(board[0])):
for x in range(len(board)):
if board[x][y] is None:
return False
return True
"!"
Given board and x,y location containing a disc.
Move the disc one square downwards and return the resulting board.
Assume that this is a legal move: all coordinates are in
bounds, the location contains a disc, and the square below is empty.
(i.e. a different function checks that this is a
legal move before drop_disc_one_square() is called)
Note: the board is a list of (equally-lengthed) lists. Each inner list
represents one whole column in the board. For example, board[0]
is the entire first column in the Connect4 board, from top to bottom.
(tests provided, code TBD)drop_disc_one_square(board,1,0)
[[None,'r','y'],[None,'y', None]]board =[['r','y', None],[None,'y', None]][['r','y', None],[None, None, 'y']]
""!
disc = board[x][y] # is either 'y' for Player Yellow or 'r' for Player Redcurrent location and write it again at the square below?
return board
def drop _ disc _ one _ square ( board , x , y )

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!