-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtxt_generation.py
More file actions
36 lines (27 loc) · 1.32 KB
/
txt_generation.py
File metadata and controls
36 lines (27 loc) · 1.32 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
import os
import shutil
#Function to generate txt file from the gameboard
def save_boards_to_txt(user_gameboard, console_gameboard):
# Hidden folder (Linux/macOS or WSL on Windows)
hidden_folder = './.console'
# Create the hidden folder if it doesn't exist
if not os.path.exists(hidden_folder):
os.makedirs(hidden_folder)
gameboards = {"user_gameboard": user_gameboard, "console_gameboard": console_gameboard}
for key, value in gameboards.items():
filename = f"{key}.txt"
with open('gameboard.txt', 'r') as file:
lines = file.readlines()
with open(filename, 'w') as output_file:
for i, line in enumerate(lines):
if i == 0:
output_file.write(line)
else:
parts = line.split('|')
updated_row = '|'.join([parts[0]] + [str(value.board[i-1][j]) if value.board[i-1][j] != 0 else " " for j in range(10)] + ['']) + '\n'
output_file.write(updated_row)
if filename == "console_gameboard.txt":
hidden_file_path = os.path.join(hidden_folder, filename)
shutil.move(filename, hidden_file_path)
if filename == "user_gameboard.txt":
os.system(f"notepad.exe {filename}")