Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- table = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
- temp_table = table
- current = [3,3]
- ###############################
- count = 0
- #while count<30:
- # rows = random.randint(0,3)
- # cols = random.randint(0,3)
- # rows1 = random.randint(0,3)
- # cols1 = random.randint(0,3)
- # if ((rows==3)and(cols==3)) or (rows1==3)and(cols1==3):
- # continue
- # else:
- # temp = table[rows][cols]
- # table[rows][cols] = table[rows1][cols1]
- # table[rows1][cols1] = temp
- # count+=1
- ###############################
- def reports():
- print("+----+----+----+----+")
- for i in range(len(table)):
- keep = "|"
- for j in range(len(table[i])):
- if (table[i][j] == 16):
- keep = keep+" |"
- else:
- keep = keep + (" %2d |"%(table[i][j]))
- print(keep)
- print("+----+----+----+----+\n")
- ###############################
- def walk(getWalk):
- temp = [0,0]
- tempValue = int()
- if getWalk == 'w' and current[0]-1 != -1:
- tempValue = table[current[0]-1][current[1]]
- table[current[0]-1][current[1]] = table[current[0]][current[1]]
- table[current[0]][current[1]] = tempValue
- current[0] = current[0]-1
- elif getWalk == 'a' and current[1]-1 != -1:
- tempValue = table[current[0]][current[1]-1]
- table[current[0]][current[1]-1] = table[current[0]][current[1]]
- table[current[0]][current[1]] = tempValue
- current[1] = current[1]-1
- elif getWalk == 's' and current[0]+1 != 4:
- tempValue = table[current[0]+1][current[1]]
- table[current[0]+1][current[1]] = table[current[0]][current[1]]
- table[current[0]][current[1]] = tempValue
- current[0] = current[0]+1
- elif getWalk == 'd' and current[1]+1 != 4:
- tempValue = table[current[0]][current[1]+1]
- table[current[0]][current[1]+1] = table[current[0]][current[1]]
- table[current[0]][current[1]] = tempValue
- current[1] = current[1]+1
- else:
- return False
- return True
- ########################################################################
- def isWin():
- for i in range(4):
- for j in range(4):
- if temp_table[i][j] == table[i][j]:
- if i==3 and j==3 and table[i][j] ==16:
- return True
- else:
- return False
- #####################################################################
- if __name__ == "__main__":
- reports()
- while True:
- if not walk(input("Swap w(up),a(left),s(down),d(right): ")):
- continue
- reports()
- if isWin():
- print("You are winner")
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement