How can I make my input function maintain its current player if a player chooses X or O in a spot where one already exists?

Viewed 32

Very new beginner learning python. Have been trying the classic challenge of writing a tic tac toe game (with help, admittedly) My game seems to run fine, but one thing I cant figure out is how to keep the game on its current player if they choose a spot already selected (the player loses its turn completely). Any ideas as to how I could go about it? My full program in attached below. All help and criticism is much appreciated.

#show a blank board
board = ["-","-","-",
         "-","-","-",
         "-","-","-"]
current_player = "X"
winner = None
game_running = True
def printBoard(board):
    print(board[0] + "|" + board[1] + "|" + board[2])
    print("------")
    print(board[3] + "|" + board[4] + "|" + board[5])
    print("------")
    print(board[6] + "|" + board[7] + "|" + board[8])
#take the player input
def player_input(board):
    answer = int(input("Enter a value 1-9 to place your marker. It's currently {} turn: ".format(current_player)))
    validity = True
    while validity:
        if answer <= 9 and answer >= 1 and board[answer-1] == "-":
            board[answer-1] = current_player
            break
        elif board[answer-1] == "X" or board[answer-1] == "O":
            print("Sorry, that spot seems to be taken. Please pick another")
            ****#not sure what to write here****
            break
        else:
            print("You just broke the game. Congratulations")
            break
#check for win or tie
def check_horizontal(board):
    global winner
    if board[0] == board[1] == board[2] and board[1] != "-":
        winner = board[0]
        return True
    elif board[3] == board[4] == board[5] and board[4] != "-":
        winner = board[3]
        return True
    elif board[6] == board[7] == board[8] and board[7] != "-":
        winner = board[6]
        return True
def check_vertical(board):
    global winner
    if board[0] == board[3] == board[6] and board[0] != "-":
        winner = board[0]
        return True
    elif board[1] == board[4] == board[7] and board[1] != "-":
        winner = board[1]
        return True
    elif board[2] == board[5] == board[8] and board[2] != "-":
        winner = board[2]
        return True
def check_diagonal(board):
    global winner
    if board[0] == board[4] == board[8] and board[0] != "-":
        winner = board[0]
        return True
    elif board[6] == board[4] == board[2] and board[4] != "-":
        winner = board[4]
        return True
def check_tie(board):
    global game_running
    if "-" not in board:
        printBoard(board)
        print("After a long battle, the game is a draw")
        game_running = False
def check_win(board):
    global game_running
    if check_diagonal(board) or check_vertical(board) or check_horizontal(board):
        print(f"The winner is {winner}")
        game_running = False
#switch the player
def switch_player():
    global current_player
    global answer
    if current_player == "X":
        current_player  = 'O'
    else:
        current_player = "X"
#Game logic
while game_running:
    printBoard(board)
    player_input(board)
    check_win(board)
    check_tie(board)
    switch_player()
1 Answers

First things first, very nice code for a beginner. Everything is nicely organized and in functions, and it works well too.

This is how I would solve your problem:

def player_input(board):
    answer = int(input("Enter a value 1-9 to place your marker. It's currently {} turn: ".format(current_player)))
    validity = True
    while validity:
        if answer <= 9 and answer >= 1 and board[answer-1] == "-":
            board[answer-1] = current_player
            break
        elif board[answer-1] == "X" or board[answer-1] == "O":
            print("Sorry, that spot seems to be taken. Please pick another")
            player_input(board)
        else:
            print("You just broke the game. Congratulations")
            break

For the part of code that you said you needed help with, I just ran the function again.

Related