I do have a problem with my connect four code ; it doesn't check whether there is a winner even though the function its okay :-/
Can anyone help me to solve my problem ???

import random
board = dict()
rows,columns = 4,4

def game():
    choose_player()


#MAKE BOARD FUNCTION

def make_board():
    global rows,columns
    global board
    rows = input("Give number of rows [more than 4]: ")
    print " ----- ---------- ------ ------ ----- ----- "
    columns = input("Give number of columns [more than 4]: ")

   
    board = dict()
    min = 4
    if rows<min:
        make_board()
    elif columns<min:
        make_board()
    for x in range(rows):
        for y in range(columns):
            #board = (x+1,y+1)
            board[(x,y)] = "EmptyCells"
    
    
    return board
            

#CHOOSE PLAYER FUNCTION

def choose_player():
    choose = input("Player 1 vs. Computer [type 1] or Player 1 vs. Player 2 [type 2] : ")
    if choose == 1:
        #p1 vs. computer
        choose_1()
    elif choose == 2:
        choose_2()
    else:
        choose_player()

#BLANKS EXIST

def blanks_exist():
    for x in range(rows):
        for y in range(columns):
            if board[(x,y)] == "EmptyCells":
                return True
    return False

def print_board():
    for x in range(rows):
        print "|",
        for y in range(columns):
            if board[(x,y)] == "EmptyCells": print "    |",
            print board[(x,y)], "|"
        print ""

#CHOOSE 1

def choose_1():
    board = make_board()
    i = 0
    
    while blanks_exist():
        
        #for i in range(rows*columns):

            #if i%2 <> 0:

            player1 = "RED PLAYER"
            print "RED PLAYER"
            
            y = input("Please choose column: 1 to column choice number: ")
            if y >=columns:
                y = input()       
            else:
                x = (rows-1)
                while x>=1:
                    if board[(x,y)] == "EmptyCells":
                        board[(x,y)] == "R"
                        winCheck(board)
                        x = 0
                    else:
                        x = x - 1
            #i = i + 1
            print_board()
            
            #else:
        
            player2 = "BLACK PLAYER"
            print "BLACK PLAYER"
            #def p_c(rows,columns):
    
            x = (rows-1)
            y = random.choice(range(columns))
            while x>=1:
                if board[(x,y)] == "EmptyCells":
                        board[(x,y)] == "B"
                        print board[(x,y)] == "B"
                        winCheck(board)
                        x = 0
                else:
                    x = x - 1
            i = i + 1
            print_board()



def choose_2():
    board = make_board()

    while blanks_exist():
    
    #for i in range(rows*columns):

        #if i%2 <> 0:

        player1 = "RED PLAYER"
        print "RED PLAYER"
            
        y = input("Please choose column: 1 to column choice number: ")
        if y>= columns:
            y = input()
                    
        else:
            x = (rows-1)
            while x>=1:
                if board[(x,y)] == "EmptyCells":
                    board[(x,y)] == "R"
                    winCheck(board)
                    x = 0
                else:
                    x = x-1
        #i = i + 1
        print board()
        #else:

        player2 = "BLACK PLAYER"
        print "BLACK PLAYER"
            
        y = input("Please choose column: 1 to column choice number: ")
        if y>= columns:
            y = input()
        else:
            x = (rows-1)
            while x>=1:
                if board[(x,y)] == "EmptyCells":
                    board[(x,y)] == "B"
                    winCheck(board)
                    x = 0
                else:
                    x = x - 1
                i = i + 1
                print board()



def winCheck(board):
    print "check win"
    winVertical(board)
    winHorizontal(board)
    winDiagonal(board)

def winVertical(board):
    for x in range(rows):
        for y in range(columns):
            if (board[(x,y)] == "R" and board[(x+1,y)] == "R" and board[(x+2,y)] == "R" and board[(x+3,y)] == "R"):
                print "Winner == Red player"
                break
            elif (board[(x,y)] == "B" and board[(x+1,y)] == "B" and board[(x+2,y)] == "B" and board[(x+3,y)] == "B"):
                print "Winner == Black player"
                break

def winHorizontal(board):
    for x in range(rows):
        for y in range(columns):
            if (board[(x,y)] == "R" and board[(x,y+1)] == "R" and board[(x,y+2)] == "R" and board[(x,y+3)] == "R"):
                print "Winner == Red player"
                break
            elif (board[(x,y)] == "B" and board[(x,y+1)] == "B" and board[(x,y+2)] == "B" and board[(x,y+3)] == "B"):
                print "Winner == Black player"
                break


def winDiagonal(board):
    
    for x in range(rows):
        for y in range(columns):
            if (board[x,y] == "R" and board[(x+1,y+1)] == "R" and board[(x+2,y+2)] == "R" and board[(x+3,y+3)] =="R"):
                print "Winner == Red player"
            elif (board[x,y] == "B" and board[(x+1,y+1)] == "B" and board[(x+2,y+2)] == "B" and board[(x+3,y+3)] =="B"):
                print "Winner == Black player"
            elif (board[x,y] == "R" and board[(x-1,y-1)] == "R" and board[(x-2,y+2)] == "R" and board[(x-3,y+3)] =="R"):
                print "Winner == Red player"
            elif (board[x,y] == "B" and board[(x-1,y-1)] == "B" and board[(x-2,y+2)] == "B" and board[(x-3,y+3)] =="B"):
                print "Winner == Black player"
            


game()

Hi eleonora,

Statements like this:

board[(x,y)] == "R"

with a double equals sign (==) only test to see whether the value at (x,y) is "R"; that statement does not assign "R" to board[(x,y)], which is what you want to do. Instead, use board[(x,y)] = "R" and it should remember the moves. Also make this change for the PC's turn.

Thank You Very Much!
But now im getting an error :
"""
winVertical
if (board[(x,y)] == "R" and board[(x+1,y)] == "R" and board[(x+2,y)] == "R" and board[(x+3,y)] == "R"):
KeyError: (5, 1)

"""

Hi eleonora,

Yes. The reason is that the winVertical function is going out-of-bounds. It checks every position in the board. For each check, it looks at (x,y), (x+1,y), (x+2,y), and (x+3,y). Because it checks every position in the board, it eventually gets around to checking the last row, but because we look at cells with x+1, +2, and so on, we go out-of-bounds.

The easy way to fix this is to put a try/except around each if-block. Get rid of the elifs - they don't save you that much time, anyway - and do the following:

def winVertical(board):
    for x in range(rows):
        for y in range(columns):
            try:
                if (board[(x,y)] == "R" and board[(x+1,y)] == "R" and board[(x+2,y)] == "R" and board[(x+3,y)] == "R"):
                    print "Winner == Red player"
                    break
            except Exception, e: pass
            try:
                if (board[(x,y)] == "B" and board[(x+1,y)] == "B" and board[(x+2,y)] == "B" and board[(x+3,y)] == "B"):
                    print "Winner == Black player"
                    break
            except Exception, e: pass

Make similar changes for winHorizontal() and WinDiagonal(). This way, we ignore the cases where we go out-of-bounds but still check all the viable cells.

By the way, it looks like you have a typo in winDiagonal(). That "board[x,y]" should be "board[(x,y)]" with parentheses.

I should also mention that you're going to have a problem when the game ends, because while it will print out a "Winner == " message it will also just keep going! The keyword "break" doesn't end the program. It merely breaks you out of a loop. What you ought to do is have the function return a value (True or False) which tells winCheck() to end the game with sys.exit(). But we can cross that bridge when we come to it.

Hope this helps.

I made the changes and i also added the sys.exit... i dunno if that i did is what you were meaning but it still it doesn't stops after the winner message
:-/

import sys
import random
board = dict()
rows,columns = 4,4

def game():
    choose_player()


#MAKE BOARD FUNCTION

def make_board():
    global rows,columns
    global board
    rows = input("Give number of rows [more than 4]: ")
    print " ----- ---------- ------ ------ ----- ----- "
    columns = input("Give number of columns [more than 4]: ")

   
    board = dict()
    min = 4
    if rows<min:
        make_board()
    elif columns<min:
        make_board()
    for x in range(rows):
        for y in range(columns):
            #board = (x+1,y+1)
            board[(x,y)] = "EmptyCells"
    
    
    return board
            

#CHOOSE PLAYER FUNCTION

def choose_player():
    choose = input("Player 1 vs. Computer [type 1] or Player 1 vs. Player 2 [type 2] : ")
    if choose == 1:
        #p1 vs. computer
        choose_1()
    elif choose == 2:
        choose_2()
    else:
        choose_player()

#BLANKS EXIST

def blanks_exist():
    for x in range(rows):
        for y in range(columns):
            if board[(x,y)] == "EmptyCells":
                return True
    return False

def print_board():
    for x in range(rows):
        print "|",
        for y in range(columns):
            if board[(x,y)] == "EmptyCells": print "    |",
            print board[(x,y)], "|"
        print ""

#CHOOSE 1

def choose_1():
    board = make_board()
    i = 0
    
    while blanks_exist():
        
        #for i in range(rows*columns):

            #if i%2 <> 0:

            player1 = "RED PLAYER"
            print "RED PLAYER"
            
            y = input("Please choose column: 1 to column choice number: ")
            if y >=columns:
                y = input()       
            else:
                x = (rows-1)
                while x>=1:
                    if board[(x,y)] == "EmptyCells":
                        board[(x,y)] = "R"
                        winCheck(board)
                        x = 0
                    else:
                        x = x - 1
            #i = i + 1
            print_board()
            
            #else:
        
            player2 = "BLACK PLAYER"
            print "BLACK PLAYER"
            #def p_c(rows,columns):
    
            x = (rows-1)
            y = random.choice(range(columns))
            while x>=1:
                if board[(x,y)] == "EmptyCells":
                        board[(x,y)] = "B"
                        
                        winCheck(board)
                        x = 0
                else:
                    x = x - 1
            i = i + 1
            print_board()



def choose_2():
    board = make_board()

    while blanks_exist():
    
    #for i in range(rows*columns):

        #if i%2 <> 0:

        player1 = "RED PLAYER"
        print "RED PLAYER"
            
        y = input("Please choose column: 1 to column choice number: ")
        if y>= columns:
            y = input()
                    
        else:
            x = (rows-1)
            while x>=1:
                if board[(x,y)] == "EmptyCells":
                    board[(x,y)] = "R"
                    winCheck(board)
                    x = 0
                else:
                    x = x-1
        #i = i + 1
        print board()
        #else:

        player2 = "BLACK PLAYER"
        print "BLACK PLAYER"
            
        y = input("Please choose column: 1 to column choice number: ")
        if y>= columns:
            y = input()
        else:
            x = (rows-1)
            while x>=1:
                if board[(x,y)] == "EmptyCells":
                    board[(x,y)] = "B"
                    winCheck(board)
                    x = 0
                else:
                    x = x - 1
                i = i + 1
                print board()



