• Welcome to the Speedsolving.com, home of the web's largest puzzle community!
    You are currently viewing our forum as a guest which gives you limited access to join discussions and access our other features.

    Registration is fast, simple and absolutely free so please, join our community of 35,000+ people from around the world today!

    If you are already a member, simply login to hide this message and begin participating in the community!

Python Cube Project

Etotheipi

Member
Joined
Mar 21, 2019
Messages
856
Location
somewhere on the complex plane.
I've been working on a little Python cube simulator type thing, partially just to learn Python better and partially to have a way to mess around with AI, alg genning and whatnot. So I made this thread for me to get help cause I'm a python newbie, and for people to suggest ideas for me to implement. I'm having a little trouble with getting the moves to work. The way I have it coded is I gave every position a 'sticker' could go a number 1-54, and then I have a list with 54 elements with the first letter of the color of the sticker in that space. So, if there was a blue sticker in position 34, then the 34th element of the list would be "b". Then the way I do moves on the cube is I made a Move class, which takes two lists as parameters. The first list has the numbers of all the stickers that will be moved, and the second list has the numbers of the places that the stickers will be moved to. So if you did:

move=Move([1,39],[39,1])

apply_move(move)

Then it should swap the stickers 1 and 39. But it doesn't seem to be working (The sticker at 1 is moved to position 39, but not the sticker at position 39 to 1). I can post my full code if needed.
 
Last edited:

KingTim96

Member
Joined
Feb 14, 2012
Messages
577
Location
Lansing, MI
WCA
2012SPIT01
YouTube
Visit Channel
I may not be much help but I have been messing around with python for a bit at my school and during the quarantine I have been making a couple side projects like a 3x3 and 4x4 timer. If you felt comfortable posting the full code that would help. Are you coding in an IDE (Pycharm, for example) or on the Command Line?
 

Etotheipi

Member
Joined
Mar 21, 2019
Messages
856
Location
somewhere on the complex plane.
I may not be much help but I have been messing around with python for a bit at my school and during the quarantine I have been making a couple side projects like a 3x3 and 4x4 timer. If you felt comfortable posting the full code that would help. Are you coding in an IDE (Pycharm, for example) or on the Command Line?
I'm using Pycharm. I'm comfortable posting all my code, I trust the community and even if I do get plagiarized there would be evidence here I did it, and I am making this for fun, so it wouldn't matter. Here's the code:
Code:
solved_cube = []
cube = []
colors = []


def apply_move(move):
    global cube
    pre_indices = move.pre_indices
    post_indices = move.post_indices
    next_cube = cube
    for i in range(len(pre_indices)):
        next_cube[post_indices[i]-1] = cube[pre_indices[i]-1]
        cube = next_cube


class Move:
    def __init__(self, pre_indices, post_indices):
        self.pre_indices = pre_indices
        self.post_indices = post_indices

    def inverted(self):
        return Move(self.post_indices, self.pre_indices)


def init_defaults():
    global cube
    global solved_cube
    global colors
    colors = ['w', 'y', 'g', 'r', 'b', 'o']

    for x in range(6):
        for y in range(9):
            solved_cube.append(colors[x])
    cube=solved_cube

R = Move([3, 6, 9, 21, 24, 27, 12, 15, 18, 43, 40, 37, 28, 30, 36, 34, 29, 33, 35, 31],
         [43, 40, 37, 3, 6, 9, 21, 24, 27, 12, 15, 18, 30, 36, 34, 28, 33, 35, 31, 29])
init_defaults()


test=Move([10,39,1],[39,10,1])
print(cube[38])
apply_move(test)
print(cube)
 
Last edited:

kubesolver

Premium Member
Joined
Nov 15, 2019
Messages
187
this code is problematic

Code:
next_cube = cube
for i in range(len(pre_indices)):
    next_cube[post_indices-1] = cube[pre_indices-1]
    cube = next_cube
To see why try to understand the follwoing code:
Code:
first = [1,2,3]
second = first
second[0] = 10
print(first)
// [10, 2, 3]
 
C

Cubinwitdapizza

Guest
I would he
I'm using Pycharm. I'm comfortable posting all my code, I trust the community and even if I do get plagiarized there would be evidence here I did it, and I am making this for fun, so it wouldn't matter. Here's the code:
solved_cube = []
cube = []
colors = []


