Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/parentesis.activity/parentesis.py.bak
blob: fef0bbb1382398e9ed3f372ccc32186d4789842c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- encoding: UTF-8 -*-
import gtk
from sugar.activity.activity import Activity, ActivityToolbox
import logica

ESTADO_VACIO = 0
ESTADO_IZQ = 1
ESTADO_DER = 2

IGUAL = "="

class Parentesis(Activity):
    def __init__(self, handle):
        Activity.__init__(self,handle)
        
        self.toolbox = ActivityToolbox(self)
        self.set_toolbox(self.toolbox)
        
        self.vbox = gtk.VBox()
        self.hbox = gtk.HBox()
        self.msj = gtk.Label()
        self.vbox.pack_start(self.msj)
        self.vbox.pack_start(self.hbox)
        self.set_canvas(self.vbox)
        self.show_all()
        
        self.solucion_izquierdos = set()
        self.solucion_derechos = set()
        
        self.problemas = logica.generarCasos()
        self.MostrarProblema(self.problemas.pop())

    def MostrarProblema(self, prob):
        for w in self.hbox.get_children(): 
            self.hbox.remove(w)
        
        prob_str = prob
        prob = list(prob)
        pos = 0
        boton = self.crear_boton(pos)
        self.agregar_widget(boton)
        while pos < len(prob):
            car = prob[pos]
            if car == "(":
                self.solucion_izquierdos.add(pos)
                pos = pos + 1
            elif car == ")":
                self.solucion_derechos.add(pos)
                pos = pos + 1
            else:
                car_etiqueta = gtk.Label(car)
                self.agregar_widget(car_etiqueta)
                pos = pos + 1
                boton = self.crear_boton(pos)
                self.agregar_widget(boton)
                
        igual = self.crear_boton_igual()
        self.agregar_widget(igual)
        self.agregar_widget(gtk.Label(eval('2+1')))

    def agregar_widget(self, widget):
        ''' Agrega el boton a la lista de botones '''
        self.hbox.pack_start(widget)
        widget.show()
    
    def crear_boton(self, pos):
        boton = BotonParentesis(pos)
        boton.connect('clicked', self.boton_activado)
        return boton
        
    def crear_boton_igual(self):
        boton = gtk.Button(IGUAL)
        boton.connect('clicked', self.boton_igual_activado)
        return boton
    
    def boton_activado(self, boton):
     boton.iterarEstado()
    
    def boton_igual_activado(self, boton):
        respuesta_izquierdos = set()
        respuesta_derechos = set()
        for widget in self.hbox.get_children():
            if isinstance(widget,BotonParentesis):
                estado = widget.getEstado()
                if estado == ESTADO_IZQ:
                    respuesta_izquierdos.add(widget.getPos())
                if estado == ESTADO_DER:
                    respuesta_derechos.add(widget.getPos())
        
        if respuesta_izquierdos == self.solucion_izquierdos and respuesta_derechos == self.solucion_derechos:
            self.msj.set_label("Muy Bien!")
            self.MostrarProblema(self.problemas.pop())
        else:
            self.msj.set_label("Incorrecto")
        
class BotonParentesis(gtk.Button):
    
    def __init__(self, pos):
        gtk.Button.__init__(self, "")
        self._pos = pos
        self._estado = ESTADO_VACIO
    
    def getPos(self):
        return self._pos
    
    def setPos(self, pos):
        self._pos = pos
    
    def iterarEstado(self):
        if self._estado == ESTADO_VACIO:
            self._estado = ESTADO_IZQ
            self.set_label("(")
        elif self._estado == ESTADO_IZQ:
            self._estado = ESTADO_DER
            self.set_label(")")
        elif self._estado == ESTADO_DER:
            self._estado = ESTADO_VACIO
            self.set_label("")

    def getEstado(self):
        return self._estado