Skip to content

Instantly share code, notes, and snippets.

@rProffer
Last active December 1, 2017 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rProffer/7911ba40e317bfff60c674dcc019dff4 to your computer and use it in GitHub Desktop.
Save rProffer/7911ba40e317bfff60c674dcc019dff4 to your computer and use it in GitHub Desktop.
artificially intelligent tag
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
class gridMap {
/*
* make a NodeData class, you can make an ArrayList<NodeData> and access it in O(1) with the added bonus
* of now having a natural correspondence between nodes and indexes
* Do this in conjunction with an adjacency list
*
*/
private List<List<node>> field;
private int row;
private int column;
public gridMap(int m, int n) { //set up
this.row = n;
this.column = m;
field = new ArrayList<List<node>>(this.column);
node temp;
Random r = new Random();
for(int x = 0; x < this.row; x++) {
List<node> xList = new ArrayList<node>(this.column);
field.add(x, xList);
for(int y = 0; y < this.column; y++) {
//build the field
System.out.print(x);
System.out.print(y);
temp = new node(r.nextInt(8)+1);
field.get(x).add(y, temp); //add the node at x,y
}
}
/** For testing purposes
* for(int x = 0; x < this.row; x++) {
System.out.println();
for(int y = 0; y < this.column; y++) {
System.out.print(field.get(x).get(y).getValue());
}
}*/
}
void build() {
node z;
for(int i = 0; i < this.field.size(); i++) {
for(int c = 0; c < this.field.get(i).size(); c++) {
z = this.field.get(i).get(c);
if(i == 0 & c == 0) {
z.setGoal();
}
z.setX(c); //coordinates
z.setY(i);
z.setHValue(Math.sqrt(Math.pow(0-i, 2)+Math.pow(0-c, 2))); //heuristic for p1
//z.setHValue(Math.sqrt(((0-i)**2)+((0-c)**2))); //set the heuristic for p1
}
}
this.smooth();
}
void edit() { //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
int a = -1;
int b = -1;
int c = -1;
int d = -1;
Scanner scanner = new Scanner(System.in);
this.toString();
while((a < 0 || a > this.field.size()) != false) {
System.out.println("Give me the column index"); //add try catch
a = scanner.nextInt();
//print(a)
}
while((b < 0 || b > this.field.get(0).size()) != false) {
System.out.println("Give me the row index"); //add try catch
b = scanner.nextInt();
}
while((c < 1 || c > 5)) { //allow dynamic radius based on total map size
System.out.println("What length of radius to you want to impact? (between 1 and 5)"); //add try catch
c = scanner.nextInt();
}
//while((d < 0 || d > 9) & isinstance(d, int) != false) {
while((d < 0 || d > 9)) {// & d instanceof Integer) {
System.out.println("Enter a value, between 0 and 9, with which to replace it"); //add try catch
d = scanner.nextInt();
}
scanner.close();
node z = this.field.get(a).get(b);
int l = z.getValue();
z.setValue(d);
if(l > d)
{this.smoll(a, b, c, d);}
else
{this.swoll(a, b, c, d);}
this.toString();
}
private void smooth() {
for(int i = 0; i < this.field.size(); i++) {
for(int j = 0; j < this.field.get(i).size(); j++) {
if(i==0) { //first row
if(j==0) { //beginning of first row
this.field.get(i).get(j).setTempV((this.field.get(i).get(j).getValue() + this.field.get(i+1).get(j).getValue() + this.field.get(i).get(j+1).getValue())/3); //get average of node values repeat all the way down
this.field.get(i).get(j).addNeighbor(this.field.get(i+1).get(j));
this.field.get(i).get(j).addNeighbor(this.field.get(i).get(j+1));
}
else if(j==this.field.get(i).size() - 1){ //end of first row
this.field.get(i).get(j).setTempV((this.field.get(i).get(j).getValue() + this.field.get(i).get(j-1).getValue() + this.field.get(i+1).get(j).getValue())/3);
this.field.get(i).get(j).addNeighbor(this.field.get(i+1).get(j));
this.field.get(i).get(j).addNeighbor(this.field.get(i).get(j-1));
}
else { //first row, neither first nor last column
this.field.get(i).get(j).setTempV((this.field.get(i).get(j).getValue() + this.field.get(i).get(j-1).getValue() + this.field.get(i+1).get(j).getValue() + this.field.get(i).get(j+1).getValue())/4);
this.field.get(i).get(j).addNeighbor(this.field.get(i).get(j-1));
this.field.get(i).get(j).addNeighbor(this.field.get(i+1).get(j));
this.field.get(i).get(j).addNeighbor(this.field.get(i).get(j+1));
}
}
else if(i == this.field.size()-1) { //last row
if(j==0) { //last row first column
this.field.get(i).get(j).setTempV((this.field.get(i).get(j).getValue() + this.field.get(i-1).get(j).getValue() + this.field.get(i).get(j+1).getValue())/3);
this.field.get(i).get(j).addNeighbor(this.field.get(i-1).get(j));
this.field.get(i).get(j).addNeighbor(this.field.get(i).get(j+1));
}
else if(j == this.field.get(i).size()-1) { //last row last column
this.field.get(i).get(j).setTempV((this.field.get(i).get(j).getValue() + this.field.get(i).get(j-1).getValue() + this.field.get(i-1).get(j).getValue())/3);
this.field.get(i).get(j).addNeighbor(this.field.get(i-1).get(j));
this.field.get(i).get(j).addNeighbor(this.field.get(i).get(j-1));
}
else { //last row and neither first nor last column
this.field.get(i).get(j).setTempV((this.field.get(i).get(j).getValue() + this.field.get(i).get(j-1).getValue() + this.field.get(i-1).get(j).getValue() + this.field.get(i).get(j+1).getValue())/4);
this.field.get(i).get(j).addNeighbor(this.field.get(i-1).get(j));
this.field.get(i).get(j).addNeighbor(this.field.get(i).get(j-1));
this.field.get(i).get(j).addNeighbor(this.field.get(i).get(j+1));
}
} //this is the one if it throws an error later
else if(j==0 & i != 0 & i != this.field.size()-1) { //first column and neither first row nor last row
this.field.get(i).get(j).setTempV((this.field.get(i).get(j).getValue() + this.field.get(i-1).get(j).getValue() + this.field.get(i+1).get(j).getValue() + this.field.get(i).get(j+1).getValue())/4);
this.field.get(i).get(j).addNeighbor(this.field.get(i+1).get(j));
this.field.get(i).get(j).addNeighbor(this.field.get(i-1).get(j));
this.field.get(i).get(j).addNeighbor(this.field.get(i).get(j+1));
}
else if(j==this.field.get(i).size()-1 & i != 0 & i != this.field.size()-1) { //last column but not top or bottom row
this.field.get(i).get(j).setTempV((this.field.get(i).get(j).getValue() +this.field.get(i-1).get(j).getValue() + this.field.get(i+1).get(j).getValue() + this.field.get(i).get(j-1).getValue())/4);
this.field.get(i).get(j).addNeighbor(this.field.get(i+1).get(j));
this.field.get(i).get(j).addNeighbor(this.field.get(i-1).get(j));
this.field.get(i).get(j).addNeighbor(this.field.get(i).get(j-1));
}
else { //middle nodes
this.field.get(i).get(j).setTempV((this.field.get(i).get(j-1).getValue() + this.field.get(i-1).get(j).getValue() + this.field.get(i+1).get(j).getValue() + this.field.get(i).get(j+1).getValue())/4);
this.field.get(i).get(j).addNeighbor(this.field.get(i+1).get(j));
this.field.get(i).get(j).addNeighbor(this.field.get(i).get(j+1));
this.field.get(i).get(j).addNeighbor(this.field.get(i).get(j-1));
this.field.get(i).get(j).addNeighbor(this.field.get(i-1).get(j));
}
}
}
for(int i = 0; i < this.field.size(); i++) {
for(int j = 0; j < this.field.get(i).size(); j++){
this.field.get(i).get(j).smooth();} //switching value for temp value
}
}
void iterateValue(node pNode) {
for(int i = 0; i < this.field.size(); i++) {
for(int j = 0; j < this.field.get(i).size(); j++){ {
this.field.get(i).get(j).setHToPrey(Math.sqrt(Math.pow(pNode.getX()-j,2)+Math.pow(pNode.getY()-i,2))); //heuristic for p2
//this.field.get(i).get(j).setHToPrey(Math.sqrt(((pNode.getX()-j)**2)+((pNode.getY()-i)**2))); //heuristic for p2
}
}
}
}
void reset() {
for(int i = 0; i < this.field.size(); i++) {
for(int j = 0; j < this.field.get(i).size(); j++){ {
this.field.get(i).get(j).knockPrevious();} //reset path; prevents standstill
//this.field.get(i).get(j).setCost(9999)
//this.field.get(i).get(j).setFScore(0)
}
}
}
node randNode() { //return a random node place p 1
Random r = new Random();
for(int i = 0; i < this.field.size(); i++) {
for(int j = 0; j < this.field.get(i).size(); j++){
if(r.nextInt(10000) > 9900)
{return this.field.get(i).get(j);}
}
}
return this.field.get(this.field.size()-1).get(this.field.get(0).size()-1);
}
node randBad() { //return a random node place p2
Random r = new Random();
for(int i = 0; i < this.field.size(); i++) {
for(int j = 0; j < this.field.get(i).size(); j++){
if(r.nextInt(10000) > 7085) {
return this.field.get(i).get(j);}
}
}
return this.field.get((int) Math.floor(this.field.size()/2)).get((int) Math.floor(this.field.size()/2));
//math might be wrong
}
/*#def search(this, 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(this.field) or j > len(this.field[i])):
#break
#if(i == centery or j == centerx):
#break
#if(this.field.get(i).get(j).isOccupied == true):
#return true
//get the position of a player and search within the radius for other players
*
*/
void swoll( int centerx, int centery, int radius, int amount) { //swell an area
for(int i = (int) Math.ceil(centerx - radius); i < Math.ceil(centerx + radius); i++) {//check whether, in python, the range for for loops is inclusive or exclusive also check your cast to int: does it go up or down
for(int j = (int) Math.ceil(centery - radius); j < Math.ceil(centery + radius); j++) {//check whether, in python, the range for for loops is inclusive or exclusive also check your cast to int: does it go up or down
if(i < 0 || j < 0)
{break;}
if(i > this.field.size() || j > this.field.get(i).size())
{break;}
this.field.get(i).get(j).setValue(Math.min(9,this.field.get(i).get(j).getValue()+(amount-(Math.max(i,j)/2))));
}
}
}
void smoll(int centerx, int centery, int radius, int amount) { //negatively swell an area
for(int i = (int) Math.ceil(centerx - radius); i < Math.ceil(centerx + radius); i++) {
for(int j = (int) Math.ceil(centery - radius); j < Math.ceil(centery + radius); j++) {
if(i < 0 || j < 0)
break;
if(i > this.field.size() || j > this.field.get(i).size())
break;
this.field.get(i).get(j).setValue(Math.max(0,this.field.get(i).get(j).getValue()-(amount+(Math.max(i,j)*2))));
}
}
}
public void print() { //print the thing
String a = " " ;
String p = "";
for(int b = 0; b < this.field.size(); b++) {
a += String.valueOf(b)+ "_";
}
System.out.println(a);
for(int i = 0; i < this.field.size(); i++) {
p = String.valueOf(i) + "|";
for(int j = 0; j < this.field.get(i).size(); j++) {
p += " "+ String.valueOf((Math.floor(this.field.get(i).get(j).getValue())));}
System.out.println(p);
System.out.println("");
}
}
void truth() { //make sure that only the goal is the goal
for(int i = 0; i < this.field.size(); i++) {
for(int j = 0; j < this.field.get(i).size(); j++){
if(this.field.get(i).get(j).isGoal()) {
System.out.println("1");}
else {
System.out.println("0");}
}
}
}
void countNeighbors() { //count neighbors of each node
for(int i = 0; i < this.field.size(); i++) {
String x = "";
for(int j = 0; j < this.field.get(i).size(); j++){
x += " "+ (this.field.get(i).get(j).getNeighbors().size());
}
System.out.println(x);
}
}
void testRide(){
this.toString();
System.out.println("testing build");
this.build();
System.out.println("truth table");
this.truth();
System.out.println("testing edit");
this.edit();
System.out.println("testing swoll");
this.swoll(3, 3, 4, 3);
this.toString();
System.out.println("testing smoll");
this.smoll(3, 3, 4, 3);
this.toString();
System.out.println("testing the function to return a random node");
node x = this.randNode();
System.out.println(x.getX());
System.out.println(x.getY());
System.out.println("testing the function to find distances between two nodes");
this.iterateValue(this.randNode());
this.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]
*/
}
import java.util.Scanner;
public class Main {
gridMap world;
public static void main(String[] args) {
Main m = new Main();
m.run();
}
void run() {
//test = tester();
//test.tester();
int m = 0;
int n = 0;
int count;
int devalue;
int value;
Scanner scanner = new Scanner(System.in);
String p;
//tst = node(2);
//tst.testRide();
//set up game;
while(m < 5 || m > 2500) {
System.out.println("Give me the number of rows (5 < x < 2500)");
m = scanner.nextInt();}
while(n < 5 || n > 2500) {
System.out.println("Give me the number of columns (5 < x < 2500)");
n = scanner.nextInt();}
world = new gridMap(m, n);
world.build();
do {
System.out.println("Would you like to edit the map? you may enter yes or no to indicate your response. please."); //why does this print twice?
p = scanner.nextLine(); //why don't you stop here? why print twice?
}while(!p.equalsIgnoreCase("yes") && !p.equalsIgnoreCase("no"));
if(p.equalsIgnoreCase("yes"))//strings can't do that
world.edit();
scanner.close();
node playerStartNode = world.randNode();
playerStartNode.occupy();
playerStartNode.setCost(0);
Player pOne = new Player(playerStartNode);
predPlayer pTwo = new 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 && devalue == 0) { //run game
count++;
value = pOne.move(); //TODO Player One's openSet needs to reevaluate node values given swoll and smoll
world.reset(); //clears cameFrom values in nodes
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();
}
System.out.printf("moves: " + count);}
}
import java.util.ArrayList;
import java.util.List;
class node implements nodeInterface{
private int value;
private boolean occupied;
private double heuristic;
private double hToPrey;
private int tempV;
private boolean goal;
private node cameFrom;
private List<node> neighbors = new ArrayList<node>();
private int cost;
private double fScore;
private int x;
private int y;
private int secCost;
private double secFScore;
public node(int n) {
this.value = n;
this.occupied = false;
this.heuristic = 0.0;
this.hToPrey = 0.0; //move to predPlayer
this.tempV = 0;
this.goal = false;
//this.cameFrom = null;
//this.neighbors = null; //make this a set
this.cost = 99999;
this.fScore = 0.0;
this.x = 0;
this.y = 0;
this.secCost = 99999;
this.secFScore = 0.0;
}
public int getX() { // get x-value
return this.x;
}
public void setX(int n) { //set x-value
this.x = n;
}
public int getY() {//get y-value
return this.y;
}
public void setY(int n) { //set y-value
this.y = n;
}
public boolean isOccupied() { //check if the node is occupied by a player
return this.occupied;
}
public void setPrevious(node n) { //set the node from which this node was reached
this.cameFrom = n;
}
public node getPrevious() { //return the node from which this node was reach
return this.cameFrom;
}
public void knockPrevious() {
this.cameFrom = null;
}
public double getFScore(){ //return fScore for player 1
return this.fScore;
}
public double getSecondaryFScore() { //return fScore for player 2
return this.secFScore;
}
public void setSecondaryFScore(double d) { //set the fScore for player 2
this.secFScore = d;
}
public void setFScore(double d) { //set fScore for player 1
this.fScore = d;
}
public int getValue() { //return the value of this node
return this.value;
}
public double getHValue() { //return the heuristic value, the straight line distance from here to the goal node
return this.heuristic;
}
public double getHToPrey() { //straight line distance between this node and the node occupied by player 1
return this.hToPrey;
}
public void setValue(int n) { //set the value as n
this.value = n;
}
public void addNeighbor(node member) { //add a neighbor
if(!this.neighbors.contains(member)) {
this.neighbors.add(member);
}
}
public List<node> getNeighbors() { //return the array of neighbors
return this.neighbors;
}
public void setHValue(double d) { //set the heuristic value
this.heuristic = d;
}
public void setHToPrey(double d) { //set the secondary heuristic value
this.hToPrey = d;
}
public void occupy() { //designate the node as occupied
this.occupied = true;
}
public void deoccupy() { //designate the node as unoccupied
this.occupied = false;
}
public boolean isGoal() { //check to see if this is the goal
return this.goal;
}
public void setGoal() { //designate the node as the goal
this.goal = true;
}
public void setTempV(int n) { //set a temporary value with which to replace the value in a moment
this.tempV = n;
}
public void smooth() { //replace the value with the tempValue
this.value = this.tempV;
}
public void setCost(int n) { //set the cost to get here from start
this.cost = n;
}
public void setSecCost(int n) { //set the cost to get here from player 2 start
this.secCost = n;
}
public int getSecCost() { //return the cost to get here from player 2 start
return this.secCost;
}
public int getCost() {//return the cost to get here from start
return this.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")
}
*/
import java.util.ArrayList;
import java.util.List;
public interface nodeInterface {
int value = 0;
boolean occupied = false;
double heuristic = -0.1;
double hToPrey = -0.1;
int tempV = 0;
boolean goal = false;
node cameFrom = null;
List<node> neighbors = new ArrayList<node>();
int cost = 0;
double fScore = -0.1;
int x = 0;
int y = 0;
int secCost = 0;
double secFScore = -0.1;
int getX();
int getY();
void setX(int n);
void setY(int n);
boolean isOccupied();
void setPrevious(node n);
node getPrevious();
void knockPrevious();
double getFScore();
double getSecondaryFScore();
void setSecondaryFScore(double d);
void setFScore(double d);
int getValue();
double getHValue();
double getHToPrey();
void setValue(int n);
void addNeighbor(node member);
List<node> getNeighbors();
void setHValue(double d);
void setHToPrey(double d);
void occupy();
void deoccupy();
boolean isGoal();
void setGoal();
void setTempV(int n);
void smooth();
void setCost(int n);
void setSecCost(int n);
int getSecCost();
int getCost();
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Player{
private node node;
public Player(node n) {
this.node = n;
}
List<node> pathfind() { //this is the A* algorithm that finds a path from the given node to the goal
List<node> neighborhood; //neighbors of each node
node x; //node taken from neighborhood list
node current; //the current node
int tentativeGScore; //
this.node.setCost(0);
List<node> openSet = new ArrayList<node>();
List<node> closedSet = new ArrayList<node>();
openSet.add(this.node);
while(openSet.size() != 0) { //while openSet is not empty
current = openSet.remove(0); //get the first node
if(current.isGoal()) { //if current = goal
//System.out.println("you found the goal. Now you just have to get there")
return this.reconstructPath(current); //rebuild the path and return as an array
}
//openSet.remove(current)
closedSet.add(current); //take current from openSet and add it to closedSet
neighborhood = current.getNeighbors(); //get your neighbors
for(int i = 0; i < neighborhood.size(); i++) {
x = neighborhood.get(i);
//look at each neighbor
if(!closedSet.contains(x)) { //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.contains(x)) {//if neighbor not in openSet, Discover a new node
openSet.add(x);}
openSet.sort(Comparator.comparing(nodeInterface::getFScore)); //test this
if(tentativeGScore <= x.getCost()) {//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
}
//System.out.println("out of for statement")
}
//System.out.println("out of while loop")
}
return null; //you dun goofed, somehow. not sure how. good luck
}
List<node> reconstructPath(node current) { //build path to allow for selection of a move
List<node> totalPath = new ArrayList<node>(); //current is goal
totalPath.add(current);
while(current.getPrevious() != null) { //while you haven't gotten to the beginning
current = current.getPrevious(); //get the previous one
totalPath.add(current);} //add it to the path
Collections.reverse(totalPath); //path created in reverse
return totalPath;
}
void printPath(node[] path) {
String line;
for(int i = 0; i < path.length; i++) {
line = "(" + String.valueOf(path[i].getX()) +"," + String.valueOf(path[i].getY()) +")";
System.out.println(line);}
}
int move() { //take the first node from path, move there. check if it's the goal. you could implement a movement points system to play off of the nodes having a cost
this.getNode().deoccupy(); //unoccupy your current node
System.out.print("(" + this.getNode().getX() + "," + this.getNode().getY() + ") " + "player 1");
System.out.println();
List<node> path = this.pathfind(); //find a path
this.setNode(path.remove(1)); //get the first one after the node you are one, p[0] is current node
this.getNode().occupy(); //occupy the next one
if(this.getNode().isGoal()) { //you won!!!!
System.out.println("Player 1 won!");
return 1;
}
return 0; //figure out a better return value?
}
node getNode() { //return the node that this player is at
return this.node;
}
void setNode(node n) { //set the node n to the player's node value
this.node = n;
}
void testRide() { //test
System.out.println("start");
gridMap x = new gridMap(100, 100);
x.build();
List<node> testArray = new ArrayList<node>();
node imp;
for(int i = 0; i < 100; i++) {
imp = new node(10);
testArray.add(imp);
}
for(int a = 0; a < testArray.size(); a++) {
testArray.get(a).setFScore(100-a); //insure disordered entries
}
System.out.println("unsorted array");
for(int b = 0; b < testArray.size(); b++) {
System.out.println(testArray.get(b).getFScore());
}
//this.sort(testArray);
testArray.sort(Comparator.comparing(nodeInterface::getFScore));
System.out.println("sorted array");
for(int c = 0; c < testArray.size(); c++) {
System.out.println(testArray.get(c).getSecondaryFScore()); //I know it works up to here
node start = x.randNode();
System.out.println("x and y values of start node");
System.out.println(start.getY());
System.out.println(start.getX());
List<node> testPath = this.pathfind();
for(int i = 0; i < testPath.size(); i++) {
System.out.println("x and y values");
System.out.println(testPath.get(i).getX());
System.out.println(testPath.get(i).getY());
System.out.println("");
}
System.out.println("");
node peep = this.getNode();
System.out.printf("X value before move", peep.getX());
System.out.printf("Y value before move", peep.getY());
this.move();
node pipe = this.getNode();
System.out.printf("X value after move", pipe.getX());
System.out.printf("Y value after move", pipe.getY());
if(peep.getX() == pipe.getX() && peep.getY() == pipe.getY()) {
}
System.out.println("movement failure"); //I think you need to make a new err object
}
int counter = 0;
while(this.getNode().isGoal() != true) {
counter++;
this.move();
}
System.out.println("number of moves from beginning to end");
System.out.println(counter);
System.out.printf("X value after move", this.node.getX());
System.out.printf("Y value after move", this.node.getY());
}
}
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
class predPlayer{
private node node;
private Player alien;
private List<node> path;
public predPlayer(node n) {
this.node = n;
this.node.setSecCost(0);
alien = new Player(n);
}
List<node> pathfind(){
int tentativeGScore;
node x;
node current;
List<node> neighborhood;
List<node> openSet = new ArrayList<node>();
List<node> closedSet = new ArrayList<node>();
openSet.add(this.node);
while(openSet.size() != 0) { //while openSet is not empty
current = openSet.remove(0);
if(current.isOccupied()) { //if current = goal
//System.out.println("You found player 1"); check to make sure overriden correctly
return this.reconstructPath(current);}
closedSet.add(current);
neighborhood = current.getNeighbors();
for(int i = 0; i < neighborhood.size(); i++) {
x = neighborhood.get(i); //look at each neighbor
if(!closedSet.contains(x)) { //if x is not in the closed set, get the gScore
tentativeGScore = current.getSecCost() + x.getValue();
if(!openSet.contains(x))//if neighbor not in openSet, Discover a new node
openSet.add(x);
openSet.sort(Comparator.comparing(nodeInterface::getSecondaryFScore));
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());}
}
}
}
return null;
}
List<node> reconstructPath(node current) { //build path to allow for selection of a move
return alien.reconstructPath(current);
}
void printPath(node[] path) {
alien.printPath(path);
}
int move() { //take the first node from path, move there. check if it's the goal.
System.out.print("(" + this.getNode().getX() + "," + this.getNode().getY() + ") " + "player 2");
System.out.println();
List<node> path = this.pathfind(); //find a path
alien.setNode(path.remove(1)); //get the first one after the node you are one, p[0] is current node
if(alien.getNode().isOccupied()) { //you won!!!!
System.out.println("Player 2 won!");
return 1;
}
return 0; //figure out a better return value?
}
node getNode() { //return the node that this player is occupying
return alien.getNode();
}
void setNode(node n) { //set the node n to the player's node value
alien.setNode(n);
}
void testRide() { //test
alien.testRide();
}
}
class tester{
public tester() {
//System.out.println("node test");
//node testNode = new node(9) ;
//testNode.testRide(); //run unit test for node
System.out.println("map test");
gridMap testMap = new gridMap(10, 10);
testMap.testRide(); //unit test for map
System.out.println("done");
System.out.println("player test");
Player testPlayer = new 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