From db8881f6c2b16e86dc7820d585783066015202c3 Mon Sep 17 00:00:00 2001 From: Constantin Berhard Date: Fri, 1 Aug 2014 01:11:29 +0200 Subject: [PATCH] SOKOBAN --- labyrinth.py | 1 + player.py | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/labyrinth.py b/labyrinth.py index 22ff646..c8f3e20 100644 --- a/labyrinth.py +++ b/labyrinth.py @@ -4,6 +4,7 @@ from enum import Enum from functools import reduce directions = {"north": (-1, 0), "east": (0, 1), "west": (0, -1), "south": (1, 0)} +directions_inv = {v:k for k, v in directions.items()} class Labyrinth: class GroundType(Enum): diff --git a/player.py b/player.py index 6d19890..59921cd 100644 --- a/player.py +++ b/player.py @@ -1,6 +1,7 @@ from util import * from thing import Thing from labyrinth import directions as directionOffsets +from labyrinth import directions_inv as directions_t goVerbs = ['go', 'walk'] sayVerbs = ['say', 'talk', 'scream'] @@ -43,6 +44,32 @@ class Player(Thing): def __str__(self): return "another player" + def isMovedBy(self,pusher): + dx = self.field.column - pusher.field.column + dy = self.field.row - pusher.field.row + pushTarget = self.field.neighbor(dx=dx,dy=dy) + travelDistance = 1 + while pushTarget is not None and not pushTarget.isWalkable(): + travelDistance+=1 + pushTarget = pushTarget.neighbor(dx=dx,dy=dy) + if pushTarget is None: + self.send("Another player pushed you out of the playing field.\nYou are dead.\nGood Bye.") + self.game.removePlayer(self) + else: + assert travelDistance >= 1 + if travelDistance == 1: + self.send("Another player pushed you to the "+directions_t[(dy,dx)]+".") + elif travelDistance == 2: + self.send("Another player pushed you SO HARD to the "+directions_t[(dy,dx)]+", that you flew right THROUGH A WALL!\nThis must have been a very rare occurence of quantum tunneling!\nYou feel a bit dizzy.") + else: + self.send("Another player pushed you SO HARD to the "+directions_t[(dy,dx)]+", that you flew through a very thick, gamma-ray proof wall.\nYou are unsure about how you survived this, but quickly remember that you are in a completely virtual world with arbitrary phsyics. This makes you feel comfortable.") + playerToPush = None + for thing in pushTarget.things: + if thing != self and isinstance(thing, Player): + playerToPush = thing # yes, this may have higher speeds than one *grin* + self.send("After coming out of the wall with incredible speed, you hit another player, who is in turn pushed away by your momentum. Poor bastard...") + self.game.labyrinth.moveThing(self,pushTarget,(lambda:playerToPush.isMovedBy(self) if playerToPush else None)) + # high(er)-level functions def readCmd(self, cmd): words = cmd.lower().split() @@ -63,7 +90,14 @@ class Player(Thing): if target is None or not target.isWalkable(): self.send("Sorry, you cannot go there") return - self.game.labyrinth.moveThing(self, target) + playerToPush = None + for thing in target.things: + if thing != self and isinstance(thing, Player): + playerToPush = thing + self.send("By moving to the "+direction[0]+" you pushed another player away.")) + + self.game.labyrinth.moveThing(self, target, (lambda:playerToPush.isMovedBy(self) if playerToPush else None)) + elif verb in sayVerbs: msg = " ".join(words[1:]) for dx, dy in [(0, 0)]+directionOffsets.values(): -- GitLab