""" To do: 1. Implement user input 2. Change which numbers are used """ from random import randint testBoard = [[0,0,1,2], #used for tests [2,2,2,2], [1,1,2,0], [2,1,0,1]] gameBoard = [[0,0,0,0], #used for actual game [0,0,0,0], [0,0,0,0], [0,0,0,0]] score = 0 def addNumber(board): #adds a number in a random empty spot on specified board free = [] #a list which will be populated with coordinates of all empty spaces for y in range(len(board)): for x in range(len(board[y])): if board[y][x] == 0: free.append([x,y]) #go through each space and add to list "free" if empty if len(free) == 0: return -1,-1,-1 #if no free spaces found, player lost, return -1 n = free[randint(0,len(free)-1)] x = n[0] y = n[1] num = randint(1,2) board[y][x] = num return x,y,num def won(winScore): for y in range(len(board)): for x in range(len(board[y])): if board[y][x] == winScore: return True else: return False def printBoard(board): #prints out the specified board followed by a line for y in board:print y print "------------","Score:",score def moveLeft(board): #move and combine everyithing to the left def pushLeft(y): #moves everything one tile to the left if possible for x in range(1,len(board[y])): #check from left to right if board[y][x] != 0: #if that tile is not empty if board[y][x-1] == 0: #if left adjacent tile is empty board[y][x-1] = board[y][x] #move tile to empty tile board[y][x] = 0 def combineLeft(y): #combines adjacent tiles of equal value into leftmost of the two tiles global score for x in range(1,len(board[y])): #check from left to right if board[y][x] != 0: #if that tile is not empty if board[y][x-1] == board [y][x]: #if left adjacent tile is of equal value board[y][x-1] += board[y][x] #combine both tiles score += board[y][x-1] #adjust score board[y][x] = 0 for y in range(len(board)): for i in range(len(board[y])-1): pushLeft(y) combineLeft(y) for i in range(len(board[y])-1): pushLeft(y) def moveRight(board): #move and combine everything to the right def pushRight(y): #moves everything one tile to the right if possible for n in range(2,len(board[y])+1): x = -n #check from right to left to move everything as far left as possible if board[y][x] != 0: #if that tile is not empty if board[y][x+1] == 0: #if right adjacent tile is empty board[y][x+1] = board[y][x] #move tile to empty tile board[y][x] = 0 def combineRight(y): #combines adjacent tiles of equal value into rightmost of the two tiles global score for n in range(2,len(board[y])+1): x = -n #check from right to left to combine equal tiles if board[y][x] != 0: #if that tile is not empty if board[y][x+1] == board [y][x]: #if right adjacent tile is of equal value board[y][x+1] += board[y][x] #combine both tiles score += board[y][x+1] #adjust score board[y][x] = 0 for y in range(len(board)): for i in range(len(board[y])-1): pushRight(y) combineRight(y) for i in range(len(board[y])-1): pushRight(y) def moveUp(board): #move and combine everything up def pushUp(): #moves everything one tile up if possible for y in range(1,len(board)): #check from top to bottom to move everything as far up as possible if board[y][x] != 0: #if that tile is not empty if board[y-1][x] == 0: #if tile above is empty board[y-1][x] = board[y][x] #move tile to empty tile board[y][x] = 0 def combineUp(): #combines adjacent tiles of equal value into topmost of the two tiles global score for y in range(1,len(board)): #check from top to bottom to combine equal tiles if board[y][x] != 0: #if that tile is not empty if board[y-1][x] == board[y][x]: #if tile above is of equal value board[y-1][x] += board[y][x] #combine both tiles score += board[y-1][x] #adjust score board[y][x] = 0 for x in range(len(board[0])): for i in range(len(board)-1): pushUp() combineUp() for i in range(len(board)-1): pushUp() def moveDown(board): #move and combine everything down def pushDown(): #moves everything one tile down if possible for n in range(2,len(board)+1): y = -n #check from bottom to top to move everything as far down as possible if board[y][x] != 0: #if that tile is not empty if board[y+1][x] == 0: #if tile below is empty board[y+1][x] = board[y][x] #move tile to empty tile board[y][x] = 0 def combineDown(): #combines adjacent tiles of equal value into bottom of the two tiles global score for n in range(2,len(board)+1): y = -n #check from bottom to top to combine equal tiles if board[y][x] != 0: #if that tile is not empty if board[y+1][x] == board[y][x]: #if tile below is of equal value board[y+1][x] += board[y][x] #combine both tiles score += board[y+1][x] #adjust score board[y][x] = 0 for x in range(len(board[0])): for i in range(len(board)-1): pushDown() combineDown() for i in range(len(board)-1): pushDown() #Test stuff printBoard(testBoard) print("left") moveLeft(testBoard) printBoard(testBoard) print("right") moveRight(testBoard) printBoard(testBoard) print("up") moveUp(testBoard) printBoard(testBoard) print("down") moveDown(testBoard) printBoard(testBoard) print("left") moveLeft(testBoard) printBoard(testBoard) print("down") moveDown(testBoard) printBoard(testBoard)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question