-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
146 lines (139 loc) · 5.45 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
__author__ = 'Bill'
import random
class game():
def __init__(self):
self.list = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
self.length = 4
self.result = False
self.msg = ""
def random(self):
if self.list.count(2048) > 0:
self.result = True
self.msg = "Win!"
else:
x = random.choice([2, self.length])
zero_list = [i for i, value in enumerate(self.list) if value == 0]
if len(zero_list) == 0:
self.result = True
self.msg = "Lost!"
else:
position = random.choice(zero_list)
self.list[position] = x
def move(self, direction):
if direction == "W":
for i in range(self.length):
column = i
templist = []
for j in range(self.length):
row = j
index = row * self.length + column
if self.list[index] != 0:
templist.append(self.list[index])
if len(templist) > 1:
cindex = 0
while cindex < len(templist) - 1:
if templist[cindex] == templist[cindex + 1]:
templist[cindex] *= 2
templist.pop(cindex + 1)
else:
cindex += 1
count = len(templist)
for zeronum in range(self.length-count):
templist.append(0)
for j in range(self.length):
row = j
index = column + row * self.length
self.list[index] = templist[row]
elif direction == "S":
for i in range(self.length-1, -1, -1):
column = i
templist = []
for j in range(self.length-1, -1, -1):
row = j
index = row * self.length + column
if self.list[index] != 0:
templist.append(self.list[index])
if len(templist) > 1:
cindex = 0
while cindex < len(templist) - 1:
if templist[cindex] == templist[cindex + 1]:
templist[cindex] *= 2
templist.pop(cindex + 1)
else:
cindex += 1
count = len(templist)
for zeronum in range(self.length-count):
templist.append(0)
templist.reverse()
for j in range(self.length-1, -1, -1):
row = j
index = column + row * self.length
self.list[index] = templist[row]
elif direction == "A":
for i in range(self.length):
row = i
templist = []
for j in range(self.length):
column = j
index = column + row * self.length
if self.list[index] != 0:
templist.append(self.list[index])
if len(templist) > 1:
cindex = 0
while cindex < len(templist) - 1:
if templist[cindex] == templist[cindex + 1]:
templist[cindex] *= 2
templist.pop(cindex + 1)
else:
cindex += 1
count = len(templist)
for zeronum in range(self.length-count):
templist.append(0)
for j in range(self.length):
column = j
index = column + row * self.length
self.list[index] = templist[column]
elif direction == "D":
for i in range(self.length-1, -1, -1):
row = i
templist = []
for j in range(self.length-1, -1, -1):
column = j
index = column + row * self.length
if self.list[index] != 0:
templist.append(self.list[index])
if len(templist) > 1:
cindex = 0
while cindex < len(templist) - 1:
if templist[cindex] == templist[cindex + 1]:
templist[cindex] *= 2
templist.pop(cindex + 1)
else:
cindex += 1
count = len(templist)
for zeronum in range(self.length-count):
templist.append(0)
templist.reverse()
for j in range(self.length-1, -1, -1):
column = j
index = column + row * self.length
self.list[index] = templist[column]
def show(self):
if self.result:
print self.msg
else:
for i in range(self.length):
row = i
output = ""
for j in range(self.length):
column = j
output += str(self.list[row * self.length + column]).rjust(8)
print output
def run(self, direction):
self.move(direction)
self.random()
self.show()
def start(self):
self.random()
self.random()
self.show()