155k views
3 votes
General Description You have been chosen to create a version of connect 4. In this version, there can be forbidden positions, or places that neither x nor o can play. However, connecting four together is still the way to win, and this can be done vertically, horizontally, diagonally (or anti-diagonally if you distinguish between the backward diagonal). Required Features 1. You must implement two new game options, one for two players, and one for x player vs computer. a. The player is always x and the computer is always o in that case. b. Player one and two alternate turns. c. Players cannot overwrite each other's moves. d. Players cannot play on forbidden places, and forbidden places do not count for victory. 2. At the start of each game:________. a. Ask the player what game board they want to load. b. Then start with the x player, and alternate. c. Check for victory after each move, not after each pair of moves. d. Players may enter a move, two integers separated by a space, or the words "load game" or "save game" which will either load or save over the current game. 3. You must implement a load game feature. Ask for the file name and load that file. If a game is currently in progress, overwrite that game and immediately start on the loaded game. 4. You must implement a save game feature. Ask for the name that you wish to save to, and save the file to that name. 5. Detect when one or the other player has adjoined the spheres (connected four). a. Display a message with the winning player. b. End that game. c. Go back to the main menu. d. If the board is full, then that is a tie. Design Document There is no design document for this project. It has been replaced with a testing script. Any questions about design documents will be ignored. Required names and Interface Your project should be in proj2.py The design of project 2 is mostly up to you, but we will require that your project create a class: class Adjoin TheSpheres: This must have a method whose definition is: def main menu self):

User Bddckr
by
6.2k points

1 Answer

2 votes

Answer:

See explaination

Step-by-step explanation:

from IPython.display import display, HTML, clear_output

import random

import time

# Game Constants

ROWS = 6

COLUMNS = 7

PIECE_NONE = ' '

PIECE_ONE = 'x'

PIECE_TWO = 'o'

PIECE_COLOR_MAP = {

PIECE_NONE : 'white',

PIECE_ONE : 'black',

PIECE_TWO : 'red',

}

DIRECTIONS = (

(-1, -1), (-1, 0), (-1, 1),

( 0, -1), ( 0, 1),

( 1, -1), ( 1, 0), ( 1, 1),

)

# Board Functions

def create_board(rows=ROWS, columns=COLUMNS):

''' Creates empty Connect 4 board '''

board = []

for row in range(rows):

board_row = []

for column in range(columns):

board_row.append(PIECE_NONE)

board.append(board_row)

return board

# Copy board

def copy_board(board):

''' Return a copy of the board '''

rows = len(board)

columns = len(board[0])

copied = create_board(rows, columns)

for row in range(rows):

for column in range(columns):

copied[row][column] = board[row][column]

return copied

def print_board(board):

''' Prints Connect 4 board '''

for row in board:

print('|' + '|'.join(row) + '|')

def drop_piece(board, column, piece):

''' Attempts to drop specified piece into the board at the

specified column

If this succeeds, return True, otherwise return False.

'''

for row in reversed(board):

if row[column] == PIECE_NONE:

row[column] = piece

return True

return False

def find_winner(board, length=4):

''' Return whether or not the board has a winner '''

rows = len(board)

columns = len(board[0])

for row in range(rows):

for column in range(columns):

if board[row][column] == PIECE_NONE:

continue

if check_piece(board, row, column, length):

return board[row][column]

return None

def check_piece(board, row, column, length):

''' Return whether or not there is a winning sequence starting from

this piece

'''

rows = len(board)

columns = len(board[0])

for dr, dc in DIRECTIONS:

found_winner = True

for i in range(1, length):

r = row + dr*i

c = column + dc*i

if r not in range(rows) or c not in range(columns):

found_winner = False

break

if board[r][c] != board[row][column]:

found_winner = False

break

if found_winner:

return True

return False

# HTML/SVG Functions

def display_html(s):

''' Display string as HTML '''

display(HTML(s))

def create_board_svg(board, radius):

''' Return SVG string containing graphical representation of board '''

rows = len(board)

columns = len(board[0])

diameter = 2*radius

svg = '<svg height="{}" width="{}">'.format(rows*diameter, columns*diameter)

svg += '<rect width="100%" height="100%" fill="blue"/>'

for row in range(rows):

for column in range(columns):

piece = board[row][column]

color = PIECE_COLOR_MAP[piece]

cx = column*diameter + radius

cy = row*diameter + radius

svg += '<circle cx="{}" cy="{}" r="{}" fill="{}"/>'.format(cx, cy, radius*.75, color)

svg += '</svg>'

return svg

User Neil Smithline
by
7.0k points