0
Q:

tic tac toe logic 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

      

1
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()
0
# Tic-Tac-Toe Program using 
# random number in Python 
  
# importing all necessary libraries 
import numpy as np 
import random 
from time import sleep 
  
# Creates an empty board 
def create_board(): 
    return(np.array([[0, 0, 0], 
                     [0, 0, 0], 
                     [0, 0, 0]])) 
  
# Check for empty places on board 
def possibilities(board): 
    l = [] 
      
    for i in range(len(board)): 
        for j in range(len(board)): 
              
            if board[i][j] == 0: 
                l.append((i, j)) 
    return(l) 
  
# Select a random place for the player 
def random_place(board, player): 
    selection = possibilities(board) 
    current_loc = random.choice(selection) 
    board[current_loc] = player 
    return(board) 
  
# Checks whether the player has three  
# of their marks in a horizontal row 
def row_win(board, player): 
    for x in range(len(board)): 
        win = True
          
        for y in range(len(board)): 
            if board[x, y] != player: 
                win = False
                continue
                  
        if win == True: 
            return(win) 
    return(win) 
  
# Checks whether the player has three 
# of their marks in a vertical row 
def col_win(board, player): 
    for x in range(len(board)): 
        win = True
          
        for y in range(len(board)): 
            if board[y][x] != player: 
                win = False
                continue
                  
        if win == True: 
            return(win) 
    return(win) 
  
# Checks whether the player has three 
# of their marks in a diagonal row 
def diag_win(board, player): 
    win = True
    y = 0
    for x in range(len(board)): 
        if board[x, x] != player: 
            win = False
    if win: 
        return win 
    win = True
    if win: 
        for x in range(len(board)): 
            y = len(board) - 1 - x 
            if board[x, y] != player: 
                win = False
    return win 
  
# Evaluates whether there is 
# a winner or a tie  
def evaluate(board): 
    winner = 0
      
    for player in [1, 2]: 
        if (row_win(board, player) or
            col_win(board,player) or 
            diag_win(board,player)): 
                 
            winner = player 
              
    if np.all(board != 0) and winner == 0: 
        winner = -1
    return winner 
  
# Main function to start the game 
def play_game(): 
    board, winner, counter = create_board(), 0, 1
    print(board) 
    sleep(2) 
      
    while winner == 0: 
        for player in [1, 2]: 
            board = random_place(board, player) 
            print("Board after " + str(counter) + " move") 
            print(board) 
            sleep(2) 
            counter += 1
            winner = evaluate(board) 
            if winner != 0: 
                break
    return(winner) 
  
# Driver Code 
print("Winner is: " + str(play_game())) 
0

New to Communities?

Join the community