def winCheck(board):
    print "check win"
    winVertical(board)
    winHorizontal(board)
    winDiagonal(board)


def winVertical(board):
    for x in range(rows):
        for y in range(columns):
            try:
                if (board[(x,y)] == "R" and board[(x+1,y)] == "R" and board[(x+2,y)] == "R" and board[(x+3,y)] == "R"):
                    print "Winner == Red player"
                    try:
                        sys.exit(1)
                    except SystemExit:
                        pass
                    #break
            except Exception, e: pass
            try:
                if (board[(x,y)] == "B" and board[(x+1,y)] == "B" and board[(x+2,y)] == "B" and board[(x+3,y)] == "B"):
                    print "Winner == Black player"
                    try:
                        sys.exit(1)
                    except SystemExit:
                        pass
                    #break
            except Exception, e: pass



#def winVertical(board):
#    for x in range(rows):
#        for y in range(columns):
#            if (board[(x,y)] == "R" and board[(x+1,y)] == "R" and board[(x+2,y)] == "R" and board[(x+3,y)] == "R"):
#                print "Winner == Red player"
#                break
#            elif (board[(x,y)] == "B" and board[(x+1,y)] == "B" and board[(x+2,y)] == "B" and board[(x+3,y)] == "B"):
#                print "Winner == Black player"
#                break


def winHorizontal(board):
    for x in range(rows):
        for y in range(columns):
            try:
                if (board[(x,y)] == "R" and board [(x,y+1)] == "R" and board[(x,y+2)] == "R" and board[(x,y+3)] == "R"):
                    print "Winner == Red player"
                    try:
                        sys.exit(1)
                    except SystemExit:
                        pass

                    #break
            except Exception, e: pass
            try:
                if (board[(x,y)] == "B" and board[(x,y+1)] == "B" and board[(x,y+2)] == "B" and board[(x,y+3)] == "B"):
                    print "Winner == Black player"
                    try:
                        sys.exit(1)
                    except Systemxit:
                        pass
                    #break
            except Exception, e: pass
            

#def winHorizontal(board):
#    for x in range(rows):
#        for y in range(columns):
#            if (board[(x,y)] == "R" and board[(x,y+1)] == "R" and board[(x,y+2)] == "R" and board[(x,y+3)] == "R"):
#                print "Winner == Red player"
#                break
#            elif (board[(x,y)] == "B" and board[(x,y+1)] == "B" and board[(x,y+2)] == "B" and board[(x,y+3)] == "B"):
#                print "Winner == Black player"
#                break


def winDiagonal(board):
    for x in range(rows):
        for y in range(columns):
            try:
                if (board[(x,y)] == "R" and board[(x+1,y+1)] == "R" and board[(x+2,y+2)] == "R" and board[(x+3,y+3)] =="R"):
                    print "Winner == Red player"
                    try:
                        sys.exit(1)
                    except Systemxit:
                        pass
                    #break
            except Exception, e: pass
            try:
                if (board[(x,y)] == "B" and board[(x+1,y+1)] == "B" and board[(x+2,y+2)] == "B" and board[(x+3,y+3)] =="B"):
                    print "Winner == Black player"
                    try:
                        sys.exit(1)
                    except Systemxit:
                        pass
                    #break
            except Exception, e: pass
            try:
                if (board[(x,y)] == "R" and board[(x-1,y-1)] == "R" and board[(x-2,y+2)] == "R" and board[(x-3,y+3)] =="R"):
                    print "Winner == Red player"
                    try:
                        sys.exit(1)
                    except Systemxit:
                        pass
                    #break
            except Exception, e: pass
            try:
                if (board[(x,y)] == "B" and board[(x-1,y-1)] == "B" and board[(x-2,y+2)] == "B" and board[(x-3,y+3)] =="B"):
                    print "Winner == Black player"
                    try:
                        sys.exit(1)
                    except Systemxit:
                        pass
                    #break
            except Exception, e: pass




#def winDiagonal(board):    
#    for x in range(rows):
#        for y in range(columns):
#            if (board[x,y] == "R" and board[(x+1,y+1)] == "R" and board[(x+2,y+2)] == "R" and board[(x+3,y+3)] =="R"):
#                print "Winner == Red player"
#            elif (board[x,y] == "B" and board[(x+1,y+1)] == "B" and board[(x+2,y+2)] == "B" and board[(x+3,y+3)] =="B"):
#                print "Winner == Black player"
#            elif (board[x,y] == "R" and board[(x-1,y-1)] == "R" and board[(x-2,y+2)] == "R" and board[(x-3,y+3)] =="R"):
#                print "Winner == Red player"
#            elif (board[x,y] == "B" and board[(x-1,y-1)] == "B" and board[(x-2,y+2)] == "B" and board[(x-3,y+3)] =="B"):
#                print "Winner == Black player"
#            


game()

Aha!

