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?