Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import random
- import time
- import turtle as t
- grid=[['']*4,['']*4,['']*4,['']*4]
- max_digits = 5
- # displays the board via command line for debugging
- def gridify(grid):
- rtn_str = "-"*(len(grid[0])*(max_digits + 2) + 5) + "\n"
- for row in grid:
- rtn_str += "|"
- for num in row:
- digits = len(str(num))
- rtn_str += " " * int((max_digits-len(str(num)))/2 + 1) + str(num) + " " * math.ceil((max_digits-len(str(num)))/2 + 1) + "|"
- rtn_str += "\n" + "-"*(len(grid[0])*(max_digits + 2) + 5) + "\n"
- return rtn_str
- # insert a new tile at random location
- def newtile(grid):
- # FILL IN HERE
- # create empty list named empty_spots
- empty_spots =[]
- # loop through up to 4
- for i in range(4):
- # loop through up to 4
- for j in range(4):
- # check if grid at spot is empty
- if grid[i][j] == "":
- # append position to empty_spots
- empty_spots.append((i, j))
- # create variable named spot and choose randomly from empty_spots
- spot = random.choice(empty_spots)
- # replace grid with a random number from [2,2,2,2,4]
- grid[spot[0]][spot[1]] = random.choice([2,2,2,2,4])
- pass
- score = 0
- # moves all tiles left and increments score as necessary
- def left(grid):
- score=0
- for i in range(4):
- # placements are finalized left to right, tracks which spots have combined to prevent double combinations
- combined=[False]*4
- for j in range(4):
- # skip blanks
- if grid[i][j]=="":
- continue
- # pick up number encountered
- moving=grid[i][j]
- grid[i][j]=""
- # move number to the left until an end case is hit
- for spot in range(j,-2,-1):
- # case 1: ran off edge of row - place number in first spot
- if spot==-1:
- grid[i][0]=moving
- break
- # skip blanks
- if grid[i][spot]=="":
- continue
- # case 2: found same number to combine with
- if grid[i][spot]==moving and not combined[spot]:
- grid[i][spot]*=2
- score+=moving*2
- combined [spot]=True
- break
- # case 3: found different number or number is already combined
- grid[i][spot+1]=moving
- break
- return score
- def right(grid):
- pass
- # score=0
- # for i in range(4):
- # combined=[False]*4
- # for j in range(3,-1,-1):
- # FILL IN HERE
- # skip blank
- # pick up number encountered
- # move number right until 4th spot
- # case 1: ran off edge of row - place number in last spot
- # skip blanks
- # case 2: found same number to combine with
- # case 3: found different number or number is already combined
- # return score
- def up(grid):
- score=0
- for j in range(4):
- combined=[False]*4
- for i in range(4):
- if grid[i][j]=="":
- continue
- moving=grid[i][j]
- grid[i][j]=""
- for spot in range(i,-2,-1):
- if spot==-1:
- grid[0][j]=moving
- break
- if grid[spot][j]=="":
- continue
- if grid[spot][j]==moving and not combined[spot]:
- grid[spot][j]*=2
- score+=moving*2
- combined [spot]=True
- break
- grid[spot+1][j]=moving
- break
- return score
- def down(grid):
- # FILL IN HERE
- pass
- # Check whether a particular move is valid (changes the board state)
- def valid_move(grid,move):
- grid2=[row.copy() for row in grid]
- move(grid2)
- if grid2 !=grid:
- return True
- # Command line user input (for debugging or before graphics implementation)
- def user_input(grid):
- while True:
- response=input("Move using W,A,S,D\n").lower()
- if response=="w":
- if valid_move(grid, up):
- return up(grid)
- elif response=="a":
- if valid_move(grid, left):
- return left(grid)
- elif response=="s":
- if valid_move(grid, down):
- return down(grid)
- elif response=="d":
- if valid_move(grid, right):
- return right(grid)
- else:
- print ("Invalid Input Try Again")
- # Check for win condition
- def is_win(grid):
- # FILL IN HERE
- pass
- # loop through rows in grid
- for row in grid
- # check if number 2048 is in the row
- if 2048 in row:
- # return true if number 2048 in row
- return True
- # return False otherwise
- return False
- # Check for loss condition
- def is_lose(grid):
- # FILL IN HERE
- # check if there are any valid moves left (left, right, up, dow
- return not valid_move(grid, left) and not valid_move(grid, right) and not valid_move(grid, up) and not valid_move(grid, down)
- # Graphics helper to draw a square
- def draw_square(size,position,color1,color2):
- # FILL IN HERE
- # bring turtle pen up
- t.penup()
- # put turtle in position
- t.setposition(position)
- # bring pen down
- t.pendown()
- # set pen color to color1 and color2
- t.color(color1, color2)
- # begin the pen fill
- t.begin_fill()
- # Loop up to number 4
- for i in range(4):
- # move forward size amount of steps with turtle
- t.forward(size)
- # turn() right 90 degrees with turtle
- t.right(90)
- # end the pen fill
- t.end_fill()
- # Color scheme choices
- bgcolor="#3873ad"
- gridcolor="#8bb3da"
- tilecolor="#d8e6f3"
- # Draw background grid
- def drawgrid():
- draw_square(640,(-320,320),bgcolor,bgcolor)
- for i in range(4):
- for j in range(4):
- draw_square(140,(-320+16+156*j,320-16-156*i),gridcolor,gridcolor)
- # Helper to change color of tile
- def change_tile_color(number, color):
- # FILL IN HERE
- pass
- # Helper to draw a tile with a number
- def draw_tile(number,position):
- # FILL IN HERE
- # draw square with size of 140, position, and tilecolor
- draw_square(140, position, tilecolor, tilecolor)
- # set color of turtle to blue and gold
- t.color("blue","gold")
- # bring the pen up
- t.penup()
- # put turtle in x=position[0] + 70, y = position[1] - 107
- t.setposition(position[0] + 70, position[1] - 107)
- # put the pen down
- t.pendown()
- # write the number with turtle
- t.write(number, align="center", font=("Ubuntu", 48, "normal"))
- # Can_move prevents the user from spamming buttons before the graphics have finished updating
- can_move=True
- # Render the grid on the background
- def render():
- t.clear()
- drawgrid()
- for i in range(4):
- for j in range(4):
- if grid[i][j]!="":
- draw_tile(grid[i][j],(-320+16+156*j,320-16-156*i))
- global can_move
- t.penup()
- t.goto((320,320))
- t.pendown()
- t.write("Score:"+str(score),align="right",font=("Ubuntu",60,"normal"))
- can_move=True
- t.update()
- # Event handlers for the different directions
- def handle_left():
- global can_move
- if can_move and valid_move(grid,left):
- can_move=False
- global score
- score+=left(grid)
- newtile(grid)
- render()
- if is_win (grid):
- t.goto((0,0))
- t.write("You Win!",align="center",font=("Ubuntu",60,"normal"))
- time.sleep(10)
- t.bye()
- if is_lose (grid):
- t.write("You lose!",align="center",font=("Ubuntu",60,"normal"))
- t.update()
- time.sleep(10)
- t.bye()
- def handle_right():
- global can_move
- # FILL IN HERE
- def handle_up():
- global can_move
- if can_move and valid_move(grid,up):
- can_move=False
- global score
- score+=up(grid)
- newtile(grid)
- render()
- if is_win (grid):
- t.goto((0,0))
- t.write("You Win!",align="center",font=("Ubuntu",60,"normal"))
- time.sleep(10)
- t.bye()
- if is_lose (grid):
- t.write("You lose!",align="center",font=("Ubuntu",60,"normal"))
- t.update()
- time.sleep(10)
- t.bye()
- def handle_down():
- global can_move
- # FILL IN HERE
- # Set up the game
- newtile(grid)
- newtile(grid)
- t.ht()
- t.getscreen().delay(0)
- t.tracer(0,0)
- render()
- t.onkey(handle_left,"Left")
- t.onkey(handle_right,"Right")
- t.onkey(handle_up,"Up")
- t.onkey(handle_down,"Down")
- t.listen()
- t.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement