Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings

Homework answers / question archive / The game will take place in a 2-dimensional grid

The game will take place in a 2-dimensional grid

Computer Science

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')


>>> play(grid, 5, 'red')


>>> 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')


>>> play(grid, 1, 'black')


>>> play(grid, 3, 'red')


>>> print( toString(grid) )

| |0

| |1

| O |2

| X X |3




Please make sure to include the following the the code



1- creates 'empty' grid with desired sizes (1 for 'empty', 1 for square grids, 1 for rectangles)

2- grid created consists of independent locations (no row aliasing)



1- plays a checker in column (with room) and modifies grid accordingly (6), returns True (1)

2- does not play a checker in full column and does not modify grid (1), returns False (1)

3- does not play a checker or modify grid when specified column is not valid (0.5), returns False (0.5)



1- identify vertical win returning correct player

2- identify horizontal win returning correct player

3- identify diagonal win

4- identify non-win, return 'empty'



1- checkers in correct placement (1) with correct symbols (1)

2- game board symbols

3- rows labelled

4- columns labelled



1- good use of whitespace (consistent indenting, blank lines)

2- module docstring (header in file provided relevant information)

3- comments in code (for non-trivial code)

4- good variable names

5- useful docstring comments in functions

Purchase A New Answer

Custom new solution created by our subject matter experts