#๐ Can someone help me with my board game homework im trying to use classes but i get a error
583 messages ยท Page 1 of 1 (latest)
@polar void
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
You haven't defined what dice is by that point in the program
game = BoardGame(board, player1pos, player2pos, dice, player="1")
``` when you do that, every variable you're using in the brackets (namely: `board`, `player1pos`, `player2pos`, and `dice`) needs to be defined
since u didnt make a variable called dice it's giving an error
If your goal is to make it so that players can roll dice to go around the board, it might be smarter to make a roll_dice method in the class itself
i will do that then
but when i do like dice="None" or somthing i get error to
Also, in your while loop you're creating a new BoardGame on each loop, which will probably reset the game each time. You'll want to use your original game variable somehow
but if i make the function i dont need that
wait how?
what's the error you get from that?
Traceback (most recent call last):
File "C:\Users\littl\OneDrive\Documents\board.py", line 79, in <module>
game.start_game(player="1")
TypeError: BoardGame.start_game() got an unexpected keyword argument 'player'
def start_game(self, board, player1pos, player2pos):
``` the function doesnt take a parameter named player
but then if i was to do game = BoardGame(board, player1pos, player2pos) i wouild get error to?
Like just some pseudocode might be:
game = BoardGame(...)
game.start_game()
game.print_board()
print("Starting game!")
while True:
game.roll_dice()
game.edit_board()
yes, because doing BoardGame(bla bla bla some stuff here) calls the __init__ function: ```py
def init(self, board, player1pos, player2pos, dice, player):
yes i will do that then
so how do i fix?
do dict=None when making your game variable
and then use the correct parameters when calling BoardGame.start_game
you mean dice?
or dict
bc idk what dict is
yeah just checking
ohh
so even tho i dont use it
i just say none
sure
is that what you ment?
yes
so i got this
def roll_dice(self, board, player1pos, player2pos, dice, player):
dice1 = random.randint(1,6) #rolls first dice
dice2 = random.randint(1,6) #rolls second dice
dice = dice1 + dice2 #adds dice
game = BoardGame(board, player1pos, player2pos, dice, player=player)
game.edit_board(player="1")
print("Player " + player + ", You have rolled: " + str(dice1) + ", " + str(dice2))
but do i run edit_board there?
or in the while true?
up to you really
I'd probably run them from that method so you don't need to store dice rolls across method calls
so how i showed?
yea
you probably don't need all of that in the roll_dice function
Keep it really simple to what the function is supposed to do
roll the dice and get the value of the dice back
what would i not need
this is such confusing code lol
how?
idk tbh im just trying to make this board game
if you lot got ideas how to make code better ill do it
def roll_dice(self):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print(f"You have rolled {dice1} and {dice2}!")
return dice1, dice2
Keep it simple
So as long as you keep calling this function, it will always roll the dices and returns the value of the 2 dices
i only need to return dice added
Sure
but yes that makes sense then when i return it ill call the other one
I kept them separate because, I don't know if you need to keep the values of both dices separate for other game mechanics
you can always sum them after the function call
!e
import random
def roll_dice():
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print(f"You have rolled {dice1} and {dice2}!")
return dice1, dice2
print(sum(roll_dice()))
@halcyon garnet :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | You have rolled 4 and 2!
002 | 6
thats true but then i gotta change class
wdym?
im kinda stuck now so pretty much first is that game doesnt get passed into the while true loop, second i get a error that dice isnt defined when in the functions it says dice="None"
line 97 doesnt make sense
you've overwritten the BoardGame class in game variable with the string game
ok let me help you on this
@polar void can you explain the game for me? Like imagine the board game came with paper instructions that instructs how a player plays or what action is available on their turn
well game wasnt defined so i had to add it to test it
so pretty much its a simple board game right now i just want your player and the other player to start one square one, you move up the board acording to the dice roll and whoever gets to the top wins
and of course, explain how to win the game, that's the most important part haha
just reaching the last square
its pretty simple but i think im overdoing it
prob bc i got a ton more to add after this but i havent tried to add it yet
so its like snakes and ladders but with no snakes or ladders?
well not yet
i will add somthing like that after
this is what we got given if it helps you
Don't worry about it then. Focus on what it is now. So you're building a very simple board game where the players take turn rolling 2 dices, that adds up, moves their position from position 0 to position 49. Whoever gets to position 49, wins.
plus a bit more
yes that what i was thinking
Is this homework?
ah ok
Are you allowed to use classes?
yes our teacher said whatever
just no gui
ah nice
so anything to do it
just dont go to far off topic
but classes isnt off topic
that means like dont go to far with adding your own stuff
hah I knew it. Keeping the 2 dices separate does something here at point 4
If you rolled a double, you have to move back with that number
oh yeah you right
what does it mean by "storing externally"?
in a file
what file?
so just store data in a text file
ok
nothing big
I'll help you with 1 through 4 but 5,6,7, you're on your own
so first off, you're probably gonna need to restart from scratch
oh yeah thats all good
well along as you can help im good with that
Remove everything below line 27
So not exactly starting from 0 but at least its a good enough start
Let me know when you're done
done
import random
import time
#board doesnt place x in right places
#make turns
board = []
player1pos = 0
player2pos = 0
game = "game"
currentplayer = "1"
class BoardGame: #allows for no need to return
def __init__(self, board, player1pos, player2pos, dice, player):
self.board = board
self.player1pos = player1pos
self.player2pos = player2pos
self.dice = dice
self.player = player
def print_board(self, board, player1pos="None", player2pos="None", dice="None", player="None"):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
remove board = []
done
done
Then in the __init__ function parameter, remove all except for self
remove self.dice = dice
remove self.player=player
change the following in your __init__ function
- self.player1pos = player1pos
- self.player2pos = player2pos
+ self.player1pos = 0
+ self.player2pos = 0
Remove all function parameters in print_board except for self
Let me know when you're done
we got this
import random
import time
#board doesnt place x in right places
#make turns
class BoardGame: #allows for no need to return
def __init__(self):
self.board = board
self.player1pos = 0
self.player2pos = 0
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
yes
Create a new function called setup_board
Put self as the function param
In it, paste this in:
for i in range(1, 50):
self.board.append(i)
Let me know when done
done
import random
import time
#board doesnt place x in right places
#make turns
class BoardGame: #allows for no need to return
def __init__(self):
self.board = board
self.player1pos = 0
self.player2pos = 0
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)```
Do the following replacement:
- self.board = board
+ self.setup_board()
loops 1 to 49 and appeneds each one
yep
done
Show me
import random
import time
#board doesnt place x in right places
#make turns
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)```
ok good
So we have the init and board set up
We need a way to allow the game to update the players position
Create a new function, lets call it, player_move
with 2 function parameters self, number
This should be pretty simple for you but
uh hang on
import random
import time
#board doesnt place x in right places
#make turns
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)
def player_move(self, number, player):
sure thing
needs 1 more function param
player
to help indicate which player to update
then in the function
like that?
yep if satement for what?
if player is player 1, self.player1pos += number
and repeat for player 2
show me when you're done
import random
import time
#board doesnt place x in right places
#make turns
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)
def player_move(self, number, player):
if player == "player1":
self.player1pos += number
elif player == "player2":
self.player2pos += number```
lookin good
player1 would make code more readable instead of 1
sure
Next, we gotta get that roll dices in
Can you do that next step?
I've given the code before
new function?
import random
import time
#board doesnt place x in right places
#make turns
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)
def player_move(self, number, player):
if player == "player1":
self.player1pos += number
elif player == "player2":
self.player2pos += number
def roll_dice(self):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print(f"You have rolled {dice1} and {dice2}!")
wait would we need self?
so return dice1 and dice2
self what?
import random
import time
#board doesnt place x in right places
#make turns
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)
def player_move(self, number, player):
if player == "player1":
self.player1pos += number
elif player == "player2":
self.player2pos += number
def roll_dice(self):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print(f"You have rolled {dice1} and {dice2}!")
return dice1, dice2
in the paramater of roll dice
bc we dont use it
def roll_dice(self):
yes we do need it
ok cool just checking
ok
next we have to sum the number and pass it to player move function
so another new function
think of a name with me here
the purpose of this new function is to check the value of the dices
if the dices are doubled, we call player move to move back "negative numbers"
if not, we call player to move forward "positive numbers"
what do you wanna call this function?
so this new function
will add dice
and check if they match
so if so we call it dice_check
unless im missing somthing
thats cool
go for it
make that function
and in it
make an if statement
the function takes 2 parameters
dice1 and dice2
then the if statement checks if dice1 is the same as dice2
Then I'm thinking aloud again and thought, I forgot about who rolled the dice
yes i was thinking that
so we can put a simple placeholder for now
to indicate which player's turn
what do you wanna call it?
self.player_turn? self.which_player? self.who_next?
I swear naming things is tricky
playerturn?
yes
hold on
this is confusing
def dice_check(self):
dice = roll_dice
if dice.dice1 == dice.dice2```
so when the dices are the same, call the player move, put the number from either dice in it, and place self.playerturn in the player move call
ah but no
so you can either do 1 of 2 things
either you call the function in the function
i.e. call self.roll_dice inside dice_check()
or
make 2 parameters in the function dice_check as dice1 and dice2
take your pick
thats what i was doing
but how do i check if dice1 is the same as dice 2
bc i cannot call it twice
ok so you wanna call it inside the function?
yes if you think thats good?
sure, works fine
just need to fix it abit
the roll_dice() function returns a tuple of int and int
so you can unpack and assign these into 2 variables
not 1
that was my problem
I'll show a different example of unpacking and assignment
it is two isnt it?
thank you
!e
a = "Bob"
b = "Susan"
print(a)
print(b)
This is the simplest and no unpacking thing
@halcyon garnet :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | Bob
002 | Susan
!e
a, b = "Bob", "Susan"
print(a)
print(b)
@halcyon garnet :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | Bob
002 | Susan
This is how you assign and unpack multiple things
There are other ways to unpack but we keep it simple to a function call
but we need to return it
!e
def people():
return ["Bob", "Susan"]
a, b = people()
print(a)
print(b)
@halcyon garnet :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | Bob
002 | Susan
that makes more sense
Do you understand from this example?
yes
nice
ok done i dont think you told me to move it yet
import random
import time
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)
def player_move(self, number, player):
if player == "player1":
self.player1pos += number
elif player == "player2":
self.player2pos += number
def roll_dice(self):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print(f"You have rolled {dice1} and {dice2}!")
return [dice1, dice2]
def dice_check(self):
dice1, dice2 = roll_dice(self)
dice = sum(roll_dice)
if dice1 == dice2:
#move back with dice
else:
#move forward with dice
```
close
wait
roll_dice
is ment ti be dice1
and two
i just add
not sum
- don't
sum()it since its not required
dice = dice1 + dice2
- the
selfneeds to be behindroll_dice. Not in the()
what do you mean
self.roll_dice?
yes and with the ()
okk
makes sense
import random
import time
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)
def player_move(self, number, player):
if player == "player1":
self.player1pos += number
elif player == "player2":
self.player2pos += number
def roll_dice(self):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print(f"You have rolled {dice1} and {dice2}!")
return [dice1, dice2]
def dice_check(self):
dice1, dice2 = self.roll_dice()
if dice1 == dice2:
#move back
else:
#move forward
```
that makes sense
now call the player move function with the values required in either and both
so in the true block, take either dice1 or dice2
and in the false block (else), dice1 + dice2
show me when done
ok will do
import random
import time
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)
def player_move(self, number, player):
if player == "player1":
self.player1pos += number
elif player == "player2":
self.player2pos += number
def roll_dice(self):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print(f"You have rolled {dice1} and {dice2}!")
return [dice1, dice2]
def dice_check(self):
dice1, dice2 = self.roll_dice()
dice = dice1 + dice2
if dice1 == dice2:
dice = dice1 - (dice2 * 2)
self.playermove(dice, #we need player)
else:
dice = sum(dice1, dice2)
self.playermove(dice, #we need player)
```
we dont know player yet
we did remember? โ๏ธ
and i done dice = dice1 - (dice2 * 2) to turn it into negitve so it will move backwards
No, that will be later
you basically need to pass self.playerturn as the 2nd param of the player move call
also player move function call is spelled incorrectly
and then we will make function in min
my spelling is not good
i will fix
import random
import time
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)
def player_move(self, number, player):
if player == "player1":
self.player1pos += number
elif player == "player2":
self.player2pos += number
def roll_dice(self):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print(f"You have rolled {dice1} and {dice2}!")
return [dice1, dice2]
def dice_check(self):
dice1, dice2 = self.roll_dice()
dice = dice1 + dice2
if dice1 == dice2:
dice = dice1 - dice2 * 2
self.player_move(dice, self.playerturn)
else:
dice = sum(dice1, dice2)
self.player_move(dice, self.playerturn)
```
In the if statement, you don't need to dice1 - dice2 *2. That's unnecessary
just call the player_move with dice1
done
but then they will move forward?
@halcyon garnet :white_check_mark: Your 3.12 eval job has completed with return code 0.
-36
@halcyon garnet :white_check_mark: Your 3.12 eval job has completed with return code 0.
-6
tada ๐
i did do that but deleted it
didnt think it worked
import random
import time
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)
def player_move(self, number, player):
if player == "player1":
self.player1pos += number
elif player == "player2":
self.player2pos += number
def roll_dice(self):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print(f"You have rolled {dice1} and {dice2}!")
return [dice1, dice2]
def dice_check(self):
dice1, dice2 = self.roll_dice()
dice = dice1 + dice2
if dice1 == dice2:
self.player_move(-dice1, self.playerturn)
else:
dice = sum(dice1, dice2)
self.player_move(dice, self.playerturn)```
yes
create a new function for the endgame
yep will do
if either player position is 49 or greater, print that that player won
wait in setup_baord we have self.board.append(i) but then board isnt defined in the init
its fine
when you create new self.bla in functions thats not in init, it will โจ magically โจ create them when the function is called
ohh
!e
class Dog:
def __init__(self, name):
self.name = name
def learn_skill(self, skill):
self.skill = skill
print(f"Learned the skill: {skill}")
spike = Dog("Spike")
print(spike.skill)
spike.learn_skill("Fetch")
print(spike.skill)
Not sure if this throws an error
@halcyon garnet :x: Your 3.12 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "/home/main.py", line 10, in <module>
003 | print(spike.skill)
004 | ^^^^^^^^^^^
005 | AttributeError: 'Dog' object has no attribute 'skill'
import random
import time
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)
def player_move(self, number, player):
if player == "player1":
self.player1pos += number
elif player == "player2":
self.player2pos += number
def roll_dice(self):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print(f"You have rolled {dice1} and {dice2}!")
return [dice1, dice2]
def dice_check(self):
dice1, dice2 = self.roll_dice()
dice = dice1 + dice2
if dice1 == dice2:
self.player_move(-dice1, self.playerturn)
else:
dice = sum(dice1, dice2)
self.player_move(dice, self.playerturn)
def check_end(self):
if self.player1pos < 48:
#end game
elif self.player2pos < 48:
#end game
else:
#reutnr no or smth```
If the function did get called before the attribute is created, it should work
!e
class Dog:
def __init__(self, name):
self.name = name
def learn_skill(self, skill):
self.skill = skill
print(f"Learned the skill: {skill}")
spike = Dog("Spike")
spike.learn_skill("Fetch")
print(spike.skill)
@halcyon garnet :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | Learned the skill: Fetch
002 | Fetch
makes sense
Alligator always eats the bigger food
other way
okk
import random
import time
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)
def player_move(self, number, player):
if player == "player1":
self.player1pos += number
elif player == "player2":
self.player2pos += number
def roll_dice(self):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print(f"You have rolled {dice1} and {dice2}!")
return [dice1, dice2]
def dice_check(self):
dice1, dice2 = self.roll_dice()
dice = dice1 + dice2
if dice1 == dice2:
self.player_move(-dice1, self.playerturn)
else:
dice = sum(dice1, dice2)
self.player_move(dice, self.playerturn)
def check_end(self):
if self.player1pos > 48:
#end game
elif self.player2pos > 48:
#end game
else:
#reutnr no or smth
ok don't need the else statement. The game just continues. And the assignment did say to display that the game is over, player has won or smth
so we will print for now
but we also need to reurn true or false
so when we run it we know
bc we will prb use in if statments
yes
import random
import time
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)
def player_move(self, number, player):
if player == "player1":
self.player1pos += number
elif player == "player2":
self.player2pos += number
def roll_dice(self):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print(f"You have rolled {dice1} and {dice2}!")
return [dice1, dice2]
def dice_check(self):
dice1, dice2 = self.roll_dice()
dice = dice1 + dice2
if dice1 == dice2:
self.player_move(-dice1, self.playerturn)
else:
dice = sum(dice1, dice2)
self.player_move(dice, self.playerturn)
def check_end(self):
if self.player1pos > 48:
print("Game ended: Player 1 Wins!")
return True
elif self.player2pos > 48:
print("Game ended: Player 2 Wins!")
return True
else:
return False
now we need this playerturn function
player1
but we need to define it on start game
as plauyer 1
dont we?
sure
but before we do the start game
lets do the turn taking thing first
so create a new function and check if the self.playerturn is player1, then change it to player2
and vice versa
it only needs self in function parameter
there's no return
so we call function turn_checker
sure
I struggle with the name. I sometimes ended up with really long ones
show me when you're done
yeah it can be hard
import random
import time
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)
def player_move(self, number, player):
if player == "player1":
self.player1pos += number
elif player == "player2":
self.player2pos += number
def roll_dice(self):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print(f"You have rolled {dice1} and {dice2}!")
return [dice1, dice2]
def dice_check(self):
dice1, dice2 = self.roll_dice()
dice = dice1 + dice2
if dice1 == dice2:
self.player_move(-dice1, self.playerturn)
else:
dice = sum(dice1, dice2)
self.player_move(dice, self.playerturn)
def check_end(self):
if self.player1pos > 48:
print("Game ended: Player 1 Wins!")
return True
elif self.player2pos > 48:
print("Game ended: Player 2 Wins!")
return True
else:
return False
def turn_checker(self):
if self.playerturn == "player1":
self.playerturn = "player2"
elif self.playerturn == "player2":
self.playerturn = "player1"
else:
print("Errro")
nice. btw, you have dice = dice1 + dice2 in dice_check. Get rid of it since its already in else
oh yes
import random
import time
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)
def player_move(self, number, player):
if player == "player1":
self.player1pos += number
elif player == "player2":
self.player2pos += number
def roll_dice(self):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print(f"You have rolled {dice1} and {dice2}!")
return [dice1, dice2]
def dice_check(self):
dice1, dice2 = self.roll_dice()
if dice1 == dice2:
self.player_move(-dice1, self.playerturn)
else:
dice = sum(dice1, dice2)
self.player_move(dice, self.playerturn)
def check_end(self):
if self.player1pos > 48:
print("Game ended: Player 1 Wins!")
return True
elif self.player2pos > 48:
print("Game ended: Player 2 Wins!")
return True
else:
return False
def turn_checker(self):
if self.playerturn == "player1":
self.playerturn = "player2"
elif self.playerturn == "player2":
self.playerturn = "player1"
else:
print("Errro")
ok so there's a few more to go
other than a way to start the game, you also need to display the board with the current player positions
which one you wanna do first?
could i use what i had before?
def print_board(self):
for i in range(0, len(self.board), 7): #loop through every 7 sections to create our board
print(self.board[i:i+7]) #print every 7 sections```
yeah but that's already there
and it only shows the board
it won't show the player positions yet
lets do that
ok
hmm
I think we're gonna need to redo this function
basically what you should do is not update the self.board and lose the original numbers
make a copy of the board, and then update the board with the player positions is how we should do it
yes ok
do you know how to copy a list?
no i dont
use the .copy()
so we copy the board to another varible
Right
and then update that one?
correct
so then we revert to other one and then update place then print
makes sense
is this in another function
no in the print_board function
you need to update it with what we just said
you know what to do?
ill try
wait i make copy in start game thi?
no
we havent touched start game yet
we're editing the print board function
you said you wanna do this first?
yes bu you was saying coping the board
correct
so in order to properly print the board
we copy the board first
and then update the board with player positions
thats the plan
Let me know if its still confusing
@polar void
this?
import random
import time
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
def print_board(self):
boardcopy = self.board.copy()
if player1pos == player2pos:
boardcopy[player1pos] = "B"
else:
boardcopy[player1pos] = "X"
boardcopy[player1pos] = "O"
for i in range(0, len(self.boardcopy), 7): #loop through every 7 sections to create our board
print(self.boardcopy[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)
def player_move(self, number, player):
if player == "player1":
self.player1pos += number
elif player == "player2":
self.player2pos += number
def roll_dice(self):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print(f"You have rolled {dice1} and {dice2}!")
return [dice1, dice2]
def dice_check(self):
dice1, dice2 = self.roll_dice()
if dice1 == dice2:
self.player_move(-dice1, self.playerturn)
else:
dice = sum(dice1, dice2)
self.player_move(dice, self.playerturn)
def check_end(self):
if self.player1pos > 48:
print("Game ended: Player 1 Wins!")
return True
elif self.player2pos > 48:
print("Game ended: Player 2 Wins!")
return True
else:
return False
def turn_checker(self):
if self.playerturn == "player1":
self.playerturn = "player2"
elif self.playerturn == "player2":
self.playerturn = "player1"
else:
print("Errro")
wait
we dont print then
what is "B"?
oh ok
is that how were ment to do it?
close
plenty of things going on here
- player1pos and player2pos is not referencing to self. So you missed that.
- boardcopy doesn't need to be referencing to self. It can be only available in the function scope, and not riddle it in class scope.
Other than that, it looks OK
you mean this?
boardcopy = self.board.copy()
Yes
But you also have self.boardcopy, which you don't need
so?
import random
import time
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
def print_board(self):
boardcopy = board.copy()
if self.player1pos == self.player2pos:
boardcopy[self.player1pos] = "B"
else:
boardcopy[self.player1pos] = "X"
boardcopy[self.player1pos] = "O"
for i in range(0, len(boardcopy), 7): #loop through every 7 sections to create our board
print(boardcopy[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)
def player_move(self, number, player):
if player == "player1":
self.player1pos += number
elif player == "player2":
self.player2pos += number
def roll_dice(self):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print(f"You have rolled {dice1} and {dice2}!")
return [dice1, dice2]
def dice_check(self):
dice1, dice2 = self.roll_dice()
if dice1 == dice2:
self.player_move(-dice1, self.playerturn)
else:
dice = sum(dice1, dice2)
self.player_move(dice, self.playerturn)
def check_end(self):
if self.player1pos > 48:
print("Game ended: Player 1 Wins!")
return True
elif self.player2pos > 48:
print("Game ended: Player 2 Wins!")
return True
else:
return False
def turn_checker(self):
if self.playerturn == "player1":
self.playerturn = "player2"
elif self.playerturn == "player2":
self.playerturn = "player1"
else:
print("Errro")
better
Last class function is the start game
what do you think start game function does?
makes the board
The board is already made in setup_board and its being called as soon as you init the class
What else?
we start the game so it would run dice
sounds like a function outside of the class
I guess thats what we're gonna do
create a new function (not a class function) for start game
initialize the BoardGame class and assign it a variable
Then you can start utilizing all of its functions
to roll a dice and all that
oh hang on
no I think we're good
Actually, no. Let's add one parameter into the __init__
for the playerturn
So when you init the game, player1 will always go first
yes ok
So I think the game flows like this:
- Init the game with player1 starting.
- print the board.
- call dice_check (not roll_dice)
- call check_end
- print the board
- call turn checker
repeat from step 2
can we not just set player to player1?
what player?
like here
def player_move(self, number, player):
if player == "player1":
self.player1pos += number
elif player == "player2":
self.player2pos += number```
yeah but when do you call player move?
i dont
you do
its in dice_checking
so its already set in there
And what was it you said the values behind self.playerturn?
ohh
ok so anyways
Make the start game function do this โ๏ธ
Let me know when done
Don't forget this โ๏ธ
ok so
i did that
import random
import time
class BoardGame: #allows for no need to return
def __init__(self):
self.setup_board()
self.player1pos = 0
self.player2pos = 0
self.playerturn = "player1"
def print_board(self):
boardcopy = board.copy()
if self.player1pos == self.player2pos:
boardcopy[self.player1pos] = "B"
else:
boardcopy[self.player1pos] = "X"
boardcopy[self.player1pos] = "O"
for i in range(0, len(boardcopy), 7): #loop through every 7 sections to create our board
print(boardcopy[i:i+7]) #print every 7 sections
def setup_board(self):
for i in range(1, 50):
self.board.append(i)
def player_move(self, number, player):
if player == "player1":
self.player1pos += number
elif player == "player2":
self.player2pos += number
def roll_dice(self):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print(f"You have rolled {dice1} and {dice2}!")
return [dice1, dice2]
def dice_check(self):
dice1, dice2 = self.roll_dice()
if dice1 == dice2:
self.player_move(-dice1, self.playerturn)
else:
dice = sum(dice1, dice2)
self.player_move(dice, self.playerturn)
def check_end(self):
if self.player1pos > 48:
print("Game ended: Player 1 Wins!")
return True
elif self.player2pos > 48:
print("Game ended: Player 2 Wins!")
return True
else:
return False
def turn_checker(self):
if self.playerturn == "player1":
self.playerturn = "player2"
elif self.playerturn == "player2":
self.playerturn = "player1"
else:
print("Errro")
so is this a while loop or where do i start it
alright cool
while loop right
ok ill try
this is very confusing
board = BoardGame()
board.setup_board()
while check_end():
board.print_board()
board.dice_check()
```
im not sure what im doing here
you don't need to setup_board() since its already called by _init__
the check_end is only accessible by the BoardGame class
Where's the start game function?
I asked you to make one โ๏ธ @polar void
But I think this is fine
Are you still confused with anything?
let me make it now
@halcyon garnet
im over now
let me send link
the start game function is not a class function that I asked you to do
so it has no reference to self etc
def start_game():
board = BoardGame()
board.setup_board()
while check_end():
board.print_board()
board.dice_check()
Like this
Of course you gotta call the function at the end of the file
so it runs and see if it works
like this?
def start_game():
board = BoardGame()
board.setup_board()
while check_end():
board.print_board()
board.dice_check()
start_game()```
yeah
its almost there
just need to board that check_end()
and check that its false
so the while loop can continue
def start_game():
board = BoardGame()
board.setup_board()
while board.check_end() == False:
board.print_board()
board.dice_check()```
Traceback (most recent call last):
File "C:\Users\littl\OneDrive\Documents\board.py", line 90, in <module>
start_game()
File "C:\Users\littl\OneDrive\Documents\board.py", line 83, in start_game
board = BoardGame()
File "C:\Users\littl\OneDrive\Documents\board.py", line 6, in __init__
self.setup_board()
File "C:\Users\littl\OneDrive\Documents\board.py", line 26, in setup_board
self.board.append(i)
AttributeError: 'BoardGame' object has no attribute 'board'```
well you're getting very close here
so in the setup_board function
before the for loop, add an empty list to self.board
def setup_board(self):
self.board = []
for i in range(1, 50):
self.board.append(i)```
yup
it seems the sum() is not working. Either you add another brackets around dice1, dice2 or change it to dice1 + dice2
pick one
Traceback (most recent call last):
File "C:\Users\littl\OneDrive\Documents\board.py", line 91, in <module>
start_game()
File "C:\Users\littl\OneDrive\Documents\board.py", line 88, in start_game
board.print_board()
File "C:\Users\littl\OneDrive\Documents\board.py", line 12, in print_board
boardcopy = board.copy()
NameError: name 'board' is not defined```
i done that
self.board.copy()
it does not seem like the other person has a go right now
how do you know?
['B', 2, 3, 4, 5, 6, 7]
[8, 9, 10, 11, 12, 13, 14]
[15, 16, 17, 18, 19, 20, 21]
[22, 23, 24, 25, 26, 27, 28]
[29, 30, 31, 32, 33, 34, 35]
[36, 37, 38, 39, 40, 41, 42]
[43, 44, 45, 46, 47, 48, 49]
You have rolled 3 and 2!
[1, 2, 3, 4, 5, 'O', 7]
[8, 9, 10, 11, 12, 13, 14]
[15, 16, 17, 18, 19, 20, 21]
[22, 23, 24, 25, 26, 27, 28]
[29, 30, 31, 32, 33, 34, 35]
[36, 37, 38, 39, 40, 41, 42]
[43, 44, 45, 46, 47, 48, 49]
You have rolled 2 and 4!
[1, 2, 3, 4, 5, 6, 7]
[8, 9, 10, 11, 'O', 13, 14]
[15, 16, 17, 18, 19, 20, 21]
[22, 23, 24, 25, 26, 27, 28]
[29, 30, 31, 32, 33, 34, 35]
[36, 37, 38, 39, 40, 41, 42]
[43, 44, 45, 46, 47, 48, 49]
You have rolled 1 and 2!```
there is no x
ok double check your code. Why is X not showing on the board?
and for some reason x does not go to 1
i found it
but we dont use turn def
boardcopy[self.player1pos] = "X"
boardcopy[self.player2pos] = "O"```
boardcopy[self.player1pos] = "X"
boardcopy[self.player1pos] = "O"```
you didn't add turn checker in your loop
so then how would it change turn?
you call turn checker
we run it from dice function?
no
ohh
i thought you said we dont need to
i fixed it
yes
do you know how to make it look more visually plessing?
any ideas
bc its hard to see the player pieces rn
Oh I'm leaving that to you. But didn't your teacher say no GUI?
yes withoit gui
just with the text
any ideas?
like chage the letter?
You could change the "X" and "O" to... idk... "Player 1" "Player 2"
mabye
ill have a look
but thank you
np
over 570 msg thank you for your time
So as promised, I helped you get from 1 to 4 and some on 5
You still have to handle the rest on your own
thats all good
i think i can do it
And with a cleaner solution as you can see with the classes
yes
thank you man
Type !close for now
Take a break. Tackle the rest after that
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.