#๐Ÿ”’ 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
#

https://paste.pythondiscord.com/CDUA

Traceback (most recent call last):
  File "C:\Users\littl\OneDrive\Documents\board.py", line 77, in <module>
    game = BoardGame(board, player1pos, player2pos, dice, player="1")
NameError: name 'dice' is not defined. Did you mean: 'dict'?```
tight roverBOT
#

@polar void

Python help channel opened

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.

rocky ridge
#

You haven't defined what dice is by that point in the program

open orchid
#
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

rocky ridge
#

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

polar void
rocky ridge
#

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

polar void
#

but if i make the function i dont need that

open orchid
polar void
# open orchid 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'
open orchid
#
def start_game(self, board, player1pos, player2pos):
``` the function doesnt take a parameter named player
polar void
rocky ridge
# polar void wait how?

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()
open orchid
#

yes, because doing BoardGame(bla bla bla some stuff here) calls the __init__ function: ```py
def init(self, board, player1pos, player2pos, dice, player):

open orchid
#

and then use the correct parameters when calling BoardGame.start_game

polar void
#

or dict

#

bc idk what dict is

open orchid
#

dice, yes, mb

#

typo

polar void
#

yeah just checking

polar void
#

so even tho i dont use it

#

i just say none

open orchid
#

sure

polar void
open orchid
#

yes

polar void
#

then how should it be

polar void
# open orchid 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?

rocky ridge
#

up to you really

#

I'd probably run them from that method so you don't need to store dice rolls across method calls

rocky ridge
#

yea

halcyon garnet
#

Keep it really simple to what the function is supposed to do

#

roll the dice and get the value of the dice back

polar void
open orchid
#

this is such confusing code lol

polar void
#

idk tbh im just trying to make this board game

#

if you lot got ideas how to make code better ill do it

halcyon garnet
#
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

halcyon garnet
#

Sure

polar void
#

but yes that makes sense then when i return it ill call the other one

halcyon garnet
#

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()))
tight roverBOT
#

@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
polar void
halcyon garnet
polar void
#

i know what you mean

#

yes ok i will do that

polar void
halcyon garnet
#

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

polar void
polar void
halcyon garnet
#

and of course, explain how to win the game, that's the most important part haha

polar void
#

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

halcyon garnet
#

so its like snakes and ladders but with no snakes or ladders?

polar void
#

i will add somthing like that after

#

this is what we got given if it helps you

halcyon garnet
# polar void well not yet

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.

polar void
#

plus a bit more

halcyon garnet
polar void
#

i added hte label

halcyon garnet
#

ah ok

halcyon garnet
polar void
#

just no gui

halcyon garnet
#

ah nice

polar void
#

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

halcyon garnet
#

If you rolled a double, you have to move back with that number

halcyon garnet
polar void
halcyon garnet
#

what file?

polar void
#

so just store data in a text file

halcyon garnet
#

ok

polar void
#

nothing big

halcyon garnet
#

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

polar void
polar void
halcyon garnet
#

So not exactly starting from 0 but at least its a good enough start

#

Let me know when you're done

polar void
#
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 
halcyon garnet
#

ok nice

#

now lets fix up a few things here

polar void
#

done

halcyon garnet
#

remove player1pos = 0

#

and player2pos =0

#

game = "game"

#

currentplayer= "1"

polar void
#

done

halcyon garnet
#

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

polar void
#

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



    
halcyon garnet
#

โœจ clean โœจ

#

OK

#

We have to fix that self.board

polar void
#

yes

halcyon garnet
#

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

polar void
#
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)```
halcyon garnet
#

Do the following replacement:

- self.board = board
+ self.setup_board()
polar void
#

loops 1 to 49 and appeneds each one

halcyon garnet
#

Show me

polar void
#
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)```
halcyon garnet
#

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

polar void
#
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):
        
polar void
halcyon garnet
#

needs 1 more function param

#

player

#

to help indicate which player to update

#

then in the function

polar void
#

like that?

halcyon garnet
#

make an if statement

#

yeah

polar void
#

yep if satement for what?

halcyon garnet
#

if player is player 1, self.player1pos += number

#

and repeat for player 2

#

show me when you're done

polar void
# halcyon garnet 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```
halcyon garnet
#

lookin good

polar void
#

player1 would make code more readable instead of 1

halcyon garnet
#

sure

#

Next, we gotta get that roll dices in

#

Can you do that next step?

#

I've given the code before

polar void
halcyon garnet
#

yeah

#

new class function

polar void
# halcyon garnet new class 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}!")
halcyon garnet
#

ok close

#

you need the return

polar void
#

wait would we need self?

halcyon garnet
#

so return dice1 and dice2

halcyon garnet
polar void
#
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
polar void
#

bc we dont use it

#

def roll_dice(self):

halcyon garnet
polar void
halcyon garnet
#

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?

polar void
#

so this new function

#

will add dice

#

and check if they match

#

so if so we call it dice_check

#

unless im missing somthing

halcyon garnet
#

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

polar void
#

yes i was thinking that

halcyon garnet
#

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

polar void
polar void
halcyon garnet
#

sure

#

lets go for it

polar void
#

hold on

#

this is confusing

    def dice_check(self):
        dice = roll_dice
        if dice.dice1 == dice.dice2```
halcyon garnet
#

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

polar void
#

so im ment to get the dice

#

which reutrns

halcyon garnet
#

ah but no

polar void
#

but how do i check it

#

i know its wrong

#

but im not sure how

halcyon garnet
#

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

polar void
#

but how do i check if dice1 is the same as dice 2

#

bc i cannot call it twice

halcyon garnet
#

ok so you wanna call it inside the function?

polar void
#

yes if you think thats good?

halcyon garnet
#

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

polar void
#

that was my problem

halcyon garnet
#

I'll show a different example of unpacking and assignment

polar void
halcyon garnet
#

!e

a = "Bob"
b = "Susan"
print(a)
print(b)

This is the simplest and no unpacking thing

tight roverBOT
#

@halcyon garnet :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | Bob
002 | Susan
halcyon garnet
#

!e

a, b = "Bob", "Susan"
print(a)
print(b)
tight roverBOT
#

@halcyon garnet :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | Bob
002 | Susan
halcyon garnet
#

This is how you assign and unpack multiple things

#

There are other ways to unpack but we keep it simple to a function call

polar void
#

but we need to return it

halcyon garnet
#

!e

def people():
  return ["Bob", "Susan"]

a, b = people()
print(a)
print(b)
tight roverBOT
#

@halcyon garnet :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | Bob
002 | Susan
polar void
#

that makes more sense

halcyon garnet
#

Do you understand from this example?

polar void
#

yes

halcyon garnet
#

nice

polar void
#

ill be back with the function

#

one sec

polar void
# halcyon garnet 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
        
        ```
halcyon garnet
#

close

polar void
#

roll_dice

#

is ment ti be dice1

#

and two

#

i just add

#

not sum

halcyon garnet
#
  1. don't sum() it since its not required
polar void
#

dice = dice1 + dice2

halcyon garnet
#
  1. the self needs to be behind roll_dice. Not in the ()
halcyon garnet
#

its not required to add them yet

polar void
#

self.roll_dice?

halcyon garnet
polar void
polar void
#
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
        
        ```
halcyon garnet
#

the sum of the 2 dices will be in the else

#

ok good

polar void
#

that makes sense

halcyon garnet
#

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

polar void
#

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

halcyon garnet
polar void
#

and i done dice = dice1 - (dice2 * 2) to turn it into negitve so it will move backwards

polar void
#

will do

halcyon garnet
#

you basically need to pass self.playerturn as the 2nd param of the player move call

halcyon garnet
polar void
#

and then we will make function in min

polar void
#

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)
        
        ```
halcyon garnet
#

In the if statement, you don't need to dice1 - dice2 *2. That's unnecessary

#

just call the player_move with dice1

#

done

polar void
#

but then they will move forward?

halcyon garnet
#

ah then dice1 * -dice2

#

!e

dice1 = 6
dice2 = 6
print(dice1 * -dice2)

Testing

tight roverBOT
#

@halcyon garnet :white_check_mark: Your 3.12 eval job has completed with return code 0.

-36
halcyon garnet
#

yikes

#

ah wait

#

!e

dice1 = 6
print(-dice1)
tight roverBOT
#

@halcyon garnet :white_check_mark: Your 3.12 eval job has completed with return code 0.

-6
halcyon garnet
#

tada ๐ŸŽ‰

polar void
#

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)```
halcyon garnet
#

better

#

ok lets handle the endgame

#

should be straightforward

polar void
#

yes

halcyon garnet
#

create a new function for the endgame

polar void
#

yep will do

halcyon garnet
#

if either player position is 49 or greater, print that that player won

polar void
#

wait in setup_baord we have self.board.append(i) but then board isnt defined in the init

halcyon garnet
#

its fine

#

when you create new self.bla in functions thats not in init, it will โœจ magically โœจ create them when the function is called

halcyon garnet
#

!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

tight roverBOT
#

@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'
halcyon garnet
#

yeah because the function isn't called yet

#

But

polar void
#
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```
halcyon garnet
#

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)
tight roverBOT
#

@halcyon garnet :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | Learned the skill: Fetch
002 | Fetch
polar void
#

makes sense

halcyon garnet
polar void
#

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
halcyon garnet
polar void
#

but we also need to reurn true or false

#

so when we run it we know

#

bc we will prb use in if statments

halcyon garnet
#

sure you can do that

#

so in both if and elif, return True

#

return False in else

polar void
#
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

halcyon garnet
#

you got it

#

So what do you think the value behind self.playerturn looks like?

polar void
#

but we need to define it on start game

#

as plauyer 1

#

dont we?

halcyon garnet
#

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

polar void
#

so we call function turn_checker

halcyon garnet
#

sure

#

I struggle with the name. I sometimes ended up with really long ones

#

show me when you're done

polar void
# halcyon garnet show me when you're done
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")


halcyon garnet
polar void
#

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")


halcyon garnet
#

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?

polar void
halcyon garnet
#

and it only shows the board

polar void
#

is it?

#

ohhh

#

yes ok

halcyon garnet
#

it won't show the player positions yet

polar void
#

lets do that

halcyon garnet
#

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

halcyon garnet
#

do you know how to copy a list?

polar void
halcyon garnet
#

use the .copy()

polar void
halcyon garnet
#

Right

polar void
#

and then update that one?

halcyon garnet
#

correct

polar void
#

so then we revert to other one and then update place then print

#

makes sense

#

is this in another function

halcyon garnet
#

no in the print_board function

#

you need to update it with what we just said

#

you know what to do?

polar void
halcyon garnet
#

ok

#

show me when you're done

polar void
halcyon garnet
#

we havent touched start game yet

#

we're editing the print board function

halcyon garnet
polar void
halcyon garnet
#

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

polar void
# halcyon garnet <@1071772788504924180>

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

polar void
#

so if both players are there

#

it would be b

halcyon garnet
#

oh ok

polar void
#

is that how were ment to do it?

halcyon garnet
#

close

#

plenty of things going on here

#
  1. player1pos and player2pos is not referencing to self. So you missed that.
  2. 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

polar void
halcyon garnet
#

But you also have self.boardcopy, which you don't need

polar void
# halcyon garnet 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")


halcyon garnet
#

Last class function is the start game

#

what do you think start game function does?

polar void
halcyon garnet
#

What else?

polar void
#

we start the game so it would run dice

halcyon garnet
#

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

polar void
halcyon garnet
#

So I think the game flows like this:

  1. Init the game with player1 starting.
  2. print the board.
  3. call dice_check (not roll_dice)
  4. call check_end
  5. print the board
  6. call turn checker
    repeat from step 2
polar void
#

can we not just set player to player1?

halcyon garnet
polar void
#

like here

#
    def player_move(self, number, player):
        if player == "player1":
            self.player1pos += number

        elif player == "player2":
            self.player2pos += number```
halcyon garnet
polar void
#

i dont

halcyon garnet
#

you do

polar void
#

wait

#

self.player_move(-dice1, self.playerturn)

halcyon garnet
#

its in dice_checking

#

so its already set in there

#

And what was it you said the values behind self.playerturn?

polar void
#

ohh

halcyon garnet
#

ok so anyways

halcyon garnet
#

Let me know when done

halcyon garnet
polar void
polar void
# halcyon garnet Don't forget 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
        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")


polar void
polar void
#

while loop right

halcyon garnet
#

yeah while loop

#

while turn checker

#

oh wait

#

while check_end

polar void
#

ok ill try

polar void
#
board = BoardGame()
board.setup_board()

while check_end():
    board.print_board()
    board.dice_check()
    ```
#

im not sure what im doing here

halcyon garnet
#

you don't need to setup_board() since its already called by _init__

#

the check_end is only accessible by the BoardGame class

halcyon garnet
polar void
#

did we

halcyon garnet
halcyon garnet
#

Are you still confused with anything?

polar void
#

@halcyon garnet

#

im over now

#

let me send link

halcyon garnet
#

the start game function is not a class function that I asked you to do

#

so it has no reference to self etc

halcyon garnet
#

Of course you gotta call the function at the end of the file

#

so it runs and see if it works

polar void
#
def start_game():
  board = BoardGame()
  board.setup_board()

  while check_end():
      board.print_board()
      board.dice_check()

start_game()```
halcyon garnet
#

yeah

#

its almost there

#

just need to board that check_end()

#

and check that its false

#

so the while loop can continue

polar void
#
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'```
halcyon garnet
halcyon garnet
#

before the for loop, add an empty list to self.board

polar void
halcyon garnet
#

it seems the sum() is not working. Either you add another brackets around dice1, dice2 or change it to dice1 + dice2

#

pick one

polar void
# halcyon garnet yup
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```
polar void
halcyon garnet
polar void
# halcyon garnet 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

halcyon garnet
polar void
#

and for some reason x does not go to 1

halcyon garnet
#

ok and ?

#

X is player 1 or player 2?

polar void
#

x is one

#

but now two doenst have a goi

halcyon garnet
#

what?

#

What did you change?

polar void
#

but we dont use turn def

polar void
#
            boardcopy[self.player1pos] = "X"
            boardcopy[self.player1pos] = "O"```
halcyon garnet
polar void
halcyon garnet
polar void
#

we run it from dice function?

halcyon garnet
#

no

polar void
#

i thought you said we dont need to

#

i fixed it

halcyon garnet
#

and?

#

All good?

polar void
#

do you know how to make it look more visually plessing?

#

any ideas

#

bc its hard to see the player pieces rn

halcyon garnet
polar void
#

just with the text

#

any ideas?

#

like chage the letter?

halcyon garnet
polar void
#

ill have a look

#

but thank you

halcyon garnet
#

np

polar void
#

over 570 msg thank you for your time

halcyon garnet
#

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

polar void
#

i think i can do it

halcyon garnet
#

And with a cleaner solution as you can see with the classes

polar void
#

thank you man

halcyon garnet
#

Take a break. Tackle the rest after that

polar void
#

ok thank you

#

!close

tight roverBOT
#
Python help channel closed

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.