Question about a logic in python code

good day everyone,
I’m trying to create a tic tac toe game project which got issue in the game logic [mainly after creating the code , something not working correctly in move as well as I was trying to assign players which one take which letter {X or O} so any help I would be grateful , here is my code:

import random

player = 1
board = [" ", " ", " ", " ", " ", " ", " ", " ", " ", " "]
game_letters = [‘X’, ‘O’]

def draw_board(board):
print(’---------’)
print(board[0] , ‘|’ , board[1] , ‘|’ , board[2])
print(’---------’)
print(board[3] , ‘|’ , board[4] , ‘|’ , board[5])
print(’---------’)
print(board[6] , ‘|’ , board[7] , ‘|’ , board[8])
print(’---------’)

def main():
draw_board(board)
print(“Welcome to TIC TAC TOE Game”)
player_1 = input()
player_2 = input()
turn = input(“Player " + str(player_1) + " would you like to choose X or O?”)
if(turn != str(“X”) and turn != str(“O”)):
turn = input("Please enter X or O: ")
switch_player(player)
print(str(player_1) + " take " + str(player)
+ str(player_2) + " take " + str(player))

``````while True:
draw_board(board)
move = int(input(str(player_1)) + " Make a move (X): ")
board[move] = "X"
if board[move] == "X" or board[move] == "O":
print(" Please make another move ")
draw_board(board)
move_2 = int(input(str(player_2) + " Make a move (X): "))
board[move] = "O"
if board[move] == "X" or board[move] == "O":

# player_1 choices
if board[0] == board[1] == board[2] == "X":  #across the top row
print(str(player_1)  +  "wins" )
break
if board[3] == board[4] == board[5] == "X":  #across the middle row
print(str(player_1)  +  "wins" )
break
if board[6] == board[7] == board[8] == "X":  #across the bottom row
print(str(player_1)  +  "wins" )
break
if board[0] == board[3] == board[6] == "X":  #first column
print(str(player_1)  +  "wins" )
break
if board[1] == board[4] == board[7] == "X":  #second column
print(str(player_1)  +  "wins" )
break
if board[2] == board[5] == board[8] == "X":  #third column
print(str(player_1)  +  "wins" )
break
if board[0] == board[4] == board[8] == "X":  #left diagonal
print(str(player_1)  +  "wins" )
break
if board[2] == board[4] == board[6] == "X":  #right diagonal
print(str(player_1)  +  "wins" )
break

# player_2 choices
if board[0] == board[1] == board[2] == "O":  #across the top row
print(str(player_2)  +  "wins" )
break
if board[3] == board[4] == board[5] == "O":  #across the middle row
print(str(player_2)  +  "wins" )
break
if board[6] == board[7] == board[8] == "O":  #across the bottom row
print(str(player_2)  +  "wins" )
break
if board[0] == board[3] == board[6] == "O":  #first column
print(str(player_2)  +  "wins" )
break
if board[1] == board[4] == board[7] == "O":  #second column
print(str(player_2)  +  "wins" )
break
if board[2] == board[5] == board[8] == "O":  #third column
print(str(player_2)  +  "wins" )
break
if board[0] == board[4] == board[8] == "O":  #left diagonal
print(str(player_2)  +  "wins" )
break
if board[2] == board[4] == board[6] == "O":  #right diagonal
print(str(player_2)  +  "wins" )
break
else:
print(" Tie ")
``````

switch players 2–>1, 1–>2

def switch_player(player):
if player == ‘X’:
player = ‘O’
elif player == ‘O’:
player = ‘X’
return player

if name == ‘main’:
main()

Hi @christine,

If you’re wanting support with python, I think you’re more likely to get support on a forum which is dedicated to python.

This forum is dedicated to using CoSpaces Edu, which uses JavaScript/Typescript and CoBlocks.

Ngā mihi,
Geoff @ TechLeap

apologies , didint know that .
thanks.

1 Like