diff --git a/core/Characters/__init__.py b/core/Characters/__init__.py index 81a2070..649a931 100644 --- a/core/Characters/__init__.py +++ b/core/Characters/__init__.py @@ -12,10 +12,11 @@ class Player(): def __init__(self, col, rows, engine): self.id=random.randint(10,100) - self.pos=[random.randint(0,5),random.randint(0,4)] + self.pos=[random.randint(0,4),random.randint(0,3)] self.Engine=engine self.selected=None self.img='P' + self.action_range=1 # it means center+1, so 3*3 grid def move(self, pos): newx=int(pos.split(',')[0]); x=self.pos[0] @@ -41,22 +42,41 @@ def move(self, pos): if not bot.id==self.id: print(css.FAIL+'[ERR]'+css.ENDC+' ID:',bot.id,'already in',newx,',',newy,'!') return - self.pos=[newx, newy] + if newx<=self.pos[0]+self.action_range and newx>=self.pos[0]-self.action_range: + if newy<=self.pos[1]+self.action_range and newy>=self.pos[1]-self.action_range: + self.pos=[newx, newy] + else: + print(errs.ActionRangeError(pos=[newx,newy])); return + else: + print(errs.ActionRangeError(pos=[newx,newy])); return else: if skip=='y': for bot in self.Engine.robots: if bot.pos==[newx, y]: if not bot.id==self.id: print(css.FAIL+'[ERR]'+css.ENDC+' ID:',bot.id,'already in',newx,',',newy,'!') - return - self.pos=[newx, y] + return + # newpos=[newx, y] + if newx<=self.pos[0]+self.action_range and newx>=self.pos[0]-self.action_range: + if y<=self.pos[1]+self.action_range and y>=self.pos[1]-self.action_range: + self.pos=[newx, y] + else: + print(errs.ActionRangeError(pos=[newx, y])); return + else: + print(errs.ActionRangeError(pos=[newx, y])); return else: for bot in self.Engine.robots: if bot.pos==[x, newy]: if not bot.id==self.id: print(css.FAIL+'[ERR]'+css.ENDC+' ID:',bot.id,'already in',newx,',',newy,'!') return - self.pos=[x, newy] + if x<=self.pos[0]+self.action_range and x>=self.pos[0]-self.action_range: + if newy<=self.pos[1]+self.action_range and newy>=self.pos[1]-self.action_range: + self.pos=[x, newy] + else: + print(errs.ActionRangeError(pos=[x,newy])); return + else: + print(errs.ActionRangeError(pos=[x,newy])); return class Kernel(): def __init__(self): diff --git a/core/Characters/__pycache__/__init__.cpython-38.pyc b/core/Characters/__pycache__/__init__.cpython-38.pyc index 6fbef1e..2ac3631 100644 Binary files a/core/Characters/__pycache__/__init__.cpython-38.pyc and b/core/Characters/__pycache__/__init__.cpython-38.pyc differ diff --git a/core/Errors/__init__.py b/core/Errors/__init__.py index deb079f..61066e1 100644 --- a/core/Errors/__init__.py +++ b/core/Errors/__init__.py @@ -30,4 +30,15 @@ def __str__(self): class HackError(Exception): def __init__(self, txt='', *args, **kwargs): - print('\033[91m[ERR]\033[0m Unable to hack.'+txt) \ No newline at end of file + print('\033[91m[ERR]\033[0m Unable to hack.'+txt) + +class ActionRangeError(Exception): + def __init__(self, b=None, pos=[], *a, **k): + super().__init__(*a, **k) + self.b=b + self.pos=pos + def __str__(self): + if self.b: + return "[ERR] Object w/ id: "+repr(self.b.id)+" is too far." + else: + return "[ERR] "+repr(self.pos)+" is too far." \ No newline at end of file diff --git a/core/Errors/__pycache__/__init__.cpython-38.pyc b/core/Errors/__pycache__/__init__.cpython-38.pyc index 279b73d..04b1b13 100644 Binary files a/core/Errors/__pycache__/__init__.cpython-38.pyc and b/core/Errors/__pycache__/__init__.cpython-38.pyc differ diff --git a/main.py b/main.py index 7e6abb3..e6453de 100644 --- a/main.py +++ b/main.py @@ -23,7 +23,7 @@ } class Engine(): - def __init__(self, col=4, rows=5, enems=3): + def __init__(self, col=4, rows=5, enems=1): self.gamepoints=0 # map setup self.columns=col @@ -44,6 +44,8 @@ def __init__(self, col=4, rows=5, enems=3): def run(self): while True: + if not len(self.robots) or (len(self.robots)==1 and self.robots[0]==self.player): + self.win() self.update_map() while self.inbshell: try: @@ -65,7 +67,7 @@ def run(self): self.player.selected.parser(input(css.OKGREEN+'[BOT]'+css.ENDC+css.OKCYAN+'[SCORE:{}]'.format(self.gamepoints)+css.ENDC+' -state:{}- >> '.format(self.player.selected.state))) else: self.show_map() - self.parser(input(css.OKGREEN+'[MAP]'+css.ENDC+css.OKCYAN+'[SCORE:{}]'.format(self.gamepoints)+css.ENDC)) # map parser + self.parser(input(css.OKGREEN+'[MAP]'+css.ENDC+css.OKCYAN+'[SCORE:{}]'.format(self.gamepoints)+css.ENDC+' >> ')) # map parser except errs.ParamError as e: print(str(e)) except errs.SameObjError as e: @@ -113,19 +115,33 @@ def parser(self, x): if bot==self.player: raise errs.SameObjError(bot, self.player) return - self.player.selected=bot + if int(bot.pos[0])>=(int(self.player.pos[0])-self.player.action_range) and int(bot.pos[0])<=(int(self.player.pos[0])+self.player.action_range): + if int(bot.pos[1])>=(int(self.player.pos[1])-self.player.action_range) and int(bot.pos[1])<=(int(self.player.pos[1])+self.player.action_range): + self.player.selected=bot + else: + raise errs.ActionRangeError(bot) + else: + raise errs.ActionRangeError(bot) elif '-pos' in params: v=params[params.index('-pos')+1] for bot in self.robots: if bot.pos==[int(i) for i in v.split(',')]: - self.player.selected=bot + if int(bot.pos[0])>=(int(self.player.pos[0])-self.player.action_range) and int(bot.pos[0])<=(int(self.player.pos[0])+self.player.action_range): + if int(bot.pos[1])>=(int(self.player.pos[1])-self.player.action_range) and int(bot.pos[1])<=(int(self.player.pos[1])+self.player.action_range): + self.player.selected=bot + else: + raise errs.ActionRangeError(bot) + else: + raise errs.ActionRangeError(bot) else: raise errs.ParamError(params) # MOVE elif cmd=='move': self.player.move(params[0]) - elif cmd=='bye': + + # EXIT && BYE + elif cmd in ['bye', 'exit']: print('[?] Are you sure?') c=getch() if c in ['y','s']: @@ -139,6 +155,27 @@ def parser(self, x): else: raise errs.CommandNotFoundError(cmd) + def win(self): + self.gamepoints+=10 + cls_() + titles=[ + '__ _____ _ _ __ _____ _ _ \n\\ \\ / / _ \\| | | | \\ \\ / /_ _| \\ | |\n \\ V / | | | | | | \\ \\ /\\ / / | || \\| |\n | || |_| | |_| | \\ V V / | || |\\ |\n |_| \\___/ \\___/ \\_/\\_/ |___|_| \\_|\n \n', + ' \n# # # # # # # ### ### \n# # # # # # # # # # # \n # # # # # ### # # # \n # # # # # ### # # # \n # # ### # # ### # # \n' + ] + # TODO dump data + print(css.OKGREEN+random.choice(titles)+css.ENDC) + while True: + print('Do you want to start a new game? (y\\n)') + r=getch() + if r.lower()=='y': + main() + elif r.lower()=='n': + print('Exiting...') + time.sleep(1); exit(code=0) + else: + print('[ERR] Invalid Key',r,'.') + continue + def update_map(self): for y in range(len(self.map)): for x in range(len(self.map[y])):