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
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:
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 XOXOOPlayer 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 V: diagonal winner(board) This function scans the entire game board passed as an argument and returns one of three values:
1 if player 1 has placed four consecutive Xs in a diagonal somewhere in the board 2 if player 2 has placed four consecutive Os in a diagonal somewhere in the board 0 (zero) if neither player has placed four consecutive pieces in a diagonal somewhere in the board
Under no circumstances is the function permitted to make a change to board. Note that diagonals can run lower-left-to-upper-right and upper-left-to-lower-right, as shown in the examples
below:
...... ............ ..........X. ...... ...XO. .X.O.. ..XOX. .XXO.. .XOXO. .OXX.. OOXOOX OOOXXOPart VI: drop bomb(board, col num)
This function drops a bomb down the given column of the board, replacing all Xs and Os 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.
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): if col_num or col_num > board._num_cols: return -1 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 return -1 # PART III # Determines if a player has won the game by putting four pieces next to each other in a single row. # Returns 1 if player 1 has won the game by placing four X's contiguously in a single row. # Returns 2 if player 2 has won the game by placing four O's contiguously in a single row. # Returns 0 if no one has won yet by placing pieces in this manner. # This function must not modify the contents of the game board. def horizontal_winner(board): for i in range(0, board._num_rows): count1 = 0 count2 = 0 for j in range(0, board._num_cols): if board._slots[i][j] == 'X': count1 += 1 count2 = 0 elif board._slots[i][j] == 'O': count2 += 1 count1 = 0 if count1 == 4 and count2 == 0: return 1 elif count2 == 4 and count1 == 0: return 2 return 0 # PART IV # Determines if a player has won the game by putting four pieces on top of each other in a single column. # Returns 1 if player 1 has won the game by placing four X's contiguously in a single column. # Returns 2 if player 2 has won the game by placing four O's contiguously in a single column. # Returns 0 if no one has won yet by placing pieces in this manner. # This function must not modify the contents of the game board. def vertical_winner(board): for j in range(0, board._num_cols): count1 = 0 count2 = 0 for i in range(0, board._num_rows): if board._slots[i][j] == 'X': count1 += 1 count2 = 0 elif board._slots[i][j] == 'O': count2 += 1 count1 = 0 if count1 == 4 and count2 == 0: return 1 elif count2 == 4 and count1 == 0: return 2 return 0 # PART V # Determines if a player has won the game by placing four pieces in a diagonal. # Returns 1 if player 1 has won the game by placing four X's diagonally. # Returns 2 if player 2 has won the game by placing four O's diagonally. # Returns 0 if no one has won yet by placing pieces in this manner. # This function must not modify the contents of the game board. def diagonal_winner(board): return None # 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): 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
Get step-by-step solutions from verified subject matter experts
