Repository navigation
Expand file tree
/
Copy pathconvert_map.py
More file actions
76 lines (59 loc) · 2.43 KB
/
Copy pathconvert_map.py
File metadata and controls
76 lines (59 loc) · 2.43 KB
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
#!/usr/bin/env python3
"""
Python script to convert map.txt to a C++ 2D array definition file.
Reads a 300x300 map and outputs it in C++ format: {{}, {}} 2D array
"""
def read_map_file(input_file):
"""Read the map file and return a 2D list of integers."""
map_data = []
with open(input_file, 'r') as file:
for line in file:
line = line.strip()
if line: # Skip empty lines
# Convert each character to integer
row = [int(char) for char in line]
map_data.append(row)
# if map_data:
# transposed = [[map_data[j][i] for j in range(len(map_data))] for i in range(len(map_data[0]))]
# return transposed
return map_data
def write_cpp_array(map_data, output_file):
"""Write the map data as a C++ 2D array definition."""
rows = len(map_data)
cols = len(map_data[0]) if rows > 0 else 0
with open(output_file, 'w') as file:
# Write header comment
file.write(f"// Auto-generated C++ map array from map.txt\n")
file.write(f"// Map dimensions: {rows} x {cols}\n")
file.write(f"// Generated by convert_map.py\n\n")
# Write array declaration
file.write(f"int map[{rows}][{cols}] = {{\n")
# Write each row
for i, row in enumerate(map_data):
file.write(" {")
# Write each value in the row
for j, value in enumerate(row):
file.write(str(value))
if j < len(row) - 1: # Not the last element in row
file.write(", ")
file.write("}")
if i < len(map_data) - 1: # Not the last row
file.write(",")
file.write("\n")
file.write("};\n")
def main():
input_file = "src/app/final/map.txt"
output_file = "map_array.cpp"
try:
print(f"Reading map from: {input_file}")
map_data = read_map_file(input_file)
print(f"Map dimensions: {len(map_data)} x {len(map_data[0]) if map_data else 0}")
print(f"Writing C++ array to: {output_file}")
write_cpp_array(map_data, output_file)
print("Conversion completed successfully!")
except FileNotFoundError:
print(f"Error: Could not find input file '{input_file}'")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()