0

So basically the task that I am trying to complete is making a connect four game using python and text-based coding to type out a connect 4 game board. I have currently finished creating the board and allowing the player to insert a counter (O) into the playing board's first row. However, I can't seem to allow the player to place counters in any of the above rows, since the data "O" is being stored in the variable that it needs to be, but then is being changed back to it's original variable once the loop restarts for the second turn. Currently I haven't developed any opponent or player 2 for the game yet, since this problem has me so stumped. I have tried various different methods like creating different loops, breaking the loop in certain areas, and some other methods I thought would resolve the issue, but nothing has worked so far. Here is the code I am talking about:

place2 = " "
place3 = " "
place4 = " "
place5 = " "
place6 = " "
place7 = " "
place8 = " "
place9 = " "
place10 = " "
place11 = " "
place12 = " "
place13 = " "
place14 = " "
place15 = " "
place16 = " "
place17 = " "
place18 = " "
place19 = " "
place20 = " "
place21 = " "
place22 = " "
place23 = " "
place24 = " "
place25 = " "
print("This is connect 4. You have to connect 4 tokens on the board before your opponent")
print("""
| {20} | {21} | {22} | {23} | {24} |
|---|---|---|---|---|
| {15} | {16} | {17} | {18} | {19} |
|---|---|---|---|---|
| {10} | {11} | {12} | {13} | {14} |
|---|---|---|---|---|
| {5} | {6} | {7} | {8} | {9} |
|---|---|---|---|---|
| {0} | {1} | {2} | {3} | {4} |
|---|---|---|---|---|
| A | B | C | D | E |
""".format(place1, place2, place3, place4, place5, place6, place7, place8, place9, place10, place11, place12, place13, place14, place15, place16, place17, place18, place19, place20, place21, place22, place23, place24, place25))
while True:
    place = input("Pick a slot to put your token in; A, B, C, D or E:   ")

    if place.casefold() == "a":
        place1 = "O"
    elif place.casefold() == "b":
        place2 = "O"
    elif place.casefold() == "c":
        place3 = "O"
    elif place.casefold() == "d":
        place4 = "O"
    elif place.casefold() == "e":
        place5 = "O"
    else:
        print("That's not an option, select again")

    if place5 == "O":
        place10 = "O"
    elif place10 == "O":
        place15 = "O"
    elif place15 == "O":
        place20 = "O"
    elif place20 == "O":
        place25 == "O"

    print("You placed your token in the " + str(place).upper() + " column")
    print("""
    | {20} | {21} | {22} | {23} | {24} |
    |---|---|---|---|---|
    | {15} | {16} | {17} | {18} | {19} |
    |---|---|---|---|---|
    | {10} | {11} | {12} | {13} | {14} |
    |---|---|---|---|---|
    | {5} | {6} | {7} | {8} | {9} |
    |---|---|---|---|---|
    | {0} | {1} | {2} | {3} | {4} |
    |---|---|---|---|---|
    | A | B | C | D | E |
    """.format(place1, place2, place3, place4, place5, place6, place7, place8, place9, place10, place11, place12, place13, place14, placee15, place16, place17, place18, place19, place20, place21, place22, place23, place24, place25))

This code is a little messy as this is my first year taking coding as a subject. I decided to test the code on the "e" column first, however whenever the player inputs "e", the code displays a "O" in both place 5 and 10. I suspect this may be due to the fact that the code says that if place5 = "O" then place10 also = "O". Is there a way to permanently store "O" in the place of the variables, and is there a proper way of making it so that if a token is in the place that is entered, the token will be placed above it?

MaxCoder63
  • 25
  • 4

1 Answers1

0

I think the main reason your code confused you is how messy it was. You could simplify it a lot by using a list for the different places, and then unpack it for the format function using the * operator. Also, you started indexing your places from 0, which is generally a good idea, but then forgot about it and treated place1 as the base of column a, even though it is place0.

I allowed myself to also change the way you place the user's choice to a loop, which is a little clearer and also is less reliant on a certain board size. This is the end result, which works - let me know if you don't understand something:

places = [" "] * 25
print("This is connect 4. You have to connect 4 tokens on the board before your opponent")

while True:
    print("""
| {20} | {21} | {22} | {23} | {24} |
|---|---|---|---|---|
| {15} | {16} | {17} | {18} | {19} |
|---|---|---|---|---|
| {10} | {11} | {12} | {13} | {14} |
|---|---|---|---|---|
| {5} | {6} | {7} | {8} | {9} |
|---|---|---|---|---|
| {0} | {1} | {2} | {3} | {4} |
|---|---|---|---|---|
| A | B | C | D | E |
""".format(*places))  # Unpack 'places' list to match format

    place = input("Pick a slot to put your token in; A, B, C, D or E:   ")
    base = ord(place.lower()) - ord("a")  # Get index of lowest place in input letter's column

    for i in range(base, len(places), 5):
        if places[i] == " ":  # Is empty
            places[i] = "O"
            break
    else:  # No break, loop ended naturally
        print("That's not an option, select again")
        continue  # Don't show 'token placed' message

    print("You placed your token in the " + str(place).upper() + " column")
chuck
  • 1,420
  • 4
  • 19