2048 of Python implementations. Modified from a Netizen's code, resolved the original Netizen version of the two small bugs:
1. The original game is only eliminated once each time, not recursive elimination. such as [2, 2, 2, 2] The left moving words should be [4, 4, 0, 0], instead of [8, 0, 0, 0]
2. The game end of the detection of bugs, has been corrected.
2048game.py
#-*-Coding:utf-8-*-"" "Created on Tue June 1 14:15:39 2014 @author: Kelvin" "" Import Random class Game2048:t Otalscore = 0 v = [[2, 8, 8, 2], [4, 2, 4, 8], [2, 4, 2, 0], [4, 2, 4, 0]] ' ' v = [[0, 0, 0, 0], [0, 0, 0], 0], [0, 0, 0, 0], [0, 0, 0, 0]] ' def __init__ (self): for I in range (4): self.v[i] = [Random.choice [0,0,0 , 2,2,4]) for x in range (4)] def display (self): print (' {0:4} {1:4} {2:4} {3:4} '. Format (self.v[0][0), self.v[0][1], s ELF.V[0][2], self.v[0][3]) print (' {0:4} {1:4} {2:4} '. Format (3:4], self.v[1][0], self.v[1][1], self.v[1][2] [3]) print (' {0:4} {1:4} {2:4} {3:4} '. Format (self.v[2][0], self.v[2][1], self.v[2][2], self.v[2][3]) print (' {0:4} {1: 4} {2:4} {3:4} '. Format (self.v[3][0], self.v[3][1], self.v[3][2], self.v[3][3]) print (' Can be divided into: {0:4} '. Format ( Self.totalscore)) print (' game is ended: {0:4} '. Format (Self.isover ())) #重新排列 def align (self,vlist, direction): For I in range (Vlist.count (0)): vlist.remove (0) zeRos = [0 for x in range (4-len (vlist))] If direction = "Left": vlist.extend (zeros) else:vlist[:0] = Zeros #将相同 Element adds, returns new integral Def addsame (self,vlist, direction): increment=0 If direction = ' Left ': For i in [0,1,2]: if Vlis T[I]==VLIST[I+1] and Vlist[i+1]!=0:vlist[i] *= 2 vlist[i+1] = 0 Increment + = vlist[i] else:for i in
[3,2,1]: if VLIST[I]==VLIST[I-1] and Vlist[i-1]!=0:vlist[i] *= 2 vlist[i-1] = 0 Increment + = vlist[i] Return increment #处理行和方向, returning add integral def handle (self, vlist, direction): Self.align (vlist, direction) increment = self. Addsame (Vlist, direction) self.align (Vlist, direction) Self.totalscore + + increment #直接加到总值 return increment #判断游戏是
No End def judge (self): if Self.isover (): Print (' You lost, game over! ') return False else:if self.totalscore >= 2048:print (' You win, game over! But you can keep playing. ' Return True #判断游戏是否真正结束 def isover (self): N = self.calccharnumber (0) if N!=0:return False else: For row in range (4): flag = Self.islistover (Self.v[row]) if Flag==false:return False for Col in ran GE (4): # Copy a column in a matrix to a list and then process vlist = [Self.v[row][col] for row in range (4)] flag = Self.islistover (vlist) I F Flag==false:return False return True #判断一个列表是否还可以合并 def islistover (self, vlist): For i in [0,1,2]: if Vlist[i]==vlist[i+1] and Vlist[i+1]!=0:return False return True def calccharnumber (self, char): n = 0 for q in
Self.v:n + = Q.count (char) return n def addelement (self): # statistic empty area number n = Self.calccharnumber (0) if n!=0: # The probability of appearing at 2 and 4 is 3/1 to produce random numbers 2 and 4 num = Random.choice ([2, 2, 2, 4]) # produces random numbers K, the previous step of 2 or 4 will be filled in the K-blank area k = Random.randrange (1 , n+1) #k的范围为 [1,n] N = 0 for I in range (4): for J in Range (4): if self.v[i][j] = = 0:n = 1 if n = = k:self.v[i][j] = num return def moveLeft (self): self.movehorizontal (' left ') def moveright ( Self): Self.movehorizoNtal (' right ') def movehorizontal (self, direction): For row in range (4): Self.handle (Self.v[row), direction) def m Oveup (self): self.movevertical ("left") def MoveDown (self): self.movevertical (' right ') def movevertical (self, directi ON): to Col in range (4): # Copy a column of a matrix into a list and then process vlist = [Self.v[row][col] for row in range (4)] Self.handle (vlist , direction) # Overwrite the value in the original matrix from the number in the processed list for row in range (4): Self.v[row][col] = Vlist[row] #主要的处理函数 def Oper Ation (self): op = input (' operator: ') if op in [' A ', ' a ']: # Move left Self.moveleft () self.addelement () elif op in [' d ', ' d ']: # Move right Self.moveright () self.addelement () elif op in [' W ', ' W ']: # Move Up Self.moveup () Self.addel Ement () elif op in [' s ', ' s ']: # Move Down Self.movedown () self.addelement () else:print (' wrong input. Please enter [w, S, A, D] or its lowercase ' #开始 print (' Input: W (Move up) S (Move Down) A (move left) D (right), press <cr>. ') G =game2048 () flag = True while Tr Ue:g.display () flag = G.judge () g.operation () flag = G.judge ()
Demo diagram
The above mentioned is the entire content of this article, I hope you can enjoy.