Python Overview For this assignment you will implement a variant of the game Con
ID: 3825578 • Letter: P
Question
Python
Overview
For this assignment you will implement a variant of the game Connect Four on game boards of arbitrary dimensions. (The classic game is played on a 6-row-by-7-column board.) Some basic code has been provided to you which depends on seven functions, six of which you will write for this assignment.
Let’s explore the Board class a little:
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:
As an example, the game board depicted below:
would be represented by the following list of lists. Note how the board is represented “upside-down” relative to how it is printed!
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. Some example winning configurations are shown below:
Player 1 wins by putting 4 pieces in a row (row #2):
Player 2 wins by putting 4 pieces in a column (column #5):
Player 1 wins by putting 4 pieces in a diagonal:
Player 2 wins by putting 4 pieces in a diagonal:
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:
Functions to Write
Your task now is to write the following functions. You will note that every function takes a Board object as its first argument. Your functions must NOT rely on any kind of global variables whatsoever. Although you are free to write helper functions, each function must otherwise be entirely self-contained and not depend on outside variables (i.e., global variables) to work properly. When your functions are graded by the grading system, global variables will be automatically stripped out of your submission.
Your functions may assume that the board arguments always represent a valid game.
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):
drop piece(board, 4, 1) for this example will return 3 because the piece landed in row #3.
Given Code
num rows-1 [0] num rows-1] [1] num rows-1 num cols-11 num rows-2] [0] num rows-2] [1] num rows 2 num cols-1] [2] [1] [2] [0] [2] num cols-1] [1] num cols [1] [0] [1] [1] [0] num cols-1]Explanation / Answer
# PART II
# Drops a piece in the board into the desired column number.
# If player is 1, drop an 'X'. If player is 2, drop an 'O'. (letter Oh).
# Returns the number of the row that the piece landed in (where the bottommost row is row #1).
# If the game piece can be dropped, return the row number where the piece landed, otherwise return -1.
# 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_piece(board, col_num, player):
if col_num < 1 or col_num > board._num_cols:
return -1
# when a piece is dropped from top, it will land at the bottom most row with a free cell in
# the given column, so checking start from bottom most row
for i in range(0, board._num_rows):
if board._slots[i][col_num-1] == '.':
if player == 1:
board._slots[i][col_num-1] = 'X'
else:
board._slots[i][col_num-1] = 'O'
return i+1
# if column is completely filled already
return -1
# Code link: https://pastebin.com/HJPU7t0t
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.