Hi, I am taking a course python essentials 1 in python institute. There is a final project in Module 4_tic-tac-toe. This is my try, but seems lots of bugs here…and room for improvement…
import random
def enter_move(board, moved):
list = board
list_moved = moved
move = int(input("Enter your move: "))
for i in range (len(list_moved)):
if move == list_moved[i]:
enter_move(board, moved)
else:
if (move == 1) or (move == 2) or (move == 3):
col = move - 1
list[0][col] = "O"
print(*list, sep="\n")
list_moved.append(move)
victory_for(list, list_moved)
elif (move == 4) or (move == 5) or (move == 6):
col = move - 4
list[1][col] = "O"
print(*list, sep="\n")
list_moved.append(move)
victory_for(list, list_moved)
elif (move == 7) or (move == 8) or (move == 9):
col = move - 7
list[2][col] = "O"
print(*list, sep="\n")
list_moved.append(move)
victory_for(list, list_moved)
else:
print("discover bug here! ")
def victory_for(board, moved):
list = board
list_moved = moved
if (len(list_moved) == 2) or (len(list_moved) == 4):
draw_move(list, list_moved)
elif (len(list_moved) == 3):
enter_move(list, list_moved)
elif (len(list_moved) == 5) or (len(list_moved) == 7):
if (list[0][0] == "X" and list[0][1] == "X" and list[0][2] == "X") or (list[1][0] == "X" and list[1][1] == "X" and list[1][2] == "X") or (list[2][0] == "X" and list[2][1] == "X" and list[2][2] == "X"):
print("Computer win! ")
elif (list[0][0] == "X" and list[1][0] == "X" and list[2][0] == "X") or (list[0][1] == "X" and list[1][1] == "X" and list[2][1] == "X") or (list[0][2] == "X" and list[1][2] == "X" and list[2][2] == "X"):
print("Computer win! ")
elif (list[0][0] == "X" and list[1][1] == "X" and list[2][2] == "X") or (list[0][2] == "X" and list[1][1] == "X" and list[2][0] == "X"):
print("Computer win! ")
else:
enter_move(list, list_moved)
elif (len(list_moved) == 6) or (len(list_moved) == 8):
if (list[0][0] == "O" and list[0][1] == "O" and list[0][2] == "O") or (list[1][0] == "O" and list[1][1] == "O" and list[1][2] == "O") or (list[2][0] == "O" and list[2][1] == "O" and list[2][2] == "O"):
print("Player win! ")
elif (list[0][0] == "O" and list[1][0] == "O" and list[2][0] == "O") or (list[0][1] == "O" and list[1][1] == "O" and list[2][1] == "O") or (list[0][2] == "O" and list[1][2] == "O" and list[2][2] == "O"):
print("Player win! ")
elif (list[0][0] == "O" and list[1][1] == "O" and list[2][2] == "O") or (list[0][2] == "O" and list[1][1] == "O" and list[2][0] == "O"):
print("Player win! ")
else:
draw_move(list, list_moved)
elif (len(list_moved) == 9):
if (list[0][0] == "X" and list[0][1] == "X" and list[0][2] == "X") or (list[1][0] == "X" and list[1][1] == "X" and list[1][2] == "X") or (list[2][0] == "X" and list[2][1] == "X" and list[2][2] == "X"):
print("Computer win! ")
elif (list[0][0] == "X" and list[1][0] == "X" and list[2][0] == "X") or (list[0][1] == "X" and list[1][1] == "X" and list[2][1] == "X") or (list[0][2] == "X" and list[1][2] == "X" and list[2][2] == "X"):
print("Computer win! ")
elif (list[0][0] == "X" and list[1][1] == "X" and list[2][2] == "X") or (list[0][2] == "X" and list[1][1] == "X" and list[2][0] == "X"):
print("Computer win! ")
else:
print("Draw, no one win the game! ")
else:
print("Discove bug, please fix it! ")
def draw_move(board, moved):
list = board
list_moved = moved
move = int(random.randint(1,9))
for i in range (len(list_moved)):
if move == list_moved[i]:
draw_move(board, moved)
if (move == 1) or (move == 2) or (move == 3):
col = move - 1
list[0][col] = "X"
print(f"Computer enter {move}! ")
print(*list, sep="\n")
list_moved.append(move)
victory_for(list, list_moved)
elif (move == 4) or (move == 5) or (move == 6):
col = move - 4
list[1][col] = "X"
print(f"Computer enter {move}! ")
print(*list, sep="\n")
list_moved.append(move)
victory_for(list, list_moved)
elif (move == 7) or (move == 8) or (move == 9):
col = move - 7
list[2][col] = "X"
print(f"Computer enter {move}! ")
print(*list, sep="\n")
list_moved.append(move)
victory_for(list, list_moved)
else:
print("discover bug here! ")
board = [[1,2,3],[4,"X",6],[7,8,9]]
print(*board, sep="\n")
enter_move(board, [5])