コード例 #1
0
    def testIncrementer(self):
        '''
        Turing machine that increments a binary integer by 1.
        '''

        ################################################################
        ################################################################

        incrementer = TM.TuringMachine()

        # State 0: Move Right
        incrementer.addTransition((0, '0'), (0, '0', Move.R))
        incrementer.addTransition((0, '1'), (0, '1', Move.R))
        incrementer.addTransition((0, eps), (1, eps, Move.L))

        # State 1: Add 1
        incrementer.addTransition((1, '0'), (2, '1', Move.L))
        incrementer.addTransition((1, '1'), (1, '0', Move.L))
        incrementer.addTransition((1, eps), (2, '1', Move.R))

        # State 2: HALT
        # TM should not halt 'outside' the string
        incrementer.addTransition((2, eps), (2, eps, Move.R))

        ################################################################
        ################################################################

        # Simulation (1)
        inputString = '1000'
        print('Input: ' + inputString)
        outputString = incrementer.simulate(inputString)
        print('Output: ' + outputString + '\n')

        # Simulation (2)
        inputString = '1011'
        print('Input: ' + inputString)
        outputString = incrementer.simulate(inputString, True)
        print('Output: ' + outputString + '\n')

        # Simulation (3)
        inputString = '1111'
        print('Input: ' + inputString)
        outputString = incrementer.simulate(inputString)
        print('Output: ' + outputString + '\n')

        # Simulation (4)
        inputString = '0'
        print('Input: ' + inputString)
        outputString = incrementer.simulate(inputString)
        print('Output: ' + outputString + '\n')
コード例 #2
0
    def testAdder(self):
        '''
        Turing machine that adds two binary integers.
        Accepts strings in format 'num1+num2'.
        '''

        ################################################################
        ################################################################
        adder = TM.TuringMachine()

        # State 0
        adder.addTransition((0, eps), (1, eps, Move.L))
        adder.addTransition((0, '0'), (0, '0', Move.R))
        adder.addTransition((0, '1'), (0, '1', Move.R))
        adder.addTransition((0, 'a'), (0, 'a', Move.R))
        adder.addTransition((0, 'b'), (0, 'b', Move.R))
        adder.addTransition((0, '+'), (0, '+', Move.R))

        # State 1
        adder.addTransition((1, '0'), (2, eps, Move.L))
        adder.addTransition((1, '1'), (3, eps, Move.L))
        adder.addTransition((1, '+'), (2, eps, Move.L))

        # State 2
        adder.addTransition((2, eps), (7, eps, Move.R))
        adder.addTransition((2, '0'), (2, '0', Move.L))
        adder.addTransition((2, '1'), (2, '1', Move.L))
        adder.addTransition((2, 'a'), (2, '0', Move.L))
        adder.addTransition((2, 'b'), (2, '1', Move.L))
        adder.addTransition((2, '+'), (4, '+', Move.L))

        # State 3
        adder.addTransition((3, '0'), (3, '0', Move.L))
        adder.addTransition((3, '1'), (3, '1', Move.L))
        adder.addTransition((3, '+'), (5, '+', Move.L))

        # State 4
        adder.addTransition((4, eps), (0, 'a', Move.R))
        adder.addTransition((4, '0'), (0, 'a', Move.R))
        adder.addTransition((4, '1'), (0, 'b', Move.R))
        adder.addTransition((4, 'a'), (4, 'a', Move.L))
        adder.addTransition((4, 'b'), (4, 'b', Move.L))

        # State 5
        adder.addTransition((5, eps), (0, 'b', Move.R))
        adder.addTransition((5, '0'), (0, 'b', Move.R))
        adder.addTransition((5, '1'), (6, 'a', Move.L))
        adder.addTransition((5, 'a'), (5, 'a', Move.L))
        adder.addTransition((5, 'b'), (5, 'b', Move.L))

        # State 6
        adder.addTransition((6, eps), (0, '1', Move.R))
        adder.addTransition((6, '0'), (0, '1', Move.R))
        adder.addTransition((6, '1'), (6, '0', Move.L))

        # State 7 - HALT

        ################################################################
        ################################################################

        # Simulation (1)
        inputString = '111+1001'
        print('Input: ' + inputString)
        outputString = adder.simulate(inputString, True)
        print('Output: ' + outputString + '\n')

        # Simulation (2)
        inputString = '111+11'
        print('Input: ' + inputString)
        outputString = adder.simulate(inputString)
        print('Output: ' + outputString + '\n')

        # Simulation (3)
        inputString = '111+0'
        print('Input: ' + inputString)
        outputString = adder.simulate(inputString)
        print('Output: ' + outputString + '\n')

        # Simulation (4)
        inputString = '100+1'
        print('Input: ' + inputString)
        outputString = adder.simulate(inputString)
        print('Output: ' + outputString + '\n')
