Skip to content

Commit 21400f5

Browse files
committed
Improve function use, main and release
1 parent fd1ce34 commit 21400f5

File tree

3 files changed

+153
-148
lines changed

3 files changed

+153
-148
lines changed

NetHack_AI.py

+59-55
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,64 @@
33
import time
44
import sys
55

6-
print('Press Space to begin')
7-
keyboard.wait("space")
6+
def main():
7+
print('Press Space to begin')
8+
keyboard.wait("space")
89

9-
while (True):
10-
if keyboard.is_pressed('Esc'):
11-
print("Exiting...")
12-
sys.exit(0)
10+
while (True):
11+
if keyboard.is_pressed('Esc'):
12+
print("Exiting...")
13+
sys.exit(0)
1314

14-
# Generate Int for Decision Making
15-
randInt = random.randint(0, 181)
16-
# Lowercase Letter Inputs
17-
if randInt < 52 :
18-
randInt = randInt % 26
19-
if randInt == 7 : # Keys to avoid (history)
20-
randInt = randInt + 1
21-
key = ord('a') + randInt
22-
keyboard.press(chr(key))
23-
time.sleep(0.1)
24-
keyboard.release(chr(key))
25-
# Uppercase Letter Inputs
26-
elif randInt < 104 :
27-
randInt = randInt % 26
28-
if randInt == 14 or randInt == 18 or randInt == 21: # Keys to avoid (save, options)
29-
randInt = randInt + 1
30-
key = ord('a') + randInt
31-
keyboard.press('shift')
32-
keyboard.press(chr(key))
33-
time.sleep(0.1)
34-
keyboard.release('shift')
35-
keyboard.release(chr(key))
36-
# Directions (numbers)
37-
elif randInt < 154 :
38-
randInt = randInt % 10 #Doesn't put them in order, but gets 0-9 at 5 randInt chances
39-
key = ord('0') + randInt
40-
keyboard.press(chr(key))
41-
time.sleep(0.1)
42-
keyboard.release(chr(key))
43-
# Exit Menus
44-
elif randInt < 169 :
45-
keyboard.press('space')
46-
time.sleep(0.1)
47-
keyboard.release('space')
48-
# Pick up Item
49-
elif randInt < 174 :
50-
keyboard.press(',')
51-
time.sleep(0.1)
52-
keyboard.release(',')
53-
# Direction at Self
54-
elif randInt < 176 :
55-
keyboard.press('.')
56-
time.sleep(0.1)
57-
keyboard.release('.')
58-
else :
59-
keyboard.press('enter')
60-
time.sleep(0.1)
61-
keyboard.release('enter')
62-
time.sleep(0.01)
15+
# Generate Int for Decision Making
16+
randInt = random.randint(0, 181)
17+
# Lowercase Letter Inputs
18+
if randInt < 52 :
19+
randInt = randInt % 26
20+
if randInt == 7 : # Keys to avoid (history)
21+
randInt = randInt + 1
22+
key = ord('a') + randInt
23+
keyboard.press(chr(key))
24+
time.sleep(0.1)
25+
keyboard.release(chr(key))
26+
# Uppercase Letter Inputs
27+
elif randInt < 104 :
28+
randInt = randInt % 26
29+
if randInt == 14 or randInt == 18 or randInt == 21: # Keys to avoid (save, options)
30+
randInt = randInt + 1
31+
key = ord('a') + randInt
32+
keyboard.press('shift')
33+
keyboard.press(chr(key))
34+
time.sleep(0.1)
35+
keyboard.release('shift')
36+
keyboard.release(chr(key))
37+
# Directions (numbers)
38+
elif randInt < 154 :
39+
randInt = randInt % 10 #Doesn't put them in order, but gets 0-9 at 5 randInt chances
40+
key = ord('0') + randInt
41+
keyboard.press(chr(key))
42+
time.sleep(0.1)
43+
keyboard.release(chr(key))
44+
# Exit Menus
45+
elif randInt < 169 :
46+
keyboard.press('space')
47+
time.sleep(0.1)
48+
keyboard.release('space')
49+
# Pick up Item
50+
elif randInt < 174 :
51+
keyboard.press(',')
52+
time.sleep(0.1)
53+
keyboard.release(',')
54+
# Direction at Self
55+
elif randInt < 176 :
56+
keyboard.press('.')
57+
time.sleep(0.1)
58+
keyboard.release('.')
59+
else :
60+
keyboard.press('enter')
61+
time.sleep(0.1)
62+
keyboard.release('enter')
63+
time.sleep(0.01)
64+
65+
if __name__ == '__main__':
66+
main()

Pokemon_AI.py

+35-35
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,41 @@
33
import time
44
import sys
55

6-
print('Press Space to begin')
7-
keyboard.wait("space")
6+
used_keys = [
7+
'w', 'a', 's', 'd', 'x', 'z', 'enter'
8+
]
89

9-
while (True):
10-
if keyboard.is_pressed('Esc'):
11-
print("Exiting...")
12-
sys.exit(0)
10+
def release_all():
11+
for key in used_keys:
12+
keyboard.release(key)
1313

