Monday, November 7, 2016

Displaying the Unfolded Cube

There isn't that much to this first step. All I wanted to do was to be able to display an unfolded cube. But because I didn't want to have to calculate the positions of everything by hand, I wrote a function that let me compute the locations of the squares based on three constants: the small gap size (between squares on the same face), the large gap size (between the faces), and the box size (the actual boxes). After having done all of this, I think I probably could have written a more general position calculator, but I'm not going to go back and rewrite this in order to do that. Also, I think this is the first time I've used global variables. My understanding is that if you define a variable before you define functions, you get a global variable. That seems to be what happens, anyway.

I've defined the colors of the faces to match the physical cube that I have (with blue on top... for now). The locations are given by a two dimensional array. The first dimension gives the face (0 = U, 1 = L, 2 = F, 3 = R, 4 = B, 5 = D) and the second dimension gives the position (0-8 starting from the top left and working across the rows and down the columns). I also added a 7th color (gray) for when I'm searching for moves that permute certain pieces but that do not care what happens to certain other pieces.

The next task will be to program in the permutations obtained from the 18 basic movements. I thought about just programming in the 6 clockwise rotations and then having the others generated by repeating these, but I suspect that I'll be checking millions of positions, so that would just make things inefficient. When it comes time to writing the search algorithm, I may need to also write in a "don't be dumb" check that doesn't let me do two moves on the same face in a row. But I'll get to that one probably in the next few days.

I've set myself up to have two windows. One to show the unfolded cube and the other to show a 3D cube. I actually want to use the second window to display move sequences with a visual aid to help me get it right. It's not necessary, but it just seemed like a fun thing to try. That will be done towards the end of the project.

Here's the existing code:
import pygame

# Global Variables
sGap = 5
bGap = 10
boxSize = 30

# Functions

def posCalc(bS, sG, bG):
    return bS*boxSize + sG*sGap + bG*bGap

def drawIt1(window, sqPos, sqColor, drawColor):
    for i in range(6):
        for ii in range(9):
            pygame.draw.rect(window,
                             drawColor[sqColor[i][ii]],
                             ( sqPos[i][ii][0], sqPos[i][ii][1], boxSize, boxSize ),
                             0)
    pygame.display.update()
    return


# Define drawing data

sqPos = [ [ [posCalc(3,3,1), posCalc(0,1,0)],
            [posCalc(4,4,1), posCalc(0,1,0)],
            [posCalc(5,5,1), posCalc(0,1,0)],
            [posCalc(3,3,1), posCalc(1,2,0)],
            [posCalc(4,4,1), posCalc(1,2,0)],
            [posCalc(5,5,1), posCalc(1,2,0)],
            [posCalc(3,3,1), posCalc(2,3,0)],
            [posCalc(4,4,1), posCalc(2,3,0)],
            [posCalc(5,5,1), posCalc(2,3,0)] ],
          [ [posCalc(0,1,0), posCalc(3,3,1)],
            [posCalc(1,2,0), posCalc(3,3,1)],
            [posCalc(2,3,0), posCalc(3,3,1)],
            [posCalc(0,1,0), posCalc(4,4,1)],
            [posCalc(1,2,0), posCalc(4,4,1)],
            [posCalc(2,3,0), posCalc(4,4,1)],
            [posCalc(0,1,0), posCalc(5,5,1)],
            [posCalc(1,2,0), posCalc(5,5,1)],
            [posCalc(2,3,0), posCalc(5,5,1)] ],
          [ [posCalc(3,3,1), posCalc(3,3,1)],
            [posCalc(4,4,1), posCalc(3,3,1)],
            [posCalc(5,5,1), posCalc(3,3,1)],
            [posCalc(3,3,1), posCalc(4,4,1)],
            [posCalc(4,4,1), posCalc(4,4,1)],
            [posCalc(5,5,1), posCalc(4,4,1)],
            [posCalc(3,3,1), posCalc(5,5,1)],
            [posCalc(4,4,1), posCalc(5,5,1)],
            [posCalc(5,5,1), posCalc(5,5,1)] ],
          [ [posCalc(6,5,2), posCalc(3,3,1)],
            [posCalc(7,6,2), posCalc(3,3,1)],
            [posCalc(8,7,2), posCalc(3,3,1)],
            [posCalc(6,5,2), posCalc(4,4,1)],
            [posCalc(7,6,2), posCalc(4,4,1)],
            [posCalc(8,7,2), posCalc(4,4,1)],
            [posCalc(6,5,2), posCalc(5,5,1)],
            [posCalc(7,6,2), posCalc(5,5,1)],
            [posCalc(8,7,2), posCalc(5,5,1)] ],
          [ [posCalc(9,7,3), posCalc(3,3,1)],
            [posCalc(10,8,3), posCalc(3,3,1)],
            [posCalc(11,9,3), posCalc(3,3,1)],
            [posCalc(9,7,3), posCalc(4,4,1)],
            [posCalc(10,8,3), posCalc(4,4,1)],
            [posCalc(11,9,3), posCalc(4,4,1)],
            [posCalc(9,7,3), posCalc(5,5,1)],
            [posCalc(10,8,3), posCalc(5,5,1)],
            [posCalc(11,9,3), posCalc(5,5,1)] ],
          [ [posCalc(3,3,1), posCalc(6,5,2)],
            [posCalc(4,4,1), posCalc(6,5,2)],
            [posCalc(5,5,1), posCalc(6,5,2)],
            [posCalc(3,3,1), posCalc(7,6,2)],
            [posCalc(4,4,1), posCalc(7,6,2)],
            [posCalc(5,5,1), posCalc(7,6,2)],
            [posCalc(3,3,1), posCalc(8,7,2)],
            [posCalc(4,4,1), posCalc(8,7,2)],
            [posCalc(5,5,1), posCalc(8,7,2)] ]
        ]

sqColor = [ [ i for ii in range(9) ] for i in range(6) ]

drawColor = [ pygame.Color(0,0,255),
              pygame.Color(255,0,0),
              pygame.Color(255,255,0),
              pygame.Color(255,140,0),
              pygame.Color(255,255,255),
              pygame.Color(0,255,0),
              pygame.Color(100,100,100) ]

### Main program
# Initialize
pygame.init()

window1Width = posCalc(12,10,3)
window1Height = posCalc(9,8,2)
window1 = pygame.display.set_mode((window1Width,window1Height))
drawIt1(window1, sqPos, sqColor, drawColor)

No comments:

Post a Comment