Browse Source

DEV: First try of a tree

master
Christos Houtouridis 6 years ago
parent
commit
60118bf1a7
5 changed files with 84 additions and 57 deletions
  1. BIN
      bin/gr/auth/ee/dsproject/pacman/Creature.class
  2. +1
    -1
      src/gr/auth/ee/dsproject/node/Globals.java
  3. +3
    -3
      src/gr/auth/ee/dsproject/node/MinMaxTree.java
  4. +10
    -3
      src/gr/auth/ee/dsproject/node/Node89978445.java
  5. +70
    -50
      src/gr/auth/ee/dsproject/pacman/Creature.java

BIN
bin/gr/auth/ee/dsproject/pacman/Creature.class View File


+ 1
- 1
src/gr/auth/ee/dsproject/node/Globals.java View File

@@ -53,7 +53,7 @@ public class Globals {
/*
* Tree settings
*/
public static final int MINMAXTREE_MAX_DEPTH = 2;
public static final int MINMAXTREE_MAX_DEPTH = 2; // MUST be multiple of 2
/*
* In order to find out when a ghost is inside a cavity we manualy


+ 3
- 3
src/gr/auth/ee/dsproject/node/MinMaxTree.java View File

@@ -58,7 +58,7 @@ public class MinMaxTree
* @return Reference to inserted node
* If insert fail, returns null
*/
public Node89978445 insert (Node89978445 node, Node89978445 parent) {
public static Node89978445 insert (Node89978445 node, Node89978445 parent) {
if (parent.children.add (node)) {
node.parent = parent;
return node;
@@ -75,7 +75,7 @@ public class MinMaxTree
* @return Reference to the child with the minimum evaluation value
* If parent has no children null.
*/
public Node89978445 minChild (Node89978445 parent)
public static Node89978445 minChild (Node89978445 parent)
{
Node89978445 node = null;
double ev, min = Globals.EVAL_MAX+1;
@@ -97,7 +97,7 @@ public class MinMaxTree
* @return Reference to the child with the maximum evaluation value
* If parent has no children null.
*/
public Node89978445 maxChild (Node89978445 parent)
public static Node89978445 maxChild (Node89978445 parent)
{
Node89978445 node = null;
double ev, max = Globals.EVAL_MIN-1;


+ 10
- 3
src/gr/auth/ee/dsproject/node/Node89978445.java View File

@@ -29,7 +29,7 @@ public class Node89978445
* Pacman's current move evaluation
* This is used also as the "return status" of the object
*/
int[] nodeXY; // Pacman's current x,y coordinate
int[] nodeXY; // Pacman's current x,y coordinate
int[][] currentGhostPos; // Array holding the Ghost (x,y) pairs
@@ -68,7 +68,7 @@ public class Node89978445
currentGhostPos = new int [PacmanUtilities.numberOfGhosts][2];
flagPos = new int [PacmanUtilities.numberOfFlags][2];
currentFlagStatus = new boolean[PacmanUtilities.numberOfFlags];
ArrayList<Node89978445> children = new ArrayList<Node89978445> ();
children = new ArrayList<Node89978445> ();
}
/**
@@ -88,7 +88,7 @@ public class Node89978445
currentGhostPos = new int [PacmanUtilities.numberOfGhosts][2];
flagPos = new int [PacmanUtilities.numberOfFlags][2];
currentFlagStatus = new boolean[PacmanUtilities.numberOfFlags];
ArrayList<Node89978445> children = new ArrayList<Node89978445> ();
children = new ArrayList<Node89978445> ();
}
/*
@@ -134,6 +134,13 @@ public class Node89978445
return nodeEvaluation;
}
public int[] getCurentPacmanPos () {
return nodeXY;
}
public int[][] getCurrentGhostPos () {
return currentGhostPos;
}
/*
* ============= Helper API ============
*/


+ 70
- 50
src/gr/auth/ee/dsproject/pacman/Creature.java View File

@@ -47,58 +47,78 @@ public class Creature implements gr.auth.ee.dsproject.pacman.AbstractCreature
/**
* @brief
* Create node for each one of the possible moves (0, 1, 2, 3) and
* evaluate these moves. After that feeds the valid ones to an ArrayList
* and select the best of them to return
* evaluate these moves.
*/
// public int calculateNextPacmanPosition (Room[][] Maze, int[] currPosition)
// {
// Node89978445 node = new Node89978445 (); // make a dummy node
// MinMaxTree rt = new MinMaxTree (node); /*<
// * Plant a tree with dummy root
// * Null parent and no evaluation
// */
//
// createSubTreePacman (0,node, Maze, currPosition);
// return 0;
// }
public int calculateNextPacmanPosition (Room[][] Maze, int[] currPosition)
{
Node89978445 cur = new Node89978445 (Maze, currPosition);
Node89978445 moveNode;
//ArrayList<Node89978445> moveNodes = new ArrayList<Node89978445> ();
double ev, max = Globals.NO_EVAL;
int decision = -1;
int[] nextPosition = {-1, -1};
/*
* loop the possible moves and fill them to list
* mark the move with the maximum evaluation value
*/
for (int i=0 ; i<4 ; ++i) {
if ((nextPosition = Node89978445.pacmanValidMove(cur, i)) != Globals.FALSE_POS) {
moveNode = new Node89978445 (Maze, nextPosition);
ev = moveNode.getEvaluation();
if (ev > max) {
max = ev;
decision = i;
}
}
}
return decision;
}
void createSubTreePacman (int depth, Node89978445 parent, Room[][] Maze, int[] currPacmanPosition)
{
// TODO Fill This
}
public int calculateNextPacmanPosition (Room[][] Maze, int[] currPosition)
{
Node89978445 cur = new Node89978445 (Maze, currPosition);
MinMaxTree mmTree = new MinMaxTree (cur); /*<
* Plant a tree with dummy root
* Null parent and no evaluation
*/
createSubTreePacman (0, cur, Maze, currPosition); //make the tree
// min-max
return 0;
}
void createSubTreePacman (int depth, Node89978445 parent, Room[][] Maze, int[] curPacmanPosition)
{
if (++depth > Globals.MINMAXTREE_MAX_DEPTH)
return;
else {
Node89978445 newNode;
Room[][] newMaze;
int[] nextPosition = {-1, -1};
// scan possible valid moves
for (int move=0 ; move<4 ; ++move) {
if ((nextPosition = Node89978445.pacmanValidMove (parent, move)) != Globals.FALSE_POS) {
// Make a copy of the maze in order to simulate next move and move Pacman
newMaze = PacmanUtilities.copy (Maze);
PacmanUtilities.movePacman (newMaze, parent.getCurentPacmanPos(), nextPosition);
// Create a node for the move in the new stated game maze
newNode = new Node89978445 (newMaze, nextPosition);
newNode.getEvaluation();
MinMaxTree.insert (newNode, parent); // add node to the min-max tree
// call the recursive ranch creator
createSubTreeGhosts (depth, newNode, newMaze, newNode.getCurrentGhostPos());
}
}
}
}
void createSubTreeGhosts (int depth, Node89978445 parent, Room[][] Maze, int[][] currGhostsPosition)
{
// TODO Fill This
}
void createSubTreeGhosts (int depth, Node89978445 parent, Room[][] Maze, int[][] currGhostsPosition)
{
if (++depth > Globals.MINMAXTREE_MAX_DEPTH)
return;
else {
Node89978445 newNode;
Room[][] newMaze;
ArrayList<int[][]> ghostMoves;
// Get all possible ghost moves
ghostMoves = PacmanUtilities.allGhostMoves(Maze, currGhostsPosition);
// loop all ghost moves
int s = ghostMoves.size();
for (int i=0 ; i<s ; ++i) {
// Make a copy of the maze in order to simulate next move and move ghosts
newMaze = PacmanUtilities.copy (Maze);
PacmanUtilities.moveGhosts(newMaze, currGhostsPosition, ghostMoves.get(i));
// Create a node for the move in the new stated game maze
newNode = new Node89978445 (newMaze, parent.getCurentPacmanPos());
newNode.getEvaluation();
MinMaxTree.insert (newNode, parent); // add node to the min-max tree
//recursive call for the rest of the tree
//createSubTreePacman (depth, newNode, newMaze, parent.getCurentPacmanPos());
}
}
}
public int[] getPacPos (Room[][] Maze)
{


Loading…
Cancel
Save