#Python Tic Tac Toe bot AI

1 messages · Page 1 of 1 (latest)

fossil briar
#
  #computer goes if user hasn't won yet
  time.sleep(1)
  if win == False and game == True:
    print("Computer's turn")
    potential_win = False
    row_0 = False
    row_1 = False
    row_2 = False
    column_0 = False
    column_1 = False
    column_2 = False
    diagonal_0 = False
    diagonal_1 = False

    #check for potential wins

    #check for potential horizontal wins
    if space[0].count(P[0]) == 1:
      if len(set(space[0])) == 2:
        potential_win = True
        row_0 = True
    if space[1].count(P[0]) == 1:
      if len(set(space[1])) == 2:
        potential_win = True
        row_1 = True
    if space[2].count(P[0]) == 1:
      if len(set(space[2])) == 2:
        potential_win = True
        row_2 = True

    #check for potential vertical wins
    if columns[0].count(P[0]) == 1:
      if len(set(columns[0])) == 2:
        potential_win = True
        column_0 = True
    if columns[1].count(P[0]) == 1:
      if len(set(columns[1])) == 2:
        potential_win = True
        column_1 = True
    if columns[2].count(P[0]) == 1:
      if len(set(columns[2])) == 2:
        potential_win = True
        column_2 = True

    #check for potential diagonal wins
    if diagonals[0].count(P[0]) == 1:
      if len(set(diagonals[0])) == 2:
        potential_win = True
        diagonal_0 = True
    if diagonals[1].count(P[0]) == 1:
      if len(set(diagonals[1])) == 2:
        potential_win = True
        diagonal_1 = True

    #random spot if no potential win
    if potential_win == False:
      blank = False
      while blank == False:
        x = random.randint(0, 2)
        y = random.randint(0, 2)
        if space[x][y] == P[0]:
          blank = True
          space[x][y] = P[2]
        else:
          blank = False
mellow dock
#

you could implement a min/max algorithm which is I believe the best way to solve this kind of problems PE_PandaSmug

fossil briar
# fossil briar ```py #computer goes if user hasn't won yet time.sleep(1) if win == False ...
    #act if there is a potential win
    if potential_win == True:
      print("potential win detected")
      if row_0 == True and potential_win == True:
        for i in range(len(space)):
          if space[0][i] == P[0]:
            space[0][i] = P[2]
            row_0 = False
            potential_win = False
      if row_1 == True and potential_win == True:
        for i in range(len(space)):
          if space[1][i] == P[0]:
            space[1][i] = P[2]
            row_1 = False
            potential_win = False
      if row_2 == True and potential_win == True:
        for i in range(len(space)):
          if space[2][i] == P[0]:
            space[2][i] = P[2]
            row_2 = False
            potential_win = False

      if column_0 == True and potential_win == True:
        for i in range(len(space)):
          if space[i][0] == P[0]:
            space[i][0] = P[2]
            column_0 = False
            potential_win = False
      if column_1 == True and potential_win == True:
        for i in range(len(space)):
          if space[i][1] == P[0]:
            space[i][1] = P[2]
            column_1 = False
            potential_win = False
      if column_2 == True and potential_win == True:
        for i in range(len(space)):
          if space[i][2] == P[0]:
            space[i][2] = P[2]
            column_2 = False
            potential_win = False
#
      if diagonal_0 == True and potential_win == True:
        for i in range(len(space)):
          if space[0][0] == P[0]:
            space[0][0] = P[2]
          if space[1][1] == P[0]:
            space[1][1] = P[2]
          if space[2][2] == P[0]:
            space[2][2] = P[2]
          diagonal_0 = False
          potential_win = False
      if diagonal_1 == True and potential_win == True:
        for i in range(len(space)):
          if space[0][2] == P[0]:
            space[0][2] = P[2]
          if space[1][1] == P[0]:
            space[1][1] = P[2]
          if space[2][0] == P[0]:
            space[2][0] = P[2]
          diagonal_1 = False
          potential_win = False
fossil briar