Advertisement
Guest User

Untitled

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