This is a very easy mistake to make. Using sys.exit() inside a try-except block will be caught by the try-except block, and then nothing happens. That is why I warned you to return a value to winCheck() and do sys.exit() from there. Really, what it should look like is this:

# Check to see if the board has a 4-in-a-row in any direction
# If yes, exit the program
def winCheck(board):
    print "check win"
    if winVertical(board) or winHorizontal(board) or winDiagonal(board):
        sys.exit()

# Check to see if the board has a vertical 4-in-a-row
# If yes, return True, otherwise return False
def winVertical(board):
    for x in range(rows):
        for y in range(columns):
            try:
                if (board[(x,y)] == "R" and board[(x+1,y)] == "R" and board[(x+2,y)] == "R" and board[(x+3,y)] == "R"):
                    print "Winner == Red player"
                    return True
            except Exception, e: pass
            try:
                if (board[(x,y)] == "B" and board[(x+1,y)] == "B" and board[(x+2,y)] == "B" and board[(x+3,y)] == "B"):
                    print "Winner == Black player"
                    return True
            except Exception, e: pass
    return False

Make the other two check functions look similar. That should do the trick.

I did it but it still doesnt stops :S

import sys
import random
board = dict()
rows,columns = 4,4

def game():
    choose_player()


#MAKE BOARD FUNCTION

def make_board():
    global rows,columns
    global board
    rows = input("Give number of rows [more than 4]: ")
    print " ----- ---------- ------ ------ ----- ----- "
    columns = input("Give number of columns [more than 4]: ")

   
    board = dict()
    min = 4
    if rows<min:
        make_board()
    elif columns<min:
        make_board()
    for x in range(rows):
        for y in range(columns):
            #board = (x+1,y+1)
            board[(x,y)] = "EmptyCells"
    
    
    return board
            

#CHOOSE PLAYER FUNCTION

def choose_player():
    choose = input("Player 1 vs. Computer [type 1] or Player 1 vs. Player 2 [type 2] : ")
    if choose == 1:
        #p1 vs. computer
        choose_1()
    elif choose == 2:
        choose_2()
    else:
        choose_player()

#BLANKS EXIST

def blanks_exist():
    for x in range(rows):
        for y in range(columns):
            if board[(x,y)] == "EmptyCells":
                return True
    return False

def print_board():
    for x in range(rows):
        print "|",
        for y in range(columns):
            if board[(x,y)] == "EmptyCells": print "    |",
            print board[(x,y)], "|"
        print ""

#CHOOSE 1

def choose_1():
    board = make_board()
    i = 0
    
    while blanks_exist():
        
        #for i in range(rows*columns):

            #if i%2 <> 0:

            player1 = "RED PLAYER"
            print "RED PLAYER"
            
            y = input("Please choose column: 1 to column choice number: ")
            if y >=columns:
                y = input()       
            else:
                x = (rows-1)
                while x>=1:
                    if board[(x,y)] == "EmptyCells":
                        board[(x,y)] = "R"
                        winCheck(board)
                        x = 0
                    else:
                        x = x - 1
            #i = i + 1
            print_board()
            
            #else:
        
            player2 = "BLACK PLAYER"
            print "BLACK PLAYER"
            #def p_c(rows,columns):
    
            x = (rows-1)
            y = random.choice(range(columns))
            while x>=1:
                if board[(x,y)] == "EmptyCells":
                        board[(x,y)] = "B"
                        
                        winCheck(board)
                        x = 0
                else:
                    x = x - 1
            i = i + 1
            print_board()



def choose_2():
    board = make_board()

    while blanks_exist():
    
    #for i in range(rows*columns):

        #if i%2 <> 0:

        player1 = "RED PLAYER"
        print "RED PLAYER"
            
        y = input("Please choose column: 1 to column choice number: ")
        if y>= columns:
            y = input()
                    
        else:
            x = (rows-1)
            while x>=1:
                if board[(x,y)] == "EmptyCells":
                    board[(x,y)] = "R"
                    winCheck(board)
                    x = 0
                else:
                    x = x-1
        #i = i + 1
        print board()
        #else:

        player2 = "BLACK PLAYER"
        print "BLACK PLAYER"
            
        y = input("Please choose column: 1 to column choice number: ")
        if y>= columns:
            y = input()
        else:
            x = (rows-1)
            while x>=1:
                if board[(x,y)] == "EmptyCells":
                    board[(x,y)] = "B"
                    winCheck(board)
                    x = 0
                else:
                    x = x - 1
                i = i + 1
                print board()



def winCheck(board):
    print "check win"
    if winVertical(board) or winHorizontal(board) or winDiagonal(board):
        sys.exit()
    


def winVertical(board):
    for x in range(rows):
        for y in range(columns):
            try:
                if (board[(x,y)] == "R" and board[(x+1,y)] == "R" and board[(x+2,y)] == "R" and board[(x+3,y)] == "R"):
                    print "Winner == Red player"

                    return True

                    #sys.exit()
                    #try:
                        #sys.exit(1)
                    #except SystemExit:
                        #pass
                    #break
            except Exception, e: pass
            try:
                if (board[(x,y)] == "B" and board[(x+1,y)] == "B" and board[(x+2,y)] == "B" and board[(x+3,y)] == "B"):
                    print "Winner == Black player"

                    return True 

                    #sys.exit()
                    #try:
                        #sys.exit(1)
                    #except SystemExit:
                        #pass
                    #break
            except Exception, e: pass
    return False


