How to copy, print list and store values in function each time the function is called (Tic tac toe game)

Viewed 41

I am a little confused on how parameters work and calling functions with return statements. I am trying to create a new graph from the old one so that if I play again it will start over. I have tried copying the list in the run function and printing it out but it seems as though the new values are being placed in the old list. I also believe that my check_win function could be simplified greatly although I am not sure how to do so.

graph = [[' ', '|', ' ', '|', ' '],
     ['-','+', '-', '+', '-'],
     [' ', '|', ' ', '|', ' '],
     ['-', '+', '-', '+', '-'],
     [' ', '|', ' ', '|', ' ']]



def draw_graph(new_graph):
    for row in new_graph:
        for col in row:
            print(col, end = '')
        print() 


def play_game():
    again = 'yes'
    while again == 'yes':
        new_graph = graph
        draw_graph(new_graph)
        won = False
        current_player = 'player1'
        while not won:
            pos = input("Which spot do you want to place your piece (1-9)? ")
            if current_player == 'player1':
                symbol = 'X'
            elif current_player == 'player2':
                symbol = 'O'
            place_spot(pos, symbol, new_graph)
            draw_graph(new_graph)
            won = check_win(new_graph)
            current_player = flip_player(current_player)
        print("Thank you for playing.")
        again = input("Do you want to play again? (yes/no) ")


def place_spot(pos, symbol, new_graph): 
    answer = '' 
    while answer != 'good':     
        if pos == '1':
            new_graph[0][0] = symbol
            break
        if pos == '2':
            new_graph[0][2] = symbol
            break
        if pos == '3':
            new_graph[0][4] = symbol
            break
        if pos == '4':
            new_graph[2][0] = symbol
            break
        if pos == '5':
            new_graph[2][2] = symbol
            break
        if pos == '6':
            new_graph[2][4] = symbol
            break
        if pos == '7':
            new_graph[4][0] = symbol
            break
        if pos == '8':
            new_graph[4][2] = symbol
            break
        if pos == '9':
            new_graph[4][4] = symbol
            break
        else:
            print("Please enter a number (1-9).")
        
      
def check_win(new_graph):
    #top row win
    if new_graph[0][0] == 'X' and new_graph[0][2] == 'X' and new_graph[0][4] == 'X':
        print(f"Player 1 wins!")
        won = True
        return won
    #middle row win
    elif new_graph[2][0] == 'X' and new_graph[2][2] == 'X' and new_graph[2][4] == 'X':
        print(f"Player 1 wins!")
        won = True
        return won
    #bottom row win
    elif new_graph[4][0] == 'X' and new_graph[4][2] == 'X' and new_graph[4][4] == 'X':
        print(f"Player 1 wins!")
        won = True
        return won
    #left diagonal win
    elif new_graph[0][0] == 'X' and new_graph[2][2] == 'X' and new_graph[4][4] == 'X':
        print(f"Player 1 wins!")
        won = True
        return won
    #right diagonal win
    elif new_graph[0][4] == 'X' and new_graph[2][2] == 'X' and new_graph[4][0] == 'X':
        print(f"Player 1 wins!")
        won = True
        return won

    #Player 2
    #top row win
    elif new_graph[0][0] == 'O' and new_graph[0][2] == 'O' and new_graph[0][4] == 'O':
        print(f"Player 2 wins!")
        won = True
        return won
    #middle row win
    elif new_graph[2][0] == 'O' and new_graph[2][2] == 'O' and new_graph[2][4] == 'O':
        print(f"Player 2 wins!")
        won = True
        return won
    #bottom row win
    elif new_graph[4][0] == 'O' and new_graph[4][2] == 'O' and new_graph[4][4] == 'O':
        print(f"Player 2 wins!")
        won = True
        return won
    #left diagonal win
    elif new_graph[0][0] == 'O' and new_graph[2][2] == 'O' and new_graph[4][4] == 'O':
        print(f"Player 2 wins!")
        won = True
        return won
    #right diagonal win
    elif new_graph[0][4] == 'O' and new_graph[2][2] == 'O' and new_graph[4][0] == 'O':
        print(f"Player 2 wins!")
        won = True
        return won

def flip_player(player):
    if player == 'player1':
        current_player = 'player2'
        return current_player
    elif player == 'player2':
        current_player = 'player1'
        return current_player

play_game()
1 Answers

You can recursively copy a list using copy.deepcopy:

>>> x = [[1], [2], [3]]
>>> y = copy.copy(x)  # a shallow copy
>>> y[0][0] = 5  # this modifies 'x[0]' as well
>>> x
[[5], [2], [3]]
>>> z = copy.deepcopy(x)
>>> z[0][0] = 9
>>> x
[[5], [2], [3]]
Related