Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Connect 4 problem
#1
Hello, i am coding a connect 4 code(currently unfinished) and have encountered a problem. If you run the code and keep entering the same number, it should fill up the column then stop. the problem i have however if that its stops 1 before being full. Here is the code:
import turtle
import math
Win = turtle.Screen()
t = turtle.Turtle()
t.speed(11)

rowNum = 6
row1 = [0,0,0,0,0,0,0]
row2 = [0,0,0,0,0,0,0]
row3 = [0,0,0,0,0,0,0]
row4 = [0,0,0,0,0,0,0]
row5 = [0,0,0,0,0,0,0]
row6 = [0,0,0,0,0,0,0]

grid = [[0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0]]  #game board

def up(distance): # goes up by (distance) squares

    t.left(90)
    t.forward(distance * 90)
    t.right(90) 

def down(distance): # goes down by (distance) squares
    t.right(90)
    t.forward(distance * 90)
    t.left(90)

def right(distance): # goes right by (distance) squares
    t.forward(distance * 90)

def left(distance): # goes down by (distance) squares
    t.right(180)
    t.forward(distance * 90)
    t.right(180) 

def cross():
    t.penup()
    t.forward(5)
    t.right(90)
    t.forward(5)
    t.left(45)
    t.pendown()
    t.forward(math.sqrt(12800))
    t.penup()
    t.right(135)
    t.forward(80)
    t.right(135)
    t.pendown()
    t.forward(math.sqrt(12800))
    t.penup()
    t.left(135)
    t.forward(90)
    t.right(90)
    t.forward(5)
    t.right(90)
    t.forward(5) # draws a cross

def triangle(): # draws a triangle
    for i in range (3):
        t.forward(80)
        t.left(120)

def location(): # moves turtle to starting location

    t.penup()
    t.right(180)
    t.forward(319)
    t.right(180)
    t.left(90)
    t.forward(180)
    t.right(90)
    t.pendown()

def square(): # draws a square
    for i in range(4):
        t.forward(90)
        t.left(90)

def row(): # draws a row
    for i in range(7):
        square()
        t.forward(90)

def drawGrid(): # draws the grid
    row()
    for i in range(5):
        t.right(180)
        t.forward(630)
        t.left(90)
        t.forward(90)
        t.left(90)
        row()
    left(7)

def userInput(): # takes the users input and draws it on the board
    upNum = 0
    num = float(input("which column would you like the cross in? "))
    while num > 7 or num == 0 or num % 1 != 0: # Makes sure the user enters a number between 1 and 7
        print ('That number is not a column. Please enter numbers between 1 and 7')
        num = float(input("which column would you like the cross in? "))
    num = int(num)
    for x in range(rowNum):
        if grid[x][num-1] == 0:
            grid[x][num-1] = 1
            upNum = x + 1
            break
    if grid[5][num-1] == 1 or grid[5][num-1] == 2: # stops the user from putting a counter in a full column 
        print (grid)
        print('This row is full. Please choose another.')
        userInput()
    print(grid)
    up(int(upNum))
    right(int(num-1))
    cross()
    left(int(num-1))
    down(int(upNum))
    if upNum == 1:
        row1[num-1] = 1
    elif upNum == 2:
        row2[num-1] = 1
    elif upNum == 3:
        row3[num-1] = 1
    elif upNum == 4:
        row4[num-1] = 1
    elif upNum == 5:
        row5[num-1] = 1
    elif upNum == 6:
        row6[num-1] = 1
    return num

def main():
    location()
    drawGrid()
    while True:
        userInput()

main()

Win.exitonclick()

i believe the problem is with the for loop in user input.
thanks in advance
Reply
#2
On line 109 you put the piece on the board. Then on line 112 you check to see if the row is full. So the piece you just played fills the row and then makes the piece you just played an invalid move. You should check that the row is full before you place the piece.
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