Nov-12-2023, 01:45 PM
I’m learning OOP by taking Estefania Cassingena Navone's Udemy course titled: "Python OOP - Object Oriented Programming for Beginners". The first major project exercise is a rudimentary dice rolling game where two players (one human, the other CPU) start off with a score of 10, toss a 6 sided die, and which ever player has the higher toss decrements their score by 1 whereas the other player increments by 1. The first player to decrement completely from 10 down to 0 wins. Those are the general rules. The full specs (which basically serves as pseudo code) that are provided by the instructor in pdf format can be found on my GitHub repo here.
The instructor gives the full solution and answer as an hours-long code-along, but before I watch that material, I wanted to try out this exercise myself. So while this is for a non-credit course for a college or university, I am trying to pretend that it is. Therefore my request for members on this forum is to give insight, feedback, suggestions, hints, and tips without doing it all for me.
I’ve done the best I can. There are a lot of features that I still need to implement. I am nowhere near finished or complete.
Here is my bare-bones latest iteration:
Here are my questions:
I asked ChatGPT question #2 and it explained how to do something but as far as I can tell, one class is not called within another class:
Class B is instantiated outside of the class declaration and during run time which is not what I am trying to do. Based on my reading of the instructor's challenge, one class needs to refer to the other class within the class declarations. No?
The instructor gives the full solution and answer as an hours-long code-along, but before I watch that material, I wanted to try out this exercise myself. So while this is for a non-credit course for a college or university, I am trying to pretend that it is. Therefore my request for members on this forum is to give insight, feedback, suggestions, hints, and tips without doing it all for me.
I’ve done the best I can. There are a lot of features that I still need to implement. I am nowhere near finished or complete.
Here is my bare-bones latest iteration:
from random import randint class Player: def __init__(self): self._counter = 10 def set_counter(self, win, lose): if win: self._counter += 1 elif lose: self._counter -= 1 return self._counter class Die: def __init__(self): self._value = None def set_value(self): # roll / toss self._value = randint(1,6) return self._value class DiceGame: def __init__(self): self.human = human self.computer = computer def start(self): print("Let the game begin!") def play(self): print("Round one, GO!") def quit(self): print(f"Game over. The winner is {winner}")It needs a lot work. The part I am focusing on and need your advice on is building the
DiceGame
class. The instructor describes this feature in this way (at the top of page 3 in the pdf linked to above):Quote:“DiceGame” (class name)
- This class will represent a dice game.
- Each instance of the game should have the following attributes:
- A human player instance.
- A computer player instance.
- This will create a relationship between these classes. A DiceGame “has a” human player and a computer player.
Here are my questions:
- How do I create relationships between two or more class definitions?
- I know how to easily instantiate classes in the Pyhon shell/interpreter, but how do I instantiate a class inside another class?
I asked ChatGPT question #2 and it explained how to do something but as far as I can tell, one class is not called within another class:
Quote:In Python, you can declare an instance of a class inside another class definition by creating an instance of the desired class as a class attribute. Here's an example:
class ClassA: def __init__(self, value): self.value = value class ClassB: def __init__(self, a_instance): self.class_a_instance = a_instance # Creating an instance of ClassA instance_of_a = ClassA(value=42) # Creating an instance of ClassB and passing the instance of ClassA as an argument instance_of_b = ClassB(a_instance=instance_of_a) # Accessing the ClassA instance from ClassB print(instance_of_b.class_a_instance.value)In this example, ClassB has a class attribute class_a_instance, which is an instance of ClassA. You create an instance of ClassA (instance_of_a) and then pass it as an argument when creating an instance of ClassB (instance_of_b). This way, instance_of_b has access to the methods and attributes of instance_of_a.
Class B is instantiated outside of the class declaration and during run time which is not what I am trying to do. Based on my reading of the instructor's challenge, one class needs to refer to the other class within the class declarations. No?