Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE CODE IN PYTHON Part II: drop piece(board, col num, player) This function drops a piece into a column of the board. If player is

PLEASE CODE IN PYTHON

Part II: drop piece(board, col num, player)

This function drops a piece into a column of the board. If player is 1, then the function drops an X down the column. Otherwise, if player is 2, then the function drops an O down the column. The piece falls until it lands in the lowest unoccupied slot in that column, and the function returns the number of the row that the piece landed in (where the bottommost row is row #1). Note that the returned row number should be in the range [1, board. num rows], not [0, board. num rows-1]

Note that the value of col num should be in the range [1, board. num cols]. If not, the function returns -1. If the column is completely filled with pieces, the function returns -1.

In cases whether an invalid move is attempted, the contents of board must be left unchanged.

Example: Before and after drop piece(board, 4, 1):

...... : ......

...... : ......

...... : ......

...... : ......

...... : ...X..

..OO.. : ..OO..

XXXOOX : XXXOOX

drop piece(board, 4, 1) for this example will return 3 because the piece landed in row #3

PROVIDED BELOW IS THE BOARD CLASS

# The Board class represents the game board. # From the player's perspective, the rows are numbered 1 through num_rows, # and the columns are numbered 1 through num_cols. Internally, however # the actual slots that hold the pieces are indexed using 0 through num_rows-1 # and 0 through num_cols-1. # DO NOT MODIFY THE Board CLASS IN ANY WAY! class Board: def __init__(self, num_rows, num_cols): self._num_rows = num_rows self._num_cols = num_cols self._slots = [] for i in range(0, num_rows): self._slots.append([]) for j in range(0, num_cols): self._slots[-1].append('.') # DO NOT MODIFY THE Board CLASS IN ANY WAY! # Displays the board. Note that slot [0][0] is in the bottom-left corner of the board. # [num_rows-1][num_cols-1] is the position of the slot in the upper-right corner def display_board(board): for i in range(board._num_rows-1, -1, -1): row = ''.join(board._slots[i]) print(row) 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Databases With PostgreSQL From Novice To Professional

Authors: Richard Stones, Neil Matthew

2nd Edition

1590594789, 978-1590594780

More Books

Students also viewed these Databases questions

Question

What is the difference between a finance and an operating lease?

Answered: 1 week ago