0

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 ?

hunteraja
  • 11
  • 1
  • 5
  • `e[e.index(element)]` returns the _first_ occurrence. Use `for index,element in enumerate(e)` now you have your index directly – Jean-François Fabre Apr 20 '18 at 17:38
  • since board is a string it becomes immutable, meaning that once it is assigned to a variable, it sticks. try `print(e)` or try updating the `board` variable with a helper function that `board = return_new_board_string(e)` – jmunsch Apr 20 '18 at 17:46
  • I've tried using enumerate(), but the output is still the same. – hunteraja Apr 20 '18 at 17:48
  • @jmunsch i forgot that strings are immutable.. i guess i should've paid more attention to that. Atleast now i know where i went wrong. Thanks ! – hunteraja Apr 20 '18 at 17:51
  • the way i found this out was to trace the code like : https://stackoverflow.com/questions/4929251/can-you-step-through-python-code-to-help-debug-issues i didn't know by just looking at it. haha. – jmunsch Apr 20 '18 at 17:54

2 Answers2

1

Board variable is defined only once. It's a string, it's immutable and it won't magically update when you change elements of the e list. As Danilo suggested in his answer you can create a function and invoke it every time you want your board to be printed:

from beautifultable import BeautifulTable

def draw_board():
    board = BeautifulTable()
    board.append_row(e[:3])
    board.append_row(e[3:6])
    board.append_row(e[6:])
    print(board)

You can use BeautifulTable to format your board. However, you need to install it first:

pip install beautifultable

Example:

>>> e[0] = 'X'
>>> draw_board()
+----------+---------+-----------+
|    X     | top-mid | top-right |
+----------+---------+-----------+
| mid-left | mid-mid | mid-right |
+----------+---------+-----------+
| btm-left | btm-mid | btm-right |
+----------+---------+-----------+
radzak
  • 2,986
  • 1
  • 18
  • 27
-1

Ok this is a bunch of code, please for future use check How to ask a good question of stack overflow policies. You could get better answer if you posted working code with list get and set methods

First you are changing the e list not board variable. The board isn't dependable from e list. You've used 1 variable to create another variable, now that second variable is created any change to 1 variable doesn't influence second one.

Soo, just make a board a function instead that takes global parameter e and it works.

def board():
    global e
    print( e[0]+ ' | '+ e[1]+ ' | '+ e[2]+'\n'+\
    '--------|------------|--------'+'\n'+\
    e[3]+ ' | '+ e[4]+ ' | '+ e[5]+'\n'+\
    '--------|------------|--------'+'\n'+\
    e[6]+ ' | '+ e[7]+ ' | '+ e[8] )

After that you add brackets () to your every variable since it is now a function and voila.


EDIT:

Also you don't need to for loop every time you want to change a variable, if variable has specific value ( aka no repetition inside list ) you can use next way:

if input_string in e: e[ e.index(input_string) ] = "\tX\t" 
else: print("this space is taken, take a hike!!") # or something  
Danilo
  • 1,017
  • 13
  • 32