Python Forum
I need help with Connect-Four Game Board
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need help with Connect-Four Game Board
#1
# 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")
Larz60+ write May-26-2021, 01:57 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.

Attached Files

.zip   Connect4 Game_python.zip (Size: 1.2 KB / Downloads: 287)
Reply


Messages In This Thread
I need help with Connect-Four Game Board - by niiakoadjei - May-25-2021, 11:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation Please help me with my connect 4 game im beyond despair and I have it due in 4 days JuanT05 1 1,354 Jan-22-2022, 04:28 AM
Last Post: deanhystad
  Connect 4 Game DarksideMoses 11 10,718 May-15-2018, 02:02 PM
Last Post: buran
  Python - Treasure Generator on 5x5 Board PhillySports124 2 4,160 Dec-12-2017, 10:11 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020