コード例 #3
0
'''
Created on 2012-8-26

@author: XPMUser
'''

import TM

if __name__ == '__main__':
	TM.t()
コード例 #4
0
ファイル: demo.py プロジェクト: AbstractBeliefs/Turing
        },
    1:{
        'a':{
            'w': 1,
            'm': -1,
            'n': 'c'
            },
        'b':{
            'w': 1,
            'm': 1,
            'n': 'b'
            },
        'c':{
            'w': 1,
            'm': 1,
            'n': 'halt'
            },
        },
}

class BB(TM.machine):
    startstate = 'a'
    startpos = 0
    states = ['a','b','c','halt']
    blank = 0

tape = {0:0}
mymach = BB(rule)
i = TM.interpreter(mymach, tape)
i.mainloop()
コード例 #5
0
    def drawingMain(self, center, img, time):
        self.clock.tick(10)
        if time == 0:
            cv.imwrite('./drawing_imgs/output/popimage.jpg', img)
            pygame.image.save(self.screen,
                              "./drawing_imgs/output/screenshot.jpg")

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

            #마우스 클릭시 동물이 바뀜
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if self.animal_now == 'horse':
                    self.animal_now = 'cat'
                elif self.animal_now == 'cat':
                    self.animal_now = 'bird'
                elif self.animal_now == 'bird':
                    self.animal_now = 'horse'

        self.screen.fill((0, 0, 0))
        self.screen.blit(
            pygame.image.load('./drawing_imgs/sprites/background.jpg'), (0, 0))

        self.pos_prev = self.pos_now
        # get hand point from video
        ##        ret,img = cap.read()

        ##        if ret == False:
        ##            continue

        points = (center[1], center[0])

        #cv.imshow('result', img)
        # if person head is found
        if type(points) is tuple:
            self.pos_now = (points[0] * 1.3 - 60, points[1] * 1.3 - 60)

        # if user goes out of the screen, change animal
        if not (0 < self.pos_now[0] < 640 and 0 < self.pos_now[1] < 480):
            self.animal_flag += 1
            self.animal_now = self.animal_init[self.flag % 3]

            # check if user collided to buckets
        if self.check_collision(self.bucket_y, self.pos_now, self.distance):
            self.spill_y += 1
        elif self.check_collision(self.bucket_g, self.pos_now, self.distance):
            self.spill_g += 1
        elif self.check_collision(self.bucket_s, self.pos_now, self.distance):
            self.spill_s += 1
        elif self.check_collision(self.bucket_p, self.pos_now, self.distance):
            self.spill_p += 1

            # check if user collided to spilled paint
        if self.check_collision(self.yellow, self.pos_now, self.distance):
            if self.spill_y != 0:
                self.color_now = '_y'
                self.time = 1
                self.opacity_now = 300
        elif self.check_collision(self.green, self.pos_now, self.distance):
            if self.spill_g != 0:
                self.color_now = '_g'
                self.time = 1
                self.opacity_now = 300
        elif self.check_collision(self.skyblue, self.pos_now, self.distance):
            if self.spill_s != 0:
                self.color_now = '_s'
                self.time = 1
                self.opacity_now = 300
        elif self.check_collision(self.pink, self.pos_now, self.distance):
            if self.spill_p != 0:
                self.color_now = '_p'
                self.time = 1
                self.opacity_now = 300

        if self.time > 0:
            self.time += 1
            self.opacity_now -= 10
            if self.time == 30:
                self.color_now = 'time_over'
                self.time = 0

        # broom img
        self.screen.blit(
            self.broom_1,
            (self.broom[0] - int(67 / 2), self.broom[1] - int(116 / 2)))

        # draw stand up/spilled paint bucket depending on 'spill_' bool
        if self.spill_y == 0:  #YELLO
            self.screen.blit(self.bucket_y_img,
                             (self.bucket_y[0] - int(self.paints_size / 2),
                              self.bucket_y[1] - int(self.paints_size / 2)))
        elif self.spill_y > 0 and self.spill_y < 3:
            self.screen.blit(self.bucket_y_2,
                             (self.bucket_y[0] - int(self.paints_size / 2),
                              self.bucket_y[1] - int(self.paints_size / 2)))
            self.spill_y += 1
        else:
            self.screen.blit(self.bucket_y_3,
                             (self.bucket_y[0] - int(self.paints_size / 2),
                              self.bucket_y[1] - int(self.paints_size / 2)))
            self.spill_y += 1
            if self.spill_y > 20:
                self.spill_y = 0
                self.X = random.randint(50, 750)
                self.Y = random.randint(50, 550)
                self.yellow = (self.X, self.Y)
                self.bucket_y = (self.yellow[0] - 70, self.yellow[1])

        if self.spill_g == 0:  #GREEN
            self.screen.blit(self.bucket_g_img,
                             (self.bucket_g[0] - int(self.paints_size / 2),
                              self.bucket_g[1] - int(self.paints_size / 2)))
        elif self.spill_g > 0 and self.spill_g < 3:
            self.screen.blit(self.bucket_g_2,
                             (self.bucket_g[0] - int(self.paints_size / 2),
                              self.bucket_g[1] - int(self.paints_size / 2)))
            self.spill_g += 1
        else:
            self.screen.blit(self.bucket_g_3,
                             (self.bucket_g[0] - int(self.paints_size / 2),
                              self.bucket_g[1] - int(self.paints_size / 2)))
            self.spill_g += 1
            if self.spill_g > 20:
                self.spill_g = 0
                self.X = random.randint(120, 750)
                self.Y = random.randint(120, 550)
                self.green = (self.X, self.Y)
                self.bucket_g = (self.green[0] - 70, self.green[1])

        if self.spill_s == 0:  #SKYBLUE
            self.screen.blit(self.bucket_s_img,
                             (self.bucket_s[0] - int(self.paints_size / 2),
                              self.bucket_s[1] - int(self.paints_size / 2)))
        elif self.spill_s > 0 and self.spill_s < 3:
            self.screen.blit(self.bucket_s_2,
                             (self.bucket_s[0] - int(self.paints_size / 2),
                              self.bucket_s[1] - int(self.paints_size / 2)))
            self.spill_s += 1
        else:
            self.screen.blit(self.bucket_s_3,
                             (self.bucket_s[0] - int(self.paints_size / 2),
                              self.bucket_s[1] - int(self.paints_size / 2)))
            self.spill_s += 1
            if self.spill_s > 20:
                self.spill_s = 0
                self.X = random.randint(50, 750)
                self.Y = random.randint(50, 550)
                self.skyblue = (self.X, self.Y)
                self.bucket_s = (self.skyblue[0] - 70, self.skyblue[1])

        if self.spill_p == 0:  #PINK
            self.screen.blit(self.bucket_p_img,
                             (self.bucket_p[0] - int(self.paints_size / 2),
                              self.bucket_p[1] - int(self.paints_size / 2)))
        elif self.spill_p > 0 and self.spill_p < 3:
            self.screen.blit(self.bucket_p_2,
                             (self.bucket_p[0] - int(self.paints_size / 2),
                              self.bucket_p[1] - int(self.paints_size / 2)))
            self.spill_p += 1
        else:
            self.screen.blit(self.bucket_p_3,
                             (self.bucket_p[0] - int(self.paints_size / 2),
                              self.bucket_p[1] - int(self.paints_size / 2)))
            self.spill_p += 1
            if self.spill_p > 20:
                self.spill_p = 0
                self.X = random.randint(50, 750)
                self.Y = random.randint(50, 550)
                self.pink = (self.X, self.Y)
                self.bucket_p = (self.pink[0] - 70, self.pink[1])

        # ERASE
        if self.check_collision(self.broom, self.pos_now, self.distance):
            self.mousepos.clear()
            self.animals.clear()
            self.opacity.clear()
            self.screen.blit(
                self.broom_2,
                (self.broom[0] - int(89 / 2), self.broom[1] - int(143 / 2)))
            sfx1 = pygame.mixer.Sound('./drawing_imgs/sound/erase.ogg')
            sfx1.set_volume(0.5)
            sfx1.play()

        # user img

        #screen.blit(flower,(pos_now[0]-int(flower_size/2),pos_now[1]-int(flower_size/2)))
        if self.flag == 1:
            self.X = random.randint(0, 40)
            self.Y = random.randint(0, 40)

        self.screen.blit(self.imgLoad('blink' + str(self.flag)),
                         (self.pos_now[0] - self.X, self.pos_now[1] - self.Y))

        if self.flag == 4:
            self.X2 = random.randint(0, 40)
            self.Y2 = random.randint(0, 40)
        self.screen.blit(self.imgLoad('blink'+str(self.flag-3 if self.flag>3 else self.flag+5)),\
                         (self.pos_now[0]-self.X2,self.pos_now[1]-self.Y2))

        if self.flag == 8:
            self.flag = 0

        self.flag += 1

        if self.opacity_now <= 10:
            self.guide_count += 1
        else:
            self.guide_count = 0

        # if user did not touch any bucket yet, no footstep printing
        if self.color_now is None:
            #guide window blit
            if self.guide_count >= 20:
                self.screen.blit(self.guide1, (100, 100))
                if (self.spill_y>0 and self.spill_y<20) or (self.spill_s>0 and self.spill_s<20) or (self.spill_g>0 and self.spill_g<20)\
                   or (self.spill_p>0 and self.spill_p<20):
                    self.screen.blit(self.guide2, (100, 100))
                    self.guide2_count += 1
            if self.guide2_count > 0:
                self.screen.blit(self.guide2, (100, 100))
                self.guide2_count += 1
                if self.guide2_count >= 15:
                    self.guide2_count = 0
                    self.guide_count = 0
            pygame.display.update()

            #continue
            #return ####################

        elif self.color_now is not 'time_over':
            self.mousepos.append(self.pos_now)
            mousepos_count = len(self.mousepos)
            self.animals.append(
                (detect.rotate_img(self.animal_now + self.color_now,
                                   self.mousepos[mousepos_count - 2],
                                   self.mousepos[mousepos_count - 1])))
            self.opacity.append(self.opacity_now)
            '''
            sfx1 = pygame.mixer.Sound('./drawing_imgs/sound/step.ogg')
            sfx1.set_volume(0.5)
            sfx1.play()
            '''

        # draw footsteps on screen
        for i in range(len(self.mousepos)):
            self.drawObject(self.animals[i], self.mousepos[i], self.opacity[i])

        if len(self.mousepos) > self.highest_len:
            self.highest_len = len(self.mousepos)
            cv.imwrite('./drawing_imgs/output/popimage.jpg', img)
            pygame.image.save(self.screen,
                              "./drawing_imgs/output/screenshot.jpg")

        #guide window blit
        if self.guide_count >= 20:
            self.screen.blit(self.guide1, (100, 100))
            if (self.spill_y>0 and self.spill_y<20) or (self.spill_s>0 and self.spill_s<20) or (self.spill_g>0 and self.spill_g<20) or\
               (self.spill_p>0 and self.spill_p<20):
                self.screen.blit(self.guide2, (100, 100))
                self.guide2_count += 1
        if self.guide2_count > 0:
            self.screen.blit(self.guide2, (100, 100))
            self.guide2_count += 1
            if self.guide2_count >= 15:
                self.guide2_count = 0
                self.guide_count = 0

        if time == 0:
            cv.imwrite('./drawing_imgs/output/popimage.jpg', img)
            pygame.image.save(self.screen,
                              "./drawing_imgs/output/screenshot.jpg")

        pygame.display.flip()
コード例 #6
0
            if (spill_y>0 and spill_y<20) or (spill_s>0 and spill_s<20) or (spill_g>0 and spill_g<20) or (spill_p>0 and spill_p<20):
                screen.blit(guide2,(100,100))
                guide2_count += 1
        if guide2_count > 0:
            screen.blit(guide2,(100,100))
            guide2_count += 1
            if guide2_count >= 15:
                guide2_count = 0
                guide_count = 0
        pygame.display.update()
        continue

    elif color_now is not 'time_over':
        mousepos.append(pos_now)
        mousepos_count = len(mousepos)
        animals.append((detect.rotate_img(animal_now+color_now,mousepos[mousepos_count-2],mousepos[mousepos_count-1])))
        opacity.append(opacity_now)
        '''
        sfx1 = pygame.mixer.Sound('sound/step.ogg')
        sfx1.set_volume(0.5)
        sfx1.play()
        '''
        
    # draw footsteps on screen
    for i in range(len(mousepos)):
        drawObject(animals[i],mousepos[i],opacity[i])

    if len(mousepos) > highest_len:
        highest_len = len(mousepos)
        cv.imwrite('output/popimage.jpg', img)
        pygame.image.save(screen,"output/screenshot.jpg")