I am working on Jupyter Notebook and I am new to Python. I have created this tic tac toe game.
from IPython.display import clear_output
import time
def display_board(board):
print' | | '
print'',board[1],'|',board[2],'|',board[3]
print'___|___|___'
print' | | '
print'',board[4],'|',board[5],'|',board[6]
print'___|___|___'
print' | | '
print'',board[7],'|',board[8],'|',board[9]
print' | |'
from random import randint
def player_input(board):
global turn_r
global first_time
if(first_time==0):
turn_r = randint(0,1)
if(turn_r == 0):
print"Player 1 's Move\n"
if(turn_r == 1):
print"Player 2's Move\n"
first_time = 1
if(turn_r == 0):
p1marker=0
while not int(p1marker) in board:
p1marker = input("Where Should I Place Your X : ")
board[p1marker] = 'X'
turn_r=1
else:
p2marker=0
while not int(p2marker) in board:
p2marker = input("Where Should I Place Your O : ")
board[p2marker] = 'O'
turn_r=0
def result_check(board):
if((board[7] == 'X' and board[8] == 'X' and board[9] == 'X') or # across the top
(board[4] == 'X' and board[5] == 'X' and board[6] == 'X') or # across the middle
(board[1] == 'X' and board[2] == 'X' and board[3] == 'X') or # across the bottom
(board[7] == 'X' and board[4] == 'X' and board[1] == 'X') or # down the middle
(board[8] == 'X' and board[5] == 'X' and board[2] == 'X') or # down the middle
(board[9] == 'X' and board[6] == 'X' and board[3] == 'X') or # down the right side
(board[7] == 'X' and board[5] == 'X' and board[3] == 'X') or # diagonal
(board[9] == 'X' and board[5] == 'X' and board[1] == 'X')):
return 'Player 1'
elif((board[7] == 'O' and board[8] == 'O' and board[9] == 'O') or # across the top
(board[4] == 'O' and board[5] == 'O' and board[6] == 'O') or # across the middle
(board[1] == 'O' and board[2] == 'O' and board[3] == 'O') or # across the bottom
(board[7] == 'O' and board[4] == 'O' and board[1] == 'O') or # down the middle
(board[8] == 'O' and board[5] == 'O' and board[2] == 'O') or # down the middle
(board[9] == 'O' and board[6] == 'O' and board[3] == 'O') or # down the right side
(board[7] == 'O' and board[5] == 'O' and board[3] == 'O') or # diagonal
(board[9] == 'O' and board[5] == 'O' and board[1] == 'O')) :
return 'Player 2'
elif((board[1] == 'X' or board[1] == 'O') and
(board[2] == 'X' or board[2] == 'O') and
(board[3] == 'X' or board[3] == 'O') and
(board[4] == 'X' or board[4] == 'O') and
(board[5] == 'X' or board[5] == 'O') and
(board[6] == 'X' or board[6] == 'O') and
(board[7] == 'X' or board[7] == 'O') and
(board[8] == 'X' or board[8] == 'O') and
(board[9] == 'X' or board[9] == 'O')):
return 'Draw'
else:
return 'None'
print "Welcome to Tic Tac Toe"
while True:
board = [0,1,2,3,4,5,6,7,8,9]
global turn_r
global first_time
turn_r = 0
first_time = 0
clear_output()
print'Player 1 will be "X"\nPlayer 2 will be "O"\nBe Ready ! ;)'
time.sleep(3)
game_on = True
while game_on:
clear_output()
display_board(board)
player_input(board)
z = result_check(board)
if(z == 'Player 1'):
clear_output()
display_board(board)
print 'Yipee ! Player 1 Has Won ! ! ! :) '
game_on = False
if(z == 'Player 2'):
clear_output()
display_board(board)
print 'Yipee ! Player 2 Has Won ! ! ! :) '
game_on = False
if(z == 'Draw'):
clear_output()
display_board(board)
print 'Wow ! It is a Draw ! ! ! :|'
game_on = False
if(z == 'None'):
continue
quit = raw_input('Do You Want to Continue (y/n) :')
if(quit == 'n'):
break
I want help to make it better. Any code optimization with explanation is welcome.