#def winVertical(board):
#    for x in range(rows):
#        for y in range(columns):
#            if (board[(x,y)] == "R" and board[(x+1,y)] == "R" and board[(x+2,y)] == "R" and board[(x+3,y)] == "R"):
#                print "Winner == Red player"
#                break
#            elif (board[(x,y)] == "B" and board[(x+1,y)] == "B" and board[(x+2,y)] == "B" and board[(x+3,y)] == "B"):
#                print "Winner == Black player"
#                break


def winHorizontal(board):
    for x in range(rows):
        for y in range(columns):
            try:
                if (board[(x,y)] == "R" and board [(x,y+1)] == "R" and board[(x,y+2)] == "R" and board[(x,y+3)] == "R"):
                    print "Winner == Red player"

                    return True
                    
                    #sys.exit()
                    #try:
                        #sys.exit(1)
                    #except SystemExit:
                        #pass

                    #break
            except Exception, e: pass
            try:
                if (board[(x,y)] == "B" and board[(x,y+1)] == "B" and board[(x,y+2)] == "B" and board[(x,y+3)] == "B"):
                    print "Winner == Black player"

                    return True
                    
                    #sys.exit()
                    #try:
                        #sys.exit(1)
                    #except Systemxit:
                        #pass
                    #break
            except Exception, e: pass
    return False
            

#def winHorizontal(board):
#    for x in range(rows):
#        for y in range(columns):
#            if (board[(x,y)] == "R" and board[(x,y+1)] == "R" and board[(x,y+2)] == "R" and board[(x,y+3)] == "R"):
#                print "Winner == Red player"
#                break
#            elif (board[(x,y)] == "B" and board[(x,y+1)] == "B" and board[(x,y+2)] == "B" and board[(x,y+3)] == "B"):
#                print "Winner == Black player"
#                break


def winDiagonal(board):
    for x in range(rows):
        for y in range(columns):
            try:
                if (board[(x,y)] == "R" and board[(x+1,y+1)] == "R" and board[(x+2,y+2)] == "R" and board[(x+3,y+3)] =="R"):
                    print "Winner == Red player"

                    return True
                    
                    #sys.exit()
                    #try:
                        #sys.exit(1)
                    #except Systemxit:
                        #pass
                    #break
            except Exception, e: pass
            try:
                if (board[(x,y)] == "B" and board[(x+1,y+1)] == "B" and board[(x+2,y+2)] == "B" and board[(x+3,y+3)] =="B"):
                    print "Winner == Black player"

                    return True

                    #sys.exit()
                    #try:
                        #sys.exit(1)
                    #except Systemxit:
                        #pass
                    #break
            except Exception, e: pass
            try:
                if (board[(x,y)] == "R" and board[(x-1,y-1)] == "R" and board[(x-2,y+2)] == "R" and board[(x-3,y+3)] =="R"):
                    print "Winner == Red player"
                    
                    return True

                    #sys.exit()
                    #try:
                        #sys.exit(1)
                    #except Systemxit:
                        #pass
                    #break
            except Exception, e: pass
            try:
                if (board[(x,y)] == "B" and board[(x-1,y-1)] == "B" and board[(x-2,y+2)] == "B" and board[(x-3,y+3)] =="B"):
                    print "Winner == Black player"

                    return True

                    #sys.exit()
                    #try:
                        #sys.exit()
                    #except Systemxit:
                        #pass
                    #break
            except Exception, e: pass
    return False



#def winDiagonal(board):    
#    for x in range(rows):
#        for y in range(columns):
#            if (board[x,y] == "R" and board[(x+1,y+1)] == "R" and board[(x+2,y+2)] == "R" and board[(x+3,y+3)] =="R"):
#                print "Winner == Red player"
#            elif (board[x,y] == "B" and board[(x+1,y+1)] == "B" and board[(x+2,y+2)] == "B" and board[(x+3,y+3)] =="B"):
#                print "Winner == Black player"
#            elif (board[x,y] == "R" and board[(x-1,y-1)] == "R" and board[(x-2,y+2)] == "R" and board[(x-3,y+3)] =="R"):
#                print "Winner == Red player"
#            elif (board[x,y] == "B" and board[(x-1,y-1)] == "B" and board[(x-2,y+2)] == "B" and board[(x-3,y+3)] =="B"):
#                print "Winner == Black player"
#            


game()

Hi eleonora,

Really? I just ran it and it stops correctly. The print_board() function did need some tweaking to make the board come out okay, and the x index was off by one (meaning, the first column was index 0 and not index 1, as the instructions have it), but other than that it worked fine.

Here's how the print_board() function should look:

def print_board():
    for x in range(rows):
        print "|",
        for y in range(columns):
            if board[(x,y)] == "EmptyCells": print "  |",
            else: print board[(x,y)], "|",
        print ""

How are you running it? I decided to play game #1 against the computer, on a game board of size 6x10. What parameters are you running it with?

IT WORKS !!!

Thanks!


But after the winner message i got this error :

"""
in winCheck
sys.exit()
SystemExit
"""

And another thing that the board start from 0 to (columns - 1)..How can i fix it?

Another thing how can i modify sleep() in order when the user writes pause to pause the game and when it writes continue the game to continue as it was before?

print ">>> Connect 4! <<<"

