Last active
May 24, 2017 20:12
-
-
Save rProffer/e464d3264225e1bfaddbe441291beb4b to your computer and use it in GitHub Desktop.
CS capstone 2016 -McManus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
from node import * | |
import random | |
class gridMap: | |
field = None | |
def __init__(self, m, n): ##set up | |
self.row = n | |
self.column = m | |
self.field = [[node(random.randint(0,9)) for x in range(self.row)] for y in range(self.column)] ##build the array | |
def build(self): | |
for i in range(len(self.field)): | |
for c in range(len(self.field[i])): | |
z = self.field[i][c] | |
if(i == 0 and c == 0): | |
z.setGoal() | |
#print("goal set") | |
z.setX(c) ##cordinates | |
z.setY(i) | |
z.setHValue(math.sqrt(((0-i)**2)+((0-c)**2))) ##set the heuristic for p1 | |
##self.field[i][c] = z | |
#self.toString() | |
#print(" ") | |
self.smooth() | |
##self.toString() | |
return self.field | |
def edit(self): #allow edits build this in later, get it working first (It will need to display the nodes in a map) | |
#get input from user, and then get that node and change the value | |
a = -1 | |
b = -1 | |
c = -1 | |
d = -1 | |
self.toString() | |
while((a < 0 or a > len(self.field)) != False): | |
a = int(input("Give me the column index")) ##add a try catch | |
#print(a) | |
while((b < 0 or b > len(self.field[1])) != False): | |
b = int(input("Now the row index")) | |
while((c < 1 or c > 5)): #allow dynamic radius based on total map size | |
c = int(input("What length of radius to you want to impact? (between 1 and 5)")) | |
while((d < 0 or d > 9) and isinstance(d, int) != False): | |
d = int(input("Enter a value, between 0 and 9, with which to replace it")) | |
z = self.field[a][b] | |
l = z.getValue() | |
z.setValue(d) | |
if(l > d): # this needs a lot of work; you should probably sit down with doctor mcmanus and a white board | |
self.smoll(a, b, c, d) #figure out how to figure out the difference in values | |
else: | |
self.swoll(a, b, c, d) #figure out how to figure out the difference in values | |
self.toString() | |
def smooth(self): | |
for i in range(len(self.field)): | |
for j in range(len(self.field[i])): | |
if(i==0): ##first row | |
if(j==0): ##beginning of first row | |
#print("j = 0", i, j, (len(self.field[i]))) | |
self.field[i][j].setTempV((self.field[i][j].getValue() + self.field[i+1][j].getValue() + self.field[i][j+1].getValue())/3) ##get avergae of node values repeat all the way down | |
self.field[i][j].addNeighbor(self.field[i+1][j]) | |
self.field[i][j].addNeighbor(self.field[i][j+1]) | |
elif(j==len(self.field[i])-1): ##end of first row | |
#print("j = len-1", i, j, (len(self.field[i]))) | |
self.field[i][j].setTempV((self.field[i][j].getValue() + self.field[i][j-1].getValue() + self.field[i+1][j].getValue())/3) | |
self.field[i][j].addNeighbor(self.field[i+1][j]) | |
self.field[i][j].addNeighbor(self.field[i][j-1]) | |
else: ##first row, neither first nor last column | |
#print("the middle", i, j, (len(self.field[i]))) | |
self.field[i][j].setTempV((self.field[i][j].getValue() + self.field[i][j-1].getValue() + self.field[i+1][j].getValue() + self.field[i][j+1].getValue())/4) | |
self.field[i][j].addNeighbor(self.field[i][j-1]) | |
self.field[i][j].addNeighbor(self.field[i+1][j]) | |
self.field[i][j].addNeighbor(self.field[i][j+1]) | |
elif(i == len(self.field)-1): ##last row | |
if(j==0): ##last row first column | |
self.field[i][j].setTempV((self.field[i][j].getValue() + self.field[i-1][j].getValue() + self.field[i][j+1].getValue())/3) | |
self.field[i][j].addNeighbor(self.field[i-1][j]) | |
self.field[i][j].addNeighbor(self.field[i][j+1]) | |
elif(j == len(self.field[i])-1): ##last row last column | |
self.field[i][j].setTempV((self.field[i][j].getValue() + self.field[i][j-1].getValue() + self.field[i-1][j].getValue())/3) | |
self.field[i][j].addNeighbor(self.field[i-1][j]) | |
self.field[i][j].addNeighbor(self.field[i][j-1]) | |
else: ##last row and neither first nor last column | |
self.field[i][j].setTempV((self.field[i][j].getValue() + self.field[i][j-1].getValue() + self.field[i-1][j].getValue() + self.field[i][j+1].getValue())/4) | |
self.field[i][j].addNeighbor(self.field[i-1][j]) | |
self.field[i][j].addNeighbor(self.field[i][j-1]) | |
self.field[i][j].addNeighbor(self.field[i][j+1]) | |
elif(j==0 and i != 0 and i != len(self.field)-1): ##first column and neither first row nor last row | |
self.field[i][j].setTempV((self.field[i][j].getValue() + self.field[i-1][j].getValue() + self.field[i+1][j].getValue() + self.field[i][j+1].getValue())/4) | |
self.field[i][j].addNeighbor(self.field[i+1][j]) | |
self.field[i][j].addNeighbor(self.field[i-1][j]) | |
self.field[i][j].addNeighbor(self.field[i][j+1]) | |
elif(j==len(self.field[i])-1 and i != 0 and i != len(self.field)-1): ##last column but not top or bottom row | |
self.field[i][j].setTempV((self.field[i][j].getValue() +self.field[i-1][j].getValue() + self.field[i+1][j].getValue() + self.field[i][j-1].getValue())/4) | |
self.field[i][j].addNeighbor(self.field[i+1][j]) | |
self.field[i][j].addNeighbor(self.field[i-1][j]) | |
self.field[i][j].addNeighbor(self.field[i][j-1]) | |
else: ##middle nodes | |
#print("else middle", i, j, (len(self.field[i]))) | |
self.field[i][j].setTempV((self.field[i][j-1].getValue() + self.field[i-1][j].getValue() + self.field[i+1][j].getValue() + self.field[i][j+1].getValue())/4) | |
self.field[i][j].addNeighbor(self.field[i+1][j]) | |
self.field[i][j].addNeighbor(self.field[i][j+1]) | |
self.field[i][j].addNeighbor(self.field[i][j-1]) | |
self.field[i][j].addNeighbor(self.field[i-1][j]) | |
for i in range(len(self.field)): | |
for j in range(len(self.field[i])): | |
self.field[i][j].smooth() ##tell node to switch values | |
def iterateValue(self, pNode): | |
for i in range(len(self.field)): | |
for j in range(len(self.field[i])): | |
self.field[i][j].setHToPrey(math.sqrt(((pNode.getX()-j)**2)+((pNode.getY()-i)**2))) ##heuristic for p2 | |
def reset(self): ##allow for multiple usage. Attempt to allow for sharing of attributes. | |
for i in range(len(self.field)): | |
for j in range(len(self.field[i])): | |
self.field[i][j].knockPrevious() ##reset path; prevents standstill | |
#self.field[i][j].setCost(9999) | |
#self.field[i][j].setFScore(0) | |
def randNode(self): ##return a random node place p 1 | |
for i in range(len(self.field)): | |
for j in range(len(self.field[i])): | |
if(random.randint(0,9920) > 9998): ##make this dynamic | |
return self.field[i][j] | |
return self.field[len(self.field)-1][len(self.field[i])-1] | |
def randBad(self): ##return a random node place p2 | |
for i in range(len(self.field)): | |
for j in range(len(self.field[i])): | |
if(random.randint(0,9999) > 7085): ##make this dynamic | |
return self.field[i][j] | |
return self.field[Math.floor((len(self.field))/2)][Math.floor((len(self.field))/2)] | |
#def search(self, centerx, centery, radius): | |
#for i in range(centerx - radius, centerx + radius): | |
#for j in range(centery - radius, centery + radius): | |
#if(i < 0 or j < 0): | |
#break | |
#if(i > len(self.field) or j > len(self.field[i])): | |
#break | |
#if(i == centery or j == centerx): | |
#break | |
#if(self.field[i][j].isOccupied == true): | |
#return true | |
##get the position of a player and search within the radius for other players | |
def swoll(self, centerx, centery, radius, amount): ##swell an area | |
for i in range(math.ceil(centerx - radius), math.ceil(centerx + radius)): | |
for j in range(math.ceil(centery - radius), math.ceil(centery + radius)): | |
if(i < 0 or j < 0): | |
break | |
if(i > len(self.field) or j > len(self.field[i])): | |
break | |
self.field[i][j].setValue(min(9,self.field[i][j].getValue()+(amount-(max(i,j)/2)))) | |
def smoll(self, centerx, centery, radius, amount): ##negatively swell an area | |
for i in range(math.ceil(centerx - radius), math.ceil(centerx + radius)): | |
for j in range(math.ceil(centery - radius), math.ceil(centery + radius)): | |
if(i < 0 or j < 0): | |
break | |
if(i > len(self.field) or j > len(self.field[i])): #Check if these are still out of range by one | |
break | |
self.field[i][j].setValue(max(0,self.field[i][j].getValue()-(amount+(max(i,j)*2)))) | |
def toString(self): ##print the thing | |
a = " " | |
for b in range(len(self.field)): | |
a += str(b)+ "_" | |
print(a) | |
for i in range(len(self.field)): | |
p = str(i) + "|" | |
for j in range(len(self.field[i])): | |
p += " "+str(math.floor(self.field[i][j].getValue())) | |
print(p) | |
print("") | |
def truth(self): ##make sure that only the goal is the goal | |
for i in range(len(self.field)): | |
for j in range(len(self.field[i])): | |
if(self.field[i][j].isGoal()): | |
print("1") | |
else: | |
print("0") | |
def countNeighbors(self): ##count neighbors of each node | |
for i in range(len(self.field)): | |
x = "" | |
for j in range(len(self.field[i])): | |
x += " "+ str(len(self.field[i][j].getNeighbors())) | |
print(x) | |
def testRide(self): | |
self.toString() | |
print("testing build") | |
self.build() | |
print("truth table") | |
self.truth() | |
print("testing edit") | |
self.edit() | |
print("testing swoll") | |
self.swoll(3, 3, 4, 3) | |
self.toString() | |
print("testing smoll") | |
self.smoll(3, 3, 4, 3) | |
self.toString() | |
print("testing the function to return a random node") | |
x = self.randNode() | |
print(x.getX()) | |
print(x.getY()) | |
print("testing the function to find distances between two nodes") | |
self.iterateValue(self.randNode()) | |
self.countNeighbors() | |
#def smooth(xStart, yStart, xEnd, yEnd): #traverse and smooth. math.ceil(x) returns the highest int value closest to a decimal | |
#i = xStart | |
#n = yStart | |
#for i in range(xEnd): | |
#for n in range(yEnd): #I fucked up global variables | |
#z = field[i][n] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gridMap import * | |
from player import * | |
from node import * | |
from tester import * | |
from predPlayer import * | |
from collections import Counter | |
def main(): | |
#test = tester() | |
#test.test() | |
m = 0 | |
n = 0 | |
p = "" | |
#tst = node(2) | |
#tst.testRide() | |
#set up game | |
while(m < 5 or m > 2500): | |
m = int(input("Give me the number of rows (5 < x < 2500)")) | |
while(n < 5 or n > 2500): | |
n = int(input("Now the number of columns (5 < x < 2500)")) | |
world = gridMap(m, n) | |
world.build() | |
while(p.lower() not in ('yes', 'no')): | |
p = input("Would you like to edit the map? you may enter yes or no to indicate your response. please.") | |
if(p == 'yes'): | |
world.edit() | |
pOne = Player(world.randNode()) | |
pTwo = predPlayer(world.randBad()) | |
world.swoll(pTwo.getNode().getX(), pTwo.getNode().getY(), (m*n)/4, 5) | |
world.iterateValue(pOne.getNode()) | |
value = 0 | |
devalue = 0 | |
count = 0 | |
while(value == 0 and devalue == 0): ##run game | |
count += 1 | |
value = pOne.move() | |
world.reset() ##clears cameFrom values in nodes | |
#print(value) | |
world.iterateValue(pOne.getNode()) ##set up heuristic because player is in a different place | |
world.smoll(pTwo.getNode().getX(), pTwo.getNode().getY(), (m*n)/10, 5) ##remove p2 influence | |
devalue = pTwo.move() | |
world.swoll(pTwo.getNode().getX(), pTwo.getNode().getY(), (m*n)/10, 5) ##institute p2 influence | |
world.reset() | |
print(count, "moves") | |
main() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class node: | |
def __init__(self, n): | |
self._value = n | |
self._occupied = False | |
self._heuristic = 0.0 | |
self._hToPrey = 0.0 | |
self._tempV = 0 | |
self._goal = False | |
self._cameFrom = None | |
self.neighbors = [] | |
self._cost = 99999 | |
self._fScore = 0 | |
self.x = 0 | |
self.y = 0 | |
self.secCost = 9999 | |
self.secFScore = 0 | |
def getX(self): ## get x-value | |
return self.x | |
def setX(self, n): ##set x-value | |
self.x = n | |
def getY(self): ##get y-value | |
return self.y | |
def setY(self, n): ##set y-value | |
self.y = n | |
def isOccupied(self): ##check if the node is occupied by a player | |
return self._occupied | |
def setPrevious(self, n): ##set the node from which this node was reached | |
self._cameFrom = n | |
def getPrevious(self): ##return the node from which this node was reach | |
return self._cameFrom | |
def knockPrevious(self): | |
self._cameFrom = None | |
def getFScore(self): ##return fScore for player 1 | |
return self._fScore | |
def getSecondaryFScore(self): ##return fScore for player 2 | |
return self.secFScore | |
def setSecondaryFScore(self, n): ##set the fScore for player 2 | |
self.secFScore = n | |
def setFScore(self, n): ##set fScore for player 1 | |
self._fScore = n | |
def getValue(self): ##return the value of this node | |
return self._value | |
def getHValue(self): ##return the heuristic value, the straight line distance from here to the goal node | |
return self._heuristic | |
def getHToPrey(self): ##straight line distance between this node and the node occupied by player 1 | |
return self._hToPrey | |
def setValue(self, n): ##set the value as n | |
self._value = n | |
def addNeighbor(self, node): ##add a neighbor | |
if(self.neighbors.count(node)==0): | |
self.neighbors.append(node) | |
def getNeighbors(self): ##return the array of neighbors | |
return self.neighbors | |
def setHValue(self, n): ##set the heuristic value | |
self._heuristic = n | |
def setHToPrey(self, n): ##set the secondary heuristic value | |
self._hToPrey = n | |
def occupy(self): ##designate the node as occupied | |
self._occupied = True | |
def deoccupy(self): ##designate the node as unoccupied | |
self._occupied = False | |
def isGoal(self): ##check to see if this is the goal | |
return self._goal | |
def setGoal(self): ##designate the node as the goal | |
self._goal = True | |
def setTempV(self, n): ##set a temporary value with which to replace the value in a moment | |
self._tempV = n | |
def smooth(self): ##replace the value with the tempValue | |
self._value = self._tempV | |
def setCost(self, n): ##set the cost to get here from start | |
self._cost = n | |
def setSecCost(self, n): ##set the cost to get here from player 2 start | |
self.secCost = n | |
def getSecCost(self): ##return the cost to get here from player 2 start | |
return self.secCost | |
def getCost(self): ##return the cost to get here from start | |
return self._cost | |
def testRide(self): ##test things | |
##probably test all the set and get methods. | |
self.setCost(10) | |
if(self.getCost() != 10): | |
print("fail on get or set cost in node") | |
self.setTempV(10) | |
if(self._tempV != 10): | |
print("fail on setTempV") | |
self.setGoal() | |
if(self.isGoal() != True): | |
print("goal failure") | |
self.occupy() | |
if(self.isOccupied() != True): | |
print("occupation failure") | |
self.deoccupy() | |
if(self.isOccupied() != False): | |
print("deoccupy fail") | |
self.setHToPrey(10) | |
if(self.getHToPrey() != 10): | |
print("h to prey fail") | |
self.setHValue(10) | |
if(self.getHValue() != 10): | |
print("hValue fail") | |
self.setValue(10) | |
if(self.getValue() != 10): | |
print("value fail") | |
self.setFScore(10) | |
if(self.getFScore() != 10): | |
print("fScore fail") | |
self.setX(10) | |
self.setY(10) | |
if(self.getX() != 10): | |
print("x fail") | |
if(self.getY() != 10): | |
print("y fail") | |
x = node(10) | |
self.addNeighbor(x) | |
y = self.getNeighbors() | |
if(x not in y): | |
print("neighbor fail") | |
self.setPrevious(x) | |
if(self.getPrevious() != x): | |
print("previous fail") | |
print("done") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from node import * ##because node ops | |
from gridMap import * ##for testing purposes | |
import random | |
import math | |
class Player: | |
def __init__(self, n): | |
##self._start = None | |
self._node = n | |
self._node.occupy() | |
self._node.setCost(0) | |
def pathfind(self): ##this is the A* algorithm that finds a path from the given node to the goal | |
#print("(",self.getNode().getX(),",",self.getNode().getY(),")", "node to be gotten") | |
self._node.setCost(0) ##setcost to 0 | |
openSet = [self._node] ##add current node to fringe | |
closedSet = [] | |
while(len(openSet) != 0): ##while openSet is not empty | |
current = openSet[0] ##get the first node | |
if(current.isGoal()): ##if current = goal | |
##print("you found the goal. Now you just have to get there") | |
return self.reconstructPath(current) ##rebuild the path and return as an array | |
##openSet.remove(current) | |
closedSet.append(openSet.pop(0)) ##take current from openSet and add it to closedSet | |
neighborhood = current.getNeighbors() ##get your neighbors | |
#print("number of neighbors") | |
#print(len(neighborhood)) | |
for x in neighborhood: ##look at each neighbor | |
if(closedSet.count(x) == 0): ##if x is not in the closed set, get the gScore | |
tentativeGScore = current.getCost() + x.getValue() ##cost to get here plus cost to get to neighbor | |
if(openSet.count(x) == 0):##if neighbor not in openSet, Discover a new node | |
openSet.append(x) | |
self.sort(openSet) ##sort so that you can get most efficient choice | |
if(tentativeGScore <= x.getCost()): ##this is weird and I probably need to fix something. ##measure for the shortest path from here to start | |
x.setPrevious(current) ##set previous node so you know where you came from | |
x.setCost(tentativeGScore) ##set the more efficient cost | |
x.setFScore(x.getCost() + x.getHValue()) ##set the fScore so you can sort | |
#print("out of for statement") | |
#print("out of while loop") | |
return 0 ##you dun goofed | |
def reconstructPath(self, current): ##build path to allow for selection of a move | |
totalPath = [current] ##current is the goal | |
while(current.getPrevious() != None): ##while you haven't gotten to the beginning | |
current = current.getPrevious() ##get the previous one | |
totalPath.append(current) ##add it to the path | |
totalPath.reverse() ##reversing puts the goal at rear and the best option at the head | |
#self.printPath(totalPath) | |
##print("returning a path") | |
return totalPath | |
def printPath(self, path): | |
for i in range(len(path)): | |
print("(",path[i].getX(),",",path[i].getY(),")") | |
def isDanger(self): #pass the currentNode to the map and call the search . won't be necessary in capstone form. | |
pass | |
def move(self): ##take the first node from path, move there. check if it's the goal. | |
self.getNode().deoccupy() ##unoccupy your current node | |
path = self.pathfind() ##find a path | |
self.setNode(path[1]) ##get the first one after the node you are one, p[0] is current node | |
self.getNode().occupy() ##occupy the next one | |
##print("movement from player 1") | |
if(self.getNode().isGoal()): ##you won!!!! | |
print("you got to the goal") | |
return 1 | |
return 0 ##figure out a better return value? | |
#def msort(self, x): | |
#result = [] | |
#if(len(x) < 20): | |
#return self.sort(x) ##sorted(x, key=lambda x: x.getFScore())? | |
#mid = math.floor(len(x)/2) | |
#y = self.msort(x[:mid]) | |
#z = self.msort(x[mid:]) | |
#i = 0 | |
#j = 0 | |
#while(i < len(y) and j < len(z)): | |
#if(y[i].getFScore() < z[j].getFScore()): | |
#result.append(z[j]) | |
#j += 1 | |
#else: | |
#result.append(y[i]) | |
#i += 1 | |
#result += y[i:] | |
#result += z[j:] | |
#return result | |
def sort(self, x): ##sort method to sort openSet to allow for selection of most efficient path. it's an insertion sort | |
for i in range(len(x)): | |
val = x[i] | |
j = i - 1 | |
while(j >= 0 and x[j].getFScore() > val.getFScore()): ##determine better fScore | |
x[j+1] = x[j] | |
j = j - 1 | |
x[j+1] = val | |
return x | |
def getNode(self): ##return the node that this player is at | |
return self._node | |
def setNode(self, n): ##set the node n to the player's node value | |
##print("(",self.getNode().getX(),",",self.getNode().getY(),")", "value before assignment") | |
##print("(",n.getX(),",",n.getY(),")", "value to be assigned") | |
self._node = n | |
##print("(",self._node.getX(),",",self._node.getY(),")", "value after assignment") | |
def testRide(self): ##test | |
print("start") ##set up | |
x = gridMap(100, 100) | |
x.build() | |
testArray = [] | |
for i in range(100): | |
imp = node(10) | |
##print("imp made") | |
##print(imp.getValue()) | |
testArray.append(imp) | |
##print("imp appended") | |
for a in range(len(testArray)): ##testing sort | |
testArray[a].setFScore(100-a) #insure disordered entries | |
print("unsorted array") | |
for b in range(len(testArray)): | |
print(testArray[b].getFScore()) | |
self.sort(testArray) | |
print("sorted array") | |
for c in range(len(testArray)): | |
print(testArray[c].getFScore()) #I know it works up to here | |
self._start = x.randNode() | |
print("x and y values of start node") | |
print(self._node.getY()) | |
print(self._node.getX()) | |
testPath = self.pathfind() ##test path | |
for d in range(len(testPath)): | |
print("x and y values") ##print path | |
print(testPath[d].getX(), testPath[d].getY()) | |
#print(testPath[d].getY()) | |
print("") | |
print("") | |
peep = self.getNode() ##get current node | |
print("X value before move", peep.getX()) | |
print("Y value before move", peep.getY()) | |
self.move() ##test move | |
pipe = self.getNode() | |
print("X value after move", pipe.getX()) | |
print("Y value after move", pipe.getY()) | |
counter = 0 | |
while(self.getNode().isGoal() != True): ##test essential components together | |
counter += 1 | |
self.move() | |
print("number of moves from beginning to end") | |
print(counter) | |
print("X value after move", self._node.getX()) | |
print("Y value after move", self._node.getY()) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from node import * ##because node ops | |
from gridMap import * ##for testing purposes | |
import random | |
import math | |
##This can be improved by making it a composition of Player | |
class predPlayer: | |
def __init__(self, n): | |
##self._start = None | |
self._node = n | |
self._node.setSecCost(0) | |
def pathfind(self): ##this is the A* algorithm that finds a path from the given node to the goal | |
#print("(",self.getNode().getX(),",",self.getNode().getY(),")", "node to be gotten") | |
openSet = [self._node] | |
closedSet = [] | |
while(len(openSet) != 0): ##while openSet is not empty | |
current = openSet[0] | |
##sort the open set | |
if(current.isOccupied()): ##if current = goal | |
#print("you found player1. Now you just have to get to them") | |
return self.reconstructPath(current) | |
closedSet.append(openSet.pop(0)) | |
neighborhood = current.getNeighbors() | |
#print("number of neighbors") | |
#print(len(neighborhood)) | |
for x in neighborhood: | |
if(closedSet.count(x) == 0): ##if x is not in the closed set, get the gScore | |
tentativeGScore = current.getSecCost() + x.getValue() | |
if(openSet.count(x) == 0):##if neighbor not in openSet, Discover a new node | |
openSet.append(x) | |
self.sort(openSet) | |
if(tentativeGScore <= x.getSecCost()): ##this is weird and I probably need to fix something. ##measure for the shortest path from here to start | |
x.setPrevious(current) | |
x.setSecCost(tentativeGScore) | |
x.setSecondaryFScore(x.getSecCost() + x.getHToPrey()) | |
#print("out of for statement") | |
#print("out of while loop") | |
return 0 | |
def reconstructPath(self, current): ##build path to allow for selection of a move | |
totalPath = [current] ##current is the goal | |
while(current.getPrevious() != None): ##might need fixing, camefrom being a node attribute might be better | |
current = current.getPrevious() | |
totalPath.append(current) | |
totalPath.reverse() ##reversing puts the goal at rear and the best option at the head | |
#self.printPath(totalPath) | |
#print("returning a path") | |
return totalPath | |
def printPath(self, path): | |
for i in range(len(path)): | |
print("(",path[i].getX(),",",path[i].getY(),")") | |
def isDanger(self): #pass the currentNode to the map and call the search . won't be necessary in capstone form. | |
pass | |
def move(self): ##take the first node from path, move there. check if it's the goal. predplayer could probably try to draw player one closer to it by manipulating swoll and smoll | |
path = self.pathfind() | |
#print(len(path)) | |
if(len(path)==1): | |
self.setNode(path[0]) ##I think path[0] is the node it's | |
else: | |
self.setNode(path[1]) | |
#print(" player2 moving") | |
if(self.getNode().isOccupied()): | |
print("you ate player 1! om nom nom") | |
return 1 | |
return 0 ##figure out a better return value? | |
#def msort(self, x): | |
#result = [] | |
#if(len(x) < 20): | |
#return self.sort(x) ##sorted(x, key=lambda x: x.getFScore())? | |
#mid = math.floor(len(x)/2) | |
#y = self.msort(x[:mid]) | |
#z = self.msort(x[mid:]) | |
#i = 0 | |
#j = 0 | |
#while(i < len(y) and j < len(z)): | |
#if(y[i].getFScore() < z[j].getFScore()): | |
#result.append(z[j]) | |
#j += 1 | |
#else: | |
#result.append(y[i]) | |
#i += 1 | |
#result += y[i:] | |
#result += z[j:] | |
#return result | |
def sort(self, x): ##sort method to sort openSet to allow for selection of most efficient path | |
for i in range(len(x)): | |
val = x[i] | |
j = i - 1 | |
while(j >= 0 and x[j].getSecondaryFScore() > val.getSecondaryFScore()): | |
x[j+1] = x[j] | |
j = j - 1 | |
x[j+1] = val | |
return x | |
def getNode(self): ##return the node that this player is at | |
return self._node | |
def setNode(self, n): ##set the node n to the player's node value | |
#print("(",self.getNode().getX(),",",self.getNode().getY(),")", "value before assignment") | |
#print("(",n.getX(),",",n.getY(),")", "value to be assigned") | |
self._node = n | |
#print("(",self._node.getX(),",",self._node.getY(),")", "value after assignment") | |
def testRide(self): ##test | |
print("start") | |
x = gridMap(100, 100) | |
x.build() | |
testArray = [] | |
for i in range(100): | |
imp = node(10) | |
##print("imp made") | |
##print(imp.getValue()) | |
testArray.append(imp) | |
##print("imp appended") | |
for a in range(len(testArray)): | |
testArray[a].setFScore(100-a) #insure disordered entries | |
print("unsorted array") | |
for b in range(len(testArray)): | |
print(testArray[b].getFScore()) | |
self.sort(testArray) | |
print("sorted array") | |
for c in range(len(testArray)): | |
print(testArray[c].getSecondaryFScore()) #I know it works up to here | |
self._start = x.randNode() | |
print("x and y values of start node") | |
print(self._start.getY()) | |
print(self._start.getX()) | |
testPath = self.pathfind() | |
for d in range(len(testPath)): | |
print("x and y values") | |
print(testPath[d].getX()) | |
print(testPath[d].getY()) | |
print("") | |
print("") | |
peep = self.getNode() | |
print("X value before move", peep.getX()) | |
print("Y value before move", peep.getY()) | |
self.move() | |
pipe = self.getNode() | |
print("X value after move", pipe.getX()) | |
print("Y value after move", pipe.getY()) | |
counter = 0 | |
while(self.getNode().isGoal() != True): | |
counter += 1 | |
self.move() | |
print("number of moves from beginning to end") | |
print(counter) | |
print("X value after move", self._node.getX()) | |
print("Y value after move", self._node.getY()) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from node import * | |
from gridMap import * | |
from player import * | |
class tester: | |
def test(self): | |
print("node test") | |
testNode = node(9) | |
testNode.testRide() ##run unit test for node | |
print("map test") | |
testMap = gridMap(10, 10) | |
testMap.testRide() ##unit test for map | |
print("done") | |
print("player test") | |
testPlayer = Player(testMap.randNode()) ##give it a node, doesn't matter which, is has its own map from which it will pull all data | |
testPlayer.testRide() ##player unit test | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment