Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code Help!!
#1
Hi, my son is in year 7 and hit a bit of a problem with is coding project and hoping someone out there in code land can help! It's a simple snakes and ladders programme and he just needs to get the players onto the board and have them move up or down 'snakes' and 'ladders'. Sadly, I have no idea. Would really appreciate any imput. This is where he has got to:
from turtle import *
def draw_board():
        
    for row_number in range(10):
        for square_number in range(10):
            penup()
            goto(square_number*SQUARE_SIZE, row_number*20)
            pendown()
            draw_square(SQUARE_SIZE)

speed(0)
SQUARE_SIZE = 20
def move(player_num):
    result = diceroll()
    #if player_num == 1:
        

def draw_square(size):
    forward(size)
    left(90)
    forward(size)
    left(90)
    forward(size)
    left(90)
    forward(size)
    left(90)

def draw_player(square,c):
    penup()
    goto(coords_of(square))
    color(c)
    begin_fill()

    forward(7)
    right(90)
    forward(7)
    right(90)
    forward(14)
    right(90)
    forward(14)
    right(90)
    forward(14)
    right(90)
    forward(7)


    end_fill()

    

def players():
  for i in range(0,amount):
    print('Player' , i+1, 'enter your name')
    name=input("\n")
    print('Welcome to snakes an laders!')
    names.append(name)
  print('\nplayers:')
  for i in names:
    print(i)
  print('\n')

def coords_of(square):
    go_up = square // 10
    go_right = square % 10
    return( SQUARE_SIZE*(go_right+0.5) , SQUARE_SIZE*(go_up+0.5))
    
    

def amountplayers():
    a=0
    while a<1:
        amountplayers=int(input('How many players(2,4) ?'))
        if amountplayers>4:
            print('Has to be between 2, 4')
    
    
        elif amountplayers<2:
            print('Has to be between 2 and 4: 3, 4')
        else:
            a+=1
    print(amountplayers, 'players\n')
    return amountplayers

def diceroll():
	d1=(random.randint(1,6))
	d2=(random.randint(1,6))
	roll=d1+d2
	return(roll)


    
square ={0:0}    
snakes={17:7,54:20,62:43,64:4,87:63,93:73,96:21,99:21}
ladders={4:10,9:22,20:38,28:56,40:19,51:16,63:18,71:20}


import random
pos=[0,0,0,0]
names=[]
def players():
  for i in range(0,amount):
    print('Player' , i+1, 'enter your name')
    name=input("\n")
    print('Welcome to snakes an laders!')
    names.append(name)
  print('\nplayers:')
  for i in names:
    print(i)
  print('\n')

def coords_of(square):
    go_up = square // 10
    go_right = square % 10
    return( SQUARE_SIZE*(go_right+0.5) , SQUARE_SIZE*(go_up+0.5))
    
    

def amountplayers():
    a=0
    while a<1:
        amountplayers=int(input('How many players(2,4) ?'))
        if amountplayers>4:
            print('Has to be between 2, 4')
    
    
        elif amountplayers<2:
            print('Has to be between 2 and 4: 3, 4')
        else:
            a+=1
    print(amountplayers, 'players\n')
    return amountplayers

def diceroll():
	d1=(random.randint(1,6))
	d2=(random.randint(1,6))
	roll=d1+d2
	return(roll)
diceroll()


    
square ={0:0}    
snakes={17:7,54:20,62:43,64:4,87:63,93:73,96:21,99:21}
ladders={4:10,9:22,20:38,28:56,40:19,51:16,63:18,71:20}


import random
pos=[0,0,0,0]
names=[]


draw_board()

amount = amountplayers()

for from_square, to_square in snakes.items():
    color('red')
    penup()
    goto(coords_of(from_square))
    pendown()
    goto(coords_of(to_square))

for from_square, to_square in ladders.items():
    color('blue')
    penup()
    goto(coords_of(from_square))
    pendown()
    goto(coords_of(to_square))

    



      
#location_1 = 0
#location_2 = 0   
#player_number = int(input('Which player(1,2)? '))
#if player_number == 1:
    #move(location_1)

#while location_1 < 100 and location_2 < 100:
 #for from_square, to_c in square.items():
        


players_pos={}
def assign():
    for i in names:
        x=names.index(i)
        players_pos[i]=pos[x]

assign()
        
def move():
    q=0
    while q<1:
        for i in names:
            y=diceroll()
            z=pos[names.index(i)]
           
            print('\n', i)
            print('player is currently on', z)
            print('Type a number from 1-6')
            input()
            print('you rolled a', y)
            w=y+z
            pos[names.index(i)]=w
            print('player is now on',w)

            if pos[names.index(i)]>100:
                q+=2
            elif pos[names.index(i)]==100:
                q+=2
            if q>1:
                break
            for a in snakes:
                if a == pos[names.index(i)]:
                    print('unluckyyy, you hit a snake!')
                    print('the snake is', snakes[a], 'long')
                    print('you go down', snakes[a], 'places')
                    pos[names.index(i)]-=snakes[a]
                    print('you are now on', pos[names.index(i)])
                for a in ladders:
                  if a ==pos[names.index(i)]:
                    print("very nice, you're on a ladder!")
                    print('the ladder is', ladders[a], 'long')
                    print('you go up', ladders[a], 'places')
                    pos[names.index(i)]+=ladders[a]

                    print('you are now on', pos[names.index(i)])

                    move()

def main():
  players()
  assign()
  move()

  print('player wins')

main()
Reply
#2
Posting a couple hundred lines of code asking for help without explaining what is going wrong is not a good way to get answers. It runs fine to start, I don't feel like playing a whole game trying to find the problem. Please clearly state what the problem is, and if possible, trim the code down to a smaller piece that replicates the problem.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

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