Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3222948
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:03:01+00:00 2026-05-17T16:03:01+00:00

I’m writing a simulation in Python for a dice game, and am trying to

  • 0

I’m writing a simulation in Python for a dice game, and am trying to find the best way to handle the following situation in an Object Oriented manner.

I have a Dice class that handles rolling the dice and reporting things about the dice. This class’s methods include a roll() method which modifies the dice values, and various reporting methods that describe the state of the dice, such as have_n_of_a_kind(), is_scoring_combination(), and get_roll_value().

There are two classes which make use of this Dice class. The Player, controlled directly by a human, is untrusted to always make legal moves. There is also a Turn class which enforces the rules of the game.

So then, both the Player and Turn class need to be able to ask the Dice about its values by calling the state describing methods, but the Player cannot be allowed to directly call the roll() method, because the human Player might roll when it is not supposed to.

Is there a good way for me to allow the Turn class to call a Dice object’s roll() method but not allow a Player to do so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:03:01+00:00Added an answer on May 17, 2026 at 4:03 pm

    Have the Player ask the Turn to roll, by calling e.g. turn.roll_dice(). The Turn can then decide whether to roll the dice or e.g. to raise NotYourTurnError.

    You can’t prevent the Player class directly calling die.roll(), although you can make roll private by renaming it __roll. However, since I assume the player is controlling the die via some sort of (G?) UI, you can simply not include any way of telling the die to roll in said interface.

    In general, you can’t hide methods like this from arbitrary Python code with any degree of security; Python is so powerful that you’ll almost certainly be able to find a way around the protection. Instead, you make them protected (_foo) or private (__foo) and assume that people who call them know what they’re doing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.