#!/usr/bin/python

import os.path

n = 10
allPositions = []
state = []
blackPairCap = 0
whitePairCap = 0
blackInaRow = 0
whiteInaRow = 0

row0 = []
row1 = []
row2 = []
row3 = []
row4 = []
row5 = []
row6 = []
row7 = []
row8 = []
row9 = []

allDirections = [[-1, 0],[1, 0],[0, -1],[0, 1],[-1, -1],[-1, 1],[1, -1],[1, 1]]

def displayBoard(playersTurn):
    print ""
    print "     0  1  2  3  4  5  6  7  8  9   "
    print "  +--+--+--+--+--+--+--+--+--+--+--+"
    #------------------------------------------------#
    print "0" + " |  " + str(row0[0]) + "  " + str(row0[1]) + "  " + str(row0[2]) + "  " + str(row0[3]) + "  " + str(row0[4]) + "  " + str(row0[5]) + "  " + str(row0[6]) + "  " + str(row0[7]) + "  " + str(row0[8]) + "  " + str(row0[9]) + "  " + "|"
    print "1" + " |  " + str(row1[0]) + "  " + str(row1[1]) + "  " + str(row1[2]) + "  " + str(row1[3]) + "  " + str(row1[4]) + "  " + str(row1[5]) + "  " + str(row1[6]) + "  " + str(row1[7]) + "  " + str(row1[8]) + "  " + str(row1[9]) + "  " + "|"
    print "2" + " |  " + str(row2[0]) + "  " + str(row2[1]) + "  " + str(row2[2]) + "  " + str(row2[3]) + "  " + str(row2[4]) + "  " + str(row2[5]) + "  " + str(row2[6]) + "  " + str(row2[7]) + "  " + str(row2[8]) + "  " + str(row2[9]) + "  " + "|"
    print "3" + " |  " + str(row3[0]) + "  " + str(row3[1]) + "  " + str(row3[2]) + "  " + str(row3[3]) + "  " + str(row3[4]) + "  " + str(row3[5]) + "  " + str(row3[6]) + "  " + str(row3[7]) + "  " + str(row3[8]) + "  " + str(row3[9]) + "  " + "|"
    print "4" + " |  " + str(row4[0]) + "  " + str(row4[1]) + "  " + str(row4[2]) + "  " + str(row4[3]) + "  " + str(row4[4]) + "  " + str(row4[5]) + "  " + str(row4[6]) + "  " + str(row4[7]) + "  " + str(row4[8]) + "  " + str(row4[9]) + "  " + "|"
    print "5" + " |  " + str(row5[0]) + "  " + str(row5[1]) + "  " + str(row5[2]) + "  " + str(row5[3]) + "  " + str(row5[4]) + "  " + str(row5[5]) + "  " + str(row5[6]) + "  " + str(row5[7]) + "  " + str(row5[8]) + "  " + str(row5[9]) + "  " + "|"
    print "6" + " |  " + str(row6[0]) + "  " + str(row6[1]) + "  " + str(row6[2]) + "  " + str(row6[3]) + "  " + str(row6[4]) + "  " + str(row6[5]) + "  " + str(row6[6]) + "  " + str(row6[7]) + "  " + str(row6[8]) + "  " + str(row6[9]) + "  " + "|"
    print "7" + " |  " + str(row7[0]) + "  " + str(row7[1]) + "  " + str(row7[2]) + "  " + str(row7[3]) + "  " + str(row7[4]) + "  " + str(row7[5]) + "  " + str(row7[6]) + "  " + str(row7[7]) + "  " + str(row7[8]) + "  " + str(row7[9]) + "  " + "|"
    print "8" + " |  " + str(row8[0]) + "  " + str(row8[1]) + "  " + str(row8[2]) + "  " + str(row8[3]) + "  " + str(row8[4]) + "  " + str(row8[5]) + "  " + str(row8[6]) + "  " + str(row8[7]) + "  " + str(row8[8]) + "  " + str(row8[9]) + "  " + "|"
    print "9" + " |  " + str(row9[0]) + "  " + str(row9[1]) + "  " + str(row9[2]) + "  " + str(row9[3]) + "  " + str(row9[4]) + "  " + str(row9[5]) + "  " + str(row9[6]) + "  " + str(row9[7]) + "  " + str(row9[8]) + "  " + str(row9[9]) + "  " + "|"
    #------------------------------------------------#
    print "  +--+--+--+--+--+--+--+--+--+--+--+\n"
    if (playersTurn % 2) == 0:
        print "WHITE's MOVE : \n"
    elif (playersTurn % 2) != 0:
        print "BLACK's MOVE : \n"

def createPositions():
    for i in range(0,n):
        for j in range(0,n):
            allPositions.append([i,j])
            
def winner():
    if (blackPairCap == 5):
        print "White is the winner!"
        return True
    elif (whitePairCap == 5):
        print "Black is the winner!"
        return True
    elif (blackInaRow >= 5):
        print "Black is the winner!"
        return True
    elif (whiteInaRow >= 5):
        print "White is the winner!"
        return True
        
player1 = ""
player2 = ""
comp1 = ""
comp2 = ""

currentPlayers = []

FILES = []

def startGameMenu():
    print "Do you have a save file that you would like to run the game from?\n"
    saveFile = raw_input("y or n")
    saveFile = str(saveFile)
    if saveFile == "y":
        saveFileGame()
    if saveFile == "n":
        newGame()
        
def goesFirst():
    from random import choice
    return choice(currentPlayers)
    
def isBlank(row,col):
    if row == 0:
        if row0[col] == '.':
            return True
    elif row == 1:
        if row1[col] == '.':
            return True
    elif row == 2:
        if row2[col] == '.':
            return True
    elif row == 3:
        if row3[col] == '.':
            return True
    elif row == 4:
        if row4[col] == '.':
            return True
    elif row == 5:
        if row5[col] == '.':
            return True
    elif row == 6:
        if row6[col] == '.':
            return True
    elif row == 7:
        if row7[col] == '.':
            return True
    elif row == 8:
        if row8[col] == '.':
            return True
    elif row == 9:
        if row9[col] == '.':
            return True
            
def isNextTo(row,col):
    if row == 0:
        if (row0[col-1] != '.') or (row0[col+1] != '.') or (row1[col] != '.') or (row1[col-1] != '.') or (row1[col+1] != '.'):
            return True
    elif row == 1:
        if (row0[col-1] != '.') or (row0[col+1] != '.') or (row0[col] != '.') or (row1[col-1] != '.') or (row1[col+1] != '.') or (row2[col-1] != '.') or (row2[col] != '.') or (row2[col+1] != '.'):
            return True
    elif row == 2:
        if (row1[col-1] != '.') or (row1[col+1] != '.') or (row1[col] != '.') or (row2[col-1] != '.') or (row2[col+1] != '.') or (row3[col-1] != '.') or (row3[col] != '.') or (row3[col+1] != '.'):
            return True
    elif row == 3:
        if (row2[col-1] != '.') or (row2[col+1] != '.') or (row2[col] != '.') or (row3[col-1] != '.') or (row3[col+1] != '.') or (row4[col-1] != '.') or (row4[col] != '.') or (row4[col+1] != '.'):
            return True
    elif row == 4:
        if (row3[col-1] != '.') or (row3[col+1] != '.') or (row3[col] != '.') or (row4[col-1] != '.') or (row4[col+1] != '.') or (row5[col-1] != '.') or (row5[col] != '.') or (row5[col+1] != '.'):
            return True
    elif row == 5:
        if (row4[col-1] != '.') or (row4[col+1] != '.') or (row4[col] != '.') or (row5[col-1] != '.') or (row5[col+1] != '.') or (row6[col-1] != '.') or (row6[col] != '.') or (row6[col+1] != '.'):
            return True
    elif row == 6:
        if (row5[col-1] != '.') or (row5[col+1] != '.') or (row5[col] != '.') or (row6[col-1] != '.') or (row6[col+1] != '.') or (row7[col-1] != '.') or (row7[col] != '.') or (row7[col+1] != '.'):
            return True
    elif row == 7:
        if (row6[col-1] != '.') or (row6[col+1] != '.') or (row6[col] != '.') or (row7[col-1] != '.') or (row7[col+1] != '.') or (row8[col-1] != '.') or (row8[col] != '.') or (row8[col+1] != '.'):
            return True
    elif row == 8:
        if (row7[col-1] != '.') or (row7[col+1] != '.') or (row7[col] != '.') or (row8[col-1] != '.') or (row8[col+1] != '.') or (row9[col-1] != '.') or (row9[col] != '.') or (row9[col+1] != '.'):
            return True
    elif row == 9:
        if (row9[col-1] != '.') or (row9[col+1] != '.') or (row8[col] != '.') or (row8[col-1] != '.') or (row8[col+1] != '.'):
            return True
            
