![]() |
"DiceGame" OOP project - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: "DiceGame" OOP project (/thread-41108.html) |
"DiceGame" OOP project - Drone4four - Nov-12-2023 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: 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) 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: 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 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? RE: "DiceGame" OOP project - snippsat - Nov-12-2023 (Nov-12-2023, 01:45 PM)Drone4four Wrote: Here are my questions:To give a example using first part of task import random class Die: def __init__(self): self.value = None def roll(self): self.value = random.randint(1, 6) return self.value class Player: def __init__(self, is_human): self.die = Die() # Instance class Die self.is_human = is_human self.counter = 10 def roll_die(self): return self.die.roll() def increment_counter(self): self.counter += 1 def decrement_counter(self): if self.counter > 0: self.counter -= 1Usage. >>> human_player = Player(is_human=True) >>> computer_player = Player(is_human=False) >>> human_player.roll_die() 5 >>> computer_player.roll_die() 4So here use what called composition rather than inheritance. If use inheritance it will would start like this: # Player now inherits from Die class Player(Die): def __init__(self, is_human): super().__init__() # Initialize the Die part of Player # Can removed the roll_die method as it's now inherited from DieThe task dos not specify that should inheritance or composition,so look into both. My guess as inheritance is not mention,is maybe comes later in course. RE: "DiceGame" OOP project - Drone4four - Nov-15-2023 Hi @snippsat. Thanks for your reply. I am humbled that the amount of research and effort I put into my forum thread that an experienced Python verteran like yourself found my line of questioning worthy enough to complete my whole assignment for me. But I thought I tried to make it as clear as possible that I just wanted nudges/hints/tips in the right direction. I have glanced over your reply and promptly looked in the other direction. You've included WAYYYY too much of the solution. Therefore, my ask from you now is to edit your answer by removing the solution and leave only the select few sentences and code snippets where you answer my 2 pointed questions. This is the homework section where members aren't supposed to do other people's assignments. Thank you. RE: "DiceGame" OOP project - snippsat - Nov-16-2023 (Nov-15-2023, 09:24 PM)Drone4four Wrote: But I thought I tried to make it as clear as possible that I just wanted nudges/hints/tips in the right direction. I have glanced over your reply and promptly looked in the other direction. You've included WAYYYY too much of the solution. Therefore, my ask from you now is to edit your answer by removing the solution and leave only the select few sentences and code snippets where you answer my 2 pointed questions.To answer your question about relationships between two or more classes ,i found best to write a example using classes in start of task.There is still DiceGame class that you need to figure out and write,so i am not doing the full assignment.It can be hard to explain a concept of your question without using some code. RE: "DiceGame" OOP project - deanhystad - Nov-16-2023 I have developed a cringe reflex to "I asked ChatGPT". Since ChatGTP is learning from the internet, and there are a lot of ChatGTP responses being posted to the internet, how long will it be before ChatGTP is mostly learning from itself? What kind of hell will that be? Searching for anything about Python is already a struggle with all the bad content out there. RE: "DiceGame" OOP project - deanhystad - Nov-16-2023 Quote:I know how to easily instantiate classes in the Pyhon shell/interpreter, but how do I instantiate a class inside another class?int is a class. str is a class. Almost any class you write will instantiate instances of other classes. There really is no difference between instantiating an int object or a Dice object. If you write a class where several of the instance variables are instances of "custom classes", you can call it an "Aggregate" class and sound all fancy. RE: "DiceGame" OOP project - Larz60+ - Nov-16-2023 deanhystad Wrote:how long will it be before ChatGTP is mostly learning from itself? I think chatGPT is responsible for a good part of the sparse information posts that we see on this site. Unfortunately when those posts are ignored by us (as they should be), the training answers are supplied by a less strict, and therefore more prone to being incorrect site. You are, and we all should be frightened, by it. |