Menu

[solved]-Write Class Imports Utilizes Feature Module Create Three Instances Class Familiar Basic Me Q39008347

Write a Class that imports and utilizes some feature of thismodule. Create three instances of that Class. Be familiar with thebasic mechanics and be able to recreate something small.

# connectfour.py

”’

This module contains the game logic that underlies a ConnectFour

game, implementing such functionality as tracking the state of agame,

updating that state as players make moves, and determining ifthere is a

winner. No user interface or network functionality is included;this is

strictly a collection of tools for implementing the gamelogic.

”’

import collections

# These constants specify the concepts of “no player”, the “redplayer”

# and the “yellow player”. Your code should use these constantsin

# place of their hard-coded values. Note that these values arenot

# intended to be displayed to a user; they’re simply intended asa

# universal way to distinguish which player we’re talking about(e.g.,

# which player’s turn it is, or which player (if any) has apiece in a

# particular cell on the board).

NONE = 0

RED = 1

YELLOW = 2

# These constants specify the size of the game board (i.e., thenumber

# of rows and columns it has). It should be possible to changethese

# constants and re-run your program so that the board will havea

# different size; be sure that you’re not hard-coding thesevalues

# anywhere in your own code, because this is something we’llbe

# attempting when we test your program.

BOARD_COLUMNS = 7

BOARD_ROWS = 6

# GameState is a namedtuple that tracks everything importantabout

# the state of a Connect Four game as it progresses. Itcontains

# two fields:

#

# ‘board’, which is a two-dimensional list of valuesdescribing

# the game board. Each value represents one cell on theboard

# and is either NONE, RED, or YELLOW, depending on whetherthe

# cell contains a red piece, a yellow piece, or is empty

#

# ‘turn’, which specifies which player will make the nextmove;

# its value will always be either RED or YELLOW

GameState = collections.namedtuple(‘GameState’, [‘board’,’turn’])

# This is the simplest example of how you create new kinds ofexceptions

# that are specific to your own program. We’ll see later in thequarter

# in more detail what this notation means; in short, we’reintroducing

# new types into our program and stating that they have strongsimilarities

# to the existing type called Exception.

class InvalidMoveError(Exception):

”’Raised whenever an invalid move is made”’

pass

class GameOverError(Exception):

”’

Raised whenever an attempt is made to make a move after the gameis

already over

”’

pass

# The next four functions are the “public” functions that areprovided

# by this module. You’ll need to call all four of thesefunctions, but

# will not need (or want) to call any of the others.

def new_game() -> GameState:

”’

Returns a GameState representing a brand new game in whichno

moves have been made yet.

”’

return GameState(board = _new_game_board(), turn = RED)

def drop(game_state: GameState, column_number: int) ->GameState:

”’

Given a game state and a column number, returns the gamestate

that results when the current player (whose turn it is) drops apiece

into the given column. If the column number is invalid, aValueError

is raised. If the game is over, a GameOverError is raised. If amove

cannot be made in the given column because the column is filledalready,

an InvalidMoveError is raised.

”’

_require_valid_column_number(column_number)

_require_game_not_over(game_state)

empty_row = _find_bottom_empty_row_in_column(game_state.board,column_number)

if empty_row == -1:

raise InvalidMoveError()

else:

new_board = _copy_game_board(game_state.board)

new_board[column_number][empty_row] = game_state.turn

new_turn = _opposite_turn(game_state.turn)

return GameState(board = new_board, turn = new_turn)

def pop(game_state: GameState, column_number: int) ->GameState:

”’

Given a game state and a column number, returns the game statethat

results when the current player (whose turn it is) pops a piecefrom the

bottom of the given column. If the column number is invalid, aValueError

is raised. If the game is over, a GameOverError is raised. If apiece

cannot be popped from the bottom of the given column because thecolumn

is empty or because the piece at the bottom of the columnbelongs to the

other player, an InvalidMoveError is raised.

”’

_require_valid_column_number(column_number)

_require_game_not_over(game_state)

if game_state.turn == game_state.board[column_number][BOARD_ROWS- 1]:

new_board = _copy_game_board(game_state.board)

for row in range(BOARD_ROWS – 1, -1, -1):

new_board[column_number][row] = new_board[column_number][row -1]

new_board[column_number][row] = NONE

new_turn = _opposite_turn(game_state.turn)

return GameState(board = new_board, turn = new_turn)

else:

raise InvalidMoveError()

def winner(game_state: GameState) -> int:

