Question

In: Computer Science

Write a class encapsulating a board game. A board game has the following additional attributes: the...

Write a class encapsulating a board game. A board game has the following additional attributes: the number of players and whether the game can end in a tie. Code the constructor and the toString method of the new class. You also need to include a client class(with the main method) to test your code.

code in Python

Solutions

Expert Solution

class Chess_Board:
    def __init__(self):
        self.board = self.create_board()

    def create_board(self):
        board_x=[]

        for x in range(8):
            board_y =[]
            for y in range(8):

                board_y.append('.')

            board_x.append(board_y)
        board_x[7][4] = 'K'
        board_x[7][3] = 'Q'
        board_x[7][2] = 'B'
        board_x[7][1] = 'N'
        board_x[7][0] = 'R'
        return board_x

class WHITE_KING(Chess_Board):
    def __init__(self):
        Chess_Board.__init__(self)
        self.position_x_WK = 7
        self.position_y_WK = 4
        self.symbol_WK = 'K'

    def move(self):
        while True:
            try:
                print ('give a x and y coordinate for WHITE KING')
                destination_x_WK = int(input())
                destination_y_WK = int(input())


                if self.board[destination_x_WK][destination_y_WK] == '.' :

                    if ( abs(self.position_x_WK-destination_x_WK) <2 and abs(self.position_y_WK-destination_y_WK) < 2 ):
                        self.board[self.position_x_WK][self.position_y_WK] = '.'
                        self.position_x_WK = destination_x_WK
                        self.position_y_WK = destination_y_WK
                        self.board[self.position_x_WK][self.position_y_WK] = self.symbol_WK

                        return self.board
                        break

                    else:
                        print ('your move is invalid, please choose cooridnates again')
                        continue

            except:
                pass


class WHITE_QUEEN(Chess_Board):
    def __init__(self):
        Chess_Board.__init__(self)
        self.position_x_WQ = 7
        self.position_y_WQ = 3
        self.symbol_WQ = 'Q'

    def move(self):
        while True:
            try:
                print ('give a x and y coordinate for WHITE QUEEN')
                destination_x_WQ = int(input())
                destination_y_WQ = int(input())


                if self.board[destination_x_WQ][destination_y_WQ] == '.' :

                    if (destination_x_WQ == self.position_x_WQ or destination_y_WQ==self.position_y_WQ or abs(self.position_x_WQ-destination_x_WQ) == abs(self.position_y_WQ-destination_y_WQ) ):
                        self.board[self.position_x_WQ][self.position_y_WQ] = '.'
                        self.position_x_WQ = destination_x_WQ
                        self.position_y_WQ = destination_y_WQ
                        self.board[self.position_x_WQ][self.position_y_WQ] = self.symbol_WQ

                        return self.board
                        break

                    else:
                        print ('your move is invalid, please choose cooridnates again')
                        continue

            except:
                pass

class WHITE_ROOK(Chess_Board):

    def __init__(self):
        Chess_Board.__init__(self)
        self.position_x_WR = 7
        self.position_y_WR = 0
        self.symbol_WR = 'R'

    def move(self):
        while True:
            try:
                print ('give a x and y coordinate for WHITE ROOK ')
                destination_x_WR = int(input())
                destination_y_WR = int(input())


                if self.board[destination_x_WR][destination_y_WR] == '.' :

                    if (destination_x_WR == self.position_x_WR or destination_y_WR==self.position_y_WR  ):
                        self.board[self.position_x_WR][self.position_y_WR] = '.'
                        self.position_x_WR = destination_x_WR
                        self.position_y_WR = destination_y_WR
                        self.board[self.position_x_WR][self.position_y_WR] = self.symbol_WR

                        return self.board
                        break

                    else:
                        print ('your move is invalid, please choose cooridnates again')
                        continue

            except:
                pass

class WHITE_BISHOP(Chess_Board):
    def __init__(self):
        Chess_Board.__init__(self)
        self.position_x_WB = 7
        self.position_y_WB = 2
        self.symbol_WB = 'B'

    def move(self):
        while True:
            try:
                print ('give a x and y coordinate for WHITE BISHOP')
                destination_x_WB = int(input())
                destination_y_WB = int(input())


                if self.board[destination_x_WB][destination_y_WB] == '.' :

                    if  abs(self.position_x_WB-destination_x_WB) == abs(self.position_y_WB-destination_y_WB) :
                        self.board[self.position_x_WB][self.position_y_WB] = '.'
                        self.position_x_WB = destination_x_WB
                        self.position_y_WB = destination_y_WB
                        self.board[self.position_x_WB][self.position_y_WB] = self.symbol_WB

                        return self.board
                        break

                    else:
                        print ('your move is invalid, please choose cooridnates again')
                        continue

            except:
                pass

class WHITE_KNIGHT(Chess_Board):
    def __init__(self):
        Chess_Board.__init__(self)
        self.position_x_WKN = 7
        self.position_y_WKN = 1
        self.symbol_WKN = 'N'

    def move(self):
        while True:
            try:
                print ('give a x and y coordinate for WHITE KNIGHT')
                destination_x_WKN = int(input())
                destination_y_WKN = int(input())


                if self.board[destination_x_WKN][destination_y_WKN] == '.' :

                    if abs(self.position_x_WKN-destination_x_WKN)**2 + abs(self.position_y_WKN-destination_y_WKN)**2 == 5 :
                        self.board[self.position_x_WKN][self.position_y_WKN] = '.'
                        self.position_x_WKN = destination_x_WKN
                        self.position_y_WKN = destination_y_WKN
                        self.board[self.position_x_WKN][self.position_y_WKN] = self.symbol_WKN

                        return self.board
                        break

                    else:
                        print ('your move is invalid, please choose cooridnates again')
                        continue

            except:
                pass