def countRow(color,row,col):
    if row == 0:
        for n in range(0,5):
            if (row0[col-(n+1)] == color) or (row0[col+(n+1)] == color) or (row1[col] == color) or (row1[col-(n+1)] == color) or (row1[col+(n+1)] == color):
                if color == "white":
                    whiteInaRow += 1
                elif color == "black":
                    blackInaRow += 1
    elif row == 1:
        for n in range(0,5):
            if (row0[col-(n+1)] == color) or (row0[col+(n+1)] == color) or (row0[col] == color) or (row1[col-(n+1)] == color) or (row1[col+(n+1)] == color) or (row2[col-(n+1)] == color) or (row2[col] == color) or (row2[col+(n+1)] == color):
                if color == "white":
                    whiteInaRow += 1
                elif color == "black":
                    blackInaRow += 1
    elif row == 2:
        for n in range(0,5):
            if (row1[col-(n+1)] == color) or (row1[col+(n+1)] == color) or (row1[col] == color) or (row2[col-(n+1)] == color) or (row2[col+(n+1)] == color) or (row3[col-(n+1)] == color) or (row3[col] == color) or (row3[col+(n+1)] == color):
                if color == "white":
                    whiteInaRow += 1
                elif color == "black":
                    blackInaRow += 1
    elif row == 3:
        for n in range(0,5):
            if (row2[col-(n+1)] == color) or (row2[col+(n+1)] == color) or (row2[col] == color) or (row3[col-(n+1)] == color) or (row3[col+(n+1)] == color) or (row4[col-(n+1)] == color) or (row4[col] == color) or (row4[col+(n+1)] == color):
                if color == "white":
                    whiteInaRow += 1
                elif color == "black":
                    blackInaRow += 1
    elif row == 4:
        for n in range(0,5):
            if (row3[col-(n+1)] == color) or (row3[col+(n+1)] == color) or (row3[col] == color) or (row4[col-(n+1)] == color) or (row4[col+(n+1)] == color) or (row5[col-(n+1)] == color) or (row5[col] == color) or (row5[col+(n+1)] == color):
                if color == "white":
                    whiteInaRow += 1
                elif color == "black":
                    blackInaRow += 1
    elif row == 5:
        for n in range(0,5):
            if (row4[col-(n+1)] == color) or (row4[col+(n+1)] == color) or (row4[col] == color) or (row5[col-(n+1)] == color) or (row5[col+(n+1)] == color) or (row6[col-(n+1)] == color) or (row6[col] == color) or (row6[col+(n+1)] == color):
                if color == "white":
                    whiteInaRow += 1
                elif color == "black":
                    blackInaRow += 1
    elif row == 6:
        for n in range(0,5):
            if (row5[col-(n+1)] == color) or (row5[col+(n+1)] == color) or (row5[col] == color) or (row6[col-(n+1)] == color) or (row6[col+(n+1)] == color) or (row7[col-(n+1)] == color) or (row7[col] == color) or (row7[col+(n+1)] == color):
                if color == "white":
                    whiteInaRow += 1
                elif color == "black":
                    blackInaRow += 1
    elif row == 7:
        for n in range(0,5):
            if (row6[col-(n+1)] == color) or (row6[col+(n+1)] == color) or (row6[col] == color) or (row7[col-(n+1)] == color) or (row7[col+(n+1)] == color) or (row8[col-(n+1)] == color) or (row8[col] == color) or (row8[col+(n+1)] == color):
                if color == "white":
                    whiteInaRow += 1
                elif color == "black":
                    blackInaRow += 1
    elif row == 8:
        for n in range(0,5):
            if (row7[col-(n+1)] == color) or (row7[col+(n+1)] == color) or (row7[col] == color) or (row8[col-(n+1)] == color) or (row8[col+(n+1)] == color) or (row9[col-(n+1)] == color) or (row9[col] == color) or (row9[col+(n+1)] == color):
                if color == "white":
                    whiteInaRow += 1
                elif color == "black":
                    blackInaRow += 1
    elif row == 9:
        for n in range(0,5):
            if (row9[col-(n+1)] == color) or (row9[col+(n+1)] == color) or (row8[col] == color) or (row8[col-(n+1)] == color) or (row8[col+(n+1)] == color):
                if color == "white":
                    whiteInaRow += 1
                elif color == "black":
                    blackInaRow += 1
        
