Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

Lets explore the Board class a little:

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(.) 

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:

image text in transcribed 

As an example, the game board depicted below:

X...  

would be represented by the following list of lists. Note how the board is represented upside-down relative to how it is printed!

[ [O,X,O,X], [X,X,O,.], [O,O,X,.] [X,.,.,.] ] 

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):

..... ..... ..... O.... OXXXX XOXOO 

Player 2 wins by putting 4 pieces in a column (column #5):

........ ....O... .X..O... OX.XO... OOXOOX.. XOXXXOX. 

Player 1 wins by putting 4 pieces in a diagonal:

............ .X.......... .OX......... .XOXO....... OOOXX....... XXOXO....... 

Player 2 wins by putting 4 pieces in a diagonal:

.......... .......... .......... .......... ...XO..... ...OO..... .XOOX..... XOXXO.X... 

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..... ...OO..... .XOOX..... XOXXO.X... 
.......... .......... ....O..... ....O..... .XO.X..... 

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):

...... ...... 
...... ...... 
...... ...... 
...... ...... 
...... ...X.. 
..OO.. ..OO.. XXXOOX XXXOOX 

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

Given Code

# 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) # PART I # Returns True if the board is completely filled with pieces. def board_full(board): for i in range(0, board._num_rows) for j in range(0, board._num_cols) if board._slots[i][j] == '.': return False  return True  # 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): return None 
# DO NOT MODIFY THE CODE BELOW IN ANY WAY! if __name__ == "__main__": print('Welcome to Connect Four: CSE 101 Edition! ') num_rows = int(input('Enter number of rows in board. Must be >= 4: ')) num_cols = int(input('Enter number of columns in board. Must be >= 4: ')) game_board = Board(num_rows, num_cols) print('Player 1: You are X') print('Player 2: You are O') print('Let\'s start the game!') winner = None  player = 1 # can be 1 or 2  bombs = [1,1] # how many bombs each player has remaining to use   while not board_full(game_board) and winner is None: print(' Player {}\'s turn!'.format(player)) display_board(game_board) col = int(input('Which column do you want to drop a piece into? (1-{}) '.format(game_board._num_cols))) use_bomb = 'n'  if bombs[player-1] > 0: use_bomb = input('Do you want to use your bomb piece? (y) ') if use_bomb.lower() == 'y': col = drop_bomb(game_board, col) if col == -1: print('You can\'t drop a bomb into that column. You lose your bomb and your turn!') bombs[player-1] -= 1 else: row = drop_piece(game_board, col, player) if row == -1: print('You can\'t drop a piece into that column. You lose your turn!') if horizontal_winner(game_board) >= 1 or vertical_winner(game_board) >= 1 or diagonal_winner(game_board): print(' Player {} won the game!'.format(player)) break   player = 3-player # take turns   display_board(game_board) if board_full(game_board): print(' Game Over! It was a tie!') else: print(' Game Over!') 
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]

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

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

More Books