good day everyone, I’m trying to implement a tic tac toe game project in [Python 3.9] and in my code I have created a sentence to assign which player take which letter whether x or o and I have created many statements but they didn’t work , kindly if anyone could assist to have a look on my code , I would be grateful.
import random
board = [" ", " ", " ", " ", " ", " ", " ", " ", " ", " "]
game_letters = [‘X’, ‘O’]
player1 = 0
player2 = 1
#—±–±–
#—±–±–
def draw_board(board): print(‘---------’) print(board[1] , ‘|’ , board[2] , ‘|’ , board[3]) print(‘---------’) print(board[4] , ‘|’ , board[5] , ‘|’ , board[6]) print(‘---------’) print(board[7] , ‘|’ , board[8] , ‘|’ , board[9]) print(‘---------’)
def main(): player1 = 0 player2 = 1 draw_board(board) print(“Welcome to TIC TAC TOE Game”) player_1 = print("Player 1 please enter your name: ") player_1 = input().upper() player_2 = print("Player 2 please enter your name: ") player_2 = input().upper() turn = input(“Player " + str(player_1) + " would you like to choose X or O?”) .upper() print(str(player_1) + " take : " + str(this_player) + str(player_2) + " take : " + str(this_player))
while True:
draw_board(board)
move = int(input(" Make a move (X): "))
valid_move = [ ]
for position in range(9):
if board[position]==0:
valid_move.append(position)
if move in valid_move:
break
print("illegal move")
board[move] = str("X")
"""if board[move] == str("X") or board[move] == str("O"):
print("Please make another move ")"""
draw_board(board)
move_2 = int(input(" Make a move (O): "))
valid_move= [ ]
for position in range(9):
if board[position]==0:
valid_move.append(position)
if move in valid_move:
break
print("illegal move")
board[move_2] = str("O")
"""if board[move_2] == str("X") or board[move] == str("O"):
print("Please make another move ")"""
# player_1 choices
if board[1] == board[2] == board[3] == "X": #across the top row
print(str(player_1) + "wins" )
break
elif board[4] == board[5] == board[6] == "X": #across the middle row
print(str(player_1) + "wins" )
break
elif board[7] == board[8] == board[9] == "X": #across the bottom row
print(str(player_1) + "wins" )
break
elif board[1] == board[4] == board[7] == "X": #first column
print(str(player_1) + "wins" )
break
elif board[2] == board[5] == board[8] == "X": #second column
print(str(player_1) + "wins" )
break
elif board[3] == board[6] == board[9] == "X": #third column
print(str(player_1) + "wins" )
break
elif board[1] == board[5] == board[9] == "X": #left diagonal
print(str(player_1) + "wins" )
break
elif board[3] == board[5] == board[7] == "X": #right diagonal
print(str(player_1) + "wins" )
break
# player_2 choices
if board[1] == board[2] == board[3] == "O": #across the top row
print(str(player_2) + "wins" )
break
elif board[4] == board[5] == board[6] == "O": #across the middle row
print(str(player_2) + "wins" )
break
elif board[7] == board[8] == board[9] == "O": #across the bottom row
print(str(player_2) + "wins" )
break
elif board[1] == board[4] == board[7] == "O": #first column
print(str(player_2) + "wins" )
break
elif board[2] == board[5] == board[8] == "O": #second column
print(str(player_2) + "wins" )
break
elif board[3] == board[6] == board[9] == "O": #third column
print(str(player_2) + "wins" )
break
elif board[1] == board[5] == board[9] == "O": #left diagonal
print(str(player_2) + "wins" )
break
elif board[3] == board[5] == board[7] == "O": #right diagonal
print(str(player_2) + "wins" )
break
else:
print(" It's a Tie ")
print("Game Over")
def next_player(this_player): return (this_player+1)%2
if name == ‘main’: main()
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Click below to sign up and get $100 of credit to try our products over 60 days!
Hi,
Your function
main()
assigns a letter to the player but its invocation is put at the end of the script (and it is conditional for some reason). You should move the invocation to the place before the part of the code that carries on the game. I modified your code a bit putting some comments into it. Look at the snippet below.This code sometimes runs to some point and gives another error. Try to fix it by yourself as it is the best way to learn something, IMO. If you stuck, then post another question.
Keep coding :-)
P.S. I noticed that you convert into strings some values that are already the strings, e.g.
input()
function returns the string (even if a user entered a number), so you do not have to convert it into string again. More info here.I hope it helps :-)