#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Virtual rubiks cube program bug


    Hi all
    I am making a virtual rubiks cube(with python/pygame) to simulate optimal solution algorithms and i am trying to test the transformation . in the code i have currently i am making a copy of my cube and setting the cubes values to the right ones of my "tempcube". the problem is that it appears to be setting my tempcubes values as well as my cube values and so the final cube is all wrong

    Any ideas how to make the tempcube stay the same as the cube before it was altered?

    code:

    Code:
    import pygame
    from pygame.locals import *
    pygame.init()
    
    test = pygame.Surface((170,170))
    square = pygame.Surface((50,50))
    
    window = pygame.display.set_mode((500,500))
    window.fill((0,128,128))
    pygame.display.flip()
    
    
    cube = [[0,0,0,0,0,0,0,0,0],[1,1,1,1,1,1,1,1,1],[2,2,2,2,2,2,2,2,2],[3,3,3,3,3,3,3,3,3],[4,4,4,4,4,4,4,4,4],[5,5,5,5,5,5,5,5,5]]
    
    
    
    tempcube = cube
    
    
    
    
    
    print "-------------------------"
    
    def front():
            tempcube = cube
            tempcube2 = tempcube
    
            
            cube[0][6]=tempcube[4][8]
            cube[0][7]=tempcube[4][5]
            cube[0][8]=tempcube[4][2]
    
            cube[2][0]=tempcube[0][6]
            cube[2][3]=tempcube[0][7]
            cube[2][6]=tempcube[0][8]
    
            cube[5][2]=tempcube[2][0]
            cube[5][1]=tempcube[2][3]
            cube[5][0]=tempcube[2][6]
    
            cube[4][8]=tempcube[5][2]
            cube[4][5]=tempcube[5][1]
            cube[4][2]=tempcube[5][0]
    
            cube[1][0]=tempcube[1][6]
            cube[1][1]=tempcube[1][3]
            cube[1][2]=tempcube[1][0]
    
            cube[1][3]=tempcube[1][7]
            cube[1][4]=tempcube[1][4]
            cube[1][5]=tempcube[1][1]
            
            cube[1][6]=tempcube[1][8]
            cube[1][7]=tempcube[1][5]
            cube[1][8]=tempcube[1][2]
            
    
            
            
    
    
    front()
    
    print "-----------------------------"
    
    print cube  
        
    
    while True:
    	for event in pygame.event.get():
    		if event.type==pygame.QUIT:
    			pygame.quit();
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    TLDR;

    To copy a list in python:

    Code:
    copy = original[:]
    Long version:

    Code:
    >>> a = [1]         # Create initial list
    >>> b = a           # try to "copy" a to b
    >>> a.append(2)     # update the original
    >>> a
    [1, 2]
    >>> b
    [1, 2]              # oh no, b seems to be referring to a
    >>> id(a)
    15065960
    >>> id(b)
    15065960            # I see, they are actually the same object
    
    >>> b = a[:]        # Now copy list correctly
    >>> b
    [1, 2]
    >>> id(b)
    15067112            # Yep, now b is another object
    >>> a.append(3)     # Let's verify...
    >>> a
    [1, 2, 3]
    >>> b
    [1, 2]              # Yep, works correctly
    Last edited by partoj; April 25th, 2013 at 06:52 AM.
    http://www.catb.org/esr/faqs/smart-questions.html#code
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by partoj
    TLDR;

    To copy a list in python:

    Code:
    copy = original[:]
    Long version:

    Code:
    >>> a = [1]         # Create initial list
    >>> b = a           # try to "copy" a to b
    >>> a.append(2)     # update the original
    >>> a
    [1, 2]
    >>> b
    [1, 2]              # oh no, b seems to be referring to a
    >>> id(a)
    15065960
    >>> id(b)
    15065960            # I see, they are actually the same object
    
    >>> b = a[:]        # Now copy list correctly
    >>> b
    [1, 2]
    >>> id(b)
    15067112            # Yep, now b is another object
    >>> a.append(3)     # Let's verify...
    >>> a
    [1, 2, 3]
    >>> b
    [1, 2]              # Yep, works correctly


    Ohhhhhhhhhhhhhh that makes sense !
    thanks a bunch

IMN logo majestic logo threadwatch logo seochat tools logo