class Engine(Chess_Board):

    def __init__(self):
        WHITE_KING.__init__(self)
        WHITE_QUEEN.__init__(self)
        WHITE_ROOK.__init__(self)
        WHITE_BISHOP.__init__(self)
        WHITE_KNIGHT.__init__(self)
        Chess_Board.__init__(self)

    def play(self):
        print('Please write what figure you choose to move: white_king, white_queen, white_rook, white_bishop'
              'or white knight')

        while True:
            choice=str(input())
            if  choice == 'white_king':
                WHITE_KING.move(self)
                break
            elif  choice == 'white_queen':
                WHITE_QUEEN.move(self)
                break
            elif  choice == 'white_bishop':
                WHITE_BISHOP.move(self)
                break
            elif  choice == 'white_knight':
                WHITE_KNIGHT.move(self)
                break
            elif  choice == 'white_rook':
                WHITE_ROOK.move(self)
                break
            else:
                print ('please choose again')


    def display(self):
        for i in range (8):
            for j in range (8):
                print (self.board[i][j], end=' ')
            print ()


    c_engine = Engine()
    c_engine.display()
    c_engine.play()
    c_engine.display()

Related Solutions

Write a class encapsulating the concept of the weather forecast, assuming that it has the following...
Write a class encapsulating the concept of the weather forecast, assuming that it has the following attributes: the temperature and the sky conditions (e.g. sunny, snowy, cloudy, rainy, etc.).  Include a default constructor, an overloaded constructor, accessors and mutators, and the methods, toString() and equals(). Temperature, in Fahrenheit, should be between -50 and +150; the default value is 70, if needed. The default sky condition is sunny. In Addition, include a method that converts Fahrenheit to Celsius. Celsius temperature = (Fahrenheit...
Write a class encapsulating the concept of a Student, assuming that the Student has the following...
Write a class encapsulating the concept of a Student, assuming that the Student has the following attributes: the name of the student, the average of the student, and the student’s GPA. Include a default constructor, an overloaded constructor, the accessors and mutators, and methods toString() and equals(). Also include a method returning the letter grade base on the following range for the average: Average Range Letter Grade 90-100 A 85-89   B+ 80-84 B 75-79 C+ 70-74 C 65-69 D+ 60-64...
Write a class encapsulating the concept of a Student, assuming that a student has the following...
Write a class encapsulating the concept of a Student, assuming that a student has the following attributes: last name, first name, id, array of grades. Include a constructor, the accessors and mutators, and method toString. Also code the following methods: one returning the GPA using the array of grades (assuming each grade represents a course grade and all courses have the same number of credit hours) and a method to add a course grade to the array of grades (this...
1. Write a superclass encapsulating a rectangle. A rectangle has two attributes representing the width and...
1. Write a superclass encapsulating a rectangle. A rectangle has two attributes representing the width and the height of the rectangle. It has methods returning the perimeter and the area of the rectangle. This class has a subclass, encapsulating a parallelepiped, or box. A parallelepiped has a rectangle as its base, and another attribute, its length; it has two methods that calculate and return its area and volume. You also need to include a client class (with the main method)...
Write a class called Time that represents the time of the day. It has attributes for...
Write a class called Time that represents the time of the day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. Write a default constructor that initializes the time to 0 hours and 0 minutes. Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the...
Write a class called Time that represents the time of the day. It has attributes for...
Write a class called Time that represents the time of the day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. Write a default constructor that initializes the time to 0 hours and 0 minutes. Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the...
Write in Java the following: A.  A bank account class that has three protected attributes: account number...
Write in Java the following: A.  A bank account class that has three protected attributes: account number (string), customer name (string), and balance (float). The class also has a parameterized constructor and a method public void withDraw (float amount) which subtracts the amount provided as a parameter from the balance. B. A saving account class that is a child class of the bank account class with a private attribute: penality rate (float). This class also has a parameterized constructor and a...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming a telephone number has only a single attribute: aString representing the telephone number. Include a constructor, the accessor and mutator, and methods 'toString' and 'equals'. Also include methods returning the AREA CODE (the first three digits/characters of the phone number; if there are fewer than three characters in the phone number of if the first three characters are not digits, then this method should...
Write a class encapsulating the concept of a corporate name (for example, IBM), assuming a corporate...
Write a class encapsulating the concept of a corporate name (for example, IBM), assuming a corporate name has the following attribute: the corporate name. Include a constructor, the accessors and mutators, and methods toString() and equals(). Also include and method that returns a potential domain name by adding a www. at the beginning and .com at the end of the corporate name (for instance, if the corporate name is IBM, that method should return www.ibm.com). Write a client class to...
Write a class Car that contains the following attributes: The name of car The direction of...
Write a class Car that contains the following attributes: The name of car The direction of car (E, W, N, S) The position of car (from imaginary zero point) The class has the following member functions: A constructor to initialize the attributes. Turn function to change the direction of car to one step right side (e.g. if the direction is to E,it should be changed to S and so on.) Overload the Turn function to change the direction to any...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT