-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvamuum.py
138 lines (110 loc) · 3.63 KB
/
vamuum.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
import os
import random
import math
from time import sleep
from termcolor import colored
class Vacuum():
position = list()
current = list()
dirty = list()
def __init__(self ,*args , **kwargs):
self._Dimension = kwargs.get('dimension')
self.make_position()
self.make_random_dirty(kwargs.get('dirty'))
self.current = [
self.get_random(self._Dimension),
self.get_random(self._Dimension)
]
self.start()
def create_board(self):
os.system('cls')
print(" _",end="")
print(" _"*(self._Dimension-1))
Counter_while=0
while Counter_while!=self._Dimension:
for i in range(self._Dimension+1):
if i !=self._Dimension:
if self.current== [Counter_while,i]:
print("|",end="")
print(colored("X", "cyan"),end= "")
elif [Counter_while,i] in self.dirty:
print("|",end="")
print(colored("D", "yellow"),end= "")
else:
print("|_",end="")
else:
print("|",end= "\n")
Counter_while+=1
def get_random(self, value):
return random.randint(0,value-1)
@property
def up(self):
self.current[0] -=1
@property
def down(self):
self.current[0] +=1
@property
def right(self):
self.current[1] +=1
@property
def left(self):
self.current[1] -=1
def make_position(self):
for i in range(10):
for j in range(10):
self.position.append([i,j])
def clear(self):
self.position.remove(self.current)
def make_random_dirty(self, value):
for i in range(value):
self.dirty.append(
[self.get_random(self._Dimension),
self.get_random(self._Dimension)]
)
def get_least_distance(self):
least_distance= []
for item in self.dirty:
new_distance = math.sqrt(
(self.current[1]-item[1])**2 +
(self.current[0]-item[0])**2)
if least_distance:
least_distance_with_current = math.sqrt(
(self.current[1]-least_distance[1])**2 +
(self.current[0]-least_distance[0])**2)
if not least_distance or new_distance < least_distance_with_current:
least_distance= item
return least_distance
def start(self):
while True:
sleep(0.5)
if self.current in self.dirty:
self.dirty.remove(self.current)
if not self.dirty:
os.system('cls')
input('YOU WIN')
exit(0)
self.create_board()
least_distance = self.get_least_distance()
if least_distance[1] < self.current[1] :
self.left
continue
elif least_distance[1] > self.current[1]:
self.right
continue
if least_distance[0] < self.current[0]:
self.up
continue
elif least_distance[0] > self.current[0]:
self.down
continue
if __name__=='__main__':
os.system('cls')
while True:
try:
dimension= int(input('Dimension? '))
dirty= int(input('\namount of dirts? '))
break
except:
print("PLS ENTER A VALID NUMBER")
continue
Vacuum(dimension=dimension, dirty=dirty)