global EMPTY, BLACK, WHITE, a, b, c, d, e, f, g
EMPTY = " "
BLACK = "X"
WHITE = "O"
a = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]
b = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]
c = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]
d = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]
e = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]
f = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]
g = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]

# >>> Combo Database Update Routine
COMBOS = lambda: [
(a[0],a[1],a[2],a[3]),
(a[1],a[2],a[3],a[4]),
(a[2],a[3],a[4],a[5]),
(b[0],b[1],b[2],b[3]),
(b[1],b[2],b[3],b[4]),
(b[2],b[3],b[4],b[5]),
(c[0],c[1],c[2],c[3]),
(c[1],c[2],c[3],c[4]),
(c[2],c[3],c[4],c[5]),
(d[0],d[1],d[2],d[3]),
(d[1],d[2],d[3],d[4]),
(d[2],d[3],d[4],d[5]),
(e[0],e[1],e[2],e[3]),
(e[1],e[2],e[3],e[4]),
(e[2],e[3],e[4],e[5]),
(f[0],f[1],f[2],f[3]),
(f[1],f[2],f[3],f[4]),
(f[2],f[3],f[4],f[5]),
(g[0],g[1],g[2],g[3]),
(g[1],g[2],g[3],g[4]),
(g[2],g[3],g[4],g[5]),
(a[1],b[1],c[1],d[1]),
(b[1],c[1],d[1],e[1]),
(c[1],d[1],e[1],f[1]),
(a[2],b[2],c[2],d[2]),
(b[2],c[2],d[2],e[2]),
(c[2],d[2],e[2],f[2]),
(a[3],b[3],c[3],d[3]),
(b[3],c[3],d[3],e[3]),
(c[3],d[3],e[3],f[3]),
(a[4],b[4],c[4],d[4]),
(b[4],c[4],d[4],e[4]),
(c[4],d[4],e[4],f[4]),
(a[5],b[5],c[5],d[5]),
(b[5],c[5],d[5],e[5]),
(c[5],d[5],e[5],f[5]),
(a[0],b[1],c[2],d[3]),
(b[0],c[1],d[2],e[3]),
(c[0],d[1],e[2],f[3]),
(d[0],e[1],f[2],g[3]),
(a[1],b[2],c[3],d[4]),
(b[1],c[2],d[3],e[4]),
(c[1],d[2],e[3],f[4]),
(d[1],e[2],f[3],g[4]),
(a[2],b[3],c[4],d[5]),
(b[2],c[3],d[4],e[5]),
(c[2],d[3],e[4],f[5]),
(d[2],e[3],f[4],g[5]),
(g[0],f[1],e[2],d[3]),
(f[0],e[1],d[2],c[3]),
(e[0],d[1],c[2],b[3]),
(d[0],c[1],b[2],a[3]),
(g[1],f[2],e[3],d[4]),
(f[1],e[2],d[3],c[4]),
(e[1],d[2],c[3],b[4]),
(d[1],c[2],b[3],a[4]),
(g[2],f[3],e[4],d[5]),
(f[2],e[3],d[4],c[5]),
(e[2],d[3],c[4],b[5]),
(d[2],c[3],b[4],a[5]),
]


# >>> Definitions
def main():
global COMP,HUMN
response = raw_input("\nWould you like to go first [y/N]?: ").lower()
print "At any time, just type 'exit' or 'quit' to leave the game...\n"
if response[0] != "y":
COMP = WHITE
HUMN = BLACK
comp_move()
else:
COMP = BLACK
HUMN = WHITE
draw_board()
human_move()
# Instead of putting things in a loop, I've set it so that the AI/HUMN functions run their course, and then call each other.
# The AI function calls the main algorithm which is expected to not only interpret possible AI moves, but to be aware of the
# game STATE, as I like to call it. (In a way, it is a loop, but there is more control over it...)

def draw_board():
# This relies completely on globals. There is an easier way to code this, but for right now,
# it helps to be able to visualize the board when coding.
print "6 | %s | %s | %s | %s | %s | %s | %s |" % (a[5],b[5],c[5],d[5],e[5],f[5],g[5])
print "5 | %s | %s | %s | %s | %s | %s | %s |" % (a[4],b[4],c[4],d[4],e[4],f[4],g[4])
print "4 | %s | %s | %s | %s | %s | %s | %s |" % (a[3],b[3],c[3],d[3],e[3],f[3],g[3])
print "3 | %s | %s | %s | %s | %s | %s | %s |" % (a[2],b[2],c[2],d[2],e[2],f[2],g[2])
print "2 | %s | %s | %s | %s | %s | %s | %s |" % (a[1],b[1],c[1],d[1],e[1],f[1],g[1])
print "1 | %s | %s | %s | %s | %s | %s | %s |" % (a[0],b[0],c[0],d[0],e[0],f[0],g[0])
print "--| a | b | c | d | e | f | g |"


def quit(message, draw=True):
if draw == True:
draw_board()
print message
exit()

def check_state():
# I recently realized that even though the COMBOS array was global, it would not provide absolute references to the a,b,c,d,e,f,g arrays.
# But lambda solves this problem very elegantly.
combos = COMBOS()
# Check if game is over.
for win in combos:
if win[0]+win[1]+win[2]+win[3] == COMP*4:
quit("The computer has won.")
if win[0]+win[1]+win[2]+win[3] == HUMN*4:
quit("Human has won.")
# Insert super-awesome algorithm here. If a move has not been determined, then continue. Else, make it.

# Base : Find a win in combos that is within gravitational restraints and brute force into making that win.
# If Case 1 occurs OR if a necessary location in Base is taken by human, reload Base and try again.
# It may occur that Base will be reloaded to the SAME Base. This is OK.
# Case 1. If there is a possible human victory in the next move, block it. If computer is first, skip to #2.
# Case 2. If there is a possible computer victory in the next move, make it. If there is a gravity error;
# a) find another winning move
# b) find a different Base
# Regardless of Base or STATE status, once a move is determined, run the according set of evaled column, row assignments.
# If there is a computer win and a human win in the next move, make the computer win.


def help_text(flargh):
print "Your error involves",flargh
print "An example of a correct input (locator) would be: a,1"
human_move()

def human_move():
coords = raw_input("\nConnect4 [Option] <column>,<row>: ").lower()
quitting =
if coords in quitting:
quit("Game Over...", False)
# Petty error handling.
yAlpha =
if len(coords) != 3 or coords[1] != ',':
help_text("a misformed locator.")
if coords[0] not in yAlpha:
help_text(coords[0]+" being out of the acceptable range [a-g].")
if eval(coords[2]) not in range(1,6):
help_text(coords[2]+" being out of the acceptable range [1-6].")
# Works brilliantly. Much easier to implement than I thought as well!
if eval(coords[0])[eval(coords[2])-1] == EMPTY:
if eval(coords[2])-2 < 0:
pass
else:
if eval(coords[0])[eval(coords[2])-2] == EMPTY:
print "Illegal Move: Gravity Error."
human_move()
eval(coords[0])[eval(coords[2])-1] = HUMN
print "HUMAN:"
draw_board()
comp_move()
else:
print "Illegal Move: That spot has been taken."
human_move()

def comp_move():
check_state()
print "COMPUTER:"
# This is done so that if additional difficulties are added, the general display routine would not be interrupted.
# (I don't want draw_board() or such to be in check_state().)
draw_board()
human_move()

# Ta-daaaa!!!
main()

import sys
import random
board = dict()
rows,columns = 4,4

def game():
    choose_player()


#MAKE BOARD FUNCTION

def make_board():
    global rows,columns
    global board
    rows = input("Give number of rows [more than 4]: ")
    print (" ----- ---------- ------ ------ ----- ----- ")
    columns = input("Give number of columns [more than 4]: ")


    board = dict()
    min = 4
    if rows<min:
        make_board()
    elif columns<min:
        make_board()
    for x in range(rows):
        for y in range(columns):
            #board = (x+1,y+1)
            board[(x,y)] = "EmptyCells"


    return board


#CHOOSE PLAYER FUNCTION

def choose_player():
    choose = input("Player 1 vs. Computer [type 1] or Player 1 vs. Player 2 [type 2] : ")
    if choose == 1:
        #p1 vs. computer
        choose_1()
    elif choose == 2:
        choose_2()
    else:
        choose_player()

#BLANKS Are There

def blanks_exist():
    for x in range(rows):
        for y in range(columns):
            if board[(x,y)] == "EmptyCells":
                return True
    return False

def print_board():
    for x in range(rows):
        print("|")
        for y in range(columns):
            if board[(x,y)] == "EmptyCells": print("    |")
            print(board[(x,y)], "|")
        print("")

#CHOOSE 1

def choose_1():
    board = make_board()
    i = 0

    while blanks_exist():

        #for i in range(rows*columns):

            #if i%2 <> 0:

            player1 = "RED PLAYER"
            print("RED PLAYER")

            y = input("Please choose column: 1 to column choice number: ")
            if y >=columns:
                y = input()       
            else:
                x = (rows-1)
                while x>=1:
                    if board[(x,y)] == "EmptyCells":
                        board[(x,y)] = "R"
                        winCheck(board)
                        x = 0
                    else:
                        x = x - 1
            #i = i + 1
            print_board()

            #else:

            player2 = "BLACK PLAYER"
            print("BLACK PLAYER")
            #def p_c(rows,columns):

            x = (rows-1)
            y = random.choice(range(columns))
            while x>=1:
                if board[(x,y)] == "EmptyCells":
                        board[(x,y)] = "B"

                        winCheck(board)
                        x = 0
                else:
                    x = x - 1
            i = i + 1
            print_board()



def choose_2():
    board = make_board()

    while blanks_exist():

    #for i in range(rows*columns):

        #if i%2 <> 0:

        player1 = "RED PLAYER"
        print("RED PLAYER")

        y = input("Please choose column: 1 to column choice number: ")
        if y>= columns:
            y = input()

        else:
            x = (rows-1)
            while x>=1:
                if board[(x,y)] == "EmptyCells":
                    board[(x,y)] = "R"
                    winCheck(board)
                    x = 0
                else:
                    x = x-1
        #i = i + 1
        print(board())
        #else:

        player2 = "BLACK PLAYER"
        print("BLACK PLAYER")

        y = input("Please choose column: 1 to column choice number: ")
        if y>= columns:
            y = input()
        else:
            x = (rows-1)
            while x>=1:
                if board[(x,y)] == "EmptyCells":
                    board[(x,y)] = "B"
                    winCheck(board)
                    x = 0
                else:
                    x = x - 1
                i = i + 1
                print(board())



