Advertisement
Guest User

Untitled

a guest
Jul 15th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.96 KB | None | 0 0
  1. import random
  2. board = ['0','1','2','3','4','5','6','7','8']
  3. a = None
  4. player_select = None
  5. comp_select = None
  6.  
  7. def AI_move_part_1():
  8.     global board
  9.     global a
  10.     if board[0]==board[3] and board[6] == '6':
  11.         board[6]=comp_select
  12.         a = 1
  13.         check_for_win()
  14.         player_moves()
  15.     if board[0]==board[6] and board[3] == '3':
  16.         board[3]=comp_select
  17.         a = 1
  18.         check_for_win()
  19.         player_moves()
  20.     if board[3]==board[6] and board[0] == '0':
  21.         board[0]=comp_select
  22.         a = 1
  23.         check_for_win()
  24.         player_moves()
  25.     if board[1]==board[4] and board[7] == '7':
  26.         board[7]=comp_select
  27.         a = 1
  28.         check_for_win()
  29.         player_moves()
  30.     if board[1]==board[7] and board[4] == '4':
  31.         board[4]=comp_select
  32.         a = 1
  33.         check_for_win()
  34.         player_moves()
  35.     if board[4]==board[7] and board[1] == '1':
  36.         board[1]=comp_select
  37.         a = 1
  38.         check_for_win()
  39.         player_moves()
  40.     if board[2]==board[5] and board[8] == '8':
  41.         board[8]=comp_select
  42.         a = 1
  43.         check_for_win()
  44.         player_moves()
  45.     if board[2]==board[8] and board[5] == '5':
  46.         board[5]=comp_select
  47.         a = 1
  48.         check_for_win()
  49.         player_moves()
  50.     if board[5]==board[8] and board[2] == '2':
  51.         board[2]=comp_select
  52.         a = 1
  53.         check_for_win()
  54.         player_moves()
  55.     if board[0]==board[1] and board[2] == '2':
  56.         board[2]=comp_select
  57.         a = 1
  58.         check_for_win()
  59.         player_moves()
  60.     if board[0]==board[2] and board[1] == '1':
  61.         board[1]=comp_select
  62.         a = 1
  63.         check_for_win()
  64.         player_moves()
  65.     if board[1]==board[2] and board[0] =='0':
  66.         board[0]=comp_select
  67.         a = 1
  68.         check_for_win()
  69.         player_moves()
  70.     if board[3]==board[4] and board[5] =='5':
  71.         board[5]=comp_select
  72.         a = 1
  73.         check_for_win()
  74.         player_moves()
  75.     if board[3]==board[5] and board[4] == '4':
  76.         board[4]=comp_select
  77.         a = 1
  78.         check_for_win()
  79.         player_moves()
  80.     if board[4]==board[5] and board[3] == '3':
  81.         board[3]=comp_select
  82.         a = 1
  83.         check_for_win()
  84.         player_moves()
  85.     if board[6]==board[7] and board[8] =='8':
  86.         board[8]=comp_select
  87.         a = 1
  88.         check_for_win()
  89.         player_moves()
  90.     if board[6]==board[8] and board[7] =='7':
  91.         board[7]=comp_select
  92.         a = 1
  93.         check_for_win()
  94.         player_moves()
  95.     if board[7]==board[8] and board[6] =='6':
  96.         board[6]=comp_select
  97.         a = 1
  98.         check_for_win()
  99.         player_moves()
  100.     if board[0]==board[4] and board[8] =='8':
  101.         board[8]=comp_select
  102.         a = 1
  103.         check_for_win()
  104.         player_moves()
  105.     if board[0]==board[8] and board[4] =='4':
  106.         board[4]=comp_select
  107.         a = 1
  108.         check_for_win()
  109.         player_moves()
  110.     if board[4]==board[8] and board[0] =='0':
  111.         board[0]=comp_select
  112.         a = 1
  113.         check_for_win()
  114.         player_moves()
  115.     if board[2]==board[4] and board[6] =='6':
  116.         board[6]=comp_select
  117.         a = 1
  118.         check_for_win()
  119.         player_moves()
  120.     if board[2]==board[6] and board[4] =='4':
  121.         board[4]=comp_select
  122.         a = 1
  123.         check_for_win()
  124.         player_moves()
  125.     if board[4]==board[6] and board[2] =='2':
  126.         board[2]=comp_select
  127.         a = 1
  128.         check_for_win()
  129.         player_moves()
  130.     if board[1]==board[3] == player_select and board[0] =='0':
  131.         board[0]=comp_select
  132.         a = 1
  133.         check_for_win()
  134.         player_moves()
  135.     if board[1]==board[5] == player_select and board[2] =='2':
  136.         board[2]=comp_select
  137.         a = 1
  138.         check_for_win()
  139.         player_moves()
  140.     if board[3]==board[7] == player_select and board[6] =='6':
  141.         board[6]=comp_select
  142.         a = 1
  143.         check_for_win()
  144.         player_moves()
  145.     if board[5]==board[7]==player_select and board[8] =='8':
  146.             board[8]=comp_select
  147.             a = 1
  148.             check_for_win()
  149.             player_moves()
  150.     if board[4] == '4':
  151.         board[4] = comp_select
  152.         a = 1
  153.         check_for_win()
  154.         player_moves()
  155.  
  156. def AI_move_part_2():
  157.     global a
  158.     global board
  159.     if board[0] == '0' or board[2] == '2' or board[6] == '6' or board[8] == '8':
  160.         n = random.choice([0,2,6,8])
  161.         if board[n] == str(n):
  162.             board[n] = comp_select
  163.             a = 1
  164.             check_for_win()
  165.             player_moves()
  166.         else:
  167.             AI_move_part_2()
  168.     if board[1] == '1' or board[5] == '5' or board[7] == '7' or board[3] == '3':
  169.         m = random.choice([1,5,7,3])
  170.         if board[m] == str(m):
  171.             board[m] = comp_select
  172.             a = 1
  173.             check_for_win()
  174.             player_moves()
  175.         else:
  176.             AI_move_part_2()
  177.    
  178.    
  179. def play_again():
  180.     global board
  181.     board = ['0','1','2','3','4','5','6','7','8']
  182.     x = input('Do you want to play again? (y/n) \n' )
  183.     if x in ['y','Y']:
  184.         print('\n'*100)
  185.         player_assign()
  186.     else:
  187.         exit()
  188.        
  189. def check_for_win():
  190.     global board
  191.     global player_select
  192.     global comp_select
  193.     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:
  194.         print("Player wins!\n")
  195.         play_again()
  196.     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:
  197.         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')
  198.         print("The Computer wins!\n")
  199.         play_again()
  200.                
  201. def player_moves():
  202.     global board
  203.     global comp_select
  204.     global player_select
  205.     global a
  206.     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:
  207.         print("Cat's Game!")
  208.         play_again()
  209.     while a == 1:
  210.         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')
  211.         b = input("Player, where would you like to place? ")
  212.         if b not in ['0','1','2','3','4','5','6','7','8'] or board[int(b)] in ['X','O']:
  213.             print('\nInvalid Move!\n')
  214.             player_moves()
  215.         board[int(b)] = player_select
  216.         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')
  217.         a = 0
  218.         check_for_win()
  219.         player_moves()
  220.     while a == 0:
  221.         AI_move_part_1()
  222.         AI_move_part_2()
  223.        
  224.        
  225. def player_assign():
  226.     global player_select
  227.     global comp_select
  228.     global a
  229.     player_select = input("Let's play Tic Tac Toe!\n \nPlayer, do you want to be X's or O's? \n")
  230.     print("These are the locations you can play.\n\n 0 |  1  | 2 \n-------------\n 3 |  4  | 5 \n-------------\n 6 |  7  | 8 \n")
  231.     if player_select in ['x','X']:
  232.         player_select = player_select.upper()
  233.         comp_select = 'O'
  234.         a = 1
  235.         player_moves()
  236.     elif player_select in ['o','O']:
  237.         player_select = player_select.upper()
  238.         comp_select = 'X'
  239.         a = 0
  240.         player_moves()
  241.     else:
  242.         print('\nInvalid Entry!\n')
  243.         player_assign()
  244.            
  245. player_assign()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement