0

I made a code of connect 4 in python turtle, and i made a list of all the columns. There are 7 columns, but if you type in '8' it will end the game with an error. Can i change it somehow that it doesn't give you an error but instead just does nothing and you can keep playing? So for example, if you type an 8 or 9 by mistake, it doesnt completely stop the game? This is my code:

import turtle

pen = turtle.Turtle()
screen = turtle.Screen()
screen.setup(900,900)

screen.title("Vier op een rij")
screen.tracer(0,0)

fontText = ("Arial", 16)

pen.ht()
pen.penup()
pen.speed(0)

def board():
    pen.color("blue")
    pen.fillcolor("blue")
    pen.goto(-350,-400)
    pen.pendown()
    pen.begin_fill()
    pen.goto(350,-400)
    pen.goto(350,200)
    pen.goto(-350,200)
    pen.goto(-350,-400)
    pen.end_fill()
    pen.penup()

def circles(x,y):
    pen.goto(x,y)
    pen.color("white")
    pen.fillcolor("white")
    pen.begin_fill()
    for i in range(7):
        pen.pendown()
        pen.circle(25)
        pen.penup()
        pen.forward(100)
    pen.end_fill()
    pen.penup()
    
def number(x,y,text):
    pen.color("black")
    pen.goto(x,y)
    pen.write(text, font=fontText, align="center")

def drawCircles():
    circles(-300, -370)
    circles(-300,-270)
    circles(-300,-170)
    circles(-300,-70)
    circles(-300,30)
    circles(-300,130)

def numbers():
    number(-300,220,"Kolom 1")
    number(-200,220,"Kolom 2")
    number(-100,220,"Kolom 3")
    number(0,220,"Kolom 4")
    number(100,220,"Kolom 5")
    number(200,220,"Kolom 6")
    number(300,220,"Kolom 7")


numbers()           
board()
drawCircles()
screen.update()

def Circle(x,y,color):
    pen.penup()
    pen.color(color)
    pen.fillcolor(color)
    pen.goto(x,y)
    pen.pendown()
    pen.begin_fill()
    pen.circle(25)
    pen.end_fill()
    pen.penup()
    
y = -370
kolomGevuld = [0,0,0,0,0,0,0]
rijGevuld = [y,y,y,y,y,y,y]
space = 100

while True:
    red = int(input("In welke kolom wil je een rode schijf plaatsen? "))
    kolomGevuld[red-1] += 1
    if kolomGevuld[red-1] < 7:
        Circle(-300 + (red-1) * space, rijGevuld[red-1],"red")
    rijGevuld[red-1] += space
    screen.update()
    
    yellow = int(input("In welke kolom wil je een gele schijf plaatsen? "))
    kolomGevuld[yellow-1] += 1
    if kolomGevuld[yellow-1] < 7:
        Circle(-300 + (yellow-1) * space, rijGevuld[yellow-1],"yellow")
    rijGevuld[yellow-1] += space
    screen.update()
    

turtle.done()

1 Answers1

1

Check if the array indices are getting out of range right after taking input and if the indices are out of range, continue with the next iteration of the while loop. I have commented down the lines of code that I have added to your code

while True:
    red = int(input("In welke kolom wil je een rode schijf plaatsen? "))
    # check if red is out of range or not and if yes, continue
    if red < 1 or red > 7:
        continue
    kolomGevuld[red-1] += 1
    if kolomGevuld[red-1] < 7:
        Circle(-300 + (red-1) * space, rijGevuld[red-1], "red")
    rijGevuld[red-1] += space
    screen.update()

    yellow = int(input("In welke kolom wil je een gele schijf plaatsen? "))
    # check if yellow is out of range or not and if yes, continue
    if yellow < 1 or yellow > 7:
        continue
    kolomGevuld[yellow-1] += 1
    if kolomGevuld[yellow-1] < 7:
        Circle(-300 + (yellow-1) * space, rijGevuld[yellow-1], "yellow")
    rijGevuld[yellow-1] += space
    screen.update()
Geeky Quentin
  • 2,469
  • 2
  • 7
  • 28