def winCheck(board):
    print("check win")
    winVertical(board)
    winHorizontal(board)
    winDiagonal(board)


def winVertical(board):
    for x in range(rows):
        for y in range(columns):
            try:
                if (board[(x,y)] == "R" and board[(x+1,y)] == "R" and board[(x+2,y)] == "R" and board[(x+3,y)] == "R"):
                    print("Winner == Red player")
                    try:
                        sys.exit(1)
                    except SystemExit:
                        pass
                    #break
            except Exception(e): pass
            try:
                if (board[(x,y)] == "B" and board[(x+1,y)] == "B" and board[(x+2,y)] == "B" and board[(x+3,y)] == "B"):
                    print("Winner == Black player")
                    try:
                        sys.exit(1)
                    except SystemExit:
                        pass
                    #break
            except Exception(e): pass



#def winVertical(board):
#    for x in range(rows):
#        for y in range(columns):
#            if (board[(x,y)] == "R" and board[(x+1,y)] == "R" and board[(x+2,y)] == "R" and board[(x+3,y)] == "R"):
#                print "Winner == Red player"
#                break
#            elif (board[(x,y)] == "B" and board[(x+1,y)] == "B" and board[(x+2,y)] == "B" and board[(x+3,y)] == "B"):
#                print "Winner == Black player"
#                break


def winHorizontal(board):
    for x in range(rows):
        for y in range(columns):
            try:
                if (board[(x,y)] == "R" and board [(x,y+1)] == "R" and board[(x,y+2)] == "R" and board[(x,y+3)] == "R"):
                    print("Winner == Red player")
                    try:
                        sys.exit(1)
                    except SystemExit:
                        pass

                    #break
            except Exception(e): pass
            try:
                if (board[(x,y)] == "B" and board[(x,y+1)] == "B" and board[(x,y+2)] == "B" and board[(x,y+3)] == "B"):
                    print("Winner == Black player")
                    try:
                        sys.exit(1)
                    except Systemxit:
                        pass
                    #break
            except Exception(e): pass


#def winHorizontal(board):
#    for x in range(rows):
#        for y in range(columns):
#            if (board[(x,y)] == "R" and board[(x,y+1)] == "R" and board[(x,y+2)] == "R" and board[(x,y+3)] == "R"):
#                print "Winner == Red player"
#                break
#            elif (board[(x,y)] == "B" and board[(x,y+1)] == "B" and board[(x,y+2)] == "B" and board[(x,y+3)] == "B"):
#                print "Winner == Black player"
#                break


def winDiagonal(board):
    for x in range(rows):
        for y in range(columns):
            try:
                if (board[(x,y)] == "R" and board[(x+1,y+1)] == "R" and board[(x+2,y+2)] == "R" and board[(x+3,y+3)] =="R"):
                    print("Winner == Red player")
                    try:
                        sys.exit(1)
                    except Systemxit:
                        pass
                    #break
            except Exception(e): pass
            try:
                if (board[(x,y)] == "B" and board[(x+1,y+1)] == "B" and board[(x+2,y+2)] == "B" and board[(x+3,y+3)] =="B"):
                    print("Winner == Black player")
                    try:
                        sys.exit(1)
                    except Systemxit:
                        pass
                    #break
            except Exception(e): pass
            try:
                if (board[(x,y)] == "R" and board[(x-1,y-1)] == "R" and board[(x-2,y+2)] == "R" and board[(x-3,y+3)] =="R"):
                    print("Winner == Red player")
                    try:
                        sys.exit(1)
                    except Systemxit:
                        pass
                    #break
            except Exception(e): pass
            try:
                if (board[(x,y)] == "B" and board[(x-1,y-1)] == "B" and board[(x-2,y+2)] == "B" and board[(x-3,y+3)] =="B"):
                    print("Winner == Black player")
                    try:
                        sys.exit(1)
                    except Systemxit:
                        pass
                    #break
            except Exception(e): pass




#def winDiagonal(board):    
#    for x in range(rows):
#        for y in range(columns):
#            if (board[x,y] == "R" and board[(x+1,y+1)] == "R" and board[(x+2,y+2)] == "R" and board[(x+3,y+3)] =="R"):
#                print "Winner == Red player"
#            elif (board[x,y] == "B" and board[(x+1,y+1)] == "B" and board[(x+2,y+2)] == "B" and board[(x+3,y+3)] =="B"):
#                print "Winner == Black player"
#            elif (board[x,y] == "R" and board[(x-1,y-1)] == "R" and board[(x-2,y+2)] == "R" and board[(x-3,y+3)] =="R"):
#                print "Winner == Red player"
#            elif (board[x,y] == "B" and board[(x-1,y-1)] == "B" and board[(x-2,y+2)] == "B" and board[(x-3,y+3)] =="B"):
#                print "Winner == Black player"
#            


game()

Editor's note:
Please don't hijack old threads! Create your own properly titled thread and use code tags to show your code. Also state your problems or error messages.

[code=python]
your Python code here
[/code]

Edited 3 Years Ago by happygeek: fixed formatting

You need to click that code button before pasting the code, otherwise the white space is messed up!

Edited 3 Years Ago by happygeek: fixed formatting

This question has already been answered. Start a new discussion instead.