Search code examples
pythonpuzzle

How can i print this in python using rows and cols?


In python, I am really confused right now regarding how to apply rows and cols(edit: lists) in order to create this piece. I'm creating a class Piece, this has the four values (left, right, up, and down) and generate at random (with randint(10, 99)). I also want to be able to turn the piece for example:

piece after 0 turns :

   30
83 00 34
   25

piece after 1 turns :

   83
25 00 30
   34

Solution

  • If I understand correctly, I wouldn't worry about rows and columns. My class would generate random values in its constructor method (__init__) to stick in a list (vals = [top, right, bottom, left]) and would have two methods for rotating left and right (rotate_left and rotate_right) and one method for getting the current list back (get_vals).

    import random as rn
    
    class Piece():
    
        def __init__(self):
            #vals = [top, right, bottom, left]        
            self.vals = [rn.randint(10,99) for _ in range(4)]
            self.n = 4
    
        def rotate_left(self):
            self.vals = [self.vals[i-self.n+1] for i in range(self.n)]
    
        def rotate_right(self):
            self.vals = [self.vals[i-1] for i in range(self.n)]
    
        def get_vals(self):
            return self.vals
    

    Rotating with this list is simply shifting the values left or right by one index.

    Edit: This is written with python 3.4 but it works with 2.7 too.