def apply_move(move):
global cube
pre_indices = move.pre_indices
post_indices = move.post_indices
next_cube = cube
for i in range(len(pre_indices)):
next_cube[post_indices-1] = cube[pre_indices-1]
cube = next_cube


class Move:
def __init__(self, pre_indices, post_indices):
self.pre_indices = pre_indices
self.post_indices = post_indices

def inverted(self):
return Move(self.post_indices, self.pre_indices)


def init_defaults():
global cube
global solved_cube
global colors
colors = ['w', 'y', 'g', 'r', 'b', 'o']

for x in range(6):
for y in range(9):
solved_cube.append(colors[x])
cube=solved_cube

R = Move([3, 6, 9, 21, 24, 27, 12, 15, 18, 43, 40, 37, 28, 30, 36, 34, 29, 33, 35, 31],
[43, 40, 37, 3, 6, 9, 21, 24, 27, 12, 15, 18, 30, 36, 34, 28, 33, 35, 31, 29])
init_defaults()


test=Move([10,39,1],[39,10,1])
print(cube[38])
apply_move(test)
print(cube)
Shoot it didnt keep indents, I might have to retry that. Is there a special way to insert code on the forums?
Yes, i believe there is a button somewhere when you post something where u can enter code.
 

Etotheipi

Member
Joined
Mar 21, 2019
Messages
856
Location
somewhere on the complex plane.
this code is problematic

Code:
next_cube = cube
for i in range(len(pre_indices)):
    next_cube[post_indices-1] = cube[pre_indices-1]
    cube = next_cube
To see why try to understand the follwoing code:
Code:
first = [1,2,3]
second = first
second[0] = 10
print(first)
// [10, 2, 3]
Thanks! Ill try again with my code =D How can I set lists equal with out linking them like that? Edit: Nvm I googled how, hopefully it works after I change it.
 
Last edited:
Joined
May 24, 2016
Messages
908
YouTube
Visit Channel
I'm using Pycharm. I'm comfortable posting all my code, I trust the community and even if I do get plagiarized there would be evidence here I did it, and I am making this for fun, so it wouldn't matter. Here's the code:
Code:
solved_cube = []
cube = []
colors = []


def apply_move(move):
    global cube
    pre_indices = move.pre_indices
    post_indices = move.post_indices
    next_cube = cube
    for i in range(len(pre_indices)):
        next_cube[post_indices[i]-1] = cube[pre_indices[i]-1]
        cube = next_cube


class Move:
    def __init__(self, pre_indices, post_indices):
        self.pre_indices = pre_indices
        self.post_indices = post_indices

    def inverted(self):
        return Move(self.post_indices, self.pre_indices)


def init_defaults():
    global cube
    global solved_cube
    global colors
    colors = ['w', 'y', 'g', 'r', 'b', 'o']

    for x in range(6):
        for y in range(9):
            solved_cube.append(colors[x])
    cube=solved_cube

R = Move([3, 6, 9, 21, 24, 27, 12, 15, 18, 43, 40, 37, 28, 30, 36, 34, 29, 33, 35, 31],
         [43, 40, 37, 3, 6, 9, 21, 24, 27, 12, 15, 18, 30, 36, 34, 28, 33, 35, 31, 29])
init_defaults()


test=Move([10,39,1],[39,10,1])
print(cube[38])
apply_move(test)
print(cube)
eww global variables

create a cube class
 
Joined
May 24, 2016
Messages
908
YouTube
Visit Channel
this code is problematic

Code:
next_cube = cube
for i in range(len(pre_indices)):
    next_cube[post_indices-1] = cube[pre_indices-1]
    cube = next_cube
To see why try to understand the follwoing code:
Code:
first = [1,2,3]
second = first
second[0] = 10
print(first)
// [10, 2, 3]
to fix that you can copy the list using

Code:
second = first[:]
 

Etotheipi

Member
Joined
Mar 21, 2019
Messages
856
Location
somewhere on the complex plane.
How do you guys think I should format sequences? I could do something where I just have a list with the moves, then I could just do something like:
Code:
for move in sequence:
     self.move(move)
Which would be really simple, but maybe some other way would be nicer.
 
Top