saving and loading map parser prototype #4

Merged
Mat_02 merged 5 commits from prototype_saves into master 2023-02-20 15:21:48 +01:00
2 changed files with 53 additions and 12 deletions
Showing only changes of commit 56ee3e943c - Show all commits

View File

@ -1,11 +1,20 @@
import os
class MapNotSquareException(Exception):
pass
"""
Matrix used is not a Square and cannot be interpretted as a piece
"""
class PieceNotSquareException(Exception):
pass
class PieceNotSquareException(Exception):
"""
Matrix used is not a Square and cannot be interpretted as a piece
"""
class save_Parser:
def __init__(self, filename:str ):
class SaveParser:
"""
Parser for the game file
"""
def __init__(self):
self.filename = filename
self.map_shape = [[0]]
self.pieces = list()
@ -41,7 +50,7 @@ class save_Parser:
load the file and prepare to parse informations
"""
def save(self):
def save(self, filename):
"""
save parsed info to the file
"""
@ -54,12 +63,45 @@ class save_Parser:
save_data +=len(piece).to_bytes(1, 'big')
save_data += self.shape_to_bytes(piece)
save_data += b'SME'
with open(self.filename, mode='bw') as file:
with open(filename, mode='bw') as file:
file.write(save_data)
def cls():
'clear the screen'
for _ in range(os.get_terminal_size()[1]):
print()
def menu():
"""draw a simple menu to test the SaveParser class"""
P = SaveParser()
print("1) define terrain")
print("2) add a piece")
print("3) show data")
print("4) save data")
print("5) load data")
item = input("Select an option :")
cls()
match item:
case 1:
pass
case 2:
pass
case 3:
pass
case 4:
filename = input('enter the file name (default: default.smap):')
filename = filename if filename else "default.smap"
P.save(filename)
case 5:
filename = input('enter the file name (default: default.smap):')
filename = filename if filename else "default.smap"
P.load(filename)
if __name__ == "__main__":
p = save_Parser('test.map')
p.define_map([[1,0,1],[1,1,1], [1,0,0]])
p.add_piece([[0, 1],[0, 1]])
p.save()
cls()
# p = SaveParser('test.smap')
# p.define_map([[1,0,1],[1,1,1], [1,0,0]])
# p.add_piece([[0, 1],[0, 1]])
# p.save()
# print(p.shape_to_bytes([[1,0,1],[1,1,1], [1,0,0]]))

View File

@ -1 +0,0 @@
SMS|SME