def inGameMenu():
    turn = 0
    displayBoard(turn)
    while not winner():
        move = raw_input("Enter move in row, column form (i.e. '6 6'): ")
        moveRow = int(move[0])
        moveCol = int(move[2])
        if turn == 0:
            if moveRow == 0:
                if turn % 2 == 0:
                    row0[moveCol] = 'w'
                else:
                    row0[moveCol] = 'b'
            elif moveRow == 1:
                if turn % 2 == 0:
                    row1[moveCol] = 'w'
                else:
                    row1[moveCol] = 'b'
            elif moveRow == 2:
                if turn % 2 == 0:
                    row2[moveCol] = 'w'
                else:
                    row2[moveCol] = 'b'
            elif moveRow == 3:
                if turn % 2 == 0:
                    row3[moveCol] = 'w'
                else:
                    row3[moveCol] = 'b'
            elif moveRow == 4:
                if turn % 2 == 0:
                    row4[moveCol] = 'w'
                else:
                    row4[moveCol] = 'b'
            elif moveRow == 5:
                if turn % 2 == 0:
                    row5[moveCol] = 'w'
                else:
                    row5[moveCol] = 'b'
            elif moveRow == 6:
                if turn % 2 == 0:
                    row6[moveCol] = 'w'
                else:
                    row6[moveCol] = 'b'
            elif moveRow == 7:
                if turn % 2 == 0:
                    row7[moveCol] = 'w'
                else:
                    row7[moveCol] = 'b'
            elif moveRow == 8:
                if turn % 2 == 0:
                    row8[moveCol] = 'w'
                else:
                    row8[moveCol] = 'b'
            elif moveRow == 9:
                if turn % 2 == 0:
                    row9[moveCol] = 'w'
                else:
                    row9[moveCol] = 'b'
        if isNextTo(moveRow,moveCol) and isBlank(moveRow,moveCol):
            if moveRow == 0:
                if turn % 2 == 0:
                    row0[moveCol] = 'w'
                else:
                    row0[moveCol] = 'b'
            elif moveRow == 1:
                if turn % 2 == 0:
                    row1[moveCol] = 'w'
                else:
                    row1[moveCol] = 'b'
            elif moveRow == 2:
                if turn % 2 == 0:
                    row2[moveCol] = 'w'
                else:
                    row2[moveCol] = 'b'
            elif moveRow == 3:
                if turn % 2 == 0:
                    row3[moveCol] = 'w'
                else:
                    row3[moveCol] = 'b'
            elif moveRow == 4:
                if turn % 2 == 0:
                    row4[moveCol] = 'w'
                else:
                    row4[moveCol] = 'b'
            elif moveRow == 5:
                if turn % 2 == 0:
                    row5[moveCol] = 'w'
                else:
                    row5[moveCol] = 'b'
            elif moveRow == 6:
                if turn % 2 == 0:
                    row6[moveCol] = 'w'
                else:
                    row6[moveCol] = 'b'
            elif moveRow == 7:
                if turn % 2 == 0:
                    row7[moveCol] = 'w'
                else:
                    row7[moveCol] = 'b'
            elif moveRow == 8:
                if turn % 2 == 0:
                    row8[moveCol] = 'w'
                else:
                    row8[moveCol] = 'b'
            elif moveRow == 9:
                if turn % 2 == 0:
                    row9[moveCol] = 'w'
                else:
                    row9[moveCol] = 'b'
        turn += 1
        displayBoard(turn)

