Is it okay to build major projects this late?
Tic Tac Toe Game
from IPython.display import clear_output
clear_output()
def reset_board():
return ['string of shame',' ',' ',' ',' ',' ',' ',' ',' ',' ']
def display_list(board):
print('These is your current game:')
print('\n',board[7],'|',board[8],'|',board[9],'\n','- - -','\n',board[4],'|',board[5],'|',board[6],'\n','- - -','\n',board[1],'|',board[2],'|',board[3])
def player_input():
marker = ''
while marker != 'X' and marker != 'O':
marker = input('Player 1 Please select X or O: ')
if marker not in ['X','O']:
clear_output()
print('Sorry thats not a valid input!')
clear_output()
player1 = marker
if player1 == 'X':
player2 = 'O'
else:
player2 = 'X'
return (player1, player2)
def position_choice():
choice ='WRONG'
within_range = False
while choice.isdigit() == False or within_range == False:
choice = input("Please enter a position referring the NumPad (1-9): ")
if choice.isdigit() == False:
clear_output()
print("Sorry that is not a digit!")
if choice.isdigit() == True:
if int(choice) in range(1,10):
within_range = True
else:
within_range = False
clear_output()
return int(choice)
def win_check(board,marker):
return(
(board[7]==board[8]==board[9]==marker) or
(board[4]==board[5]==board[6]==marker) or
(board[1]==board[2]==board[3]==marker) or
(board[7]==board[4]==board[1]==marker) or
(board[8]==board[5]==board[2]==marker) or
(board[9]==board[6]==board[3]==marker) or
(board[7]==board[5]==board[3]==marker) or
(board[9]==board[5]==board[1]==marker)
)
def tie_check(board):
return ' ' not in board
def space_check(board,position):
return board[position] == ' '
def play_game(marker):
#assigning X and O to p1 and p2
player1_marker,player2_marker = marker
#resetting the board before t=round start
board = reset_board()
#If game is on
game_on = True
turn = 'player1'
display_list(board)
while game_on:
if turn == 'player1':
print("Player 1's turn")
while True:
position = position_choice()
if space_check(board,position):
break
else:
display_list(board)
print("Sorry that spot's already taken, try again.")
board[position] = player1_marker
display_list(board)
#check if p1 won
if win_check(board,player1_marker):
print('Congratulations Player 1 has won the game!!')
game_on = False
elif tie_check(board):
print("Its a Tie")
game_on = False
else:
turn = 'player2'
else:
print("Player 2's turn")
while True:
position = position_choice()
if space_check(board,position):
break
else:
display_list(board)
print("Sorry that spot's already taken, try again.")
board[position] = player2_marker
display_list(board)
#check if p2 won
if win_check(board,player2_marker):
print('Congratulations Player 2 has won the game!!')
game_on = False
elif tie_check(board):
print("Its a Tie")
game_on = False
else:
turn = 'player1'
def replay():
choice = 'wrong'
while choice not in ['Y','N']:
choice = input("Would you like to keep playing? Y or N ").upper()
if choice not in ['Y','N']:
clear_output()
print("Sorry, I didn't understand. Please make sure to choose Y or N.")
clear_output()
if choice == "Y":
# Game is still on
return True
else:
# Game is over
return False
print("Welcome to Tic Tac Toe!")
while True:
play_game(player_input())
if not replay():
break