Python Forum
I need help with Connect-Four Game Board - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: I need help with Connect-Four Game Board (/thread-33771.html)



I need help with Connect-Four Game Board - niiakoadjei - May-25-2021

# TIC-TAC-TOE FIELD

#| | | | | | | |0
#| | | | | | | |1
#| | | | | | | |2
#| | | | | | | |3
#| | | | | | | |4
#| | | | | | | |5
#012345678911111
#          01234

# We create a function to Draw a Tic-Tac-Toe field
# Let's use out temporary variable field as defined in the moves function
def drawField(field):
    for row in range(6):
        #i.e. 0,1,2,3,4
                # We map:      0,.,1,.,2
        #if row%2 == 1:
        #newRow = 6  # to convert any float number to integer
        # print(" | | ")
        for column in range(1,16):  # i.e. 0,1,2,3,4
                                # We map:  0,.,1,.,2 to match our moves columns and rows

            if column % 2 == 1:
                newColumn = int(column / 2)  # to convert any float number to integer

                if column != 16:
                    print("|", end="")
                else:
                    print("")

            else:
                print(field[newColumn][row], end="")

        else:
            print("")

Player = 1

currentField = [[" "," "," "," "," "," "],[" "," "," "," "," "," "],[" "," "," "," "," "," "],[" "," "," "," "," "," "],[" "," "," "," "," "," "],[" "," "," "," "," "," "],[" "," "," "," "," "," "]]

print(currentField)

#moveRow = 5
drawField(currentField)
while(True): # means while(True == True)
    print("Player's Turn: ",Player)

    moveColumn = int(input("Please enter a column number (Hint: 0 - 6): \n"))

    if Player == 1:
        moveRow = 6
        for i in reversed(range(moveRow)):
            moveRow = i
        # Make move for Player 1

            if currentField[moveColumn][moveRow] == " ":
                currentField[moveColumn][moveRow] = "X"
                break

            else:
                if currentField[moveColumn][0] == "X" or "O":
                    print("Column is full. Select another column: ")

        #print(currentField)

        drawField(currentField)
        Player = 2


    else:
        moveRow = 6
        # # Make move for Player 2
        for i in reversed(range(moveRow)):
            moveRow = i
            if currentField[moveColumn][moveRow] == " ":
                currentField[moveColumn][moveRow] = "O"
                break

            else:
                print("Column is full. Select another column: ")
                continue

        drawField(currentField)
        Player = 1
#I need help connecting the checkForWin function to the board. I know there are more efficient ways but I am a novice and want to follow the topics covered so far in order to deliver this project
def checkForWin(field):
    # Checking for horizontal tiles
    boardHeight = len(currentField[0])
    boardWidth = len(currentField)
    
    for y in range(boardHeight):
        for x in range(boardWidth - 3):
            if field[x][y] == field[x + 1][y] == field[x + 2][y] == field[x + 3][y]:
                print("PLAYER",Player,"WINS!")
                return
            # else:
            #     continue

    # Checking for vertical tiles
    for x in range(boardWidth):
        for y in range(boardHeight - 3):
            if field[x][y] == field[x][y + 1] == field[x][y + 2] == field[x][y + 3]:
                print("PLAYER",Player,"WINS")
                return


    # Checking for positive diagonal tiles (/)
    for x in range(boardWidth - 3):
        for y in range(3, boardHeight):
            if field[x][y] == field[x + 1][y - 1] == field[x + 2][y - 2] == field[x + 3][y - 3]:
                print("PLAYER",Player,"WINS")
                return
            # else:
            #     continue

    # Checking for positive diagonal tiles (\)
    for y in range(boardHeight - 3):
        for x in range(3, boardWidth):
            if field[x][y] == field[x + 1][y + 1] == field[x + 2][y + 2] == field[x + 3][y + 3]:
                print("PLAYER 1 WINS")



RE: I need help with Connect-Four Game Board - ilikedofs - May-26-2021

i ran it through my interpreter. i don't know if it just it have. here it is
Error:
File "main.py", line 54 def drawField(field): ^ SyntaxError: invalid syntax
i don't know why. but if this is for homework please send it to the homework forum where more people can help
and please put your code in a tag and please read this link if you plan to use this site more
https://python-forum.io/misc.php?action=help&hid=25


RE: I need help with Connect-Four Game Board - jefsummers - May-26-2021

OP - what error or behavior are you getting? Posting error message (in entirety) would be helpful