2048 of Python implementations. Modified from a Netizen's code, resolved the original user version of the two small bugs:
1. The original game is eliminated only once at a time, not recursively. If [2, 2, 2, 2] move left, it should be [4, 4, 0, 0] instead of [8, 0, 0, 0]
2. Detection of the end of the game has a bug, has been corrected.
2048game.py
#-*-Coding:utf-8-*-"" "Created on Tue Jul 1 14:15:39 @author: Kelvin" "" Import random class Game2048:totalscore = 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], self.v[0][2], self.v[0][3])) Print (' {0:4} {1:4} {2:4} {3:4} '. Format (self.v[1][0], self.v[1][1], self.v[1][2], self.v[1][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 (' The game is over: {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 #将相同的元素相加, return new points def addsame (Self,vlist, Direct ION): increment=0 if direction = = ' Left ': For i in [0,1,2]: if vlist[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 #处理行和方向, return new points def handle (self, vList, Direction): Self.align (vList, direction) increment = Self.addsame (vList, direction) self.align (vList, direction) self. Totalscore + = increment #直接加到总值 return increment #判断游戏是否结束 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 RA Nge (4): flag = Self.islistover (Self.v[row]) if Flag==false: Return False for Col in range (4): # Copy a column from the matrix into a list and then process vList = [Self.v[row][col] for row in range (4)] fl AG = Self.islistover (vList) if 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, Cha r): n = 0 for q in Self.v:n + = Q.count (char) return n def addelement (self): # statistics white space number n n = self.calccharnumber (0 If n!=0: # 2 and 4 appear with a chance of 3/1 to produce a random number 2 and 4 num = Random.choice ([2, 2, 2, 4]) # generates a random number k, the previous step of the resulting 2 or 4 will be filled with the K blank area k = RANDOM.R Andrange (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 (sel f): Self.movehorizontal (' right ') def movehorizontal (self, direction): For row in range (4): Self.handle (Self.v[row], di rection) def moveUp (self): Self.movevertical (' left ') def movedown (self): self.movevertical ("right") def movevertical (self, direction): for Col In range (4): # Copy a column from the 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 operation (self): op = Inpu T (' operator: ') if op in [' A ', ' a ']: # Move left Self.moveleft () self.addelement () elif op in [' d ', ' d ']: # Move Right Self.mo Veright () self.addelement () elif op in [' W ', ' W ']: # Move Up Self.moveup () self.addelement () elif op in [' s ', ' s ']: # Move Down Self.movedown () self.addelement () else:print (' wrong input. Enter [w, S, A, D] or lowercase ') #开始print (' Input: W (Move up) S (Move Down) A (shift left) D (shift right), press
.') G =game2048 () flag = Truewhile True:g.display () flag = G.judge () g.operation () flag = G.judge ()
Demo diagram
The above mentioned is the whole content of this article, I hope you can like.