Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

P1: The Game Board (Grid) . [35 marks] Save all your functions from this part in a file called connect4.py. The game will take place

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

P1: The Game Board (Grid) . [35 marks] Save all your functions from this part in a file called connect4.py. The game will take place in a 2-dimensional grid. A typical game has 6 rows and 7 columns giving 42 possible places, which we will call locations, for the pieces, called checkers, to be and a maximum of 42 moves (21 for each player). You will use a 2-dimensional list (list of lists) to represent the game grid. The inner lists will store strings. Each string must be one of 'red', 'black' or 'empty'. If grid[2][4] == 'red', then a red checker is row 2 and column 4. Columns and rows are labeled staring with ZERO, so grid[2][4] corresponds to the 3rd row and the 5th column. Note that column 0 is the left-most column of the game and row 0 is the top-most row of the game. Make a function called makeGrid(nRows, nCols) that takes two integers as input and outputs (returns) a 2-dimensional list that is an empty game consisting of nRows rows and nCols columns. All nCols strings in each row must be the string 'empty'. For example, >>> makeGrid(5,4) [ ['empty', 'empty', 'empty' ['empty', 'empty', empty', ['empty', 'empty', 'empty' 'empty'], ['empty', 'empty', empty'], ['empty', 'empty', 'empty'] ] empty', 'empty'), empty', 'empty'), 2 The function will always be called with inputs satisfying 4 s nRows 10 and 4 s nCols S 10. Next, make a boolean function called play(grid, column, checker). The function tries to play the checker (either 'red' or 'black') in the specified column of the grid. If column is valid (i.e., it is in the right range) and there is room to play another checker in that column of the grid, then the function should modify the grid to add the checker in the given column and return True. Otherwise, it returns False. For example, >>> grid = makeGrid(4,4) >>> play(grid, 1, 'red') True >>> play(grid, 5, 'red') False >>> print(grid) [['empty', 'empty', 'empty', 'empty'], ['empty', 'empty', 'empty', 'empty'), ['empty', 'empty', 'empty', 'empty'], ['empty', 'red', 'empty', 'empty'] ] Next, make a function called win(grid, column) that returns a string. The function checks if a player has won the game (four checkers of the same colour in a row, column or diagonal) or not. The specified column is the last column in which the piece was played in the game (this should make it easier to check if that last play was a winning play). If a player has won the game then the function returns the checker name ('red' or 'black') that won. Otherwise, it returns 'empty'. To make the function more robust, it should also return 'empty' if the input column is out of the valid range of columns or if there is no piece played in the specified column. Next, make a function called toString(grid) that returns a string representation of the game. The checker 'red' will be represented by an X', the checker 'black' will be represented by an 'O and empty locations will be represented by a single space (" "). The string must contain newline characters, border characters ('l' pipes, '-'dashes and '+' pluses) and labels (numbering) of the rows and columns. The format of the output string should follow this example: >>> grid = makeGrid(4,5) >>> play(grid, 1, 'red') True >>> play(grid, 1, 'black') True >>> play(grid, 3, 'red') True >>> print( toString(grid)) 10 1 1 10 12 | X X 13 01234 Put all FOUR functions in a file called connect4.py. P2: Connect Four Game [15 marks] Write a program (in a file called c4game.py) that lets two players play a game of connect four. Program Your program will be driven by a main() function. Be sure to include a main guard (if statement) in your file. The program will proceed as follows: 1. The users are asked for the size of the game to play in a single question. The expected input should something like 4,6" to play a game with 4 rows and 6 columns. There can be any amount of whitespace around the numbers and the comma when the user enters this. So 4, 6, 4 , 6" and 4,6" are all valid. If the users enter 'quit' then the program ends with a parting message like "Thanks for playing". The user will NEVER enter anything other a valid row,column combination or quit. 2. The program checks if there are empty locations in the game grid. If the grid is full, it outputs a message "the game is a tie" and then repeated step 1. If there is at least one empty location, then proceed to step 3. 3. The program asks 'red' which column to play (remember column labels start with O) and then tries to play a red checker in the given column. a. If this is successful, the grid is shown and then the program checks if the last move was a winning move. If it was a winning move the game ends with an appropriate message ('Red wins the game after M moves') and the program goes back to step 1. If was not a winning move, the program proceeds to step 4. b. If this is unsuccessful, a message is displayed (that it was an invalid move) and we retry step 3. 4. The program checks if there are empty locations in the game grid. If the grid is full, it outputs a message "The game is a tie" and then repeated step 1. If there is at least one empty location, then proceed to step 5. 5. The program asks 'black' which column to play (remember column labels start with 0) and then tries to play a black checker in the given column. a. If this is successful, the grid is shown and then the program checks if the last move was a winning move. If it was a winning move the game ends with an appropriate message ('Black wins the game after M moves') and the progra goes back to step 1. If it was not a winning move, we proceed to step 6. b. If this is unsuccessful, a message is displayed (that it was an invalid move) and we retry step 5. 6. Go back and repeat step 2. in the above description, M is the total number or valia moves that nave been played in the given game. The minimum number that this can be for a valid game is 7 (when red wins after its first 4 moves). A sample run of the game is as follows: (User input is shown highlighted light yellow; your game will NOT show this highlighting; it is just there for illustrative purposes). Please enter the size of the game you want to play: 4, 5 Where does red (X) want to play? 4 1 10 11 1 12 1 X3 +-----+ 01234 Where does black (0) want to play? 4 1 10 1 11 1 012 | --+ 01234 X3 Where does red (x) want to play? 7 That is not a valid move. Where does red (x) want to play? 1 10 1 11 1 012 | X X3 +-----+ 01234 Where does black (0) want to play? 4 1 10 1 01 1 O2 | X X3 01234 Where does red (X) want to play? 2 1 10 | 011 1 O2 | XX X3 +-----+ 01234 (0) want to play? 1 Where does black 1 10 1 011 012 OXX X3 +-----+ 01234 Where does red (X) want to play? 3 | 10 1 01 | O2 | OXXXX 3 +-----+ 01234 Red wins after 7 moves Please enter the size of the game you want to play: quit Thanks for playing

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

Microsoft Outlook 2023

Authors: James Holler

1st Edition

B0BP9P1VWJ, 979-8367217322

More Books

Students also viewed these Databases questions