lundi 20 avril 2020

Connect 4 Python

I'm trying to make a basic connect4 in python, just using loops, if statements or functions, once i made the board i have a problem with the game itself. The problem is that i cant get that when i put an "x" or "o" where there is already one the program puts it in the row above it. In other words that the players cant supersedes the other one position.

def drawField(field):
    for row in range(12):
        if row%2 == 0:#0,2,4
            practicalRow = int(row/2)#0.0,1.0,2.0
            for column in range(15):#0,1,2,3,4
                                 #eo,,e1,.e2 in currentField
                if column%2 == 0:
                    if column != 14:
                        print("|", end ="")
                    else:
                        print("|")
                else:
                    practicalColumn = int(column/2)
                    print(field[practicalRow][practicalColumn], end ="")
        else:
            print("---------------")
Player = 1
last = -1
currentField = [[" ", " ", " ", " ", " ", " ", " "],[" ", " ", " ", " ", " ", " ", " "],[" ", " ", " ", " ", " ", " ", " "],[" ", " ", " ", " ", " ", " ", " "],[" ", " ", " ", " ", " ", " ", " "],[" ", " ", " ", " ", " ", " ", " "]]
drawField(currentField)
while(True): # true == true
    print("Players turn: ",Player)
    MoveColumn = int(input("Enter column\n"))
    if Player == 1:
        #make move for 1
        for row in range(len(currentField)):
            if currentField[row][MoveColumn] == " ":
                row = last
                currentField[row][MoveColumn] = "x"
                break
            else:
                last -= 1
                row = last
                currentField[row][MoveColumn] = "x"
        Player = 2

    else:
        #make move for 2
        for row in range(len(currentField)):
            if currentField[row][MoveColumn] == " ":
                row = last
                currentField[row][MoveColumn] = "o"
                break
            else:
                last-=1
        Player = 1
    drawField(currentField)

Aucun commentaire:

Enregistrer un commentaire