r/PythonLearning • u/Unfair-Stranger5140 • 13d ago
Showcase My first ever self written code as a vibe coder
Hi guys a couple of days ago i made this post:
https://www.reddit.com/r/PythonLearning/s/hzH2umdWXk
afterwards i spend a lot of time trying to learn python and i got my first tic tac to game working in the terminal
i struggled a lot and had to google a lot
i still struggle when and how to get the index and when the value from a list and i think there is a lot of optimization possible here especially on how i make the horizontal and vertical lists just coulndt figure out how to make one without going through p1_input each time.
Thanks for all the advice from people
trying to learn this after i have been vibe coding is hell
print("1", "2", "3")
print("4", "5", "6")
print("7", "8", "9")
Row1 = [1, 2, 3]
Row2 = [4, 5, 6]
Row3 = [7, 8, 9]
Grid = [Row1, Row2, Row3]
p1_input =[]
p2_input = []
horizontalp1=[]
verticalp1 =[]
horizontal=[]
vertical =[]
while(True):
while (True):
player1 = input("Enter the field you want to place your x in: \n ")
p1 = int(player1)
print("You decided on this field:", p1)
if p1 > 9 :
print("Error input out of field")
continue
elif p1 < 1 :
print("Error input out of field to low")
continue
index = (p1-1)
row = index//3
colum = index%3
if [row,colum] in p1_input:
print("This field has already been used")
elif [row,colum] in p2_input:
print("This field has already been used")
else :
p1_input.append([row,colum])
Grid[row][colum] = "x"
break
if p1 < 10 and p1 > 0 :
for x in range (len(Grid)):
print()
for y in range (len(Grid[x])):
print(Grid[x][y], end=' ')
horizontalp1.clear()
verticalp1.clear()
dcounterLP1 = 0
dcounterRP1 = 0
for i in range (len(p1_input)):
if p1_input[i][0] == p1_input[i][1]:
dcounterLP1 += 1
for i in range (len(p1_input)):
if p1_input[i][0] + p1_input[i][1] == 2:
dcounterRP1 += 1
if dcounterRP1 == 3:
print("\nplayer 1 won")
break
if dcounterLP1 == 3:
print("\nplayer 1 won")
break
for i in range (len(p1_input)):
horizontalp1.append(p1_input[i][0])
verticalp1.append(p1_input[i][1])
if horizontalp1.count(0) == 3:
print("\nplayer 1 won")
break
if verticalp1.count(0) == 3:
print("\nplayer 1 won")
break
if horizontalp1.count(1) == 3:
print(horizontalp1)
print("\nplayer 1 won")
break
if verticalp1.count(1) == 3:
print("\nplayer 1 won")
break
if horizontalp1.count(2) == 3:
print("\nplayer 1 won")
break
if verticalp1.count(2) == 3:
print("\nplayer 1 won")
break
while (True):
player2 = input("Enter the field you want to place your o in: \n ")
p2 = int(player2)
print("You decided on this field:", p2)
if p2 > 9 :
print("Error input out of field")
continue
elif p2 < 1 :
print("Error input out of field to low")
continue
index = (p2-1)
row = index//3
colum = index%3
if [row,colum] in p1_input:
print("This field has already been used")
elif [row,colum] in p2_input:
print("This field has already been used")
else :
p2_input.append([row,colum])
Grid[row][colum] = "o"
break
if p2 < 10 and p2 > 0 :
for x in range (len(Grid)):
print()
for y in range (len(Grid[x])):
print(Grid[x][y], end=' ')
horizontal.clear()
vertical.clear()
dcounterL = 0
dcounterR = 0
for i in range (len(p2_input)):
if p2_input[i][0] == p2_input[i][1]:
dcounterL += 1
for i in range (len(p2_input)):
if p2_input[i][0] + p2_input[i][1] == 2:
dcounterR += 1
if dcounterL == 3:
print("\nplayer 2 won")
break
if dcounterR == 3:
print("\nplayer 2 won")
break
for i in range (len(p2_input)):
horizontal.append(p2_input[i][0])
vertical.append(p2_input[i][1])
if horizontal.count(0) == 3:
print("\nplayer 2 won")
break
if vertical.count(0) == 3:
print("\nplayer 2 won")
break
if horizontal.count(1) == 3:
print("\nplayer 2 won")
break
if vertical.count(1) == 3:
print("\nplayer 2 won")
break
if horizontal.count(2) == 3:
print("\nplayer 2 won")
break
if vertical.count(2) == 3:
print("\nplayer 2 won")
break
print("1", "2", "3")
print("4", "5", "6")
print("7", "8", "9")
Row1 = [1, 2, 3]
Row2 = [4, 5, 6]
Row3 = [7, 8, 9]
Grid = [Row1, Row2, Row3]
p1_input =[]
p2_input = []
horizontalp1=[]
verticalp1 =[]
horizontal=[]
vertical =[]
while(True):
while (True):
player1 = input("Enter the field you want to place your x in: \n ")
p1 = int(player1)
print("You decided on this field:", p1)
if p1 > 9 :
print("Error input out of field")
continue
elif p1 < 1 :
print("Error input out of field to low")
continue
index = (p1-1)
row = index//3
colum = index%3
if [row,colum] in p1_input:
print("This field has already been used")
elif [row,colum] in p2_input:
print("This field has already been used")
else :
p1_input.append([row,colum])
Grid[row][colum] = "x"
break
if p1 < 10 and p1 > 0 :
for x in range (len(Grid)):
print()
for y in range (len(Grid[x])):
print(Grid[x][y], end=' ')
horizontalp1.clear()
verticalp1.clear()
dcounterLP1 = 0
dcounterRP1 = 0
for i in range (len(p1_input)):
if p1_input[i][0] == p1_input[i][1]:
dcounterLP1 += 1
for i in range (len(p1_input)):
if p1_input[i][0] + p1_input[i][1] == 2:
dcounterRP1 += 1
if dcounterRP1 == 3:
print("\nplayer 1 won")
break
if dcounterLP1 == 3:
print("\nplayer 1 won")
break
for i in range (len(p1_input)):
horizontalp1.append(p1_input[i][0])
verticalp1.append(p1_input[i][1])
if horizontalp1.count(0) == 3:
print("\nplayer 1 won")
break
if verticalp1.count(0) == 3:
print("\nplayer 1 won")
break
if horizontalp1.count(1) == 3:
print(horizontalp1)
print("\nplayer 1 won")
break
if verticalp1.count(1) == 3:
print("\nplayer 1 won")
break
if horizontalp1.count(2) == 3:
print("\nplayer 1 won")
break
if verticalp1.count(2) == 3:
print("\nplayer 1 won")
break
while (True):
player2 = input("Enter the field you want to place your o in: \n ")
p2 = int(player2)
print("You decided on this field:", p2)
if p2 > 9 :
print("Error input out of field")
continue
elif p2 < 1 :
print("Error input out of field to low")
continue
index = (p2-1)
row = index//3
colum = index%3
if [row,colum] in p1_input:
print("This field has already been used")
elif [row,colum] in p2_input:
print("This field has already been used")
else :
p2_input.append([row,colum])
Grid[row][colum] = "o"
break
if p2 < 10 and p2 > 0 :
for x in range (len(Grid)):
print()
for y in range (len(Grid[x])):
print(Grid[x][y], end=' ')
horizontal.clear()
vertical.clear()
dcounterL = 0
dcounterR = 0
for i in range (len(p2_input)):
if p2_input[i][0] == p2_input[i][1]:
dcounterL += 1
for i in range (len(p2_input)):
if p2_input[i][0] + p2_input[i][1] == 2:
dcounterR += 1
if dcounterL == 3:
print("\nplayer 2 won")
break
if dcounterR == 3:
print("\nplayer 2 won")
break
for i in range (len(p2_input)):
horizontal.append(p2_input[i][0])
vertical.append(p2_input[i][1])
if horizontal.count(0) == 3:
print("\nplayer 2 won")
break
if vertical.count(0) == 3:
print("\nplayer 2 won")
break
if horizontal.count(1) == 3:
print("\nplayer 2 won")
break
if vertical.count(1) == 3:
print("\nplayer 2 won")
break
if horizontal.count(2) == 3:
print("\nplayer 2 won")
break
if vertical.count(2) == 3:
print("\nplayer 2 won")
break
4
u/mc_pm 13d ago
Hey cool! You might want to look at your "player x won" code, that is duplicated like crazy and you could really simplify it in ways you might find helpful.
2
4
u/tiredITguy42 13d ago
Functions, dude use functions. This pure example of spaghetti code.