PLEASE CODE IN PYTHON The game board itself is represented using a list of lists
ID: 3830508 • Letter: P
Question
PLEASE CODE IN PYTHON
The game board itself is represented using a list of lists of characters. Each position in the board is called a “slot” and holds either a period, capital letter X, or capital letter O: a ’.’ represents an empty slot in the board, an ’X’ represents a piece for player 1, and an ’O’ represents a piece for player 2. The board has at least 4 rows and 4 columns. The piece in slots[0][0] is in the lower-left corner of the board, whereas the piece in slots[ num rows-1][ num cols-1] is in the upper-right corner of the board:
Players take turns dropping pieces by indicating which column they want to drop the piece into. Valid column numbers entered by the player are in the range 1 through board. num cols, where board refers to a Board object. Play continues until a player has placed four pieces in a single row, column or diagonally.
Finally, in our variant of Connect Four, each player has one special “bomb” piece that can be dropped in one of the columns of the board. Each player has only one bomb. The bomb piece causes all the pieces in the column to be removed. An example is shown below. Before and after dropping a bomb down column 4:
.......... ..........
.......... ..........
...XO..... ....O.....
...OO..... ....O.....
.XOOX..... .XO.X.....
XOXXO.X... XOX.O.X..
Part VI: drop bomb(board, col num) :
This function drops a bomb down the given column of the board, replacing all ’X’s and ’O’s with ’.’ and returning the value col num. If col num is outside the range [1, board. col num], the function instead returns -1. In cases whether an invalid bomb drop is attempted, the contents of board must be left unchanged.
PROVIDED BELOW IS THE BOARD CLASS
Explanation / Answer
# 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
# pastebin code link: https://pastebin.com/2KUuPP9z
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.