Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- try:
- import psyco
- psyco.full()
- except:
- pass
- import sys
- import heapq as h
- import copy as c
- import time
- import random
- import socket
- import time
- choice = 0
- dag= {}
- win=0
- md = {1:{1:0,2:1,3:2,4:1,5:2,6:3,7:2,8:3,9:4}, \
- 2:{1:1,2:0,3:1,4:2,5:1,6:2,7:3,8:2,9:3}, \
- 3:{1:2,2:1,3:0,4:3,5:2,6:1,7:4,8:3,9:2}, \
- 4:{1:1,2:2,3:3,4:0,5:1,6:2,7:1,8:2,9:3}, \
- 5:{1:2,2:1,3:2,4:1,5:0,6:1,7:2,8:1,9:2}, \
- 6:{1:3,2:2,3:1,4:2,5:1,6:0,7:3,8:2,9:1}, \
- 7:{1:2,2:3,3:4,4:1,5:2,6:3,7:0,8:1,9:2}, \
- 8:{1:3,2:2,3:3,4:2,5:1,6:2,7:1,8:0,9:1}, \
- 9:{1:4,2:3,3:2,4:3,5:2,6:1,7:2,8:1,9:0} }
- class Node:
- def __init__(self, contents, cost, hcost = 0):
- self.contents = contents
- self.cost = cost
- self.hcost = hcost
- self.parent = None
- def setParent(self, parent):
- self.parent = parent
- def __le__(self, other):
- return (self.cost+self.hcost) <= (other.cost+other.hcost)
- def getKids(node, moves):
- global choice
- kids = []
- child = Node(None, 0)
- board = node.contents
- pCost = node.cost
- for square in board.keys():
- if board[square] == 0:
- for move in moves[square]:
- temp = board.copy()
- temp[square] = board[move]
- temp[move] = board[square]
- child = Node(temp,pCost+1)
- if choice is 1:
- child.hcost = 0
- elif choice is 2:
- child.hcost += misplaced(child)
- elif choice is 3:
- child.hcost += manhattan(child)
- kids.append(child)
- return kids
- def isGoal(node):
- if (node.contents[1] == 1) and \
- (node.contents[2] == 2) and \
- (node.contents[3] == 3) and \
- (node.contents[4] == 4) and \
- (node.contents[5] == 5) and \
- (node.contents[6] == 6) and \
- (node.contents[7] == 7) and \
- (node.contents[8] == 8):
- return True
- else:
- return False
- def getPath(end, start):
- current = c.copy(end)
- path = []
- path.append(end)
- while current.contents != start.contents:
- back = current.parent
- path.append(back)
- current = back
- path.reverse()
- return path
- def printNode(node):
- for i in range(1,4):
- dag[i]=node.contents[i]
- for i in range(4,7):
- dag[i]=node.contents[i]
- for i in range(7,10):
- dag[i]=node.contents[i]
- def misplaced(node):
- distance = 0
- for i in range(1,9):
- if i != node.contents[i]:
- distance += 1
- return distance
- def manhattan(node):
- global md
- distance = 0
- for i in range(1,10):
- if node.contents[i] != 0:
- distance += md[i][node.contents[i]]
- return distance
- def aStarSearch(board):
- pq = []
- h.heapify(pq)
- moves = {}
- moves[1] = [2,4]
- moves[2] = [1,3,5]
- moves[3] = [2,6]
- moves[4] = [1,5,7]
- moves[5] = [2,4,6,8]
- moves[6] = [3,5,9]
- moves[7] = [4,8]
- moves[8] = [5,7,9]
- moves[9] = [6,8]
- visited = {}
- global choice
- start = Node(board, 0)
- if choice is 2:
- start.hcost = misplaced(start)
- elif choice is 3:
- start.hcost = manhattan(start)
- time0 = time.time()
- h.heappush(pq, start)
- printNode(start)
- print "\n\n"
- maxNodes = 0
- while (len(pq)>0):
- if len(pq) > maxNodes:
- maxNodes = len(pq)
- node = h.heappop(pq)
- visNode = tuple(node.contents.items())
- if visNode not in visited:
- if isGoal(node):
- return "\n\nGoal!!\n", node, len(visited), maxNodes, \
- time.time() - time0
- else:
- kids = getKids(node, moves)
- for child in kids:
- child.setParent(node)
- h.heappush(pq, child)
- visited[visNode] = True
- return "Sorry Charlie. No solution.", start, len(visited), maxNodes, \
- time.time() - time0
- def getzeropos():
- for key,value in dag.iteritems():
- if value==0:
- return key
- def printHelp():
- print "-p\tPath: reconstruct full path to goal"
- sys.exit()
- def newgame(sock):
- global win
- data=sock.recv(4096)
- print data
- lines = data.split('\n')
- frow=lines[3].split('|')
- srow=lines[8].split('|')
- trow=lines[13].split('|')
- board = {}
- if frow[1].strip() == '':
- frow[1]='0'
- elif frow[2].strip() == '':
- frow[2]='0'
- elif frow[3].strip() == '':
- frow[3]='0'
- elif srow[1].strip() == '':
- srow[1]='0'
- elif srow[2].strip() == '':
- srow[2]='0'
- elif srow[3].strip() == '':
- srow[3]='0'
- elif trow[1].strip() == '':
- trow[1]='0'
- elif trow[2].strip() == '':
- trow[2]='0'
- elif trow[3].strip() == '':
- trow[3]='0'
- board[1] = int(frow[1].strip())
- board[2] = int(frow[2].strip())
- board[3] = int(frow[3].strip())
- board[4] = int(srow[1].strip())
- board[5] = int(srow[2].strip())
- board[6] = int(srow[3].strip())
- board[7] = int(trow[1].strip())
- board[8] = int(trow[2].strip())
- board[9] = int(trow[3].strip())
- choice=3
- result, goal, visNodes, maxNodes, time = aStarSearch(board)
- zeropos=getzeropos()
- payload=""
- for arg in sys.argv:
- start = Node(board, 0)
- path = getPath(goal, start)
- for i in range(1, len(path)):
- printNode(path[i])
- aftzeropos=getzeropos()
- if aftzeropos > zeropos:
- diff=aftzeropos-zeropos
- if diff == 1:
- payload=payload+"l"
- elif diff == 3:
- payload=payload+"u"
- else:
- diff=zeropos-aftzeropos
- if diff == 1:
- payload=payload+"r"
- elif diff == 3:
- payload=payload+"d"
- zeropos=aftzeropos
- print payload
- sock.send(payload)
- ee="o"
- while ee:
- ee= sock.recv(4096)
- print ee
- if "The key is" in ee:
- print ee
- sys.exit()
- elif "Solved" in ee:
- print ee
- win=win+1
- print win
- sock.send("k\n")
- newgame(sock)
- def main():
- global choice,dag,win
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.connect(("pieceofeight2.shallweplayaga.me",8273))
- data=sock.recv(4096)
- print data
- lines = data.split('\n')
- frow=lines[3].split('|')
- srow=lines[8].split('|')
- trow=lines[13].split('|')
- board = {}
- if frow[1].strip() == '':
- frow[1]='0'
- elif frow[2].strip() == '':
- frow[2]='0'
- elif frow[3].strip() == '':
- frow[3]='0'
- elif srow[1].strip() == '':
- srow[1]='0'
- elif srow[2].strip() == '':
- srow[2]='0'
- elif srow[3].strip() == '':
- srow[3]='0'
- elif trow[1].strip() == '':
- trow[1]='0'
- elif trow[2].strip() == '':
- trow[2]='0'
- elif trow[3].strip() == '':
- trow[3]='0'
- board[1] = int(frow[1].strip())
- board[2] = int(frow[2].strip())
- board[3] = int(frow[3].strip())
- board[4] = int(srow[1].strip())
- board[5] = int(srow[2].strip())
- board[6] = int(srow[3].strip())
- board[7] = int(trow[1].strip())
- board[8] = int(trow[2].strip())
- board[9] = int(trow[3].strip())
- choice=3
- result, goal, visNodes, maxNodes, time1 = aStarSearch(board)
- zeropos=getzeropos()
- payload=""
- for arg in sys.argv:
- start = Node(board, 0)
- path = getPath(goal, start)
- for i in range(1, len(path)):
- printNode(path[i])
- aftzeropos=getzeropos()
- if aftzeropos > zeropos:
- diff=aftzeropos-zeropos
- if diff == 1:
- payload=payload+"l"
- elif diff == 3:
- payload=payload+"u"
- else:
- diff=zeropos-aftzeropos
- if diff == 1:
- payload=payload+"r"
- elif diff == 3:
- payload=payload+"d"
- zeropos=aftzeropos
- print payload
- sock.send(payload)
- ee="e"
- while ee:
- ee=sock.recv(4096)
- print ee
- if "The key is" in ee:
- print ee
- sys.exit()
- elif "Solved" in ee:
- print ee
- win=win+1
- print win
- sock.send("k\n")
- newgame(sock)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement