Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1) Dont copy paste from cheg P1: The Game Board (Grid) [7 marks] Save all your functions from this part in a file called connect4.py.

1) Dont copy paste from cheg P1: The Game Board (Grid) [7 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. 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'] ] The function will always be called with inputs satisfying 4 nRows 10 and 4 nCols 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 ('|' 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) ) | |0 | |1 | O |2 | X X |3 +-----+ 01234

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_2

Step: 3

blur-text-image_3

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

arranging events, and many more.

Answered: 1 week ago