Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python project, really need some help thank you! skeleton code below, followed by instructions. ''' project 07 REPLACE WITH HEAD COMMENT HERE ''' pieces =

Python project, really need some help thank you!

skeleton code below, followed by instructions.

''' project 07 REPLACE WITH HEAD COMMENT HERE ''' pieces = {'black':'b', 'white':'w'} COLUMN = 7 ROW = 6 def initialize(): ''' REPLACE WITH DOCSTRING ''' pass def choose_color(): ''' REPLACE WITH DOCSTRING ''' pass def board_display(board): ''' REPLACE WITH DOCSTRING ''' print("Current board:") C = COLUMN R = ROW hline = ' ' + (' ' * 2) + ('+---' * C) + '+' + ' ' numline = ' '.join([(' ' + str(i) + ' ') \ for i in range(1, C + 1)]) str_ = (' ' * 3) + numline + hline for r in range(0, R): str_ += str(r+1) + ' |' for c in range(0, C): str_ += ' ' + \ (str(board[r][c]) \ if board[r][c] is not 0 else ' ') + ' |' str_ += hline print (str_) def drop_disc(board, column, color): ''' REPLACE WITH DOCSTRING ''' pass def check_disc(board, row, column): ''' REPLACE WITH DOCSTRING ''' pass def is_game_over(board): ''' REPLACE WITH DOCSTRING ''' pass def main(): ''' REPLACE WITH DOCSTRING ''' banner = """ ____ ___ _ _ _ _ _____ ____ _____ _ _ / ___/ _ \| \ | | \ | | ____/ ___|_ _| || | | | | | | | \| | \| | _|| | | | | || |_ | |__| |_| | |\ | |\ | |__| |___ | | |__ _| \____\___/|_| \_|_| \_|_____\____| |_| |_| """ intro = """ Connect Four is a two-player connection game in which the players first choose a color and \ then take turns dropping one colored disc from the top into a seven-column, six-row vertically suspended grid. \ The pieces fall straight down, occupying the lowest available space within the column. \ The objective of the game is to be the first to form a horizontal, vertical, or diagonal line of four of one's own discs. """ usage = """ Usage: pass: give up, admit defeat exit: exit the game i: drop a disk into column i """ print(banner) print(intro) print(usage) continue_game = 'yes' # can't use "continue" because it has a special meaning while continue_game == 'yes': # YOUR CODE GOES HERE continue_game = input("Would you like to play again? ").lower() else: print(" Thanks for playing! See you again soon!") if __name__ == "__main__": main()

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

CSE 231 Spring 2019 Programming Project 07 This assignment is worth 50 points (5% of the course grade) and must be completed and turned in before 11:59 PM on Monday, March 25, 2019 Assignment Overview This assignment will give you more experience on the use of: 1. control 2. iteration 3. lists 4. functions The goal of this project is to play a game of CONNECT4 Assignment Background Connect Four is a two-player connection game in which the players first choose a color and then take turns dropping one colored disc from the top into a seven-column, six-row vertically suspended grid (wikipedia.org). The pieces fall straight down, occupying the lowest available space within the column. The objective of the game is to be the first to form a horizontal, vertical, or diagonal line of four of one's own discs. Connect Four is a solved game: the first player can always win by playing the right moves. You can play online here ttps://www.mathsisfun.com/games/conne Project Descriptiorn Your program must meet the following specifications The program begins by offering a welcome message and displays the rules of the game. Then, the player is asked to choose a color. That control is provided in the starting code provided. Here is what you need to add 1. 2. Initialize board to all zeros You will need a while loop to play the game (this will be in addition to the while loop in the provided code that is used to ask whether the human player wants to play another game.) What Boolean expression should control this loop? 3. Play alternates between the two players. Use a Boolean variable to keep track of who is playing The player who first forms a horizontal, vertical, or diagonal line of four of one's own discs is the winner 4. 5. When the game has come to its conclusion, the program should output whether playerl or player2 won. It then should ask the user whether to play again. (And if the user selects to play again, your program should.) 6. If the user chooses not to play the game again, the program sh ould display a goodbye message 7. Your program should verify that each of the user's moves is valid. (That is, the column must be legal, the user must choose between 1 and 7 stones, and the column must not be full.) If the user's move is illegal, your program should repeatedly print a sensible error message and get a new input, until the user selects a legal move Project Specifications You need to complete the functions in the proj07.py file (along with code and function headers and comments), where all the game-play logic and mechanisms will emulate the steps described previously If you run the program (proj07.py), the game will start as follows Connect Four is a two-player connection game in which the players first choose a color and then take turns dropping one colored disc from the top into a seven-column, six-row vertically suspended grid. The pieces fall straight down, occupying the lowest available space within the colmn. The objective of the game is to be the first to form a horizontal, vertical, or diagonal line of four of one's own discs. Usage: pass: give up, admit defeat exit: exit the game drop a disk into column i Pick a color: white You are 'white and your opponent is 'black Current board: 1 2 345 67 white's turn4 Current board: 1 234 5 67 black's turn> 1. def initialize () This function takes no arguments. It returns a list of lists that represents the game board using COLUMN = 7 and ROW = 6" The format of the list is: board -[ rowO], rowl],.. (Hint: start with an empty list, then create an empty row, and append that row. Repeat until all the rows are in the list. If you wish, you can use list comprehension to create the list in one line. Alternatively, you can simply create a list of lists with zeros by hand-not elegant, but there are 42 zeros so it is doable in this case because the size is fixed.) 2. def choose color ): This function asks for a color inside a loop until a valid color name, black or white, is entered. If a wrong color or an arbitrary string color is entered, will print an error message. Once it receives a correct color assignment, it will store those color values into two variables and return them with player's color first and opponent's color second. After the function returns, in main) print a message as shown here. A player should be able to enter a correct color in any mix of case, but the returned color pairs must be lower case. Choose_color() Pick a color redl 1 When a pair of values is returned it is actually returned as a Python "tuple", a data type we learn about next. Wrong color, type only 'black' or 'white', try again Pick a color: white You are 'white' and your opponent is 'black'. 3. def board display (board): This function displays the board. It takes one argument: the list of lists that represents the board. In our case the discs will be denoted by b and w when displayed. We provide this function. 4. def drop disc (board, column, color): This function attempts to drop specified disk color (b' or 'w) into the board at the specified column. If successful, after modifying the board, return the modified row; if the specified column is full, return "full", otherwise return None. Note that if the specified column is empty, the disc should be dropped into the last row (i.e. bottom row) of the column 1 2345 7 1 If we call this function on the board given in the Figure above, then we will get: drop_disc (board, 3, 'black') This column is full. Please try again > return 'full' drop_disc (board, 8, 'black ) Column should be in range 1 to 7. Please try again. return None drop-disc (board, 7, 'black'> -) return 6 # and updates board dropdisc (board, 2, 'black') return 4 # and updates board - 5. def check disc (board, row, column) This function returns True if there is a winning sequence containing the disc at position (row, column); returns None if row or column are out of range; returns Ealse otherwise If we call this function on the board given in the Figure above, then we will get: check-disc [board, 3,5) Falge check disc[board, 6,2) True check disc(board, 4,4) > True check disc(board, 5, 4) > True check disc(board, 3,2) > True checkdisc(board, 2, 1) Falge check_disc (board, 1,1) >False check disc[board,0,1) None check disc[board, 3,6) True checkdisc[board, 5,3) False check disc[board, 4,1) True check-disc(board, 5, 2) False # highlighted in red above - - # (5,2) in't in the winning equence Note: testing for sequences of four on diagonals is harder than testing for the other criteria so we provide separate Mimir tests where one test does not test diagonals. The key to solving this function lies in the coordinates of the rows, columns and diagonals. Designing loops to test along rows and columns is relatively easy. For diagonals because the board is fixed at 6x7 it is small and some diagonals do not need to be considered (why?), you have a choice: (1) design loops to generate the correct coordinates or (2) make lists of coordinates. The fact that the problem specification fixes the board size to 6x7 you have the freedom to do option (2), i.e. list the possibilities. 6. def is game over (board) This function takes one argument: the board. Returns the color of the winner black, or ,hite, ) f one exists, or 'draw' if board is full, otherwise return None. Begin with a call to check_disc. . Case1 1 234 5 67 black's turn 11 Invalid column: 1 exit Thanks for playing! See you again soon! Test 2 Connect Four 1s two-player connection game in which the players first choose a color and into a seven-column, six-row vertically suspended grid. straight down, occupying the lowest available space within the colimn. The objective of the game is to be the first to form a horizontal, vertical, or diagonal line of four of one's own discs. then take turns dropping one colored disc from the top The pieces fall Usage: pass: give up, admit defeat exit: exit the game drop a disk into column i Pick a color: white You are White, and your opponent is 'black'. Current board: 1 2345 67 white's turn > 4 Current board: 1 2 34567 black's turn>7 Current board: 1 234 5 6 7 white's turn 7 Current board: 1 2 34567 black's turn:7 Current board: 1 234 5 67 white's turn7 Current board: 1 2345 67 black's turn> 5 Current board: 1 2 34567 white's turn>5 Current board: black's turn6 Current board: 1 234 5 67 white's turn7 Current board: 1 2 345 7 black's turn:6 Current board: 1 234 5 67 white's turn 6 Current board: 1 2 345 7 white wins! Would you like to play again? no Thanks for playing! See you again soon! Grading Rubric Computer Project #7 Scoring Summary General Requi rements: (5 pts) (descriptive comments, mnemonic identifiers, format, etc.. .) Coding Standard 1-9 Implementations: (4 pts) (9 pts) initialize () drop_disc (board, column, turn) (14 pts) ck_disc (board, row, column) (5 pts) (6 pts) (7 pts) 7 pts for handling all but diagonal cases 7 pts for handling diagonal cases is_game_over [board, connected, turn) Test 1 Test 2 CSE 231 Spring 2019 Programming Project 07 This assignment is worth 50 points (5% of the course grade) and must be completed and turned in before 11:59 PM on Monday, March 25, 2019 Assignment Overview This assignment will give you more experience on the use of: 1. control 2. iteration 3. lists 4. functions The goal of this project is to play a game of CONNECT4 Assignment Background Connect Four is a two-player connection game in which the players first choose a color and then take turns dropping one colored disc from the top into a seven-column, six-row vertically suspended grid (wikipedia.org). The pieces fall straight down, occupying the lowest available space within the column. The objective of the game is to be the first to form a horizontal, vertical, or diagonal line of four of one's own discs. Connect Four is a solved game: the first player can always win by playing the right moves. You can play online here ttps://www.mathsisfun.com/games/conne Project Descriptiorn Your program must meet the following specifications The program begins by offering a welcome message and displays the rules of the game. Then, the player is asked to choose a color. That control is provided in the starting code provided. Here is what you need to add 1. 2. Initialize board to all zeros You will need a while loop to play the game (this will be in addition to the while loop in the provided code that is used to ask whether the human player wants to play another game.) What Boolean expression should control this loop? 3. Play alternates between the two players. Use a Boolean variable to keep track of who is playing The player who first forms a horizontal, vertical, or diagonal line of four of one's own discs is the winner 4. 5. When the game has come to its conclusion, the program should output whether playerl or player2 won. It then should ask the user whether to play again. (And if the user selects to play again, your program should.) 6. If the user chooses not to play the game again, the program sh ould display a goodbye message 7. Your program should verify that each of the user's moves is valid. (That is, the column must be legal, the user must choose between 1 and 7 stones, and the column must not be full.) If the user's move is illegal, your program should repeatedly print a sensible error message and get a new input, until the user selects a legal move Project Specifications You need to complete the functions in the proj07.py file (along with code and function headers and comments), where all the game-play logic and mechanisms will emulate the steps described previously If you run the program (proj07.py), the game will start as follows Connect Four is a two-player connection game in which the players first choose a color and then take turns dropping one colored disc from the top into a seven-column, six-row vertically suspended grid. The pieces fall straight down, occupying the lowest available space within the colmn. The objective of the game is to be the first to form a horizontal, vertical, or diagonal line of four of one's own discs. Usage: pass: give up, admit defeat exit: exit the game drop a disk into column i Pick a color: white You are 'white and your opponent is 'black Current board: 1 2 345 67 white's turn4 Current board: 1 234 5 67 black's turn> 1. def initialize () This function takes no arguments. It returns a list of lists that represents the game board using COLUMN = 7 and ROW = 6" The format of the list is: board -[ rowO], rowl],.. (Hint: start with an empty list, then create an empty row, and append that row. Repeat until all the rows are in the list. If you wish, you can use list comprehension to create the list in one line. Alternatively, you can simply create a list of lists with zeros by hand-not elegant, but there are 42 zeros so it is doable in this case because the size is fixed.) 2. def choose color ): This function asks for a color inside a loop until a valid color name, black or white, is entered. If a wrong color or an arbitrary string color is entered, will print an error message. Once it receives a correct color assignment, it will store those color values into two variables and return them with player's color first and opponent's color second. After the function returns, in main) print a message as shown here. A player should be able to enter a correct color in any mix of case, but the returned color pairs must be lower case. Choose_color() Pick a color redl 1 When a pair of values is returned it is actually returned as a Python "tuple", a data type we learn about next. Wrong color, type only 'black' or 'white', try again Pick a color: white You are 'white' and your opponent is 'black'. 3. def board display (board): This function displays the board. It takes one argument: the list of lists that represents the board. In our case the discs will be denoted by b and w when displayed. We provide this function. 4. def drop disc (board, column, color): This function attempts to drop specified disk color (b' or 'w) into the board at the specified column. If successful, after modifying the board, return the modified row; if the specified column is full, return "full", otherwise return None. Note that if the specified column is empty, the disc should be dropped into the last row (i.e. bottom row) of the column 1 2345 7 1 If we call this function on the board given in the Figure above, then we will get: drop_disc (board, 3, 'black') This column is full. Please try again > return 'full' drop_disc (board, 8, 'black ) Column should be in range 1 to 7. Please try again. return None drop-disc (board, 7, 'black'> -) return 6 # and updates board dropdisc (board, 2, 'black') return 4 # and updates board - 5. def check disc (board, row, column) This function returns True if there is a winning sequence containing the disc at position (row, column); returns None if row or column are out of range; returns Ealse otherwise If we call this function on the board given in the Figure above, then we will get: check-disc [board, 3,5) Falge check disc[board, 6,2) True check disc(board, 4,4) > True check disc(board, 5, 4) > True check disc(board, 3,2) > True checkdisc(board, 2, 1) Falge check_disc (board, 1,1) >False check disc[board,0,1) None check disc[board, 3,6) True checkdisc[board, 5,3) False check disc[board, 4,1) True check-disc(board, 5, 2) False # highlighted in red above - - # (5,2) in't in the winning equence Note: testing for sequences of four on diagonals is harder than testing for the other criteria so we provide separate Mimir tests where one test does not test diagonals. The key to solving this function lies in the coordinates of the rows, columns and diagonals. Designing loops to test along rows and columns is relatively easy. For diagonals because the board is fixed at 6x7 it is small and some diagonals do not need to be considered (why?), you have a choice: (1) design loops to generate the correct coordinates or (2) make lists of coordinates. The fact that the problem specification fixes the board size to 6x7 you have the freedom to do option (2), i.e. list the possibilities. 6. def is game over (board) This function takes one argument: the board. Returns the color of the winner black, or ,hite, ) f one exists, or 'draw' if board is full, otherwise return None. Begin with a call to check_disc. . Case1 1 234 5 67 black's turn 11 Invalid column: 1 exit Thanks for playing! See you again soon! Test 2 Connect Four 1s two-player connection game in which the players first choose a color and into a seven-column, six-row vertically suspended grid. straight down, occupying the lowest available space within the colimn. The objective of the game is to be the first to form a horizontal, vertical, or diagonal line of four of one's own discs. then take turns dropping one colored disc from the top The pieces fall Usage: pass: give up, admit defeat exit: exit the game drop a disk into column i Pick a color: white You are White, and your opponent is 'black'. Current board: 1 2345 67 white's turn > 4 Current board: 1 2 34567 black's turn>7 Current board: 1 234 5 6 7 white's turn 7 Current board: 1 2 34567 black's turn:7 Current board: 1 234 5 67 white's turn7 Current board: 1 2345 67 black's turn> 5 Current board: 1 2 34567 white's turn>5 Current board: black's turn6 Current board: 1 234 5 67 white's turn7 Current board: 1 2 345 7 black's turn:6 Current board: 1 234 5 67 white's turn 6 Current board: 1 2 345 7 white wins! Would you like to play again? no Thanks for playing! See you again soon! Grading Rubric Computer Project #7 Scoring Summary General Requi rements: (5 pts) (descriptive comments, mnemonic identifiers, format, etc.. .) Coding Standard 1-9 Implementations: (4 pts) (9 pts) initialize () drop_disc (board, column, turn) (14 pts) ck_disc (board, row, column) (5 pts) (6 pts) (7 pts) 7 pts for handling all but diagonal cases 7 pts for handling diagonal cases is_game_over [board, connected, turn) Test 1 Test 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions