Skip to content

Commit 82f0605

Browse files
committed
generated mario levels using the lstm model
1 parent 6e4915b commit 82f0605

13 files changed

+26
-26
lines changed

Diff for: 01_preprocess_data.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import os
1111
import json
12-
import numpy
12+
import numpy as np
1313
import matplotlib.pyplot as plt
1414
from tqdm import tqdm_notebook
1515

Diff for: 03_generate_txt_from_model.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
from tensorflow import keras
1515
from tqdm.notebook import tqdm
1616

17-
seed = np.loadtxt('super_mario_as_a_string/data_preprocessed/seed.txt', dtype=float)[:3*17 - 1].copy()
17+
seed = np.loadtxt('./data_preprocessed/seed.txt', dtype=float)[:3*17 - 1].copy()
1818

19-
with open('super_mario_as_a_string/data_preprocessed/ix_to_char.json', 'r') as json_f:
19+
with open('./data_preprocessed/ix_to_char.json', 'r') as json_f:
2020
ix_to_char = json.load(json_f)
2121

22-
with open('super_mario_as_a_string/data_preprocessed/char_to_ix.json', 'r') as json_f:
22+
with open('./data_preprocessed/char_to_ix.json', 'r') as json_f:
2323
char_to_ix = json.load(json_f)
2424

2525
model = keras.models.load_model(
26-
'super_mario_as_a_string/trained_models/mario_lstm.h5',
26+
'./trained_models/mario_lstm.h5',
2727
compile=False
2828
)
2929

@@ -52,7 +52,7 @@ def onehot_to_string(onehot):
5252
print(onehot_to_string(seed))
5353

5454
def get_seed():
55-
seed = np.loadtxt('super_mario_as_a_string/data_preprocessed/seed.txt', dtype=float)[:3*17 - 1]
55+
seed = np.loadtxt('./data_preprocessed/seed.txt', dtype=float)[:3*17 - 1]
5656
seed[17+14] = 0
5757
seed[17+14][char_to_ix['x']] = 1
5858
seed[17*2+14] = 0
@@ -118,6 +118,6 @@ def get_seed():
118118
gen.shape
119119

120120
for i, g in enumerate(gen):
121-
with open(f'super_mario_as_a_string/generated_levels_txt/{i+1}.txt', 'w+') as txt_f:
121+
with open(f'./generated_levels_txt/{i+1}.txt', 'w+') as txt_f:
122122
txt_f.write(onehot_to_string(g))
123123

Diff for: 04_convert_txt_to_png.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@
1313
from PIL import Image
1414
import matplotlib.pyplot as plt
1515

16-
tiles_dir = 'super_mario_as_a_string/tiles'
16+
tiles_dir = './tiles'
1717

1818
chars2pngs = {
19-
"-": Image.open(f'{tiles_dir}/smb-background.png'),
20-
"X": Image.open(f'{tiles_dir}/smb-unpassable.png'),
21-
"S": Image.open(f'{tiles_dir}/smb-breakable.png'),
22-
"?": Image.open(f'{tiles_dir}/smb-question.png'),
23-
"Q": Image.open(f'{tiles_dir}/smb-question.png'),
24-
"o": Image.open(f'{tiles_dir}/smb-coin.png'),
25-
"E": Image.open(f'{tiles_dir}/smb-enemy.png'),
26-
"<": Image.open(f'{tiles_dir}/smb-tube-top-left.png'),
27-
">": Image.open(f'{tiles_dir}/smb-tube-top-right.png'),
28-
"[": Image.open(f'{tiles_dir}/smb-tube-lower-left.png'),
29-
"]": Image.open(f'{tiles_dir}/smb-tube-lower-right.png'),
30-
"x": Image.open(f'{tiles_dir}/smb-path.png'), # self-created
31-
"Y": Image.open(f'{tiles_dir}/Y.png'), # self-created
32-
"N": Image.open(f'{tiles_dir}/N.png'), # self-created
33-
"B": Image.open(f'{tiles_dir}/cannon_top.png'),
34-
"b": Image.open(f'{tiles_dir}/cannon_bottom.png'),
19+
"-": Image.open(f'./tiles/smb-background.png'),
20+
"X": Image.open(f'./tiles/smb-unpassable.png'),
21+
"S": Image.open(f'./tiles/smb-breakable.png'),
22+
"?": Image.open(f'./tiles/smb-question.png'),
23+
"Q": Image.open(f'./tiles/smb-question.png'),
24+
"o": Image.open(f'./tiles/smb-coin.png'),
25+
"E": Image.open(f'./tiles/smb-enemy.png'),
26+
"<": Image.open(f'./tiles/smb-tube-top-left.png'),
27+
">": Image.open(f'./tiles/smb-tube-top-right.png'),
28+
"[": Image.open(f'./tiles/smb-tube-lower-left.png'),
29+
"]": Image.open(f'./tiles/smb-tube-lower-right.png'),
30+
"x": Image.open(f'./tiles/smb-path.png'), # self-created
31+
"Y": Image.open(f'./tiles/Y.png'), # self-created
32+
"N": Image.open(f'./tiles/N.png'), # self-created
33+
"B": Image.open(f'./tiles/cannon_top.png'),
34+
"b": Image.open(f'./tiles/cannon_bottom.png'),
3535
}
3636

3737
def char_array_to_image(array):
@@ -46,10 +46,10 @@ def char_array_to_image(array):
4646
return image
4747

4848
for i in range(10):
49-
with open(f'super_mario_as_a_string/generated_levels_txt/{i+1}.txt', 'r') as txt_f:
49+
with open(f'./generated_levels_txt/{i+1}.txt', 'r') as txt_f:
5050
infile = np.array([list(line.rstrip()) for line in txt_f.readlines()])
5151
plt.figure(figsize=(20, 4))
5252
plt.imshow(char_array_to_image(infile))
5353
plt.axis('off')
54-
plt.savefig(f'super_mario_as_a_string/generated_levels_png/{i}.png', dpi=200, bbox_inches='tight')
54+
plt.savefig(f'./generated_levels_png/{i}.png', dpi=200, bbox_inches='tight')
5555

Diff for: generated-levels/0.png

118 KB
Loading

Diff for: generated-levels/1.png

121 KB
Loading

Diff for: generated-levels/2.png

174 KB
Loading

Diff for: generated-levels/3.png

160 KB
Loading

Diff for: generated-levels/4.png

139 KB
Loading

Diff for: generated-levels/5.png

128 KB
Loading

Diff for: generated-levels/6.png

105 KB
Loading

Diff for: generated-levels/7.png

53.9 KB
Loading

Diff for: generated-levels/8.png

179 KB
Loading

Diff for: generated-levels/9.png

118 KB
Loading

0 commit comments

Comments
 (0)