Firearrow5235
Firearrow5235

Reputation: 296

"Connect 4" code seems to not detect game piece

I'm working on a "Connect 4" game in python and my code doesn't seem to be detecting the existence of a game piece. I can't see why. The only thing I can think of is somewhere my code is getting the idea that there's still a "O" in the spot even though it's shown as changed.

import random

a = ["A", "O", "O", "O", "O", "O", "O", "O"]
b = ["B", "O", "O", "O", "O", "O", "O", "O"]
c = ["C", "O", "O", "O", "O", "O", "O", "O"]
d = ["D", "O", "O", "O", "O", "O", "O", "O"]
e = ["E", "O", "O", "O", "O", "O", "O", "O"]
f = ["F", "O", "O", "O", "O", "O", "O", "O"]
n = [" ", "1", "2", "3", "4", "5", "6", "7"]

play = True
turn = random.randint(0, 1)

while play:
    print a
    print b
    print c
    print d
    print e
    print f
    print n

    turn = 0

    if turn == 0:
        playerChoice = int(raw_input("Enter the column you'd like to drop your piece in:"))
        if f[playerChoice] == "O":
            f[playerChoice] = "@"
        elif f[playerChoice] != "O":
            if e[playerChoice] == "0":
                e[playerChoice] = "@"
            elif e[playerChoice] != "O":
                if d[playerChoice] == "O":
                     d[playerChoice] = "@"
                elif d[playerChoice] != "O":
                     if c[playerChoice] == "O":
                         c[playerChoice] = "@"
                     elif c[playerChoice] != "O":
                         if b[playerChoice] == "O":
                             b[playerChoice] = "@"
                         elif b[playerChoice] != "0":
                             if a[playerChoice] == "O":
                                 a[playerChoice] = "@"
                             elif a[playerChoice] != "O":
                                print "This column is full!"
                                play = False



raw_input("press a key to continue")

and here's the output:

['A', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['B', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['C', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['D', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['E', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['F', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
[' ', '1', '2', '3', '4', '5', '6', '7']
Enter the column you'd like to drop your piece in:1
['A', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['B', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['C', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['D', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['E', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['F', '@', 'O', 'O', 'O', 'O', 'O', 'O']
[' ', '1', '2', '3', '4', '5', '6', '7']
Enter the column you'd like to drop your piece in:1
['A', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['B', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['C', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['D', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['E', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['F', '@', 'O', 'O', 'O', 'O', 'O', 'O']
[' ', '1', '2', '3', '4', '5', '6', '7']

Upvotes: 0

Views: 115

Answers (1)

Bill Lynch
Bill Lynch

Reputation: 81936

Two of your conditions have a 0 (number zero) instead of a O (letter O).

if e[playerChoice] == "0":
....
elif b[playerChoice] != "0":

That being said, I don't understand why your logic can't just be:

playerChoice = int(raw_input("Enter the column you'd like to drop your piece in:"))

if f[playerChoice] == "O":
    f[playerChoice] = "@"
elif e[playerChoice] == "O":
    e[playerChoice] = "@"
elif d[playerChoice] == "O":
    d[playerChoice] = "@"
elif c[playerChoice] == "O":
    c[playerChoice] = "@"
elif b[playerChoice] == "O":
    b[playerChoice] = "@" 
elif a[playerChoice] == "O":
    a[playerChoice] = "@" 
else 
    print "This column is full!"

Upvotes: 4

Related Questions