Question
Define a class called RandomPlayer that can be used for an un intelligent computer player that chooses at random from the available columns. This class
Define a class called RandomPlayer that can be used for an unintelligent computer player that chooses at random from the available columns.
This class should be a subclass of the Player class that you implemented in Problem 2, and you should take full advantage of inheritance. In particular, you should not need to include any attributes in your RandomPlayer class, because all of the necessary attributes (the players checker, and its count of the number of moves) will be inherited from Player.
Similarly, you should not need to redefine the __repr__ or opponent_checkermethods, because they will be inherited from Player, and we dont want these methods to behave any differently for a RandomPlayer than they do for a Player.
However, you will need to do the following:
Make sure that your class header specifies that RandomPlayer inherits from Player.
Because all of the attributes of a RandomPlayer are inherited from Player, you will not need to define a constructor for this class. Rather, we can just use the constructor that is inherited from Player.
Write a method next_move(self, board) that overrides (i.e., replaces) the next_move method that is inherited from Player. Rather than asking the user for the next move, this version of next_move should choose at random from the columns in the specified board that are not yet full, and return the index of that randomly selected column. You may assume that this method will only be called in cases in which there is at least one available column.
In addition, make sure that you increment the number of moves that the RandomPlayer object has made.
Notes:
To ensure that the method does not select the index of a column that is already full, we recommend that you begin by constructing a list containing the indices of all available columns i.e., all columns to which you can still add a checker. For example, lets say that the parameter board represents the following board:
|O| | |X| | | | |X| | |O| | | | |X| | |O| |O| | |X|X| |O|X|X|O| |O|X|O|X|X|O|X| |O|O|X|O|O|O|X| --------------- 0 1 2 3 4 5 6
The list of available columns in this case would be [1,2,4,5,6].
To build this list, you should consider the columns one at a time, and add the index of any available column to the list. This can be done using a loop, or you might want to use a list comprehension instead. We also encourage you to take advantage of one of your Board methods to determine if a given column is available!
We have included an import statement for the random module so that you can use the appropriate function to make a random choice from the list of available columns.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started