Skip to content

Instantly share code, notes, and snippets.

@priyanshnama
Last active December 25, 2019 11:35
Show Gist options
  • Save priyanshnama/0514b29008d70bf5ca78a72c22adcc7f to your computer and use it in GitHub Desktop.
Save priyanshnama/0514b29008d70bf5ca78a72c22adcc7f to your computer and use it in GitHub Desktop.
Tic-Tac-Toe (O-X) Game entirely made in python. Just run the Python file in terminal and enjoy!!
from random import randint
from time import sleep
def board_reset():
board_reseted = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
return board_reseted
def game_exit():
print("Thanks for Playing")
exit()
def display_board(board_display):
print(f' {board_display[7]} | {board_display[8]} | {board_display[9]}')
print("-----------")
print(f' {board_display[4]} | {board_display[5]} | {board_display[6]}')
print("-----------")
print(f' {board_display[1]} | {board_display[2]} | {board_display[3]}')
pass
def player_char(board_char, first_char, second_char):
count = 0
for item in board_char:
if item == " ":
count += 1
if count % 2 == 0:
return first_char
else:
return second_char
def player_input(board_input, position, first_input, second_input):
c = player_char(board_input, first_input, second_input)
last_chance = c
if 10 > position > 0 and board_input[position] == " ":
board_input[position] = c
print('\n' * 100)
display_board(board_input)
pass
def win_match(board_win):
x = 'X'
o = 'O'
winner_match = ''
if (board_win[7] == x and board_win[8] == x and board_win[9] == x) or (
board_win[4] == x and board_win[5] == x and board_win[6] == x) or (
board_win[1] == x and board_win[2] == x and board_win[3] == x) or (
board_win[7] == x and board_win[4] == x and board_win[1] == x) or (
board_win[8] == x and board_win[5] == x and board_win[2] == x) or (
board_win[9] == x and board_win[6] == x and board_win[3] == x) or (
board_win[7] == x and board_win[5] == x and board_win[3] == x) or (
board_win[9] == x and board_win[5] == x and board_win[1] == x):
winner_match = 'X'
elif (board_win[7] == o and board_win[8] == o and board_win[9] == o) or (
board_win[4] == o and board_win[5] == o and board_win[6] == o) or (
board_win[1] == o and board_win[2] == o and board_win[3] == o) or (
board_win[7] == o and board_win[4] == o and board_win[1] == o) or (
board_win[8] == o and board_win[5] == o and board_win[2] == o) or (
board_win[9] == o and board_win[6] == o and board_win[3] == o) or (
board_win[7] == o and board_win[5] == o and board_win[3] == o) or (
board_win[9] == o and board_win[5] == o and board_win[1] == o):
winner_match = 'O'
return winner_match
def board_full(board_f):
for place in range(len(board_f)):
if board_f[place] == ' ' and place > 0:
return False
return True
def get_position():
got = False
while not got:
try:
position = int(input("Choose your next position (1 to 9) -- "))
got = True
except ValueError:
print("Something Went wrong")
return position
def setup_game():
print("Welcome to Tic-Tac-Toe")
board_setup = board_reset()
print("Player1: Do you want to be X or O?")
player1_setup = input()
player1_setup = player1_setup.upper()
if player1_setup == 'X':
player2_setup = 'O'
else:
player2_setup = 'X'
return board_setup, player1_setup, player2_setup
def play_game(board_game, first_game, second_game):
board_game = board_reset()
win = win_match(board_game)
while win == '' and not board_full(board_game):
position = get_position()
player_input(board_game, position, first_game, second_game)
win = win_match(board_game)
if win == '':
return "It's a Tie"
elif win == 'X':
return "X has Won"
elif win == 'O':
return "O has Won"
def play_again():
print("Do you want to start the game ? Y or N:")
answer = input()
if answer == 'Y' or answer == 'y':
return True
else:
return False
last_chance = ''
board, player1, player2 = setup_game()
while play_again():
first = player1
second = player2
if randint(1, 2) == 1:
first = player2
second = player1
print(f'{first} will play first')
result = play_game(board, first, second)
print(f"Result - {result}")
print("Game over Thanks for Playing")
sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment