Python implements 2048 games and python2048 games
Python 2048. Modified the code of a user to solve two bugs in the original user version:
1. The original game is eliminated only once each time, rather than recursively. For example, if [2, 2, 2] is left, it should be [4, 4, 0, 0] instead of [8, 0, 0, 0].
2. There is a bug in the detection of the end of the game, which has been corrected.
2048game. py
#-*-Coding: UTF-8-*-"Created on Tue Jul 1 14:15:39 2014 @ 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 ([,]) for x in range (4)] def display (self): print ('{0: 4} {3: 4 }'. format (self. v [0] [0], self. v [0] [1], self. v [0] [2], self. v [0] [3]) print }'. format (self. v [1] [0], self. v [1] [1], self. v [1] [2], self. v [1] [3]) print }'. format (self. v [2] [0], self. v [2] [1], self. v [2] [2], self. v [2] [3]) print }'. format (self. v [3] [0], self. v [3] [1], self. v [3] [2], self. v [3] [3]) print ('score: {0: 4 }'. format (self. totalScore )) Print ('whether the game is over: {0: 4 }'. format (self. isOver () # rearrange 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 # Add the same element and return the Add credit def addSame (self, vList, direction ): 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 # process rows and directions, return new points def handle (self, vList, direction): self. align (vList, direction) increment = self. addSame (vList, direction) self. align (vList, direction) self. totalScore + = increment # Add directly to the total value return increment # determine whether the game ends def judge (self): if self. isOver (): print ('You lose, the game is over! ') Return False else: if self. totalScore> = 2048: print (' you win, the game is over! But you can continue to play. ') Return True # determine whether the game really ends 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 range (4): # copy a column in the Matrix to a list. Then, vList = [self. v [row] [col] for row in range (4)] flag = self. isListOver (vList) if flag = False: return False return True # determine whether a list can be combined with def isListOver (self, vList): for I in [, 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): # count the number of blank areas N = self. calcCharNumber (0) if N! = 0: # the probability of occurrence of 2 and 4 is 3/1 to generate random numbers 2 and 4 num = random. choice ([2, 2, 2, 4]) # generates a random number k. The 2 or 4 generated in the previous step will be filled in the k blank area k = random. randrange (1, N + 1) # The range of k is [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): s Elf. handle (self. v [row], direction) def moveUp (self): self. moveVertical ('left') def moveDown (self): self. moveVertical ('right') def moveVertical (self, direction): for col in range (4): # copy a column in the Matrix to a list. Then vList = [self. v [row] [col] for row in range (4)] self. handle (vList, direction) # overwrite the value in the original matrix for row in range (4): self. v [row] [col] = vList [row] # main processing function def operation (self): op = input ('struct' Ator: ') if op in ['A', 'a']: # Move self to the left. moveLeft () self. addElement () elif op in ['D', 'D']: # Move self to the right. moveRight () self. addElement () elif op in ['w', 'w']: # Move self up. moveUp () self. addElement () elif op in ['s ','s']: # Move self down. moveDown () self. addElement () else: print ('incorrect input. Enter [W, S, A, D] or lower case ') # Start print ('input: W (move up) S (move down) A (move left) D (right shift), press <CR>. ') g = game2048 () flag = Truewhile True: g. display () flag = g. judge () g. operation () flag = g. judge ()
Demo Diagram
The above is all the content of this article. I hope you will like it.