Simple 2048 small game

The GUI is not implemented here. If necessary, you can implement it yourself:

Next is the code module, which 2048 games on the original network there are a lot of, I will not write up in detail, are written in the comments inside. The only thing to notice is that we need to know the transpose of the matrix, which we’re going to use here

Import random board = [[0, 0, 0, 0], [0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0] print('{0:4} {1:4} {2:4} {3:4}'.format(board[0][0], board[0][1], board[0][2], board[0][3])) print('{0:4} {1:4} {2:4} {3:4}'.format(board[1][0], board[1][1], board[1][2], board[1][3])) print('{0:4} {1:4} {2:4} {3:4}'.format(board[2][0], board[2][1], board[2][2], Board [2] [3]) print (' {0-4} {and} {then} {3:4} '. The format (board [3] [0], the board [3] [1], the board [3] [2], the board [3] [3]), 'score:' Def init(board): for I in range(4): for j in range(4): Random position = random. Sample (range(0, 15), 2) board[int(randomposition[0] / 4)][randomposition[0] % 4] = 2 board[int(randomposition[1] / 4)][randomposition[1] % 4] = 2 def addSameNumber(boardList, direction): "" Param direction: param direction: Direction == 'right' direction == 'left' return: If direction == 'left': for I in [0, 1, 2]: if boardList[I] == boardList[I +1]! = 0: boardList[i] *= 2 boardList[i + 1] = 0 addNumber += boardList[i] return {'continueRun': True, 'addNumber': AddNumber} return {'continueRun': False, 'addNumber': addNumber} else: for I in [3, 2, 1]: if boardList[i] == boardList[i-1] ! = 0: boardList[i] *= 2 boardList[i - 1] = 0 addNumber += boardList[i] return {'continueRun': True, 'addNumber': addNumber} return {'continueRun': False, 'addNumber': addNumber} def align(boardList, direction): Align non-zero numbers direction == 'left' : left-aligned, for example [8,0,0,2] left-aligned [8,2,0,0] direction == 'right' : Aligned to the right, for example, after,0,0,2 [8] right-aligned,0,8,2 [0] "' 0 # remove list first, such as,0,0,2 [8] - > [8, 2], 1. # boardlist.remove (0) : removes the first match of a value in the list, so [8,0,0,2] removes 0 twice for x in range(boardlist.count (0)): [8,2]->[8,2,0,0] if direction == 'left': boardList.extend([0 for x in range(4 - len(boardList))]) else: boardList[:0] = [0 for x in range(4 - len(boardList))] def handle(boardList, direction): Param boardList: a list structure that stores data in a row (column) :param direction: Direction of movement: use direction 'left' for both up and left, and 'right' for both right and down :return: Return the score added to the row (column). Align (boardList, direction) result = addSameNumber(boardList, direction) # when result['continueRun'] is True While result['continueRun']: # reappend it, then remerge it, Addscore += result['addNumber'] align(boardList, direction) result = addSameNumber(boardList, direction) Def operator(board) def operator(board) def operator(board) def operator(board) def operator(board) def operator(board) def operator(board) def operator(board) def operator(board) # The points added to each operation, AddScore = 0 gameOver = False # default left direction = 'left' op = input(" please input your operation :") if op in ['a', 'A']: = 'left' = 'left' addScore += handle(board[row], direction)['addscore'] elif op in ['d', 'D']: direction = 'right' for row in range(4): addScore += handle(board[row], direction)['addscore'] elif op in ['w', 'W']: Direction = 'left' board = list(list, zip(*board)) addScore += handle(board[row], direction)['addscore'] board = list(map(list, zip(*board))) elif op in ['s', 'S']: Board = list(map(list, zip(*board))) AddScore += handle(board[row], direction)[' addScore '] board = list(map(list, zip(*board)) else: print(" error! Return {'gameOver': gameOver, 'addScore': addScore, 'board': Board} # number = 0 for q on board Number_0 += q.count(0) # number_0 == 0 # number_0 == 0 gameOver = True return {'gameOver': gameOver, 'addScore': addScore, 'board': Board} # add a 2 or 4 to the empty space with a probability of 3:1 else: Addnum = random. Choice ([2,2,2,4]) position_0_list = [] # for I in range(4): if board[i][j] == 0: Position_0_list.append (I *4 + j) # Random Position = random. Sample (position_0_list, 1) board[int(randomposition[0] / 4)][randomposition[0] % 4] = addnum return {'gameOver': gameOver, 'addScore': addScore, 'board': board} if __name__ == '__main__': GameOver = False init(board) score = 0 print(W(top) S(bottom) A(left) D(right). = True: display(board, score) operator_result = operator(board) board = operator_result['board'] if operator_result['gameOver'] == True: Print (" Game over, you lose!" Print (" score :", score) gameOver = operator_result['gameOver'] break else: Score += operator_result['addScore'] if score >= 2048: print( Print (" Your final score :", score) # gameOver = True breakCopy the code