def saveFileGame():
    fileName = raw_input("What is the name of the save file (with extension)? \n")
    FILES.append(fileName)
    f = open(fileName, 'r')
    lineList = f.readlines()
    counter = 0
    for line in lineList:
        if counter == 0:
            row0.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
        if counter == 1:
            row1.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
        if counter == 2:
            row2.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
        if counter == 3:
            row3.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
        if counter == 4:
            row4.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
        if counter == 5:
            row5.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
        if counter == 6:
            row6.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
        if counter == 7:
            row7.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
        if counter == 8:
            row8.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
        if counter == 9:
            row9.extend([line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]])
        counter += 1
    f.close()
    player1 = "white"
    player2 = "black"
    currentPlayers.append(player1)
    currentPlayers.append(player2)
    inGameMenu()
    
def blankBoard():
    row0.extend(['.','.','.','.','.','.','.','.','.','.'])
    row1.extend(['.','.','.','.','.','.','.','.','.','.'])
    row2.extend(['.','.','.','.','.','.','.','.','.','.'])
    row3.extend(['.','.','.','.','.','.','.','.','.','.'])
    row4.extend(['.','.','.','.','.','.','.','.','.','.'])
    row5.extend(['.','.','.','.','.','.','.','.','.','.'])
    row6.extend(['.','.','.','.','.','.','.','.','.','.'])
    row7.extend(['.','.','.','.','.','.','.','.','.','.'])
    row8.extend(['.','.','.','.','.','.','.','.','.','.'])
    row9.extend(['.','.','.','.','.','.','.','.','.','.'])
    
def newGame():
    blankBoard()
    print "Welcome to Pente!\n"
    print "Would you like to play a new game?\n"
    answer = raw_input("y or n:")
    answer = str(answer)
    if (answer == "y"):
        numPlayers = raw_input("How many players are going to be playing? (1-4):\n")
        numPlayers = int(numPlayers)
        for p in range(0,numPlayers):
            print "Would you like to be Player 1, Player 2, PC 1, or PC 2?\n"
            player = raw_input("p1, p2, pc1, or pc2:")
            player = str(player)
            if (player == "p1"):
                currentPlayers.append(player1)
            elif (player == "p2"):
                currentPlayers.append(player2)
            elif (player == "pc1"):
                currentPlayers.append(comp1)
            elif (player == "pc2"):
                currentPlayers.append(comp2)
            print "Which color would you like to play as?\n"
            color = raw_input("b (black) or w (white):")
            color = str(color)
            if (color == "b"):
                currentPlayers[p] = "black"
            elif (color == "w"):
                currentPlayers[p] = "white"
    inGameMenu()
    
startGameMenu()
inGameMenu()

Recommended Answers

All 3 Replies

Oh my God, man; have you never heard of lists and loops?

row0, row2 etc. can become a list of lists called rows, and that code where you explicitly print each element of each row can be put into a simple loop.

No, this doesn't solve your problem, but code this messy is almost guaranteed to have issues.

[Snipped for brevity]

Your problems all stem from the fact that you aren't recognizing the 'scope' of your variables. You shouldn't be using global variables at all in fact, and your code could use a more coherent structure (for your own sake).

Here's a quick example of a better structure to use for your code:

#!/usr/bin/python

import os.path
from random import choice

def displayBoard( ... ):
    ...

def createPositions( ... ):
    ...

...

def main():
    # Initialize all your variables here
    win = False

    while not win:
        # Call your 'game' functions
        ...
        win = winner()
    
    # Print any final messages to the user, etc.

if __name__ == '__main__':
    main()

Every single one of your functions needs to be revamped to make use of local variables only. A function should be completely independent of the code outside the function. It should receive the inputs from the main code, do the work that it needs to do, and then return a result. It should not be using global variables willy-nilly because you'll run into many problems like you have in this code.

Also your functions should not be calling themselves. This is recursion and it's not useful in this type of situation. You should simply set up a while loop as I demonstrated above and continually call each function until the game has been won

commented: to be honest man, this guy has a lot more to learn that just the proper use of functions. +6

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.