Question: PYTHON PROGRAMMING: For the instructions: You will implement a game called Gomoku in Python using classes. Gomoku is an abstract strategy board game. It is
PYTHON PROGRAMMING:
For the instructions:
You will implement a game called Gomoku in Python using classes. Gomoku is an abstract strategy board game. It is traditionally played with Go pieces (black and white stones) on a Go board with 15x15 intersections
You must implement the following classes and methods.
You are not allowed to add methods and you are only allowed to access attributes using the provided methods (that is no name mangling allowed).
1. class GoPiece. It represents black or white pieces used in the game. You need to implement the following methods & attributes.: a. def __init__(self, color): this method creates a Gomoku piece. It has an attribute named color which is either 'black'or 'white'. The attribute must be private, i.e. the name is preceded by double underscores. Raise MyError('Wrong color.') for a color that is not black or white. Set the default value to 'black' b. def __str__(self): this method displays the black piece as , and white piece as . c. def get_color(self): this method returns the color of the piece as a string 'black'or 'white'. 2. class Gomoku. It contains methods for setting up the game, displaying the game board, and playing the game. You need to implement the following methods & attributes as specified.
All attributes must be private:
a. Method__init__(self,board_size,win_count,current_player) with four attributes, three have defaults, all must be private. i. board_size : the size of the game board, which by default is 15 (representing a 15x15 board). Check that the value is an int so it will raise a ValueError if not. ii. win_count : the number of pieces in an unbroken row that a player must get to win, which by default is 5. Check that the value is an int so it will raise a ValueError if not. iii. current_player : the color of the current player. Default is 'black'. Raise MyError('Wrong color.') for color that is not black or white. iv. go_board: a list of lists to represent the game board. Specifically, it is a list of row-lists.
b. Method assign_piece(self, piece, row, column): it places the piece (a GoPiece object) at the specified position on the game board. Row 1 is the top row, column 1 is the left-most column. Raise MyError('Invalid position.') if the specified row or column is too big or too small to be on the game board. Raise MyError('Position is occupied.') if the space is already occupied by a piece.
c. Method get_current_player(self): it returns the current player as a string 'black'or 'white'.
d. Method switch_current_player(self): it returns the other player as a string, that is, if the current_player is 'white'it returns the string 'black'otherwise it returns 'white'.
e. Method __str__(self): returns a string that represents the board for printing. We provide the complete method.
f. Method current_player_is_winner(): This method returns True when current_player is a winner, otherwise False. It will check all rows and columns to check if there is an unbroken sequence of size win_count (default is five-in-a-row). Important: first implement this to test for horizontal and vertical winners leave the diagonal test until everything else in the project is correct (only one test considers diagonalssee test descriptions).
3. main() function: it creates an instance of the class Gomoku, It will alternatively let the black and white players place their piece on the game board, until there is a winner, or until a player inputs q to quit. Each time a player places a piece on the game board, this function will check whether a winner is generated using the method current_player_is_winner, and display the current game board. Remember to check that the row and column are ints. If anywhere in the program a ValueError is raised, catch it in the main function.
Skeleton Code:
class GoPiece(object):
''' Comment goes here.'''
def __init__(self,color):
''' Comment goes here.'''
pass # replace and delete
def __str__(self):
''' Comment goes here.'''
# Two strings to help you
# return ' '
# return ' '
pass # replace and delete
def get_color(self):
''' Comment goes here.'''
pass # replace and delete
class MyError(Exception):
def __init__(self,value):
self.__value = value
def __str__(self):
return self.__value
class Gomoku(object):
''' Comment goes here.'''
def __init__(self,board_size,win_count,current_player):
''' Comment goes here.'''
pass # replace and delete
# self.__go_board = [ [ ' - ' for j in range(self.__board_size)] for i in range(self.__board_size)]
# raise MyError('Wrong color.')
def assign_piece(self,piece,row,col):
''' Comment goes here.'''
pass # replace and delete
# raise MyError('Invalid position.')
# raise MyError('Position is occupied.')
def get_current_player(self):
''' Comment goes here.'''
pass # replace and delete
def switch_current_player(self):
''' Comment goes here.'''
pass # replace and delete
def __str__(self):
s = ' '
for i,row in enumerate(self.__go_board):
s += "{:>3d}|".format(i+1)
for item in row:
s += str(item)
s += " "
line = "___"*self.__board_size
s += " " + line + " "
s += " "
for i in range(1,self.__board_size+1):
s += "{:>3d}".format(i)
s += " "
s += 'Current player: ' + ('' if self.__current_player == 'black' else '')
return s
def current_player_is_winner(self):
''' Comment goes here.'''
pass # replace and delete
def main():
board = Gomoku()
print(board)
play = input("Input a row then column separated by a comma (q to quit): ")
while play.lower() != 'q':
play_list = play.strip().split(',')
try:
pass # replace and delete
# raise MyError("Incorrect input.")
# print("{} Wins!".format(board.get_current_player()))
except MyError as error_message:
print("{:s} Try again.".format(str(error_message)))
print(board)
play = input("Input a row then column separated by a comma (q to quit): ")
if __name__ == '__main__':
main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