”’

Determines the winning player in the given game state, ifany.

If the red player has won, RED is returned; if the yellowplayer

has won, YELLOW is returned; if no player has won yet, NONEis

returned.

”’

winner = NONE

for col in range(BOARD_COLUMNS):

for row in range(BOARD_ROWS):

if _winning_sequence_begins_at(game_state.board, col, row):

if winner == NONE:

winner = game_state.board[col][row]

elif winner != game_state.board[col][row]:

# This handles the rare case of popping a piece

# causing both players to have four in a row;

# in that case, the last player to make a move

# is the winner.

return _opposite_turn(game_state.turn)

return winner

# Modules often contain functions, variables, or classes whosenames begin

# with underscores. This is no accident; in Python, this is theagreed-upon

# standard for specifying functions, variables, or classes thatshould be

# treated as “private” or “hidden”. The rest of your programshould not

# need to call any of these functions directly; they are utilityfunctions

# called by the functions above, so that those functions can beshorter,

# simpler, and more readable.

def _new_game_board() -> [[int]]:

”’

Creates a new game board. Initially, a game board has thesize

BOARD_COLUMNS x BOARD_ROWS and is comprised only of integerswith the

value NONE

”’

board = []

for col in range(BOARD_COLUMNS):

board.append([])

for row in range(BOARD_ROWS):

board[-1].append(NONE)

return board

def _copy_game_board(board: [[int]]) -> [[int]]:

”’Copies the given game board”’

board_copy = []

for col in range(BOARD_COLUMNS):

board_copy.append([])

for row in range(BOARD_ROWS):

board_copy[-1].append(board[col][row])

return board_copy

def _find_bottom_empty_row_in_column(board: [[int]],column_number: int) -> int:

”’

Determines the bottommost empty row within a given column,useful

when dropping a piece; if the entire column in filled withpieces,

this function returns -1

”’

for i in range(BOARD_ROWS – 1, -1, -1):

if board[column_number][i] == NONE:

return i

return -1

def _opposite_turn(turn: str) -> str:

”’Given the player whose turn it is now, returns the oppositeplayer”’

if turn == RED:

return YELLOW

else:

return RED

def _winning_sequence_begins_at(board: [[int]], col: int, row:int) -> bool:

”’

Returns True if a winning sequence of pieces appears on theboard

beginning in the given column and row and extending in any ofthe

eight possible directions; returns False otherwise

”’

return _four_in_a_row(board, col, row, 0, 1)

or _four_in_a_row(board, col, row, 1, 1)

or _four_in_a_row(board, col, row, 1, 0)

or _four_in_a_row(board, col, row, 1, -1)

or _four_in_a_row(board, col, row, 0, -1)

or _four_in_a_row(board, col, row, -1, -1)

or _four_in_a_row(board, col, row, -1, 0)

or _four_in_a_row(board, col, row, -1, 1)

def _four_in_a_row(board: [[int]], col: int, row: int, coldelta:int, rowdelta:

int) -> bool:

”’

Returns True if a winning sequence of pieces appears on theboard

beginning in the given column and row and extending in adirection

specified by the coldelta and rowdelta

”’

start_cell = board[col][row]

if start_cell == NONE:

return False

else:

for i in range(1, 4):

if not _is_valid_column_number(col + coldelta * i)

or not _is_valid_row_number(row + rowdelta * i)

or board[col + coldelta *i][row + rowdelta * i] !=start_cell:

return False

return True

def _require_valid_column_number(column_number: int) ->None:

”’Raises a ValueError if its parameter is not a valid columnnumber”’

if type(column_number) != int or not_is_valid_column_number(column_number):

raise ValueError(‘column_number must be int between 0 and

{}’.format(BOARD_COLUMNS – 1))

def _require_game_not_over(game_state: GameState) ->None:

”’

Raises a GameOverError if the given game state represents asituation

where the game is over (i.e., there is a winning player)

”’

if winner(game_state) != NONE:

raise GameOverError()

def _is_valid_column_number(column_number: int) -> bool:

”’Returns True if the given column number is valid; returnsFalse otherwise”’

return 0 <= column_number < BOARD_COLUMNS

def _is_valid_row_number(row_number: int) -> bool:

”’Returns True if the given row number is valid; returns Falseotherwise”’

return 0 <= row_number < BOARD_ROWS

Expert Answer


Answer to Write a Class that imports and utilizes some feature of this module. Create three instances of that Class. Be familiar w… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *