Tic-Tac-Toe.py
Tags: python
Published: 2023-08-20 04:34:41

This program was written in Python 3, and was heavily influenced by several tutorials.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/adf880mnc7051hvsxmqz.png)

The program starts by importing the Random module, and assigning values to our global variables. A player's turn is randomly selected, and assigned to the _player_turn_ variable. The _winner_ variable is used to determine who the winner is, if there is one. The _game_running_ variable will terminate the program once it's set to false, otherwise the program will continue to run. 

```
import random


players = ['X', 'O']
player_turn = random.choice(players)
winner = None
game_running = True
```

**Generates, and then displays the game board to the user(s).**

```
board = [
    ' ', ' ', ' ',
    ' ', ' ', ' ',
    ' ', ' ', ' ',
]



def generate_game_board(game_board):
    print(game_board[0] + '|' + game_board[1] + '|' + game_board[2])
    print('-+-+-')
    print(game_board[3] + '|' + game_board[4] + '|' + game_board[5])
    print('-+-+-')
    print(game_board[6] + '|' + game_board[7] + '|' + game_board[8])
```

**Takes the input of the current player, and places their move on their desired section of the game board.** 

```
def player_input(game_board):
    _input = int(input("Choose where you want to go by entering a number 1 - 9: "))
    if _input >=1 and _input <=9 and game_board[_input - 1] == ' ':
        game_board[_input - 1] = player_turn
    else:
        print("You can't go there!")



def change_player():
    global player_turn
    if player_turn == players[0]:
        player_turn = players[1]
    else:
        player_turn = players[0]
```

**Checks to see if there is a winner, or if the game was a draw. If either of those conditions are met the game will terminate.**

```
def vertical_winner(game_board):
    global winner
    if game_board[0] == game_board[3] == game_board[6] and game_board[3] != ' ':
        winner = game_board[0]
        return True
    elif game_board[1] == game_board[4] == game_board[7] and game_board[4] != ' ':
        winner = game_board[1]
        return True
    elif game_board[2] == game_board[5] == game_board[8] and game_board[5] != ' ':
        winner = game_board[2]
        return True



def horizontal_winner(game_board):
    global winner
    if game_board[0] == game_board[1] == game_board[2] and game_board[1] != ' ':
        winner = game_board[0]
        return True
    elif game_board[3] == game_board[4] == game_board[5] and game_board[4] != ' ':
        winner = game_board[0]
        return True
    elif game_board[6] == game_board[7] == game_board[8] and game_board[7] != ' ':
        winner = game_board[0]
        return True



def diagonal_winner(game_board):
    global winner
    if game_board[0] == game_board[4] == game_board[8] and game_board[4] != ' ':
        winner = game_board[0]
        return True
    elif game_board[2] == game_board[4] == game_board[6] and game_board[4] != ' ':
        winner = game_board[2]
        return True



def check_for_winner(game_board):
    global game_running
    if vertical_winner(game_board) or horizontal_winner(game_board) or diagonal_winner(game_board):
        print("Player " + winner + " is the winner.")
        game_running = False



def check_for_tie(game_board):
    global game_running
    if ' ' not in game_board:
        generate_game_board(game_board)
        print("It's a tie!")
        game_running = False
```

**This is the main function of the program.**

```
while game_running == True:
    generate_game_board(board)
    player_input(board)
    check_for_winner(board)
    check_for_tie(board)
    change_player()
```


[Git Link](https://github.com/lataw/Tic-Tac-Toe/blob/430e1af626e272eb6707200c01c82a9265ebf3db/tictactoe.py)