14-
randInt = random.randint(0, 360)
15-
if(randInt < 100):
16-
keyboard.press('z')
17-
time.sleep(0.2)
18-
keyboard.release('z')
19-
elif randInt < 150 :
20-
keyboard.press('x')
21-
time.sleep(0.2)
22-
keyboard.release('x')
23-
elif randInt < 200 :
24-
keyboard.press('w')
25-
time.sleep(0.2)
26-
keyboard.release('w')
27-
elif randInt < 250 :
28-
keyboard.press('a')
29-
time.sleep(0.2)
30-
keyboard.release('a')
31-
elif randInt < 300 :
32-
keyboard.press('s')
33-
time.sleep(0.2)
34-
keyboard.release('s')
35-
elif randInt < 350 :
36-
keyboard.press('d')
37-
time.sleep(0.2)
38-
keyboard.release('d')
39-
else :
40-
keyboard.press('enter')
14+
def main():
15+
print('Press Space to begin')
16+
keyboard.wait("space")
17+
18+
while (True):
19+
if keyboard.is_pressed('Esc'):
20+
print("Exiting...")
21+
release_all()
22+
sys.exit(0)
23+
24+
randInt = random.randint(0, 360)
25+
if(randInt < 100):
26+
keyboard.press('z')
27+
elif randInt < 150 :
28+
keyboard.press('x')
29+
elif randInt < 200 :
30+
keyboard.press('w')
31+
elif randInt < 250 :
32+
keyboard.press('a')
33+
elif randInt < 300 :
34+
keyboard.press('s')
35+
elif randInt < 350 :
36+
keyboard.press('d')
37+
else :
38+
keyboard.press('enter')
4139
time.sleep(0.2)
42-
keyboard.release('enter')
43-
time.sleep(0.01)
40+
release_all()
41+
42+
if __name__ == '__main__':
43+
main()

Spelunky_AI.py

+59-58
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,69 @@
33
import time
44
import sys
55

6+
used_keys = [
7+
'i', 'j', 'k', 'l', 'x', 'z', 's', 'a', 'space'
8+
]
9+
610
def release_all():
7-
keyboard.release('i')
8-
keyboard.release('j')
9-
keyboard.release('k')
10-
keyboard.release('l')
11-
keyboard.release('x')
12-
keyboard.release('z')
13-
keyboard.release('s')
14-
keyboard.release('a')
15-
keyboard.release('space')
11+
for key in used_keys:
12+
keyboard.release(key)
13+
14+
def main():
15+
print('Press Space to begin')
16+
keyboard.wait("space")
1617

17-
print('Press Space to begin')
18-
keyboard.wait("space")
18+
while (True):
19+
if keyboard.is_pressed('Esc'):
20+
print("Exiting...")
21+
release_all()
22+
sys.exit(0)
1923

20-
while (True):
21-
if keyboard.is_pressed('Esc'):
22-
print("Exiting...")
23-
release_all()
24-
sys.exit(0)
24+
# First key press
25+
randInt = random.randint(0, 100)
26+
if(randInt < 10):
27+
keyboard.press('i')
28+
elif randInt < 20 :
29+
keyboard.press('j')
30+
elif randInt < 30 :
31+
keyboard.press('k')
32+
elif randInt < 40 :
33+
keyboard.press('l')
34+
elif randInt < 60 :
35+
keyboard.press('x')
36+
elif randInt < 80 :
37+
keyboard.press('z')
38+
elif randInt < 82 :
39+
keyboard.press('s')
40+
elif randInt < 85 :
41+
keyboard.press('a')
42+
else :
43+
keyboard.press('space')
2544

26-
# First key press
27-
randInt = random.randint(0, 100)
28-
if(randInt < 10):
29-
keyboard.press('i')
30-
elif randInt < 20 :
31-
keyboard.press('j')
32-
elif randInt < 30 :
33-
keyboard.press('k')
34-
elif randInt < 40 :
35-
keyboard.press('l')
36-
elif randInt < 60 :
37-
keyboard.press('x')
38-
elif randInt < 80 :
39-
keyboard.press('z')
40-
elif randInt < 82 :
41-
keyboard.press('s')
42-
elif randInt < 85 :
43-
keyboard.press('a')
44-
else :
45-
keyboard.press('space')
45+
# Second key press
46+
randInt = random.randint(0, 100)
47+
if(randInt < 10):
48+
keyboard.press('i')
49+
elif randInt < 20 :
50+
keyboard.press('j')
51+
elif randInt < 30 :
52+
keyboard.press('k')
53+
elif randInt < 40 :
54+
keyboard.press('l')
55+
elif randInt < 60 :
56+
keyboard.press('x')
57+
elif randInt < 80 :
58+
keyboard.press('z')
59+
elif randInt < 82 :
60+
keyboard.press('s')
61+
elif randInt < 85 :
62+
keyboard.press('a')
63+
else :
64+
keyboard.press('space')
4665

47-
# Second key press
48-
randInt = random.randint(0, 100)
49-
if(randInt < 10):
50-
keyboard.press('i')
51-
elif randInt < 20 :
52-
keyboard.press('j')
53-
elif randInt < 30 :
54-
keyboard.press('k')
55-
elif randInt < 40 :
56-
keyboard.press('l')
57-
elif randInt < 60 :
58-
keyboard.press('x')
59-
elif randInt < 80 :
60-
keyboard.press('z')
61-
elif randInt < 82 :
62-
keyboard.press('s')
63-
elif randInt < 85 :
64-
keyboard.press('a')
65-
else :
66-
keyboard.press('space')
66+
time.sleep(0.1)
6767

68-
time.sleep(0.1)
68+
release_all()
6969

70-
release_all()
70+
if __name__ == '__main__':
71+
main()

0 commit comments

Comments
 (0)