File tree 1 file changed +66
-8
lines changed
1 file changed +66
-8
lines changed Original file line number Diff line number Diff line change 19
19
moves = lines [index ]
20
20
21
21
22
- new_array = [0 ]* len (size_strip )
23
22
24
23
for move in moves :
25
24
if move = 'l'
26
25
new_nums = move_left (nums )
27
- num_empty = sum ([1 for x in frequencies if x == 0 ])
28
- if new_nums == nums :
29
- pos = random () % num_empty
30
- if (random () % 10 ) == 0 :
31
- new_value = 4
32
- else :
33
- new_value = 2
34
26
else :
35
27
new_nums = move_right (nums )
36
28
29
+ #get new random val
30
+ num_empty = sum ([1 for x in frequencies if x == 0 ])
31
+ if new_nums == nums :
32
+ pos = random () % num_empty
33
+ if (random () % 10 ) == 0 :
34
+ new_value = 4
35
+ else :
36
+ new_value = 2
37
+
38
+ #insert it
39
+ empty_counter = 0
40
+ for i ,num in enumerate (new_num ):
41
+ if new_nums [i ] != 0 :
42
+ continue
43
+ if new_nums [i ] == 0 and empty_counter == pos :
44
+ new_nums [i ] = new_value
45
+ else :
46
+ empty_counter += 1
47
+
48
+ nums = new_nums
49
+
37
50
51
+ def move_left (nums ):
52
+ new_nums = [0 ]* len (size_strip )
53
+ inPair = False
54
+ pastNum = 0
55
+ index = 0
56
+ for i , num in enumerate (nums ):
57
+ if num == 0 :
58
+ continue
59
+ else :
60
+ if inPair and pastNum == num :
61
+ new_nums [index ] = pastNum * 2
62
+ index += 1
63
+ elif inPair and pastNum != num :
64
+ new_nums [index ] = pastNum
65
+ new_nums [index + 1 ] = num
66
+ inPair = False
67
+ index += 2
68
+ elif not inPair :
69
+ pastNum = num
70
+ inPair = True
71
+ return new_nums
72
+
73
+ def move_right (nums ):
74
+ new_nums = [0 ]* len (size_strip )
75
+ inPair = False
76
+ pastNum = 0
77
+ index = 0
78
+ pairs = enumerate (nums )
79
+ pairs .reverse ()
80
+ for i , num in pairs :
81
+ if num == 0 :
82
+ continue
83
+ else :
84
+ if inPair and pastNum == num :
85
+ new_nums [index ] = pastNum * 2
86
+ index += 1
87
+ elif inPair and pastNum != num :
88
+ new_nums [index ] = pastNum
89
+ new_nums [index + 1 ] = num
90
+ inPair = False
91
+ index += 2
92
+ elif not inPair :
93
+ pastNum = num
94
+ inPair = True
95
+
38
96
You can’t perform that action at this time.
0 commit comments