Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

http://www.cse.msu.edu/~cse231/Online/Projects/Project07/ CSE 231 Spring 2019 Programming Project 07 This assignment is worth 50 points (5% of the course grade) and must be completed and turned

http://www.cse.msu.edu/~cse231/Online/Projects/Project07/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 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 https://www.mathsisfun.com/games/connect4.html Project Description 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 . Initialize board to all zeros 2. 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. white's turn :> 4 Current board 1 23 45 67 black's turn> 1. def initialize O This function takes no arguments. It returns a list of lists that represents the game board using cOLUMN7 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, it will print an error message. Once it receives a correct color assignment, t will store those color values into two variables and return them with player's color first and opponent's color second1. 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 returmed color pairs must be lower case. Choose color Pick a color: red 1When 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 23 45 67 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 retur ful1' drop_disc (board, 8, 'black') Column should be in range 1 to 7. Please try again return None drop-disc (board, drop-disc (board, 7, 2, 'black') 'black') return 6 return 4 # and updates board # 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 False otherwise 1 2 3 4 5 67 If we call this function on the board given in the Figure above, then we will get check-disc (board, 3, 5) False check disc (board, 6,2) > True check disc (board, 4, 4) >True check disc (board, 5,4) > True check disc (board, 3,2) True check disc (board, 2,1) > False check disc (board, 1,1) False check disc (board, 0,1) > None check disc (board, 3,6) > True check disc (board, 5,3)> False check disc (board,4,1) -> True check-disc (board, 5, 2) False # highlighted in red above # (5,2) isn't in the winning sequence 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 comect 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), .e. list the possibilities 6. def is qame over (board) This function takes one argument: the board. Returns the color of the winner ('black or 'white') if one exists, or 'draw' if board is full, otherwise return None. Begin with a call to check disc Case 1 if we call this function on the board given in the Figure above, then we will get is_game_over (board) The board is full so this game ends in a draw. return 'draw' Case 2 1 2 3 45 67 if we call this function on the board given in the Figure above, then we will get is game over (board) white' wins! yay!! return 'white' 7. def main ): Game Commands: exit: If you type exit on the prompt, the game will exit by displaying a goodbye message. pass: If you type pass, this will cause you to give up the game and admit defeat. An integer: If you type an integer, this will be subtracted by 1 and passed to drop_disc function. For more detail please refer to project description and test cases Assignment Deliverable The deliverable for this assignment is the following file: proj07.py the source code for your Python program Be sure to use the specified file name and to submit it for grading via the Mimir before the project deadline. Assignment Notes 1. To clarify the project specifications, sample output is appended to the end of this document 2. Items 1-6 of the Coding Standard will be enforced for this project. 3. We provide a proj02.py program for you to start with. It has a simple while loop (notice how input is prompted before the loop and at the bottom of the loop). 4. You are not allowed to use advanced data structures such as classes etc.... 5. You do not need to check for any input errors other than those mentioned in this description. Test Cases Test 1 Connect Four is a 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 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. then take turns dropping one colored disc from the top The pieces fal1 Usage: pass:give up, admit defeat exit: exit the game drop a disk into column i Pick a color: green Wrong color enter only 'black' or 'white', try again Pick a color: black You are "black, and your opponent is 'white'. Current board 1 23 45 67 black's turn :>11 Invalid column: 1 pass black gave up! white is the winneryay!!! Would you like to play again? yes Pick a color: black You are 'black' and Current board: your opponent is 'white 1 234 5 6 7 black's turn :> hi Invalid option Usage pass: give up, admit defeat exit: exit the gamee drop a disk into column i black's turn exit Thanks for playing! See you again soon! Test 2 Connect Four is a 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 column. The objective of the game is to be the first to form a horizontal, vertical, or diagonal 1ine 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 23 4 5 67 white's turn4 Current board: black's turn :> 7 Current board white's turn :> 5 Current board: 1 2 3 4 567 black's turn> 6 Current board: 1 23 4 5 67 white's turn7 Current board: 1 234 5 6 7 black's turn :> 6 Current board 1 23 45 67 white's turn :> 6 Current board: 1 2 3 4 5 67 white wins! Would you like to play again? no Thanks for playing! See you again soon! Grading Rubric Computer Project #7 Scoring Summary General Requirements: (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 7pts for handling diagonal cases s_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 https://www.mathsisfun.com/games/connect4.html Project Description 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 . Initialize board to all zeros 2. 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. white's turn :> 4 Current board 1 23 45 67 black's turn> 1. def initialize O This function takes no arguments. It returns a list of lists that represents the game board using cOLUMN7 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, it will print an error message. Once it receives a correct color assignment, t will store those color values into two variables and return them with player's color first and opponent's color second1. 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 returmed color pairs must be lower case. Choose color Pick a color: red 1When 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 23 45 67 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 retur ful1' drop_disc (board, 8, 'black') Column should be in range 1 to 7. Please try again return None drop-disc (board, drop-disc (board, 7, 2, 'black') 'black') return 6 return 4 # and updates board # 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 False otherwise 1 2 3 4 5 67 If we call this function on the board given in the Figure above, then we will get check-disc (board, 3, 5) False check disc (board, 6,2) > True check disc (board, 4, 4) >True check disc (board, 5,4) > True check disc (board, 3,2) True check disc (board, 2,1) > False check disc (board, 1,1) False check disc (board, 0,1) > None check disc (board, 3,6) > True check disc (board, 5,3)> False check disc (board,4,1) -> True check-disc (board, 5, 2) False # highlighted in red above # (5,2) isn't in the winning sequence 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 comect 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), .e. list the possibilities 6. def is qame over (board) This function takes one argument: the board. Returns the color of the winner ('black or 'white') if one exists, or 'draw' if board is full, otherwise return None. Begin with a call to check disc Case 1 if we call this function on the board given in the Figure above, then we will get is_game_over (board) The board is full so this game ends in a draw. return 'draw' Case 2 1 2 3 45 67 if we call this function on the board given in the Figure above, then we will get is game over (board) white' wins! yay!! return 'white' 7. def main ): Game Commands: exit: If you type exit on the prompt, the game will exit by displaying a goodbye message. pass: If you type pass, this will cause you to give up the game and admit defeat. An integer: If you type an integer, this will be subtracted by 1 and passed to drop_disc function. For more detail please refer to project description and test cases Assignment Deliverable The deliverable for this assignment is the following file: proj07.py the source code for your Python program Be sure to use the specified file name and to submit it for grading via the Mimir before the project deadline. Assignment Notes 1. To clarify the project specifications, sample output is appended to the end of this document 2. Items 1-6 of the Coding Standard will be enforced for this project. 3. We provide a proj02.py program for you to start with. It has a simple while loop (notice how input is prompted before the loop and at the bottom of the loop). 4. You are not allowed to use advanced data structures such as classes etc.... 5. You do not need to check for any input errors other than those mentioned in this description. Test Cases Test 1 Connect Four is a 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 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. then take turns dropping one colored disc from the top The pieces fal1 Usage: pass:give up, admit defeat exit: exit the game drop a disk into column i Pick a color: green Wrong color enter only 'black' or 'white', try again Pick a color: black You are "black, and your opponent is 'white'. Current board 1 23 45 67 black's turn :>11 Invalid column: 1 pass black gave up! white is the winneryay!!! Would you like to play again? yes Pick a color: black You are 'black' and Current board: your opponent is 'white 1 234 5 6 7 black's turn :> hi Invalid option Usage pass: give up, admit defeat exit: exit the gamee drop a disk into column i black's turn exit Thanks for playing! See you again soon! Test 2 Connect Four is a 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 column. The objective of the game is to be the first to form a horizontal, vertical, or diagonal 1ine 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 23 4 5 67 white's turn4 Current board: black's turn :> 7 Current board white's turn :> 5 Current board: 1 2 3 4 567 black's turn> 6 Current board: 1 23 4 5 67 white's turn7 Current board: 1 234 5 6 7 black's turn :> 6 Current board 1 23 45 67 white's turn :> 6 Current board: 1 2 3 4 5 67 white wins! Would you like to play again? no Thanks for playing! See you again soon! Grading Rubric Computer Project #7 Scoring Summary General Requirements: (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 7pts for handling diagonal cases s_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 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

Databases Illuminated

Authors: Catherine M. Ricardo, Susan D. Urban, Karen C. Davis

4th Edition

1284231585, 978-1284231588

More Books

Students also viewed these Databases questions