I'm new to Python (started a few days ago) and I've been trying to code Monopoly (with reference of a different user's code) and I can't seem to get past a specific error stating that my 'Game' object has no attribute 'square_number'
import random
class rolling(object):
def roll(self):
return random.randint(1, 6)
class Player(object):
PlayerList = []
SquareNum = []
other = []
def __init__(self, name):
self.name = name
self.balance = 1500
self.square_number = 0
self.square_name = None
Player.other.append(self)
Player.PlayerList.append(self.name)
Player.SquareNum.append(self.square_number)
print(self.name, "successfully added \n")
# print(self.square_number, "assigned")
# print(Player.SquareNum)
def roll_and_move(self):
dice1 = rolling.roll(self)
dice2 = rolling.roll(self)
totaldice = dice1 dice2
if dice1 == dice2:
print("YOU LANDED A DOUBLE!")
if totaldice self.square_number >= len(Game.Board_Squares):
pass
else:
self.square_number = self.square_number totaldice
self.square_name = Game.Board_Squares[self.square_number - 1]
print(Game.current_player, "rolled", dice1, "and", dice2, "moving " str(totaldice) " spaces")
print(Game.current_player, "is on", self.square_name, "\n")
Player.PlayerList.sort(key=Player.PlayerList[0].__eq__) # first player in the list moves to last player in the list
Game.running_game(self)
class Game(object):
def __init__(self):
Game.Board_Squares = ['Mediterranean Avenue', 'Community Chest 1', 'Baltic Avenue', 'Income Tax', 'Reading Railroad',
'Oriental Avenue', 'Chance 1', 'Vermont Avenue', 'Connecticut Avenue', 'Just Visiting Jail',
'St. Charles Place', 'Electric Company', 'States Avenue', 'Virginia Avenue', 'Pennsylvania Railroad',
'St. James Place', 'Community Chest 2', 'Tennessee Avenue', 'New York Avenue', 'Free Parking',
'Kentucky Avenue', 'Chance 2', ' Indiana Avenue', 'Illinois Avenue', 'B. & O. Railroad',
'Atlantic Avenue', 'Ventnor Avenue', 'Water Works', 'Marvin Gardens', 'Go To Jail', 'Pacific Avenue',
'North Carolina Avenue', 'Community Chest 3', ' Pennsylvania Avenue', 'Short Line', 'Chance 3',
'Park Place', 'Luxury Tax', 'Boardwalk', 'Go']
print("Welcome to Monopoly!")
n = int(input("Enter the number of players: "))
if n > 8:
print("You may only have 8 players!")
elif n == 0:
print("You may not have 0 players!")
else:
for i in range(0, n):
i = 1
names = input("Player" str(i) ": ")
Player(names)
# Player.PlayerList.append(names)
self.running_game()
def running_game(self):
Game.current_player = Player.PlayerList[0] # current player is first in the list
input("\x1B[3mPress ENTER to continue\x1B[0m\n")
Player.roll_and_move(self)
Game()
In all honesty, I have no idea what I've been trying to do. I can't seem to pinpoint the issue completely and that makes finding a solution all the much harder. What I notice is that when my 'roll_and_move' is called, anything with 'self.' doesn't seem to function, and seems to want to go to my 'Game' class.
CodePudding user response:
Your "Game" class only has constructor method and running_game method. The "square_number" is in the Player class and it is available upon initialization.
In the code above, I have noticed that you are trying to call class method without initializing the object. For instance,
dice1 = rolling.roll(self)
dice2 = rolling.roll(self)
# should be
# dice1 = rolling().roll()
# dice2 = rolling().roll()
# notice the parenthesis () after rolling which indicates that rolling class is initialized and method roll can then be called.
Player.roll_and_move(self)
# this should be Player(name="test").roll_and_move()
I would recommend you to read more on class, object initialization, method.
CodePudding user response:
This answer gets started on the concepts of classes, objects, and instances. It does not answer the original question because of several issues in the code.
A class is a template for an object. An object is an instance of a class. So, a required initial code change is to create an instance of the Game() class.
That has been done in the final line of the following code.
Additional comments and commented code has been added for information.
import random
class rolling(object):
def roll(self):
return random.randint(1, 6)
class Player(object):
PlayerList = []
SquareNum = []
other = []
def __init__(self, name):
self.name = name
self.balance = 1500
self.square_number = 0
self.square_name = None
Player.other.append(self)
Player.PlayerList.append(self.name)
Player.SquareNum.append(self.square_number)
print(self.name, "successfully added \n")
# print(self.square_number, "assigned")
# print(Player.SquareNum)
def roll_and_move(self):
dice1 = rolling.roll(self)
dice2 = rolling.roll(self)
totaldice = dice1 dice2
if dice1 == dice2:
print("YOU LANDED A DOUBLE!")
# To help understand issues break the code down into smaller pieces
# Get the number of board squared from the Game class.
number_of_board_squares = len(Game.Board_Squares)
# Get the square number from the curren tplayer instance
player_current_square_number = self.square_number # error because
# there is no instance of Player for self to refer to.
if totaldice \
self.player_current_square_number \
>= number_of_board_squares:
pass
else:
self.square_number = self.square_number totaldice
self.square_name = Game.Board_Squares[self.square_number - 1]
print(Game.current_player, "rolled", dice1, "and", dice2,
"moving " str(totaldice) " spaces")
print(Game.current_player, "is on", self.square_name, "\n")
Player.PlayerList.sort(key=Player.PlayerList[
0].__eq__) # first player in the list moves to last player in the list
Game.running_game(self)
class Game(object):
def __init__(self):
Game.Board_Squares = ['Mediterranean Avenue', 'Community Chest 1',
'Baltic Avenue', 'Income Tax',
'Reading Railroad',
'Oriental Avenue', 'Chance 1', 'Vermont Avenue',
'Connecticut Avenue', 'Just Visiting Jail',
'St. Charles Place', 'Electric Company',
'States Avenue', 'Virginia Avenue',
'Pennsylvania Railroad',
'St. James Place', 'Community Chest 2',
'Tennessee Avenue', 'New York Avenue',
'Free Parking',
'Kentucky Avenue', 'Chance 2', ' Indiana Avenue',
'Illinois Avenue', 'B. & O. Railroad',
'Atlantic Avenue', 'Ventnor Avenue',
'Water Works', 'Marvin Gardens', 'Go To Jail',
'Pacific Avenue',
'North Carolina Avenue', 'Community Chest 3',
' Pennsylvania Avenue', 'Short Line', 'Chance 3',
'Park Place', 'Luxury Tax', 'Boardwalk', 'Go']
print("Welcome to Monopoly!")
n = int(input("Enter the number of players: "))
if n > 8:
print("You may only have 8 players!")
elif n == 0:
print("You may not have 0 players!")
else:
for i in range(0, n):
i = 1
names = input("Player" str(i) ": ")
Player(names)
# Player.PlayerList.append(names)
self.running_game() # start the current game instance.
def running_game(self):
# Game.current_player = Player.PlayerList[0] # current player is first in the list
input("\x1B[3mPress ENTER to continue\x1B[0m\n")
Player.roll_and_move(self)
new_game = Game() # Create an instance of a game