So i'm doing a python course online where i was given an assigment to do a tic- tac-toe game. I made the whole thing but i'm stuck at the replacement of list elements when the placeholder is to be replaced by O or X in the block chosen by user. I've already tried many ways to replace the list elements permanently and spent hours to find the way i could run it properly, but it still prints the same original board of tic-tac-toe.
# 2 players should be able to play the game (both sitting at the same computer)
# The board should be printed out every time a player makes a move
# You should be able to accept input of the player position and then place a symbol on the board
# x1 | y1 | z1
# ---- | ---- | ----
# x2 | y2 | z2
# ---- | ---- | ----
# x3 | y3 | z3
x1= 'top-left'
y1= 'top-mid'
z1= 'top-right'
x2= 'mid-left'
y2= 'mid-mid'
z2= 'mid-right'
x3= 'btm-left'
y3= 'btm-mid'
z3= 'btm-right'
e = [x1,y1,z1,x2,y2,z2,x3,y3,z3]
board = e[0]+ ' | '+ e[1]+ ' | '+ e[2]+'\n'+ \
'--------|------------|--------'+'\n'+\
e[3]+ ' | '+ e[4]+ ' | '+ e[5]+'\n'+ \
'--------|------------|--------'+'\n'+\
e[6]+ ' | '+ e[7]+ ' | '+ e[8]
def instructions():
print('Instuctions: \n')
print('Game: tic-tac-toe \n')
print('Player 1: O and Player 2: X \n\n')
print(' top-left | top-mid | top-right')
print(' -------- | ------- | ----------')
print(' mid-left | mid-mid | mid-right')
print(' -------- | ------- | ----------')
print(' btm-left | btm-mid | btm-right')
print('\n\n')
print('Choose your input when your turn comes.\n\n')
def player_one(inputvalue):
global e
for element in e:
if element == inputvalue:
e[e.index(element)]=' O ' #element that i wanna replace
break
query =checkSuccess()
if (query == True):
print('Tic-Tac-Toe ! Player1 has won the game! ')
elif (query == False):
print(board) # this still prints the original, old board.
def player_two(input):
global e
for element in e:
if element== input:
e[e.index(element)] =' X '
break
query =checkSuccess()
if (query == True):
print('Tic-Tac-Toe ! Player2 has won the game! ')
elif (query == False):
print(board)
ticTacToe()
board = e[0]+ ' | '+ e[1]+ ' | '+ e[2]+'\n'+ \
e[3]+ ' | '+ e[4]+ ' | '+ e[5]+'\n'+ \
e[6]+ ' | '+ e[7]+ ' | '+ e[8]
def checkSuccess():
if(e[0]==e[1]==e[2] or e[3]==e[4]==e[5] or e[6]==e[7]==e[8] or e[0] == e[3] == e[6] or
e[1]==e[4]==e[7] or e[2]==e[5]==e[8] or e[0]==e[4]==e[8] or e[6]==e[4]==e[2]):
return True
else:
return False
def ticTacToe():
p1 = input("Player1, Enter your choice \n")
player_one(p1)
p2 = input("Player2, Enter your choice \n")
player_two(p2)
instructions()
print(board)
ticTacToe()
The output i get:
top-left | top-mid | top-right
mid-left | mid-mid | mid-right
btm-left | btm-mid | btm-right
Player1, Enter your choice
mid-mid
top-left | top-mid | top-right
mid-left | mid-mid | mid-right
btm-left | btm-mid | btm-right
Player2, Enter your choice
btm-right
top-left | top-mid | top-right
mid-left | mid-mid | mid-right
btm-left | btm-mid | btm-right
Player1, Enter your choice
The o/p is still the same. Is there a way to replace the elements and print the new values of list ?