Advertisement
Guest User

ctf

a guest
Jun 18th, 2013
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.12 KB | None | 0 0
  1. #!/usr/bin/python
  2. try:
  3.     import psyco        
  4.     psyco.full()
  5. except:
  6.     pass
  7. import sys
  8. import heapq as h      
  9. import copy as c        
  10. import time
  11. import random
  12. import socket
  13. import time
  14.  
  15. choice = 0
  16. dag= {}
  17. win=0
  18.  
  19. md = {1:{1:0,2:1,3:2,4:1,5:2,6:3,7:2,8:3,9:4}, \
  20.       2:{1:1,2:0,3:1,4:2,5:1,6:2,7:3,8:2,9:3}, \
  21.       3:{1:2,2:1,3:0,4:3,5:2,6:1,7:4,8:3,9:2}, \
  22.       4:{1:1,2:2,3:3,4:0,5:1,6:2,7:1,8:2,9:3}, \
  23.       5:{1:2,2:1,3:2,4:1,5:0,6:1,7:2,8:1,9:2}, \
  24.       6:{1:3,2:2,3:1,4:2,5:1,6:0,7:3,8:2,9:1}, \
  25.       7:{1:2,2:3,3:4,4:1,5:2,6:3,7:0,8:1,9:2}, \
  26.       8:{1:3,2:2,3:3,4:2,5:1,6:2,7:1,8:0,9:1}, \
  27.       9:{1:4,2:3,3:2,4:3,5:2,6:1,7:2,8:1,9:0} }
  28.  
  29.  
  30. class Node:
  31.     def __init__(self, contents, cost, hcost = 0):
  32.         self.contents = contents
  33.         self.cost = cost
  34.         self.hcost = hcost
  35.         self.parent = None
  36.     def setParent(self, parent):
  37.         self.parent = parent
  38.     def __le__(self, other):
  39.         return (self.cost+self.hcost) <= (other.cost+other.hcost)
  40. def getKids(node, moves):
  41.     global choice          
  42.     kids = []
  43.     child = Node(None, 0)  
  44.     board = node.contents
  45.     pCost = node.cost      
  46.     for square in board.keys():
  47.         if board[square] == 0:
  48.             for move in moves[square]:
  49.                 temp = board.copy()
  50.                 temp[square] = board[move]
  51.                 temp[move] = board[square]
  52.                 child = Node(temp,pCost+1)
  53.                 if choice is 1:
  54.                     child.hcost = 0
  55.                 elif choice is 2:                    
  56.                     child.hcost += misplaced(child)
  57.                 elif choice is 3:
  58.                     child.hcost += manhattan(child)
  59.                 kids.append(child)                    
  60.     return kids
  61.  
  62.  
  63. def isGoal(node):
  64.     if (node.contents[1] == 1) and \
  65.        (node.contents[2] == 2) and \
  66.        (node.contents[3] == 3) and \
  67.        (node.contents[4] == 4) and \
  68.        (node.contents[5] == 5) and \
  69.        (node.contents[6] == 6) and \
  70.        (node.contents[7] == 7) and \
  71.        (node.contents[8] == 8):
  72.         return True
  73.     else:
  74.         return False
  75.  
  76.  
  77. def getPath(end, start):
  78.     current = c.copy(end)
  79.     path = []
  80.     path.append(end)
  81.     while current.contents != start.contents:
  82.         back = current.parent
  83.         path.append(back)
  84.         current = back
  85.     path.reverse()
  86.     return path
  87.  
  88.  
  89. def printNode(node):
  90.     for i in range(1,4):
  91.     dag[i]=node.contents[i]
  92.     for i in range(4,7):
  93.     dag[i]=node.contents[i]
  94.     for i in range(7,10):
  95.     dag[i]=node.contents[i]
  96.  
  97.  
  98. def misplaced(node):
  99.     distance = 0
  100.     for i in range(1,9):
  101.         if i != node.contents[i]:
  102.             distance += 1
  103.     return distance
  104.  
  105.  
  106. def manhattan(node):
  107.     global md
  108.     distance = 0
  109.     for i in range(1,10):
  110.         if node.contents[i] != 0:
  111.             distance += md[i][node.contents[i]]
  112.     return distance
  113.  
  114. def aStarSearch(board):
  115.     pq = []
  116.     h.heapify(pq)
  117.     moves = {}
  118.     moves[1] = [2,4]
  119.     moves[2] = [1,3,5]
  120.     moves[3] = [2,6]
  121.     moves[4] = [1,5,7]
  122.     moves[5] = [2,4,6,8]
  123.     moves[6] = [3,5,9]
  124.     moves[7] = [4,8]
  125.     moves[8] = [5,7,9]
  126.     moves[9] = [6,8]
  127.     visited = {}
  128.  
  129.     global choice
  130.     start = Node(board, 0)
  131.     if choice is 2:
  132.         start.hcost = misplaced(start)
  133.     elif choice is 3:
  134.         start.hcost = manhattan(start)
  135.  
  136.     time0 = time.time()
  137.     h.heappush(pq, start)
  138.     printNode(start)
  139.     print "\n\n"
  140.  
  141.     maxNodes = 0
  142.  
  143.     while (len(pq)>0):            
  144.  
  145.         if len(pq) > maxNodes:
  146.             maxNodes = len(pq)
  147.            
  148.  
  149.         node = h.heappop(pq)
  150.  
  151.  
  152.         visNode = tuple(node.contents.items())
  153.         if visNode not in visited:
  154.  
  155.             if isGoal(node):
  156.                 return "\n\nGoal!!\n", node, len(visited), maxNodes, \
  157.                        time.time() - time0
  158.             else:
  159.  
  160.                 kids = getKids(node, moves)
  161.                 for child in kids:
  162.                     child.setParent(node)
  163.                     h.heappush(pq, child)
  164.                     visited[visNode] = True
  165.  
  166.  
  167.     return "Sorry Charlie.  No solution.", start, len(visited), maxNodes, \
  168.            time.time() - time0
  169.  
  170. def getzeropos():
  171.     for key,value in dag.iteritems():
  172.     if value==0:
  173.         return key
  174.  
  175.  
  176. def printHelp():
  177.     print "-p\tPath: reconstruct full path to goal"
  178.     sys.exit()
  179. def newgame(sock):
  180.     global win
  181.     data=sock.recv(4096)
  182.     print data
  183.     lines = data.split('\n')
  184.     frow=lines[3].split('|')
  185.     srow=lines[8].split('|')
  186.     trow=lines[13].split('|')
  187.     board = {}
  188.     if frow[1].strip() == '':
  189.         frow[1]='0'
  190.     elif frow[2].strip() == '':
  191.     frow[2]='0'
  192.     elif frow[3].strip() == '':
  193.     frow[3]='0'
  194.     elif srow[1].strip() == '':
  195.     srow[1]='0'
  196.     elif srow[2].strip() == '':
  197.     srow[2]='0'
  198.     elif srow[3].strip() == '':
  199.     srow[3]='0'
  200.     elif trow[1].strip() == '':
  201.     trow[1]='0'
  202.     elif trow[2].strip() == '':
  203.     trow[2]='0'
  204.     elif trow[3].strip() == '':
  205.     trow[3]='0'                                    
  206.     board[1] = int(frow[1].strip())
  207.     board[2] = int(frow[2].strip())
  208.     board[3] = int(frow[3].strip())
  209.     board[4] = int(srow[1].strip())
  210.     board[5] = int(srow[2].strip())
  211.     board[6] = int(srow[3].strip())
  212.     board[7] = int(trow[1].strip())
  213.     board[8] = int(trow[2].strip())
  214.     board[9] = int(trow[3].strip())
  215.     choice=3
  216.     result, goal, visNodes, maxNodes, time = aStarSearch(board)
  217.     zeropos=getzeropos()
  218.     payload=""
  219.     for arg in sys.argv:
  220.         start = Node(board, 0)
  221.         path = getPath(goal, start)
  222.         for i in range(1, len(path)):
  223.             printNode(path[i])
  224.         aftzeropos=getzeropos()
  225.         if aftzeropos > zeropos:
  226.             diff=aftzeropos-zeropos
  227.             if diff == 1:
  228.                 payload=payload+"l"
  229.             elif diff == 3:
  230.                 payload=payload+"u"
  231.         else:
  232.             diff=zeropos-aftzeropos
  233.             if diff == 1:
  234.                 payload=payload+"r"
  235.             elif diff == 3:
  236.                 payload=payload+"d"
  237.         zeropos=aftzeropos
  238.     print payload
  239.     sock.send(payload)
  240.     ee="o"
  241.     while ee:
  242.         ee= sock.recv(4096)
  243.         print ee
  244.         if "The key is" in ee:
  245.             print ee
  246.             sys.exit()
  247.         elif "Solved" in ee:
  248.             print ee
  249.             win=win+1
  250.             print win
  251.             sock.send("k\n")
  252.             newgame(sock)
  253.    
  254. def main():
  255.     global choice,dag,win
  256.     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  257.     sock.connect(("pieceofeight2.shallweplayaga.me",8273))
  258.     data=sock.recv(4096)
  259.     print data
  260.     lines = data.split('\n')
  261.     frow=lines[3].split('|')
  262.     srow=lines[8].split('|')
  263.     trow=lines[13].split('|')
  264.     board = {}
  265.     if frow[1].strip() == '':
  266.         frow[1]='0'
  267.     elif frow[2].strip() == '':
  268.     frow[2]='0'
  269.     elif frow[3].strip() == '':
  270.     frow[3]='0'
  271.     elif srow[1].strip() == '':
  272.     srow[1]='0'
  273.     elif srow[2].strip() == '':
  274.     srow[2]='0'
  275.     elif srow[3].strip() == '':
  276.     srow[3]='0'
  277.     elif trow[1].strip() == '':
  278.     trow[1]='0'
  279.     elif trow[2].strip() == '':
  280.     trow[2]='0'
  281.     elif trow[3].strip() == '':
  282.     trow[3]='0'                                    
  283.     board[1] = int(frow[1].strip())
  284.     board[2] = int(frow[2].strip())
  285.     board[3] = int(frow[3].strip())
  286.     board[4] = int(srow[1].strip())
  287.     board[5] = int(srow[2].strip())
  288.     board[6] = int(srow[3].strip())
  289.     board[7] = int(trow[1].strip())
  290.     board[8] = int(trow[2].strip())
  291.     board[9] = int(trow[3].strip())
  292.     choice=3
  293.     result, goal, visNodes, maxNodes, time1 = aStarSearch(board)
  294.     zeropos=getzeropos()
  295.     payload=""
  296.     for arg in sys.argv:
  297.         start = Node(board, 0)
  298.         path = getPath(goal, start)
  299.         for i in range(1, len(path)):
  300.             printNode(path[i])
  301.         aftzeropos=getzeropos()
  302.         if aftzeropos > zeropos:
  303.             diff=aftzeropos-zeropos
  304.             if diff == 1:
  305.                 payload=payload+"l"
  306.             elif diff == 3:
  307.                 payload=payload+"u"
  308.         else:
  309.             diff=zeropos-aftzeropos
  310.             if diff == 1:
  311.                 payload=payload+"r"
  312.             elif diff == 3:
  313.                 payload=payload+"d"
  314.         zeropos=aftzeropos
  315.     print payload
  316.     sock.send(payload)
  317.     ee="e"
  318.     while ee:
  319.         ee=sock.recv(4096)
  320.         print ee
  321.         if "The key is" in ee:
  322.             print ee
  323.             sys.exit()
  324.         elif "Solved" in ee:
  325.             print ee
  326.             win=win+1
  327.             print win
  328.             sock.send("k\n")
  329.             newgame(sock)
  330. if __name__ == "__main__":
  331.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement