Advertisement
Bkmz

Untitled

Sep 20th, 2011
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.98 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3.  
  4. import pprint as pp
  5.  
  6. # todo rewert to int, or hastag
  7. W = "W"
  8. B = "B"
  9. R = "R"
  10. O = "O"
  11. Y = "Y"
  12. G = "G"
  13.  
  14. Top = "Top"
  15. Bottom = "Bottom"
  16. Right = "Right"
  17. Left = "Left"
  18. Front = "Front"
  19. Back = "Back"
  20.  
  21.  
  22. #using it for turning cube sides
  23. # clockwise
  24. CW  = "0"
  25. # counterclockwise
  26. CCW = "1"
  27.  
  28. class Cube(object):
  29.     """This class provide basic functionality
  30.    of how the cube works"""
  31.  
  32.  
  33.     # provides basics of cube
  34.     polarity_color = (W, Y),(B, G),(O, R)
  35.     polarity_sides = (Top,Bottom),(Left,Right),(Front, Back)
  36.  
  37.     # connections of cube sides
  38.     connection_side = dict()
  39.     connection_side[W] = [R,G,O,B]
  40.     connection_side[B] = [W,R,Y,O]
  41.     connection_side[R] = [W,G,Y,B]
  42.     connection_side[O] = [W,B,Y,G]
  43.     connection_side[Y] = [R,G,O,B]
  44.     connection_side[G] = [W,O,Y,R]
  45.      
  46.     def __init__(self):
  47.         # creating default marking
  48.         self.state = self._defaultMarking()
  49.        
  50.  
  51.         self._check()
  52.         self.turn(Top, CW)
  53.  
  54.  
  55.     """Default marking provide polarity of solved cube"""
  56.     def _defaultMarking(self):
  57.         state = dict()
  58.         state[Front] = [[R,R,R],
  59.                          [R,R,R],
  60.                          [R,R,R]]
  61.  
  62.         state[Back]  = [[O,O,O],
  63.                          [O,O,O],
  64.                          [O,O,O]]
  65.  
  66.         state[Top]    = [[W,W,W],
  67.                           [W,W,W],
  68.                           [W,W,W]]
  69.  
  70.         state[Bottom] = [[Y,Y,Y],
  71.                           [Y,Y,Y],
  72.                           [Y,Y,Y]]
  73.  
  74.         state[Left]  = [[G,G,G],
  75.                          [G,G,G],
  76.                          [G,G,G]]
  77.  
  78.         state[Right] = [[B,B,B],
  79.                          [B,B,B],
  80.                          [B,B,B]]
  81.  
  82.         return state
  83.  
  84.     """Checking state of cube functionality"""
  85.     def _check(self):
  86.         self._checkCenterPolarity()
  87.  
  88.     """Checking only valid of center"""
  89.     def _checkCenterPolarity(self):
  90.         # default we asume that cube are wrong
  91.         flag = 0
  92.  
  93.         for sides in self.polarity_sides:
  94.  
  95.             for colors in self.polarity_color:
  96.                 if (self.state[sides[0]][1][1] == colors[0] and self.state[sides[1]][1][1] == colors[1] or
  97.                     self.state[sides[1]][1][1] == colors[0] and self.state[sides[0]][1][1] == colors[1]):
  98.                     flag += 1
  99.  
  100.         if flag == 3:
  101.             print "Debug:\tCube centers are valid"
  102.             return True
  103.         else:
  104.             raise ValueError("3 Cube centers are not valid. Check it")
  105.  
  106.     """Next, big part of methods it's a movement alghotithms
  107.    First of all we need to implement the basics turning of all sides.
  108.    Lets do it! =)"""
  109.  
  110.     def turn(self, side, cw=CW):
  111.         pass
  112.        
  113.  
  114.     """Pretty Printing of Current Values"""
  115.     def __str__(self):
  116.         out  = "        "+self.state[Top][0][0]+self.state[Top][0][1]+self.state[Top][0][2]+"    \n"
  117.         out += "        "+self.state[Top][1][0]+self.state[Top][1][1]+self.state[Top][1][2]+"    \n"
  118.         out += "        "+self.state[Top][2][0]+self.state[Top][2][1]+self.state[Top][2][2]+"    \n"
  119.  
  120.         out += (self.state[Back][0][0]+self.state[Back][0][1]+self.state[Back][0][2]+"|"+
  121.             self.state[Left][0][0]+self.state[Left][0][1]+self.state[Left][0][2]+"|"+
  122.             self.state[Front][0][0]+self.state[Front][0][1]+self.state[Front][0][2]+"|"+
  123.             self.state[Right][0][0]+self.state[Right][0][1]+self.state[Right][0][2]+"\n")
  124.  
  125.         out += (self.state[Back][1][0]+self.state[Back][1][1]+self.state[Back][1][2]+"|"+
  126.             self.state[Left][1][0]+self.state[Left][1][1]+self.state[Left][1][2]+"|"+
  127.             self.state[Front][1][0]+self.state[Front][1][1]+self.state[Front][1][2]+"|"+
  128.             self.state[Right][1][0]+self.state[Right][1][1]+self.state[Right][1][2]+"\n")
  129.  
  130.         out += (self.state[Back][2][0]+self.state[Back][2][1]+self.state[Back][2][2]+"|"+
  131.             self.state[Left][2][0]+self.state[Left][2][1]+self.state[Left][2][2]+"|"+
  132.             self.state[Front][2][0]+self.state[Front][2][1]+self.state[Front][2][2]+"|"+
  133.             self.state[Right][2][0]+self.state[Right][2][1]+self.state[Right][2][2]+"\n")
  134.  
  135.         out += "        "+self.state[Bottom][0][0]+self.state[Bottom][0][1]+self.state[Bottom][0][2]+"    \n"
  136.         out += "        "+self.state[Bottom][1][0]+self.state[Bottom][1][1]+self.state[Bottom][1][2]+"    \n"
  137.         out += "        "+self.state[Bottom][2][0]+self.state[Bottom][2][1]+self.state[Bottom][2][2]+"    \n"
  138.  
  139.         return out
  140.         # return pp.pformat(self.state)
  141.  
  142.     """Helping to pretty output"""
  143.     def _helpStr(self, side=None):
  144.         if not side == None:
  145.             out = ""
  146.             for i in self.state[side]:
  147.                 for j in i:
  148.                     out += j
  149.                 out += "\n"
  150.         else:
  151.             out = "   \n" * 3
  152.         return out
  153.  
  154.  
  155. if __name__ == "__main__":
  156.     cubic = Cube()
  157.     print cubic
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement