Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.77 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. # Labyrinth client.
  4.  
  5. from protocolBase import *
  6. from message import *
  7. import socket as s
  8. import time, sys
  9. PORT = 32323
  10.  
  11. class labyrinth:
  12.  
  13. def __init__(self):
  14. conn = s.socket(s.AF_INET, s.SOCK_STREAM)
  15. conn.setsockopt(s.SOL_SOCKET, s.SO_REUSEADDR, 1)
  16. conn.connect(('localhost', PORT))
  17.  
  18. fsock = conn.makefile()
  19. port = int(fsock.readline())
  20. # Set up protocol for message passing
  21. self.pb = protocolBase()
  22. # Beauty sleep
  23. time.sleep(0.1)
  24. self.pb.connect('localhost', port)
  25.  
  26. # Interface for navigating the labyrinth
  27. def north(self):
  28. """ Sending request to server to go north. """
  29. self.send("north")
  30. msg = self.recv()
  31.  
  32. return [msg.get(0), msg.get(1), msg.get(2)]
  33.  
  34. def south(self):
  35. """ Sending request to server to go south. """
  36. self.send("south")
  37. msg = self.recv()
  38.  
  39. return [msg.get(0), msg.get(1), msg.get(2)]
  40.  
  41. def east(self):
  42. """ Sending request to server to go east. """
  43. self.send("east")
  44. msg = self.recv()
  45.  
  46. return [msg.get(0), msg.get(1), msg.get(2)]
  47.  
  48. def west(self):
  49. """ Sending request to server to go west. """
  50. self.send("west")
  51. msg = self.recv()
  52.  
  53. return [msg.get(0), msg.get(1), msg.get(2)]
  54.  
  55. def look(self):
  56. """ Sending request to server to look around.
  57. Recieves status of adjacent tiles from server. """
  58. self.send("look")
  59. msg = self.recv()
  60.  
  61. msg_tokens = []
  62. tiles = []
  63.  
  64. for i in range(msg.size()):
  65. msg_tokens.append(msg.get(i))
  66. for tok in msg_tokens:
  67. tiles.append(tok.split("|"))
  68.  
  69. return tiles
  70.  
  71. def disarm(self, direction):
  72. """ Sending request to disarm an adjacent trap. """
  73. self.send("disarm " + direction)
  74. msg = self.recv()
  75.  
  76. return msg.get(0)
  77.  
  78. def fire(self):
  79. self.send("fire")
  80. msg = self.recv()
  81.  
  82. return msg.get(0)
  83.  
  84. def inventory(self):
  85. self.send("inventory")
  86. msg = self.recv()
  87.  
  88. return int(msg.get(0))
  89.  
  90. # Methods for communicating with the server
  91. # Nevermind these.
  92.  
  93. def send(self, op):
  94. """ Sends the given operation to the server. """
  95. tokens = op.split()
  96. msg = message()
  97. for token in tokens:
  98. msg.add(token)
  99. self.pb.send(msg)
  100.  
  101. def recv(self):
  102. """ Recieves a response from the server. """
  103. msg = self.pb.recv()
  104.  
  105. if msg.get(0) == "timeout":
  106. print "You failed to find Toby before the time ran out!"
  107. self.cleanup()
  108. elif msg.get(0) == "toby":
  109. print "You found Toby. Good job!"
  110. self.cleanup()
  111. elif msg.get(0) == "dead":
  112. print "You died!"
  113. self.cleanup()
  114.  
  115. return msg
  116.  
  117. # Cleanup
  118. def cleanup(self):
  119. """ Close connections and notify server that session is over. """
  120. self.pb.cleanup()
  121. sys.exit()
  122.  
  123.  
  124. # Test to show some code usage:
  125. #if __name__ == "__main__":
  126.  
  127. # tile is either "tile", "toby" or "wall"
  128. # trap is either "trap" or "safe"
  129. # grue is either "grue or "safe"
  130.  
  131. # Create a new labyrinth client
  132. #lab = labyrinth()
  133.  
  134. # To navigate the labyrinth use the directional operations:
  135. # These gives a list of info on return: [tile, trap, grue]
  136. # These will make you walk in a "circle" in the labyrinth.
  137. NONE, WALL, BEGN, FNSH, SEEN, GOOD, GRUE = tuple('1023.+4')
  138. lab = labyrinth()
  139. SAMPLE = """
  140. 111141111011111101101011111110
  141. 101000001011121101001014111011
  142. 101110111011111101011011111010
  143. 401010101000010001111011141011
  144. 101050101111010011111011111010
  145. 101010101101010110000014111010
  146. 101110100101040111011011111011
  147. 101000101101010011111011141001
  148. 101111111101010000010011111001
  149. 100000100001011111111111111001
  150. 111111111101010001000000010001
  151. 100001011111010111111111111111
  152. 111401010001010111100040011000
  153. 101001010111010114101111010011
  154. 101011010101010111101111011041
  155. 101001010101010000001111001011
  156. 101111110101011101101011101011
  157. 100000010101110101105011111011
  158. 111111010100000101100001101011
  159. 101001010114111101111101101011
  160. 101501010114111001111100001011
  161. 100001010110101101111111111111
  162. 111111110110101101100000010000
  163. 000000010000000001101111011111
  164. 111111111111111111101001011011
  165. 100100101111111111101001011011
  166. 101110101111111111101001011011
  167. 100000001111431111101011111011
  168. 111141101111111111101011000001
  169. 101010101111111111101011141111
  170. """
  171.  
  172. def solve(maze, posx, posy, sizex, sizey): #definerer attributter for kart, posisjon i kart og størrelse på kart
  173. found = 0 #setter "found" dvs. om toby er funnet til false eller 0
  174. SeenField = 0
  175. #print sizex
  176. #print sizey
  177. #if posx+1:
  178. #lab.east()
  179. #if posx-1:
  180. #lab.west()
  181. #if posy+1:
  182. #lab.south()
  183. #if posy-1:
  184. #lab.north()
  185.  
  186. 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
  187. 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
  188. if maze[posy][posx] == NONE: #når vi har sett en rute i kartet, og det ikke er en vegg...
  189. maze[posy][posx] = SEEN #...setter vi denne ruten som "et sett punkt" i lista over punkter vi har sett.
  190. if maze[posy][posx] == GRUE:
  191. maze[posy][posx] = SEEN
  192. a = []
  193. b = []
  194. a.insert(0, posx)
  195. b.insert(0, posy)
  196. print a
  197. print b
  198. #if posx+1:
  199. #print lab.east()
  200. #if posx-1:
  201. #print lab.west()
  202. #if posy+1:
  203. #print lab.south()
  204. #if posy-1:
  205. #print lab.north()
  206. print posx, posy #printer hvor vi har vært (antall trekk)
  207. #if posx+1, posy:
  208. #print lab.east()
  209. #if posx-1, posy:
  210. #print lab.west()
  211. #if posx, posy+1:
  212. #print lab.south()
  213. #if posx, posy-1:
  214. #print lab.north()
  215. #if solve(maze, posx+1, posy, sizex, sizey):
  216. #print lab.east()
  217. #if solve(maze, posx-1, posy, sizex, sizey):
  218. #print lab.west()
  219. #if solve(maze, posx, posy+1, sizex, sizey):
  220. #print lab.south()
  221. #if solve(maze, posx, posy-1, sizex, sizey):
  222. #print lab.north()
  223. 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.
  224. solve(maze, posx-1, posy, sizex, sizey) or
  225. solve(maze, posx, posy+1, sizex, sizey) or
  226. solve(maze, posx, posy-1, sizex, sizey)):
  227. #print posx, posy
  228. if maze[posy][posx] == SEEN: #legger til denne posisjonen, og fortsetter.
  229. maze[posy][posx] = GOOD
  230. found = 1
  231. #if posx+1:
  232. #print lab.east()
  233. #if posx-1:
  234. #print lab.west()
  235. #if posy+1:
  236. #print lab.south()
  237. #if posy-1:
  238. #print lab.north()
  239. #if solve(maze, posx+1, posy, sizex, sizey):
  240. #print lab.east()
  241. #if solve(maze, posx-1, posy, sizex, sizey):
  242. #print lab.west()
  243. #if solve(maze, posx, posy+1, sizex, sizey):
  244. #print lab.south()
  245. #if solve(maze, posx, posy-1, sizex, sizey):
  246. #print lab.north()
  247. elif maze[posy][posx] == FNSH:
  248. found = 1
  249. #print len(maze)
  250. #print SAMPLE.splitlines()
  251. #print (maze, 0, 1, len(maze[0]), len(maze))
  252. #print posx, posy
  253. #print SeenField
  254. return found
  255.  
  256. def main():
  257. maze = [list(x) for x in SAMPLE.splitlines() if x]
  258. solve(maze, 14, 1, len(maze[0]), len(maze)) #startposisjon for løser med størrelsen til kart definert.
  259. for line in maze:
  260. print "".join(line)
  261.  
  262. if __name__ == "__main__":
  263. main()
  264.  
  265. # The look command gives a list of info of your adjacent tiles.
  266. # The list contains lists as elements; [ [tile, trap, grue], ...]
  267. # To see examples here is a call to look.
  268. #for each in lab.look():
  269. #print each
  270.  
  271. # If you find a trap in front of you, you can disarm it with:
  272. #print "disarm", lab.disarm("east")
  273.  
  274. # If you have a grue attached to you, or see one in your way of path
  275. # you can light a match to be immune to gures for x minutes:
  276. #lab.fire()
  277. # You also have the possibility to check the number of matches on you:
  278. #print lab.inventory()
  279.  
  280.  
  281. # Now go find toby!
  282. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement