Connect 4 Game - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Connect 4 Game (/thread-9921.html) Pages:
1
2
|
Connect 4 Game - DarksideMoses - May-04-2018 Hello I am working on a school assignment for a Connect 4 Game. I have a error when I hit run on the code below it says line 76, in checkrow if x + i < 7 and connect[x + i][y] == turn : TypeError: can only concatenate list (not "int") to list If anyone could help with that that would be awesome. import turtle screen = turtle.Screen() screen.delay(0) chip = turtle.Turtle() chip.shape("circle") chip.color("green") chip.penup() chip.speed(0) connect = [] CHIP_DIST = 30 # the distance each chip will be apart ORIGIN_X = -90 ORIGIN_Y = -100 RIGHTMOST_COL = ORIGIN_X + CHIP_DIST * 6 y = chip.ycor() x = chip.xcor() for i in range(7): connect.append([]) x = connect[i] for j in range(7): x.append(0) chip.goto(ORIGIN_X + i * CHIP_DIST, ORIGIN_Y + j * CHIP_DIST) chip.stamp() chip.color("blue") chip.goto(ORIGIN_X + 3 * CHIP_DIST, ORIGIN_Y + 7 * CHIP_DIST) running = True def r(): if running: x = chip.xcor() if x < RIGHTMOST_COL: chip.setx(x + CHIP_DIST) else: chip.setx(ORIGIN_X) def l(): if running: if x > ORIGIN_X: chip.setx(x - CHIP_DIST) else: chip.setx(RIGHTMOST_COL) turn = 1 def d(): global turn, running if running: x = int((chip.xcor() - ORIGIN_X) / CHIP_DIST) col = connect[x] for y in range(len(col)): if col[y] == 0: chip.sety(ORIGIN_Y + y * CHIP_DIST) chip.stamp() chip.goto(ORIGIN_X + 3 * CHIP_DIST, ORIGIN_Y + 7 * CHIP_DIST) connect[x][y] = turn # add code here if turn == 1: turn = 2 chip.color("red") else: turn = 1 chip.color("blue") break def checkrow(x,y): count = 1 for i in range(1, 4): if x + i < 7 and connect[x + i][y] == turn: count += 1 else: break for i in range(1, 4): if x - i > -1 and connect[x - i][y] == turn: count += 1 else: break if count >= 4: return True return False def checkcolumn(x, y): count = 1 for i in range(1, 4): if y + i < 7 and connect[x][y + i] == turn: count += 1 else: break for i in range(1, 4): if y - i > -1 and connect[x][y - i] == turn: count += 1 else: break if count >= 4: return True return False def haswinner(x, y): return checkrow(x, y) or checkcolumn(x, y) if haswinner(x, y): running = False print "Player " + str(turn) + " is the winner!" else: if turn == 1: turn = 2 chip.color("red") else: turn = 1 chip.color("blue") screen.onkey(r, "Right") screen.onkey(l, "Left") screen.onkey(d, "Down") screen.listen() turtle.done() RE: Connect 4 Game - nilamo - May-04-2018 (May-04-2018, 02:59 PM)DarksideMoses Wrote: TypeError: can only concatenate list (not "int") to listThat error should be pretty self-explanitory. It doesn't make sense to add a number to a list. >>> ["spam", "eggs"] + 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate list (not "int") to list x is a list. Did you want it to be something else?Quote:connect = [] # ...snip... for i in range(7): connect.append([]) x = connect[i] RE: Connect 4 Game - DarksideMoses - May-10-2018 Thanks nilamo for the help now I have a error on the code below it says. line 113 else : ^ IndentationError: unindent does not match any outer indentation level. I tried to move some things around in the code but that just gave me more errors. If you have an idea on how to fix the error I would love to hear it. import turtle screen = turtle.Screen() screen.delay(0) chip = turtle.Turtle() chip.shape("circle") chip.color("green") chip.penup() chip.speed(0) connect = [] CHIP_DIST = 30 # the distance each chip will be apart ORIGIN_X = -90 ORIGIN_Y = -100 RIGHTMOST_COL = ORIGIN_X + CHIP_DIST * 6 y = chip.ycor() x = chip.xcor() for i in range(7): connect.append([]) x = connect[i] for j in range(7): x.append(0) chip.goto(ORIGIN_X + i * CHIP_DIST, ORIGIN_Y + j * CHIP_DIST) chip.stamp() chip.color("blue") chip.goto(ORIGIN_X + 3 * CHIP_DIST, ORIGIN_Y + 7 * CHIP_DIST) running = True def r(): if running: x = chip.xcor() if x < RIGHTMOST_COL: chip.setx(x + CHIP_DIST) else: chip.setx(ORIGIN_X) def l(): if running: if x > ORIGIN_X: chip.setx(x - CHIP_DIST) else: chip.setx(RIGHTMOST_COL) turn = 1 def d(): global turn, running if running: x = int((chip.xcor() - ORIGIN_X) / CHIP_DIST) col = connect[x] for y in range(len(col)): if col[y] == 0: chip.sety(ORIGIN_Y + y * CHIP_DIST) chip.stamp() chip.goto(ORIGIN_X + 3 * CHIP_DIST, ORIGIN_Y + 7 * CHIP_DIST) connect[x][y] = turn # add code here if turn == 1: turn = 2 chip.color("red") else: turn = 1 chip.color ("blue") break def check_row(x, y): count = 1 for i in range(1, 4): if x + i < 7 and connect[x + i][y] == turn: count += 1 else: break for i in range(1, 4): if x - i > -1 and connect[x - i][y] == turn: count += 1 else: break if count >= 4: return True return False def check_column(x, y): count = 1 for i in range(1, 4): if y + i < 7 and connect[x][y + i] == turn: count += 1 else: break for i in range(1, 4): if y - i > -1 and connect[x][y - i] == turn: count += 1 else: break if count >= 4: return True return False def haswinner(x, y): return check_row(x ,y) or check_column(x, y) if haswinner(x,y) : running = False print "Player " + str(turn) + " is the winner!" #this is where the error is else : if turn == 1 : turn = 2 chip.color("red") else : turn = 1 chip.color("blue") screen.onkey(r, "Right") screen.onkey(l, "Left") screen.onkey(d, "Down") screen.listen() turtle.done() RE: Connect 4 Game - nilamo - May-10-2018 (May-10-2018, 03:11 PM)DarksideMoses Wrote:if haswinner(x,y) : running = False print "Player " + str(turn) + " is the winner!" #this is where the error is else : if turn == 1 : turn = 2 chip.color("red") else : turn = 1 chip.color("blue") I think the error you get is pretty clear. Your indentation is all over the place in that block. RE: Connect 4 Game - DarksideMoses - May-10-2018 Ok it fixed that now it says I still have to fix first error how would I fix that? RE: Connect 4 Game - nilamo - May-10-2018 What's the error? Please share the full traceback. RE: Connect 4 Game - DarksideMoses - May-10-2018 ok so the error with indention is fixed. But this one line 76 TypeError: can only concatenate list (not "int") to list is still there I thought it was fixed cause there was no red line but when I pressed play it showed the error. RE: Connect 4 Game - nilamo - May-10-2018 (May-04-2018, 02:59 PM)DarksideMoses Wrote:if x + i < 7 and connect[x + i][y] == turn: That's your line 76 from above. The only way that makes sense, is if x is a list, or if your code has changed. There's no way for us to know which is the case, unless you share the entire error message.
RE: Connect 4 Game - DarksideMoses - May-11-2018 @nilamo here is the code in its entirety after I made some corrections. If you could give me some guidance on how to fix the errors that would be very helpful. import turtle screen = turtle.Screen() screen.delay(0) chip = turtle.Turtle() chip.shape("circle") chip.color("green") chip.penup() chip.speed(0) connect = [] CHIP_DIST = 30 # the distance each chip will be apart ORIGIN_X = -90 ORIGIN_Y = -100 RIGHTMOST_COL = ORIGIN_X + CHIP_DIST * 6 y = chip.ycor() x = chip.xcor() for i in range(7): connect.append([]) x = connect[i] for j in range(7): x.append(0) chip.goto(ORIGIN_X + i * CHIP_DIST, ORIGIN_Y + j * CHIP_DIST) chip.stamp() chip.color("blue") chip.goto(ORIGIN_X + 3 * CHIP_DIST, ORIGIN_Y + 7 * CHIP_DIST) running = True def r(): if running: x = chip.xcor() if x < RIGHTMOST_COL: chip.setx(x + CHIP_DIST) else: chip.setx(ORIGIN_X) def l(): if running: x = chip.xcor() if x > ORIGIN_X: chip.setx(x - CHIP_DIST) else: chip.setx(RIGHTMOST_COL) turn = 1 def d(): global turn, running if running: x = int((chip.xcor() - ORIGIN_X) / CHIP_DIST) col = connect[x] for y in range(len(col)): if col[y] == 0: chip.sety(ORIGIN_Y + y * CHIP_DIST) chip.stamp() chip.goto(ORIGIN_X + 3 * CHIP_DIST, ORIGIN_Y + 7 * CHIP_DIST) connect[x][y] = turn # add code here if turn == 1: turn = 2 chip.color("red") else: turn = 1 chip.color ("blue") break def check_row(x, y): count = 1 for i in range(1, 4): if x + i < 7 and connect[x + i][y] == turn: count += 1 else: break for i in range(1, 4): if x - i > -1 and connect[x - i][y] == turn: count += 1 else: break if count >= 4: return True return False def check_column(x, y): count = 1 for i in range(1, 4): if y + i < 7 and connect[x][y + i] == turn: count += 1 else: break for i in range(1, 4): if y - i > -1 and connect[x][y - i] == turn: count += 1 else: break if count >= 4: return True return False def has_winner(x, y): return check_row(x, y) or check_column(x, y) if has_winner(x, y): if has_winner(x, y): running = False print "Player " + str(turn) + " is the winner!" else: if turn == 1: turn = 2 chip.color("red") else: turn = 1 chip.color("blue") screen.onkey(r, "Right") screen.onkey(l, "Left") screen.onkey(d, "Down") screen.listen() turtle.done() RE: Connect 4 Game - nilamo - May-11-2018 (May-11-2018, 03:27 PM)DarksideMoses Wrote:x = chip.xcor() for i in range(7): connect.append([]) x = connect[i] Line 21 is still rebinding x as a list.https://python-forum.io/Thread-Connect-4-Game?pid=46031#pid46031 |