Organize the library of competitive professionals ~ Dice ~

I have created a library of dice for ICPC. I'm disappointed that the dice didn't come out in the actual production, but I hope it will be helpful for those who will come out in the future. The implementation of rotation operation etc. is based on Ushi-san's library. Also, the alphabet and rotation functions on each side have names that are easy for you to understand, so please rewrite them for your convenience.

Dice diagram

In the figure below, u on the dice surface is up, d is down, f is front, b is back, l is left, and r is right. In other words, in the figure below, the negative direction of the y-axis represents the front side, and the positive direction of the x-axis represents the right side **. English is also available for other aspects as well.

IMG_0742.jpg

Code description

When placing the dice, ** specify two sides that do not come to the other side ** and how to place the dice is uniquely determined. In addition, there are 24 ways to place the dice, as it is decided by fixing the upper surface and then considering the case of the horizontal surface. The state of the class variable is the one that writes out all 24 ways. In other words, initialization can be performed by giving information on two sides that are known at the time of initialization. Also, if there is no dice with the two sides given by ** initialization, an error is output **. Also note that you need to initialize with the initial position coordinates of the dice.

Once initialized, all that is left is to implement ** 6-direction rotation **. At this time, each Rotate function has an uppercase alphabet at the end to indicate the direction of rotation. The positive direction of the y-axis is North, the negative direction is South, the positive direction of the x-axis is East, and the negative direction is West. And the rotation direction that does not change the coordinates is left direction (Left) and right direction (Right) when viewed from the front. In this function, the faces of the dice are swapped, and the coordinate values are updated for rotations in four directions that change the coordinates.

code

dice.py


# coding: UTF-8
import sys

class Dice:
    #All states of the dice(u,d,f,b,l,order of r)(u+d=f+b=l+r=7 holds)
    state=[[1,6,2,5,4,3],[1,6,3,4,2,5],[1,6,4,3,5,2],[1,6,5,2,3,4],
           [2,5,1,6,3,4],[2,5,3,4,6,1],[2,5,4,3,1,6],[2,5,6,1,4,3],
           [3,4,1,6,5,2],[3,4,2,5,1,6],[3,4,5,2,6,1],[3,4,6,1,2,5],
           [4,3,1,6,2,5],[4,3,2,5,6,1],[4,3,5,2,1,6],[4,3,6,1,5,2],
           [5,2,1,6,4,3],[5,2,3,4,1,6],[5,2,4,3,6,1],[5,2,6,1,3,4],
           [6,1,2,5,3,4],[6,1,3,4,5,2],[6,1,4,3,2,5],[6,1,5,2,4,3]]

    #u,d,f,b,l,0 for each r,1,2,3,4,State1 as 5,Specified in state2
    #value1,value2 is state1 respectively,Faces corresponding to state2
    #now is the coordinates of the initial position
    #Error output if not appropriate
    def __init__(self,state1,value1,state2,value2,now):
        self.now=[now[0],now[1]]
        for i in range(24):
            if Dice.state[i][state1]==value1 and Dice.state[i][state2]==value2:
                self.u,self.d,self.f,self.b,self.l,self.r=Dice.state[i]
                break
        else:
            print('Error:Constructor\'s Argument is missing.',file=sys.stderr)
            exit()

    #Of the y-axis seen from above+Rotate in the direction(The back is the bottom!)
    def RotateN(self):
        self.d,self.f,self.u,self.b=self.b,self.d,self.f,self.u
        self.now[1]+=1

    #Of the y-axis seen from above-Rotate in the direction(The front is the bottom!)
    def RotateS(self):
        self.d,self.f,self.u,self.b=self.f,self.u,self.b,self.d
        self.now[1]-=1

    #X-axis when viewed from above+Rotate in the direction(The right side is the bottom!)
    def RotateE(self):
        self.d,self.l,self.u,self.r=self.r,self.d,self.l,self.u
        self.now[0]+=1

    #X-axis when viewed from above-Rotate in the direction(The left side is the bottom side!)
    def RotateW(self):
        self.d,self.l,self.u,self.r=self.l,self.u,self.r,self.d
        self.now[0]-=1

    #Rotate counterclockwise when viewed from the front(Counterclockwise)
    def RotateL(self):
        self.f,self.l,self.b,self.r=self.r,self.f,self.l,self.b

    #Rotate clockwise when viewed from the front(clockwise)
    def RotateR(self):
        self.f,self.l,self.b,self.r=self.l,self.b,self.r,self.f

Verifyed problem

AOJ Biased Dice Submission → http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=4985962

Recommended Posts

Organize the library of competitive professionals ~ Dice ~
Library organization of competition professionals ~ enumeration of divisors ~
Organize the meaning of methods, classes and objects
Library organization of competition professionals ~ Two-dimensional linear indefinite equation ~
[Python] Get the list of ExifTags names of Pillow library
The beginning of cif2cell
The meaning of self
The story of sys.path.append ()
Revenge of the Types: Revenge of types
Inherit the standard library to find the average value of Queue
Run the intellisense of your own python library with VScode.
Settings to debug the contents of the library with VS Code