151k views
5 votes
You have been hired as a programmer by a company that develops video games. You are required to create

classes and objects that will represent the elements of the game. You are also required to use object
composition to create complex objects.
Instructions:
• Create a class called Character that has the following attributes: name (String), id (int), strength
(int), speed (int), intelligence (int), and item (gameltem). strength, speed and intelligence cannot be
less than 0 and cannot exceed 100. Implement appropriate getters and setters for all attributes.
The class must have methods to implement the following actions:
o List comparison in strength, speed and intelligence between two Characters. Remember to
integrate the strength, speed and intelligence of gameltem to the character. The total of
strength, speed or intelligence cannot exceed 100.
o Find if two Characters are equal. The Characters are equal if they have the same strength,
speed and intelligence.
Find if two Characters are duplicates. The Characters are equal if they have the same name,
id, strength, speed and intelligence.
o Find the expected winner between two Characters. The winner is the one with highest value
in the comparison between the average of strength, speed and intelligence. Remember to
integrate the strength, speed and intelligence of gameltem to the character.
o Copy a Character.
• Create a class called Gameltem that has the following attributes: name (String), id (int),
strengthModifier(int), speedModifier (int), and intelligence Modifier (int). Implement appropriate
getters and setters for all attributes. The class must have methods to implement the following
actions:
o List comparison in strength, speed and intelligence between two gameltem.
o Find if two Gameltems are equal. The Gameltems are equal if they have the same strength,
speed and intelligence.
o Find if two Gameltems are duplicates. The Gameltems are equal if they have the same
name, id, strength, speed and intelligence.
o Copy a Gameltem.

User Bluelovers
by
7.6k points

1 Answer

3 votes

Final Answer:

Below are the classes for Character and Gameltem, along with the requested methods:

```python

class Gameltem:

def __init__(self, name, id, strength_modifier, speed_modifier, intelligence_modifier):

self.name = name

self.id = id

self.strength_modifier = strength_modifier

self.speed_modifier = speed_modifier

self.intelligence_modifier = intelligence_modifier

def __eq__(self, other):

return ( self.name == other.name

and self.id == other.id

and self.strength_modifier == other.strength_modifier

and self.speed_modifier == other.speed_modifier

and self.intelligence_modifier == other.intelligence_modifier )

def __copy__(self):

return Gameltem( self.name,

self.id,

self.strength_modifier,

self.speed_modifier,

self.intelligence_modifier, )

def __str__(self):

return f"Gameltem(name={self.name}, id={self.id}, strength_modifier={self.strength_modifier}, speed_modifier={self.speed_modifier}, intelligence_modifier={self.intelligence_modifier})"

class Character:

def __init__(self, name, id, strength, speed, intelligence, item):

self.name = name

self.id = id

self.strength = strength

self.speed = speed

self.intelligence = intelligence

self.item = item

def __eq__(self, other):

return ( self.strength == other.strength

and self.speed == other.speed

and self.intelligence == other.intelligence )

def __copy__(self):

return Character( self.name,

self.id,

self.strength,

self.speed,

self.intelligence,

self.item.__copy__(), )

def list_comparison(self, other):

return ( self.strength + self.item.strength_modifier,

self.speed + self.item.speed_modifier,

self.intelligence + self.item.intelligence_modifier, )

def are_duplicates(self, other):

return ( self.name == other.name

and self.id == other.id

and self.strength == other.strength

and self.speed == other.speed

and self.intelligence == other.intelligence

and self.item == other.item )

def expected_winner(self, other):

self_avg = sum(self.list_comparison(self)) / 3

other_avg = sum(self.list_comparison(other)) / 3

return self if self_avg > other_avg else other

def __str__(self):

return f"Character(name={self.name}, id={self.id}, strength={self.strength}, speed={self.speed}, intelligence={self.intelligence}, item={str(self.item)})"

Example usage:

gameltem1 = Gameltem("Sword", 1, 10, 5, 0)

gameltem2 = Gameltem("Bow", 2, 5, 10, 3)

character1 = Character("Warrior", 1, 50, 30, 20, gameltem1)

character2 = Character("Archer", 2, 40, 40, 15, gameltem2)

print(character1.list_comparison(character2))

print(character1 == character2)

print(character1.are_duplicates(character2))

print(character1.expected_winner(character2))

character3 = character1.__copy__()

print(character1.are_duplicates(character3))

Step-by-step explanation:

1. Gameltem Class:

- The `__init__` method initializes the Gameltem with the specified attributes.

- The `__eq__` method checks if two Gameltems are equal.

- The `__copy__` method creates a copy of the Gameltem.

- The `__str__` method provides a string representation of the Gameltem.

2. Character Class:

- The `__init__` method initializes the Character with the specified attributes.

- The `__eq__` method checks if two Characters are equal.

- The `__copy__` method creates a copy of the Character.

- The `list_comparison` method calculates the modified strength, speed, and intelligence based on the associated Gameltem.

- The `are_duplicates` method checks if two Characters are duplicates.

- The `expected_winner` method determines the expected winner between two Characters based on the average of modified attributes.

- The `__str__` method provides a string representation of the Character.

3. Example Usage:

- Two Gameltems and two Characters are created.

- Various methods are demonstrated, including list comparison, equality check, duplicate check, and expected winner determination.

- A copy of a Character is created, and duplicate check is performed.

This code provides a framework for implementing game elements using object-oriented programming and object composition.

User Latashia
by
8.0k points