Winning condition in Connect four game (Python)?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I've been working on a connect four game for a few weeks now (beginning programmer) and im almost done. But the only problem I have is that I can't seem to get the win conditions to work. I honestly have no idea how to fix it so im hoping someone on here can see whats wrong. The upper part until the last comment (#) works fine, it prints the board, gives error msg's at wrong input and drops the pieces where you want. the only thing that still doesn't work is the win conditions. I can't finish a game, it will let me get to 7 in a row and not give a win condition. My code can be found below!



ROW_COUNT = 6                               #CREEEREN VAN HET BORD
COLUMN_COUNT = 7

Board =
for x in range(ROW_COUNT): Board.append(list([0] * COLUMN_COUNT))

def drop_piece(Board, row, Column, piece): #PLAATSEN VAN EEN RONDJE
Board[row][Column] = piece

def is_valid_location(Board, Column): #CHECKEN OF DE PLEK OP HET BORD LEEG IS
return Board[-1][Column] == 0

def get_next_open_row(Board, Column): #CHECKEN OF DE ROW LEEG IS
for r in range(ROW_COUNT):
if Board[r][Column]==0:
return r

gameOver = False
turn = True

while not gameOver:
if turn: player = 1
else: player = 2
UserInput = input("Player " + str(player) + ", Make your turn(0-6):")
if UserInput.isdigit():
Column = int(UserInput)
else:
print("Your input has to be between 0 and 6!")
UserInput = input("Player " + str(player) + ", Make your turn(0-6):")

if Column != 0 and Column != 1 and Column != 2 and Column != 3 and Column != 4 and Column != 5 and Column != 6:
print("Your input has to be between 0 and 6!")
UserInput = input("Player " + str(player) + ", Make your turn(0-6):")
else:
if is_valid_location(Board, Column):
row = get_next_open_row(Board, Column)
drop_piece(Board, row, Column, player)
turn = not turn
else: print("Invalid selection")

for row in reversed(Board):
print(row) # EVERYTHING UNTIL HERE WORKS FINE!

#HORIZONTAAL
for C in range(COLUMN_COUNT - 3):
for R in range(ROW_COUNT):
if Board[R][C] == 1 and Board[R][C + 1] == 1 and Board[R][C + 2] == 1 and Board[R][C + 3] == 1:
print("You've won")
#VERTICAAL
for C in range(COLUMN_COUNT):
for R in range(ROW_COUNT - 3):
if Board[R][C] == 1 and Board[R + 1][C] == 1 and Board[R + 2][C] == 1 and Board[R + 3][C] == 1:
print("You've won")
#DIAGONAAL
for C in range(COLUMN_COUNT - 3):
for R in range(ROW_COUNT - 3):
if Board[R][C] == 1 and Board[R + 1][C + 1] == 1 and Board[R + 2][C + 2] == 1 and Board[R + 3][C + 3] == 1:
print ("You've won")









share|improve this question























  • List comparisons based on slices would simplify this: e.g., if Board[R][C:C+4] == [1,1,1,1]:

    – chepner
    Nov 16 '18 at 15:51


















0















I've been working on a connect four game for a few weeks now (beginning programmer) and im almost done. But the only problem I have is that I can't seem to get the win conditions to work. I honestly have no idea how to fix it so im hoping someone on here can see whats wrong. The upper part until the last comment (#) works fine, it prints the board, gives error msg's at wrong input and drops the pieces where you want. the only thing that still doesn't work is the win conditions. I can't finish a game, it will let me get to 7 in a row and not give a win condition. My code can be found below!



ROW_COUNT = 6                               #CREEEREN VAN HET BORD
COLUMN_COUNT = 7

Board =
for x in range(ROW_COUNT): Board.append(list([0] * COLUMN_COUNT))

def drop_piece(Board, row, Column, piece): #PLAATSEN VAN EEN RONDJE
Board[row][Column] = piece

def is_valid_location(Board, Column): #CHECKEN OF DE PLEK OP HET BORD LEEG IS
return Board[-1][Column] == 0

def get_next_open_row(Board, Column): #CHECKEN OF DE ROW LEEG IS
for r in range(ROW_COUNT):
if Board[r][Column]==0:
return r

gameOver = False
turn = True

while not gameOver:
if turn: player = 1
else: player = 2
UserInput = input("Player " + str(player) + ", Make your turn(0-6):")
if UserInput.isdigit():
Column = int(UserInput)
else:
print("Your input has to be between 0 and 6!")
UserInput = input("Player " + str(player) + ", Make your turn(0-6):")

if Column != 0 and Column != 1 and Column != 2 and Column != 3 and Column != 4 and Column != 5 and Column != 6:
print("Your input has to be between 0 and 6!")
UserInput = input("Player " + str(player) + ", Make your turn(0-6):")
else:
if is_valid_location(Board, Column):
row = get_next_open_row(Board, Column)
drop_piece(Board, row, Column, player)
turn = not turn
else: print("Invalid selection")

for row in reversed(Board):
print(row) # EVERYTHING UNTIL HERE WORKS FINE!

#HORIZONTAAL
for C in range(COLUMN_COUNT - 3):
for R in range(ROW_COUNT):
if Board[R][C] == 1 and Board[R][C + 1] == 1 and Board[R][C + 2] == 1 and Board[R][C + 3] == 1:
print("You've won")
#VERTICAAL
for C in range(COLUMN_COUNT):
for R in range(ROW_COUNT - 3):
if Board[R][C] == 1 and Board[R + 1][C] == 1 and Board[R + 2][C] == 1 and Board[R + 3][C] == 1:
print("You've won")
#DIAGONAAL
for C in range(COLUMN_COUNT - 3):
for R in range(ROW_COUNT - 3):
if Board[R][C] == 1 and Board[R + 1][C + 1] == 1 and Board[R + 2][C + 2] == 1 and Board[R + 3][C + 3] == 1:
print ("You've won")









share|improve this question























  • List comparisons based on slices would simplify this: e.g., if Board[R][C:C+4] == [1,1,1,1]:

    – chepner
    Nov 16 '18 at 15:51














0












0








0








I've been working on a connect four game for a few weeks now (beginning programmer) and im almost done. But the only problem I have is that I can't seem to get the win conditions to work. I honestly have no idea how to fix it so im hoping someone on here can see whats wrong. The upper part until the last comment (#) works fine, it prints the board, gives error msg's at wrong input and drops the pieces where you want. the only thing that still doesn't work is the win conditions. I can't finish a game, it will let me get to 7 in a row and not give a win condition. My code can be found below!



ROW_COUNT = 6                               #CREEEREN VAN HET BORD
COLUMN_COUNT = 7

Board =
for x in range(ROW_COUNT): Board.append(list([0] * COLUMN_COUNT))

def drop_piece(Board, row, Column, piece): #PLAATSEN VAN EEN RONDJE
Board[row][Column] = piece

def is_valid_location(Board, Column): #CHECKEN OF DE PLEK OP HET BORD LEEG IS
return Board[-1][Column] == 0

def get_next_open_row(Board, Column): #CHECKEN OF DE ROW LEEG IS
for r in range(ROW_COUNT):
if Board[r][Column]==0:
return r

gameOver = False
turn = True

while not gameOver:
if turn: player = 1
else: player = 2
UserInput = input("Player " + str(player) + ", Make your turn(0-6):")
if UserInput.isdigit():
Column = int(UserInput)
else:
print("Your input has to be between 0 and 6!")
UserInput = input("Player " + str(player) + ", Make your turn(0-6):")

if Column != 0 and Column != 1 and Column != 2 and Column != 3 and Column != 4 and Column != 5 and Column != 6:
print("Your input has to be between 0 and 6!")
UserInput = input("Player " + str(player) + ", Make your turn(0-6):")
else:
if is_valid_location(Board, Column):
row = get_next_open_row(Board, Column)
drop_piece(Board, row, Column, player)
turn = not turn
else: print("Invalid selection")

for row in reversed(Board):
print(row) # EVERYTHING UNTIL HERE WORKS FINE!

#HORIZONTAAL
for C in range(COLUMN_COUNT - 3):
for R in range(ROW_COUNT):
if Board[R][C] == 1 and Board[R][C + 1] == 1 and Board[R][C + 2] == 1 and Board[R][C + 3] == 1:
print("You've won")
#VERTICAAL
for C in range(COLUMN_COUNT):
for R in range(ROW_COUNT - 3):
if Board[R][C] == 1 and Board[R + 1][C] == 1 and Board[R + 2][C] == 1 and Board[R + 3][C] == 1:
print("You've won")
#DIAGONAAL
for C in range(COLUMN_COUNT - 3):
for R in range(ROW_COUNT - 3):
if Board[R][C] == 1 and Board[R + 1][C + 1] == 1 and Board[R + 2][C + 2] == 1 and Board[R + 3][C + 3] == 1:
print ("You've won")









share|improve this question














I've been working on a connect four game for a few weeks now (beginning programmer) and im almost done. But the only problem I have is that I can't seem to get the win conditions to work. I honestly have no idea how to fix it so im hoping someone on here can see whats wrong. The upper part until the last comment (#) works fine, it prints the board, gives error msg's at wrong input and drops the pieces where you want. the only thing that still doesn't work is the win conditions. I can't finish a game, it will let me get to 7 in a row and not give a win condition. My code can be found below!



ROW_COUNT = 6                               #CREEEREN VAN HET BORD
COLUMN_COUNT = 7

Board =
for x in range(ROW_COUNT): Board.append(list([0] * COLUMN_COUNT))

def drop_piece(Board, row, Column, piece): #PLAATSEN VAN EEN RONDJE
Board[row][Column] = piece

def is_valid_location(Board, Column): #CHECKEN OF DE PLEK OP HET BORD LEEG IS
return Board[-1][Column] == 0

def get_next_open_row(Board, Column): #CHECKEN OF DE ROW LEEG IS
for r in range(ROW_COUNT):
if Board[r][Column]==0:
return r

gameOver = False
turn = True

while not gameOver:
if turn: player = 1
else: player = 2
UserInput = input("Player " + str(player) + ", Make your turn(0-6):")
if UserInput.isdigit():
Column = int(UserInput)
else:
print("Your input has to be between 0 and 6!")
UserInput = input("Player " + str(player) + ", Make your turn(0-6):")

if Column != 0 and Column != 1 and Column != 2 and Column != 3 and Column != 4 and Column != 5 and Column != 6:
print("Your input has to be between 0 and 6!")
UserInput = input("Player " + str(player) + ", Make your turn(0-6):")
else:
if is_valid_location(Board, Column):
row = get_next_open_row(Board, Column)
drop_piece(Board, row, Column, player)
turn = not turn
else: print("Invalid selection")

for row in reversed(Board):
print(row) # EVERYTHING UNTIL HERE WORKS FINE!

#HORIZONTAAL
for C in range(COLUMN_COUNT - 3):
for R in range(ROW_COUNT):
if Board[R][C] == 1 and Board[R][C + 1] == 1 and Board[R][C + 2] == 1 and Board[R][C + 3] == 1:
print("You've won")
#VERTICAAL
for C in range(COLUMN_COUNT):
for R in range(ROW_COUNT - 3):
if Board[R][C] == 1 and Board[R + 1][C] == 1 and Board[R + 2][C] == 1 and Board[R + 3][C] == 1:
print("You've won")
#DIAGONAAL
for C in range(COLUMN_COUNT - 3):
for R in range(ROW_COUNT - 3):
if Board[R][C] == 1 and Board[R + 1][C + 1] == 1 and Board[R + 2][C + 2] == 1 and Board[R + 3][C + 3] == 1:
print ("You've won")






python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 15:46









daanneekdaanneek

132




132













  • List comparisons based on slices would simplify this: e.g., if Board[R][C:C+4] == [1,1,1,1]:

    – chepner
    Nov 16 '18 at 15:51



















  • List comparisons based on slices would simplify this: e.g., if Board[R][C:C+4] == [1,1,1,1]:

    – chepner
    Nov 16 '18 at 15:51

















List comparisons based on slices would simplify this: e.g., if Board[R][C:C+4] == [1,1,1,1]:

– chepner
Nov 16 '18 at 15:51





List comparisons based on slices would simplify this: e.g., if Board[R][C:C+4] == [1,1,1,1]:

– chepner
Nov 16 '18 at 15:51












1 Answer
1






active

oldest

votes


















0














Based on your indentation, those checks aren't even being looked at until the while loop ends. Indent all three of the checks so they're inside the while loop. Additionally, your checks need to set gameOver to True so that the game actually ends when you win.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341161%2fwinning-condition-in-connect-four-game-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Based on your indentation, those checks aren't even being looked at until the while loop ends. Indent all three of the checks so they're inside the while loop. Additionally, your checks need to set gameOver to True so that the game actually ends when you win.






    share|improve this answer




























      0














      Based on your indentation, those checks aren't even being looked at until the while loop ends. Indent all three of the checks so they're inside the while loop. Additionally, your checks need to set gameOver to True so that the game actually ends when you win.






      share|improve this answer


























        0












        0








        0







        Based on your indentation, those checks aren't even being looked at until the while loop ends. Indent all three of the checks so they're inside the while loop. Additionally, your checks need to set gameOver to True so that the game actually ends when you win.






        share|improve this answer













        Based on your indentation, those checks aren't even being looked at until the while loop ends. Indent all three of the checks so they're inside the while loop. Additionally, your checks need to set gameOver to True so that the game actually ends when you win.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 18:14









        ahotaahota

        37139




        37139
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341161%2fwinning-condition-in-connect-four-game-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine