Advertisement
Guest User

2048 in python

a guest
Jul 20th, 2014
2,063
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.30 KB | None | 0 0
  1. import random
  2. import time
  3. board = [["_", "_", "_", "_"],["_", "_", "_", "_"],["_", "_", "_", "_"], ["_", "_", "_", "_"]]
  4.  
  5. def spit(list):
  6.     listcount = 0
  7.     while listcount < len(list):
  8.         print list[listcount][0], list[listcount][1], list[listcount][2], list[listcount][3]
  9.         listcount = listcount + 1
  10.    
  11. def newtile(list):
  12.         blank = list[0].count("_") + list[1].count("_") + list[2].count("_") + list[3].count("_")
  13.         position = random.randint(0, blank-1)
  14.         counter = 0
  15.         lcountera = 0
  16.         lcounterb = 0
  17.         while counter < position:
  18.             checking = list[lcountera][lcounterb]
  19.             if checking == "_":
  20.                 list[lcountera][lcounterb] = "-"
  21.                 counter = counter + 1
  22.                 lcounterb = lcounterb + 1
  23.                 if lcounterb == 4:
  24.                     lcountera = lcountera + 1
  25.                     lcounterb = 0
  26.                     if lcountera == 4:
  27.                         lcountera = 0
  28.             elif checking != "_":
  29.                 lcounterb = lcounterb + 1
  30.                 if lcounterb == 4:
  31.                     lcountera = lcountera + 1
  32.                     lcounterb = 0
  33.                     if lcountera == 4:
  34.                         lcountera = 0
  35.                
  36.  
  37.         if counter == position:
  38.             list[lcountera][lcounterb] = "2"
  39.        
  40.  
  41. def fixtiles(list):
  42.     fcountera = 0
  43.     fcounterb = 0
  44.     while fcountera < 4:
  45.         fixing = list[fcountera][fcounterb]
  46.         if fixing == "-":
  47.             list[fcountera][fcounterb] = "_"
  48.         fcounterb = fcounterb + 1
  49.         if fcounterb == 4:
  50.             fcountera = fcountera + 1
  51.             fcounterb = 0
  52. def up(list):
  53.     uplista = [list[0][0], list[1][0], list[2][0], list[3][0]]
  54.     uplistb = [list[0][1], list[1][1], list[2][1], list[3][1]]
  55.     uplistc = [list[0][2], list[1][2], list[2][2], list[3][2]]
  56.     uplistd = [list[0][3], list[1][3], list[2][3], list[3][3]]
  57.    
  58.     acount = uplista.count("_")
  59.     bcount = uplistb.count("_")
  60.     ccount = uplistc.count("_")
  61.     dcount = uplistd.count("_")
  62.  
  63.  
  64.     ac = 0
  65.     bc = 0
  66.     cc = 0
  67.     dc = 0
  68.  
  69.     while ac < acount:
  70.         uplista.remove("_")
  71.         ac = ac + 1
  72.     while bc < bcount:
  73.         uplistb.remove("_")
  74.         bc = bc + 1
  75.     while cc < ccount:
  76.         uplistc.remove("_")
  77.         cc = cc + 1
  78.     while dc < dcount:
  79.         uplistd.remove("_")
  80.         dc = dc + 1
  81.  
  82.  
  83.     while len(uplista) < 4:
  84.         uplista.append("_")
  85.  
  86.     while len(uplistb) < 4:
  87.         uplistb.append("_")
  88.  
  89.     while len(uplistc) < 4:
  90.         uplistc.append("_")
  91.  
  92.     while len(uplistd) < 4:
  93.         uplistd.append("_")
  94.  
  95.    
  96.  
  97.    
  98.     ula = [uplista[0], uplistb[0], uplistc[0], uplistd[0]]
  99.     ulb = [uplista[1], uplistb[1], uplistc[1], uplistd[1]]
  100.     ulc = [uplista[2], uplistb[2], uplistc[2], uplistd[2]]
  101.     uld = [uplista[3], uplistb[3], uplistc[3], uplistd[3]]
  102.    
  103.     list[0] = ula
  104.     list[1] = ulb
  105.     list[2] = ulc
  106.     list[3] = uld
  107.  
  108. def down(list):
  109.     uplista = [list[0][0], list[1][0], list[2][0], list[3][0]]
  110.     uplistb = [list[0][1], list[1][1], list[2][1], list[3][1]]
  111.     uplistc = [list[0][2], list[1][2], list[2][2], list[3][2]]
  112.     uplistd = [list[0][3], list[1][3], list[2][3], list[3][3]]
  113.    
  114.  
  115.     acount = uplista.count("_")
  116.     bcount = uplistb.count("_")
  117.     ccount = uplistc.count("_")
  118.     dcount = uplistd.count("_")
  119.  
  120.     uplista = uplista[::-1]
  121.     uplistb = uplistb[::-1]
  122.     uplistc = uplistc[::-1]
  123.     uplistd = uplistd[::-1]
  124.  
  125.  
  126.     ac = 0
  127.     bc = 0
  128.     cc = 0
  129.     dc = 0
  130.  
  131.     while ac < acount:
  132.         uplista.remove("_")
  133.         ac = ac + 1
  134.     while bc < bcount:
  135.         uplistb.remove("_")
  136.         bc = bc + 1
  137.     while cc < ccount:
  138.         uplistc.remove("_")
  139.         cc = cc + 1
  140.     while dc < dcount:
  141.         uplistd.remove("_")
  142.         dc = dc + 1
  143.  
  144.  
  145.     while len(uplista) < 4:
  146.         uplista.append("_")
  147.  
  148.     while len(uplistb) < 4:
  149.         uplistb.append("_")
  150.  
  151.     while len(uplistc) < 4:
  152.         uplistc.append("_")
  153.  
  154.     while len(uplistd) < 4:
  155.         uplistd.append("_")
  156.  
  157.  
  158.     uplista = uplista[::-1]
  159.     uplistb = uplistb[::-1]
  160.     uplistc = uplistc[::-1]
  161.     uplistd = uplistd[::-1]
  162.  
  163.     ula = []
  164.     ulb = []
  165.     ulc = []
  166.     uld = []
  167.  
  168.     ula = [uplista[0], uplistb[0], uplistc[0], uplistd[0]]
  169.     ulb = [uplista[1], uplistb[1], uplistc[1], uplistd[1]]
  170.     ulc = [uplista[2], uplistb[2], uplistc[2], uplistd[2]]
  171.     uld = [uplista[3], uplistb[3], uplistc[3], uplistd[3]]
  172.    
  173.     list[0] = ula
  174.     list[1] = ulb
  175.     list[2] = ulc
  176.     list[3] = uld
  177.          
  178. def left(list):
  179.     uplista = list[0]
  180.     uplistb = list[1]
  181.     uplistc = list[2]
  182.     uplistd = list[3]
  183.  
  184.     acount = uplista.count("_")
  185.     bcount = uplistb.count("_")
  186.     ccount = uplistc.count("_")
  187.     dcount = uplistd.count("_")
  188.  
  189.  
  190.     ac = 0
  191.     bc = 0
  192.     cc = 0
  193.     dc = 0
  194.  
  195.     while ac < acount:
  196.         uplista.remove("_")
  197.         ac = ac + 1
  198.     while bc < bcount:
  199.         uplistb.remove("_")
  200.         bc = bc + 1
  201.     while cc < ccount:
  202.         uplistc.remove("_")
  203.         cc = cc + 1
  204.     while dc < dcount:
  205.         uplistd.remove("_")
  206.         dc = dc + 1
  207.  
  208.  
  209.     while len(uplista) < 4:
  210.         uplista.append("_")
  211.  
  212.     while len(uplistb) < 4:
  213.         uplistb.append("_")
  214.  
  215.     while len(uplistc) < 4:
  216.         uplistc.append("_")
  217.  
  218.     while len(uplistd) < 4:
  219.         uplistd.append("_")
  220.  
  221.  
  222.    
  223.     list[0] = uplista
  224.     list[1] = uplistb
  225.     list[2] = uplistc
  226.     list[3] = uplistd
  227.  
  228.  
  229. def right(list):
  230.     uplista = list[0]
  231.     uplistb = list[1]
  232.     uplistc = list[2]
  233.     uplistd = list[3]
  234.  
  235.     acount = uplista.count("_")
  236.     bcount = uplistb.count("_")
  237.     ccount = uplistc.count("_")
  238.     dcount = uplistd.count("_")
  239.  
  240.  
  241.     uplista = uplista[::-1]
  242.     uplistb = uplistb[::-1]
  243.     uplistc = uplistc[::-1]
  244.     uplistd = uplistd[::-1]
  245.  
  246.  
  247.     ac = 0
  248.     bc = 0
  249.     cc = 0
  250.     dc = 0
  251.  
  252.     while ac < acount:
  253.         uplista.remove("_")
  254.         ac = ac + 1
  255.     while bc < bcount:
  256.         uplistb.remove("_")
  257.         bc = bc + 1
  258.     while cc < ccount:
  259.         uplistc.remove("_")
  260.         cc = cc + 1
  261.     while dc < dcount:
  262.         uplistd.remove("_")
  263.         dc = dc + 1
  264.  
  265.  
  266.     while len(uplista) < 4:
  267.         uplista.append("_")
  268.  
  269.     while len(uplistb) < 4:
  270.         uplistb.append("_")
  271.  
  272.     while len(uplistc) < 4:
  273.         uplistc.append("_")
  274.  
  275.     while len(uplistd) < 4:
  276.         uplistd.append("_")
  277.  
  278.     uplista = uplista[::-1]
  279.     uplistb = uplistb[::-1]
  280.     uplistc = uplistc[::-1]
  281.     uplistd = uplistd[::-1]
  282.    
  283.     list[0] = uplista
  284.     list[1] = uplistb
  285.     list[2] = uplistc
  286.     list[3] = uplistd
  287.  
  288.  
  289. def upadd(list):
  290.     clm = 0
  291.     while clm < 4:
  292.         if list[0][clm] != "_" and list[0][clm] == list[1][clm]:
  293.             list[0][clm] = str(int(list[0][clm])*2)
  294.             list[1][clm] = "_"
  295.  
  296.         if list[1][clm] != "_" and list[1][clm] == list[2][clm]:
  297.             list[1][clm] = str(int(list[1][clm])*2)
  298.             list[2][clm] = "_"
  299.  
  300.         if list[2][clm] != "_" and list[2][clm] == list[3][clm]:
  301.             list[2][clm] = str(int(list[2][clm])*2)
  302.             list[3][clm] = "_"
  303.  
  304.         clm = clm + 1
  305.  
  306. def downadd(list):
  307.     clm = 0
  308.     while clm < 4:
  309.         if list[3][clm] != "_" and list[3][clm] == list[2][clm]:
  310.             list[3][clm] = str(int(list[3][clm])*2)
  311.             list[2][clm] = "_"
  312.  
  313.         if list[2][clm] != "_" and list[2][clm] == list[1][clm]:
  314.             list[2][clm] = str(int(list[2][clm])*2)
  315.             list[1][clm] = "_"
  316.  
  317.         if list[1][clm] != "_" and list[1][clm] == list[0][clm]:
  318.             list[1][clm] = str(int(list[1][clm])*2)
  319.             list[0][clm] = "_"
  320.  
  321.         clm = clm + 1
  322.  
  323. def rightadd(list):
  324.     row = 0
  325.     while row < 4:
  326.         if list[row][3] != "_" and list[row][3] == list[row][2]:
  327.             list[row][3] = str(int(list[row][3])*2)
  328.             list[row][2] = "_"
  329.  
  330.         if list[row][2] != "_" and list[row][2] == list[row][1]:
  331.             list[row][2] = str(int(list[row][2])*2)
  332.             list[row][1] = "_"
  333.  
  334.         if list[row][1] != "_" and list[row][1] == list[row][0]:
  335.             list[row][1] = str(int(list[row][1])*2)
  336.             list[row][0] = "_"
  337.         row = row + 1
  338.  
  339.  
  340. def leftadd(list):
  341.     row = 0
  342.     while row < 4:
  343.         if list[row][0] != "_" and list[row][0] == list[row][1]:
  344.             list[row][0] = str(int(list[row][0])*2)
  345.             list[row][1] = "_"
  346.  
  347.         if list[row][1] != "_" and list[row][1] == list[row][2]:
  348.             list[row][1] = str(int(list[row][1])*2)
  349.             list[row][2] = "_"
  350.  
  351.         if list[row][2] != "_" and list[row][2] == list[row][3]:
  352.             list[row][2] = str(int(list[row][2])*2)
  353.             list[row][3] = "_"
  354.         row = row + 1
  355.    
  356.        
  357.  
  358. print">>>2048.exe..."
  359. time.sleep(2)
  360. print"..."
  361. time.sleep(2)
  362. print"..."
  363. time.sleep(2)
  364. print"..."
  365. time.sleep(4)
  366.  
  367. print'''
  368. eeeeeeee  eeeeeeeeeeee  8888            eeeeeeeeee  
  369.      88  8888      88  8888            88      88  
  370.      88  8888      88  8888    8888    88eeeeee88  
  371. eeeeee88  8888      88  8888eeee8888  8888      8888
  372. 88        8888      88          8888  8888      8888
  373. 88eeeeee  88eeeeeeee88          8888  8888eeeeee8888
  374.  
  375.                       GGE
  376.                    Good Game
  377.                  Entertainment
  378.                      2014
  379.                          
  380.  
  381. '''
  382.  
  383.  
  384. print'''
  385.  __  __               ______            _      _                    
  386. |  \/  |             |  ____|          (_)    (_)                    
  387. | \ / | __ _ _   _  | |__  __  ___ __  _ _ __ _  ___ _ __   ___ ___
  388. | |\/| |/ _` | | | | |  __| \ \/ / '_ \| | '__| |/ _ \ '_ \ / __/ _ \
  389. | |  | | (_| | |_| | | |____ >  <| |_) | | |  | |  __/ | | | (_|  __/
  390. |_|  |_|\__,_|\__, | |______/_/\_\ .__/|_|_|  |_|\___|_| |_|\___\___|
  391.                __/ |             | |                                
  392.               |___/              |_|
  393.  
  394. '''
  395.  
  396.  
  397. print'''
  398.   _____  _  _        _                
  399.  / ____|| |(_)      | |              
  400. | |  __ | | _   ___ | |__    ___  ___
  401. | | |_ || || | / __|| '_ \ / _ \/ __|
  402. | |__| || || || (__ | | | ||  __/\__ \
  403.  \_____||_||_| \___||_| |_| \___||___/
  404.  
  405. '''
  406. print '''well... thats ironic...
  407.  
  408.  
  409. '''
  410. play = "yes"
  411. print'''
  412.    INSTRUCTIONS:
  413.        up : move all tiles up
  414.        down : all tiles down
  415.        left: you get the idea
  416.        right: you are stupid if you dont get the idea
  417.        quit : quit ze game, obviously....
  418. '''
  419. newtile(board)
  420. fixtiles(board)
  421. spit(board)
  422.  
  423. while play == "yes":
  424.     input = raw_input("Move : ")
  425.     if input == "up":
  426.         up(board)
  427.         upadd(board)
  428.         up(board)
  429.         newtile(board)
  430.         fixtiles(board)
  431.         spit(board)        
  432.     elif input == "down":
  433.         down(board)
  434.         downadd(board)
  435.         down(board)
  436.         newtile(board)
  437.         fixtiles(board)
  438.         spit(board)
  439.     elif input == "left":
  440.         left(board)
  441.         leftadd(board)
  442.         left(board)
  443.         newtile(board)
  444.         fixtiles(board)
  445.         spit(board)
  446.     elif input == "right":
  447.         right(board)
  448.         rightadd(board)
  449.         right(board)
  450.         newtile(board)
  451.         fixtiles(board)
  452.         spit(board)
  453.     elif input == "quit":
  454.         play = "no"
  455.         print "you give up too easy..."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement