Home > other >  How do I solve TypeError: 'set' object is not subscriptable?
How do I solve TypeError: 'set' object is not subscriptable?

Time:11-07

I am building a fifteen puzzle program that, for now, runs in Terminal

I have two scripts, first one as follows...

main.py

from Tablero import Tablero
from pynput import keyboard
# from pynput import mouse

t = Tablero()

def main():
  print(t)

  # registra los eventos del teclado hasta que la tecla es soltada
  with keyboard.Listener(
      on_press=on_press,
      on_release=on_release) as listener:
    listener.join()

def on_press(key):
  t.refresh()

def on_release(key):
  print("soltado")
  if key == keyboard.Key.esc:
    # Stop listener
    return False
  elif key == keyboard.Key.up:
    t.tablero, t.esp_vacio = t.mover_arriba(t.tablero, t.esp_vacio)
  elif key == keyboard.Key.right:
    print("derecha")
  elif key == keyboard.Key.down:
    print("abajo")
  elif key == keyboard.Key.left:
    print("izquierda")

  t.refresh()

if __name__ == "__main__":
  main()

Second one...

Tablero.py

from copy import deepcopy
from os import system

MAX_FIL = 4
MAX_COL = 4

class Tablero:
  """ Modela el tablero del juego """

  def __init__(self):
    # un arreglo 2D para representar el tablero
    self.solucion = [[" 1", " 2", " 3", " 4"],
                     [" 5", " 6", " 7", " 8"],
                     [" 9", "10", "11", "12"],
                     ["13", "14", "15", "__"]]

    # función deepcopy para hacer una copia del tablero solucion original
    # de manera que siempre este disponible como tal
    self.tablero= deepcopy(self.solucion)

    self.esp_vacio = {MAX_FIL - 1, MAX_COL - 1}

  def __repr__(self):
    for i in range(MAX_FIL):
      for j in range(MAX_COL):
        print(self.tablero[i][j], end=" ")
      print()

    # la función __repr__ DEBE regresar algo siempre
    return ""

  def refresh(self):
    system("clear")
    print(self)

  # funcion para programar el movimiento de espacio vacío (esp_vacio) del tablero
  def mover(self, tablero, esp_vacio, x, y):
    # verificar si el moviento es valido
    if(esp_vacio[0]   x < 0 or esp_vacio[0]   x > 3 or esp_vacio[1]   y < 0 or esp_vacio[1]   y > 3):
      return tablero, esp_vacio

    # intercambio de posición entre el espacio vacio y el espacio ocupado
    tablero[esp_vacio[0]][esp_vacio[1]], tablero[esp_vacio[0]   x][esp_vacio[1]   y]\
    = tablero[esp_vacio[0]   x][esp_vacio[1]   y], tablero[esp_vacio[0]][esp_vacio[1]]

    # actualiza el espacioo vacio en el tablero
    esp_vacio[0]  = x
    esp_vacio[1]  = y

    return tablero, esp_vacio

  def mover_arriba(self, tablero, esp_vacio):
    return self.mover(tablero, esp_vacio, -1, 0)

  def mover_derecha(self, tablero, esp_vacio):
    return self.mover(tablero, esp_vacio, 0, 1)

  def mover_abajo(self, tablero, esp_vacio):
    return self.mover(tablero, esp_vacio, 1, 0)

  def mover_izquierda(self, tablero, esp_vacio):
    return self.mover(tablero, esp_vacio, 0, -1)
    

When I run main.py it print the array as it should, but when I press up arrow key, the program crashes and throws: TypeError: 'set' object is not subscriptable

I am very confused, the only way the error stops from appearing is to erase the line 34 in main.py script

    t.tablero, t.esp_vacio = t.mover_arriba(t.tablero, t.esp_vacio)

Thank you so much in advance for your help!

CodePudding user response:

does defining esp_vacio a list help? because set objects has no index

self.esp_vacio = [MAX_FIL - 1, MAX_COL - 1]
  • Related