tic tac toe with computer python code code example

Example 1: program for tic tac toe in python

board = ['-', '-', '-',
         '-', '-', '-',
         '-', '-', '-']
gameplay = [1, 0, 1, 0, 1, 0, 1, 0, 1]
def display_board():
    print(board[0] + '|' + board[1] + '|' + board[2])
    print(board[3] + '|' + board[4] + '|' + board[5])
    print(board[6] + '|' + board[7] + '|' + board[8])

def win_check():
    # Row Check
    for col in range(7):
        if board[col] is board[col+1] is board[col+2] == 'X':
            print('You win')
            return True
        if board[col] is board[col+1] is board[col+2] == 'O':
            print('You win')
            return True

    # Column Check
    for row in range(3):
        if board[row] is board[row+3] is board[row+6] == 'X':
            print('You win')
            return True
        if board[row] is board[row+3] is board[row+6] == 'O':
            print('You win')
            return True

    # Diagonal Check
    dia = 0
    if board[dia] is board[dia+4] is board[dia+8] == 'X':
        print('You win')
        display_board()
        return True
    elif board[dia] is board[dia+4] is board[dia+8] == 'O':
        print('You win')
        display_board()
        return True
    dia = 2
    if board[dia] is board[dia+2] is board[dia+4] == 'X':
        print('You win')
        display_board()
        return True
    elif board[dia] is board[dia+2] is board[dia+4] == 'O':
        print('You win')
        display_board()
        return True

def play_game():
    i = 0
    if gameplay[i] == 1:
        board[val] = 'X'
        gameplay.pop(i)
        res = win_check()
        if res is True:
            return True
        else:
            display_board()
            inval()
    else:
        board[val] = 'O'
        gameplay.pop(i)
        res = win_check()
        if res is True:
            return True
        else:
            display_board()
            inval()


def inval():
    global val
    val = int(input('Choose the values from 0 to 8'))
    try:
        if val<=8 and val>=0:
            for item in range(9):
                if item == val:
                    res = play_game()
                    if res is True:
                        break
                    break
        else:
            print('Enter Valid Input!!!!')
            inval()

    except TypeError:
        print('Enter Valid Input!!!!')
        inval()



display_board()
inval()

Example 2: 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