Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. import random
  2.  
  3. table = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
  4. temp_table = table
  5. current = [3,3]
  6. ###############################
  7. count = 0
  8. #while count<30:
  9. # rows = random.randint(0,3)
  10. # cols = random.randint(0,3)
  11. # rows1 = random.randint(0,3)
  12. # cols1 = random.randint(0,3)
  13. # if ((rows==3)and(cols==3)) or (rows1==3)and(cols1==3):
  14. # continue
  15. # else:
  16. # temp = table[rows][cols]
  17. # table[rows][cols] = table[rows1][cols1]
  18. # table[rows1][cols1] = temp
  19. # count+=1
  20. ###############################
  21. def reports():
  22. print("+----+----+----+----+")
  23. for i in range(len(table)):
  24. keep = "|"
  25. for j in range(len(table[i])):
  26. if (table[i][j] == 16):
  27. keep = keep+" |"
  28. else:
  29. keep = keep + (" %2d |"%(table[i][j]))
  30. print(keep)
  31. print("+----+----+----+----+\n")
  32. ###############################
  33. def walk(getWalk):
  34. temp = [0,0]
  35. tempValue = int()
  36. if getWalk == 'w' and current[0]-1 != -1:
  37. tempValue = table[current[0]-1][current[1]]
  38. table[current[0]-1][current[1]] = table[current[0]][current[1]]
  39. table[current[0]][current[1]] = tempValue
  40. current[0] = current[0]-1
  41. elif getWalk == 'a' and current[1]-1 != -1:
  42. tempValue = table[current[0]][current[1]-1]
  43. table[current[0]][current[1]-1] = table[current[0]][current[1]]
  44. table[current[0]][current[1]] = tempValue
  45. current[1] = current[1]-1
  46. elif getWalk == 's' and current[0]+1 != 4:
  47. tempValue = table[current[0]+1][current[1]]
  48. table[current[0]+1][current[1]] = table[current[0]][current[1]]
  49. table[current[0]][current[1]] = tempValue
  50. current[0] = current[0]+1
  51. elif getWalk == 'd' and current[1]+1 != 4:
  52. tempValue = table[current[0]][current[1]+1]
  53. table[current[0]][current[1]+1] = table[current[0]][current[1]]
  54. table[current[0]][current[1]] = tempValue
  55. current[1] = current[1]+1
  56. else:
  57. return False
  58. return True
  59. ########################################################################
  60. def isWin():
  61. for i in range(4):
  62. for j in range(4):
  63. if temp_table[i][j] == table[i][j]:
  64. if i==3 and j==3 and table[i][j] ==16:
  65. return True
  66. else:
  67. return False
  68.  
  69. #####################################################################
  70. if __name__ == "__main__":
  71. reports()
  72. while True:
  73. if not walk(input("Swap w(up),a(left),s(down),d(right): ")):
  74. continue
  75. reports()
  76. if isWin():
  77. print("You are winner")
  78. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement