tictactoe using python code example

Example: tic tac toe algorithm python

#algorithim for X player
#python 3.85


import random


def algoX(lisp):
  '''my bacic stupid idiotic dunder-headded ape-brained algorithim'''
  
  for i in range(len(lisp)):
    if lisp[i] == 1:
      lisp[i] = 'X'
    elif lisp[i] == 2:
      lisp[i] = 'O'
  
  def zeros(list):
    out = 0
    for i in range(len(list)):
      if list[i] == 0:
        out += 1
    return out
  
  def count(list, simb):
    out = 0
    for i in range(len(list)):
      if list[i] == simb:
        out += 1
    return out
  
  if count(lisp[0:3], 'X') == 2 and zeros(lisp[0:3]) == 1:
    lisp[0:3] = 'X','X','X'
  
  elif count(lisp[3:6], 'X') == 2 and zeros(lisp[3:6]) == 1:
    lisp[3:6] = 'X','X','X'
  
  elif count(lisp[6:9], 'X') == 2 and zeros(lisp[6:9]) == 1:
    lisp[6:9] = 'X','X','X'
    
  elif count([lisp[0],
              lisp[3],
              lisp[6]],'X') == 2 and zeros([lisp[0],lisp[3],lisp[6]]) == 1:
    for i in range(3):
      lisp[i*3] = 'X'
  
  elif count([lisp[1],
              lisp[4],
              lisp[7]],'X') == 2 and zeros([lisp[1],lisp[4],lisp[7]]) == 1:
    for i in range(3):
      lisp[(i*3)+1] = 'X'
  
  elif count([lisp[2],
              lisp[5],
              lisp[8]],'X') == 2 and zeros([lisp[2],lisp[5],lisp[8]]) == 1:
    for i in range(3):
      lisp[(i*3)+2] = 'X'
      
  elif count([lisp[0],
              lisp[4],
              lisp[8]],'X') == 2 and zeros([lisp[0],lisp[4],lisp[8]]) == 1:
    for i in range(3):
      lisp[(i*4)] = 'X'
  
  elif count([lisp[2],
              lisp[4],
              lisp[6]],'X') == 2 and zeros([lisp[2],lisp[4],lisp[6]]) == 1:
    
    lisp[2], lisp[4], lisp[6] = 'X','X','X'
  
  else:
    '''prevent loss'''
    if count(lisp[0:3], 'O') == 2 and zeros(lisp[0:3]) == 1:
      for i in range(3):
        if lisp[i] == 0:
          lisp[i] = 'X'
    
    elif count(lisp[3:6], 'O') == 2 and zeros(lisp[3:6]) == 1:
      for i in range(3,6):
        if lisp[i] == 0:
          lisp[i] = 'X'
    
    elif count(lisp[6:9], 'O') == 2 and zeros(lisp[6:9]) == 1:
      for i in range(6,9):
        if lisp[i] == 0:
          lisp[i] = 'X'
    
    elif count([lisp[0],
                lisp[3],
                lisp[6]],'O') == 2 and zeros([lisp[0],lisp[3],lisp[6]]) == 1:
      for i in range(3):
        if lisp[i*3] == 0:
          lisp[i*3] = 'X'
          
    elif count([lisp[1],
              lisp[4],
              lisp[7]],'X') == 2 and zeros([lisp[1],lisp[4],lisp[7]]) == 1:
      for i in range(3):
        if lisp[(i*3)+1] == 0:
          lisp[(i*3)+1] = 'X'
    
    elif count([lisp[2],
              lisp[5],
              lisp[8]],'X') == 2 and zeros([lisp[2],lisp[5],lisp[8]]) == 1:
      for i in range(3):
        if lisp[(i*3)+2] == 0:
          lisp[(i*3)+2] = 'X'
    
    elif count([lisp[0],
              lisp[4],
              lisp[8]],'X') == 2 and zeros([lisp[0],lisp[4],lisp[8]]) == 1:
      for i in range(3):
        if lisp[i*4] == 0:
          lisp[(i*4)] = 'X'
    
    elif count([lisp[2],
              lisp[4],
              lisp[6]],'X') == 2 and zeros([lisp[2],lisp[4],lisp[6]]) == 1:
      if lisp[2] == 0:
        lisp[2] = 'X'
        
      elif lisp[4] == 0:
        lisp[4] = 'X'
        
      elif lisp[6] == 0:
        lisp[6] = 'X'
    
    else:
      '''regular options'''
      if lisp[4] == 0:
        lisp[4] = 'X'
      else:
        while True:
          rand = random.randint(0,8)
          if lisp[rand] == 'X' or lisp[rand] == 'O':
            continue
          else:
            lisp[rand] = 'X'
            break
     
    
          
      
    
        
  return lisp