Python Forum
"DiceGame" OOP project
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"DiceGame" OOP project
#1
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)
  • 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:
  1. How do I create relationships between two or more class definitions?
  2. 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?
Reply
#2
(Nov-12-2023, 01:45 PM)Drone4four Wrote: Here are my questions:
  1. How do I create relationships between two or more class definitions?

  2. I know how to easily instantiate classes in the Pyhon shell/interpreter, but how do I instantiate a class inside another class?
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 -= 1
Usage.
>>> human_player = Player(is_human=True)
>>> computer_player = Player(is_human=False)
>>> human_player.roll_die()
5
>>> computer_player.roll_die()
4
So 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 Die
The 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.
Reply
#3
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.
Reply
#4
(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.

This is the homework section where members aren't supposed to do other people's assignments.
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.
buran likes this post
Reply
#5
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.
Reply
#6
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.
Reply
#7
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020