Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- board = ['0','1','2','3','4','5','6','7','8']
- a = None
- player_select = None
- comp_select = None
- def AI_move_part_1():
- global board
- global a
- if board[0]==board[3] and board[6] == '6':
- board[6]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[0]==board[6] and board[3] == '3':
- board[3]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[3]==board[6] and board[0] == '0':
- board[0]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[1]==board[4] and board[7] == '7':
- board[7]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[1]==board[7] and board[4] == '4':
- board[4]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[4]==board[7] and board[1] == '1':
- board[1]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[2]==board[5] and board[8] == '8':
- board[8]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[2]==board[8] and board[5] == '5':
- board[5]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[5]==board[8] and board[2] == '2':
- board[2]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[0]==board[1] and board[2] == '2':
- board[2]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[0]==board[2] and board[1] == '1':
- board[1]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[1]==board[2] and board[0] =='0':
- board[0]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[3]==board[4] and board[5] =='5':
- board[5]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[3]==board[5] and board[4] == '4':
- board[4]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[4]==board[5] and board[3] == '3':
- board[3]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[6]==board[7] and board[8] =='8':
- board[8]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[6]==board[8] and board[7] =='7':
- board[7]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[7]==board[8] and board[6] =='6':
- board[6]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[0]==board[4] and board[8] =='8':
- board[8]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[0]==board[8] and board[4] =='4':
- board[4]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[4]==board[8] and board[0] =='0':
- board[0]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[2]==board[4] and board[6] =='6':
- board[6]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[2]==board[6] and board[4] =='4':
- board[4]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[4]==board[6] and board[2] =='2':
- board[2]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[1]==board[3] == player_select and board[0] =='0':
- board[0]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[1]==board[5] == player_select and board[2] =='2':
- board[2]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[3]==board[7] == player_select and board[6] =='6':
- board[6]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[5]==board[7]==player_select and board[8] =='8':
- board[8]=comp_select
- a = 1
- check_for_win()
- player_moves()
- if board[4] == '4':
- board[4] = comp_select
- a = 1
- check_for_win()
- player_moves()
- def AI_move_part_2():
- global a
- global board
- if board[0] == '0' or board[2] == '2' or board[6] == '6' or board[8] == '8':
- n = random.choice([0,2,6,8])
- if board[n] == str(n):
- board[n] = comp_select
- a = 1
- check_for_win()
- player_moves()
- else:
- AI_move_part_2()
- if board[1] == '1' or board[5] == '5' or board[7] == '7' or board[3] == '3':
- m = random.choice([1,5,7,3])
- if board[m] == str(m):
- board[m] = comp_select
- a = 1
- check_for_win()
- player_moves()
- else:
- AI_move_part_2()
- def play_again():
- global board
- board = ['0','1','2','3','4','5','6','7','8']
- x = input('Do you want to play again? (y/n) \n' )
- if x in ['y','Y']:
- print('\n'*100)
- player_assign()
- else:
- exit()
- def check_for_win():
- global board
- global player_select
- global comp_select
- if board[0] == board[1] == board[2] == player_select or board[3] == board[4] == board[5] == player_select or board[6] == board[7] == board[8] == player_select or board[0] == board[4] == board[8] == player_select or board[2] == board[4] == board[6] == player_select or board[0] == board[3] == board[6] == player_select or board[1] == board[4] == board[7] == player_select or board[2] == board[5] == board[8] == player_select:
- print("Player wins!\n")
- play_again()
- elif board[0] == board[1] == board[2] == comp_select or board[3] == board[4] == board[5] == comp_select or board[6] == board[7] == board[8] == comp_select or board[0] == board[4] == board[8] == comp_select or board[2] == board[4] == board[6] == comp_select or board[0] == board[3] == board[6] == comp_select or board[1] == board[4] == board[7] == comp_select or board[2] == board[5] == board[8] == comp_select:
- print(f'\n {board[0]} | {board[1]} | {board[2]} \n-------------\n {board[3]} | {board[4]} | {board[5]} \n-------------\n {board[6]} | {board[7]} | {board[8]} \n')
- print("The Computer wins!\n")
- play_again()
- def player_moves():
- global board
- global comp_select
- global player_select
- global a
- if '1' not in board and '2' not in board and '3' not in board and '4' not in board and '5' not in board and '6' not in board and '7' not in board and '8' not in board and '0' not in board:
- print("Cat's Game!")
- play_again()
- while a == 1:
- print(f'\n {board[0]} | {board[1]} | {board[2]} \n-------------\n {board[3]} | {board[4]} | {board[5]} \n-------------\n {board[6]} | {board[7]} | {board[8]} \n')
- b = input("Player, where would you like to place? ")
- if b not in ['0','1','2','3','4','5','6','7','8'] or board[int(b)] in ['X','O']:
- print('\nInvalid Move!\n')
- player_moves()
- board[int(b)] = player_select
- print(f'\n {board[0]} | {board[1]} | {board[2]} \n-------------\n {board[3]} | {board[4]} | {board[5]} \n-------------\n {board[6]} | {board[7]} | {board[8]} \n')
- a = 0
- check_for_win()
- player_moves()
- while a == 0:
- AI_move_part_1()
- AI_move_part_2()
- def player_assign():
- global player_select
- global comp_select
- global a
- player_select = input("Let's play Tic Tac Toe!\n \nPlayer, do you want to be X's or O's? \n")
- print("These are the locations you can play.\n\n 0 | 1 | 2 \n-------------\n 3 | 4 | 5 \n-------------\n 6 | 7 | 8 \n")
- if player_select in ['x','X']:
- player_select = player_select.upper()
- comp_select = 'O'
- a = 1
- player_moves()
- elif player_select in ['o','O']:
- player_select = player_select.upper()
- comp_select = 'X'
- a = 0
- player_moves()
- else:
- print('\nInvalid Entry!\n')
- player_assign()
- player_assign()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement