Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # Labyrinth client.
- from protocolBase import *
- from message import *
- import socket as s
- import time, sys
- PORT = 32323
- class labyrinth:
- def __init__(self):
- conn = s.socket(s.AF_INET, s.SOCK_STREAM)
- conn.setsockopt(s.SOL_SOCKET, s.SO_REUSEADDR, 1)
- conn.connect(('localhost', PORT))
- fsock = conn.makefile()
- port = int(fsock.readline())
- # Set up protocol for message passing
- self.pb = protocolBase()
- # Beauty sleep
- time.sleep(0.1)
- self.pb.connect('localhost', port)
- # Interface for navigating the labyrinth
- def north(self):
- """ Sending request to server to go north. """
- self.send("north")
- msg = self.recv()
- return [msg.get(0), msg.get(1), msg.get(2)]
- def south(self):
- """ Sending request to server to go south. """
- self.send("south")
- msg = self.recv()
- return [msg.get(0), msg.get(1), msg.get(2)]
- def east(self):
- """ Sending request to server to go east. """
- self.send("east")
- msg = self.recv()
- return [msg.get(0), msg.get(1), msg.get(2)]
- def west(self):
- """ Sending request to server to go west. """
- self.send("west")
- msg = self.recv()
- return [msg.get(0), msg.get(1), msg.get(2)]
- def look(self):
- """ Sending request to server to look around.
- Recieves status of adjacent tiles from server. """
- self.send("look")
- msg = self.recv()
- msg_tokens = []
- tiles = []
- for i in range(msg.size()):
- msg_tokens.append(msg.get(i))
- for tok in msg_tokens:
- tiles.append(tok.split("|"))
- return tiles
- def disarm(self, direction):
- """ Sending request to disarm an adjacent trap. """
- self.send("disarm " + direction)
- msg = self.recv()
- return msg.get(0)
- def fire(self):
- self.send("fire")
- msg = self.recv()
- return msg.get(0)
- def inventory(self):
- self.send("inventory")
- msg = self.recv()
- return int(msg.get(0))
- # Methods for communicating with the server
- # Nevermind these.
- def send(self, op):
- """ Sends the given operation to the server. """
- tokens = op.split()
- msg = message()
- for token in tokens:
- msg.add(token)
- self.pb.send(msg)
- def recv(self):
- """ Recieves a response from the server. """
- msg = self.pb.recv()
- if msg.get(0) == "timeout":
- print "You failed to find Toby before the time ran out!"
- self.cleanup()
- elif msg.get(0) == "toby":
- print "You found Toby. Good job!"
- self.cleanup()
- elif msg.get(0) == "dead":
- print "You died!"
- self.cleanup()
- return msg
- # Cleanup
- def cleanup(self):
- """ Close connections and notify server that session is over. """
- self.pb.cleanup()
- sys.exit()
- # Test to show some code usage:
- #if __name__ == "__main__":
- # tile is either "tile", "toby" or "wall"
- # trap is either "trap" or "safe"
- # grue is either "grue or "safe"
- # Create a new labyrinth client
- #lab = labyrinth()
- # To navigate the labyrinth use the directional operations:
- # These gives a list of info on return: [tile, trap, grue]
- # These will make you walk in a "circle" in the labyrinth.
- NONE, WALL, BEGN, FNSH, SEEN, GOOD, GRUE = tuple('1023.+4')
- lab = labyrinth()
- SAMPLE = """
- 111141111011111101101011111110
- 101000001011121101001014111011
- 101110111011111101011011111010
- 401010101000010001111011141011
- 101050101111010011111011111010
- 101010101101010110000014111010
- 101110100101040111011011111011
- 101000101101010011111011141001
- 101111111101010000010011111001
- 100000100001011111111111111001
- 111111111101010001000000010001
- 100001011111010111111111111111
- 111401010001010111100040011000
- 101001010111010114101111010011
- 101011010101010111101111011041
- 101001010101010000001111001011
- 101111110101011101101011101011
- 100000010101110101105011111011
- 111111010100000101100001101011
- 101001010114111101111101101011
- 101501010114111001111100001011
- 100001010110101101111111111111
- 111111110110101101100000010000
- 000000010000000001101111011111
- 111111111111111111101001011011
- 100100101111111111101001011011
- 101110101111111111101001011011
- 100000001111431111101011111011
- 111141101111111111101011000001
- 101010101111111111101011141111
- """
- def solve(maze, posx, posy, sizex, sizey): #definerer attributter for kart, posisjon i kart og størrelse på kart
- found = 0 #setter "found" dvs. om toby er funnet til false eller 0
- SeenField = 0
- #print sizex
- #print sizey
- #if posx+1:
- #lab.east()
- #if posx-1:
- #lab.west()
- #if posy+1:
- #lab.south()
- #if posy-1:
- #lab.north()
- if 0 <= posx < sizex and 0 <= posy < sizey: #dersom posisjonen på x-aksen/y-aksen er mindre eller lik 0, og størrelsen på kartet er større enn 0, fortsetter vi
- if maze[posy][posx] in (NONE, BEGN, GRUE): #vi setter startposisjonen til å være y: et punkt definert som none og x: et punkt definert som begin
- if maze[posy][posx] == NONE: #når vi har sett en rute i kartet, og det ikke er en vegg...
- maze[posy][posx] = SEEN #...setter vi denne ruten som "et sett punkt" i lista over punkter vi har sett.
- if maze[posy][posx] == GRUE:
- maze[posy][posx] = SEEN
- a = []
- b = []
- a.insert(0, posx)
- b.insert(0, posy)
- print a
- print b
- #if posx+1:
- #print lab.east()
- #if posx-1:
- #print lab.west()
- #if posy+1:
- #print lab.south()
- #if posy-1:
- #print lab.north()
- print posx, posy #printer hvor vi har vært (antall trekk)
- #if posx+1, posy:
- #print lab.east()
- #if posx-1, posy:
- #print lab.west()
- #if posx, posy+1:
- #print lab.south()
- #if posx, posy-1:
- #print lab.north()
- #if solve(maze, posx+1, posy, sizex, sizey):
- #print lab.east()
- #if solve(maze, posx-1, posy, sizex, sizey):
- #print lab.west()
- #if solve(maze, posx, posy+1, sizex, sizey):
- #print lab.south()
- #if solve(maze, posx, posy-1, sizex, sizey):
- #print lab.north()
- if (solve(maze, posx+1, posy, sizex, sizey) or #sjekker om det er noen ruter nord, syd, øst eller vest for startposisjonen som kan besøkes.
- solve(maze, posx-1, posy, sizex, sizey) or
- solve(maze, posx, posy+1, sizex, sizey) or
- solve(maze, posx, posy-1, sizex, sizey)):
- #print posx, posy
- if maze[posy][posx] == SEEN: #legger til denne posisjonen, og fortsetter.
- maze[posy][posx] = GOOD
- found = 1
- #if posx+1:
- #print lab.east()
- #if posx-1:
- #print lab.west()
- #if posy+1:
- #print lab.south()
- #if posy-1:
- #print lab.north()
- #if solve(maze, posx+1, posy, sizex, sizey):
- #print lab.east()
- #if solve(maze, posx-1, posy, sizex, sizey):
- #print lab.west()
- #if solve(maze, posx, posy+1, sizex, sizey):
- #print lab.south()
- #if solve(maze, posx, posy-1, sizex, sizey):
- #print lab.north()
- elif maze[posy][posx] == FNSH:
- found = 1
- #print len(maze)
- #print SAMPLE.splitlines()
- #print (maze, 0, 1, len(maze[0]), len(maze))
- #print posx, posy
- #print SeenField
- return found
- def main():
- maze = [list(x) for x in SAMPLE.splitlines() if x]
- solve(maze, 14, 1, len(maze[0]), len(maze)) #startposisjon for løser med størrelsen til kart definert.
- for line in maze:
- print "".join(line)
- if __name__ == "__main__":
- main()
- # The look command gives a list of info of your adjacent tiles.
- # The list contains lists as elements; [ [tile, trap, grue], ...]
- # To see examples here is a call to look.
- #for each in lab.look():
- #print each
- # If you find a trap in front of you, you can disarm it with:
- #print "disarm", lab.disarm("east")
- # If you have a grue attached to you, or see one in your way of path
- # you can light a match to be immune to gures for x minutes:
- #lab.fire()
- # You also have the possibility to check the number of matches on you:
- #print lab.inventory()
- # Now go find toby!
- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement