Advertisement
Elec0

Genetic Algorithm Exp. 1 Phase 1 Code

Oct 1st, 2012 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.94 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.util.ArrayList;
  3.  
  4. import info.gridworld.actor.Actor;
  5. import info.gridworld.actor.Critter;
  6. import info.gridworld.grid.Location;
  7.  
  8.  
  9. public class GenomeBase extends Critter
  10. {
  11.     private static int DEFAULT_ATK = 1, DEFAULT_DEF = 1, DEFAULT_CON = 1;
  12.     private int dAtk, dDef, dCon;
  13.     public int Atk, Def, Con;
  14.     private byte DefRegenStep;
  15.     private boolean canAttack;
  16.    
  17.     /**
  18.      * Default constructor of GenomeBase, all stats default to 1
  19.      */
  20.     public GenomeBase()
  21.     {
  22.         dAtk = Atk = DEFAULT_ATK;
  23.         dDef = Def = DEFAULT_DEF;
  24.         dCon = Con = DEFAULT_CON;
  25.        
  26.         canAttack = true;
  27.        
  28.         setColor(Color.white);
  29.     }
  30.     public GenomeBase(int Atk, int Def, int Con)
  31.     {
  32.         // Set all default and current stats
  33.         this.dAtk = this.Atk = Atk;
  34.         this.dDef = this.Def = Def;
  35.         this.dCon = this.Con = Con;
  36.        
  37.         canAttack = true;
  38.        
  39.         setColor(Color.white); // Default GenomeBase color is white
  40.     }
  41.    
  42.     /**
  43.      * Attack another GenomeBase
  44.      * @param toAttack, the GenomeBase to attack
  45.      */
  46.     public void attack(GenomeBase toAttack)
  47.     {
  48.         if(canAttack)
  49.         {
  50.             canAttack = false;
  51.             toAttack.attacked(this);
  52.         }
  53.     }
  54.    
  55.     /**
  56.      * Being attacked by another GenomBase
  57.      * This is private to avoid confusion, attack another GenomeBase through attack()
  58.      * @param attacker the GenomeBase who is attacking
  59.      */
  60.     private void attacked(GenomeBase attacker)
  61.     {
  62.         if(attacker.Atk > 0)
  63.         {
  64.             int DmgIncoming = attacker.Atk;
  65.             if(Def > 0)
  66.             {
  67.                 DmgIncoming -= Def;
  68.                 Def -= DmgIncoming;
  69.                
  70.                 if(Def < 0)
  71.                     Def = 0;
  72.                
  73.                 if(DmgIncoming > 0)
  74.                 {
  75.                     Con -= DmgIncoming;
  76.                     if(Con <= 0)
  77.                     {
  78.                         removeSelfFromGrid();
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.        
  84.         // Repoiste, if still alive
  85.         attack(attacker);
  86.     }
  87.    
  88.     /**
  89.      * GenomeBase regeneration, gains Con and Def with conditions.
  90.      */
  91.     private void regen()
  92.     {
  93.         if(Con < dCon)
  94.             Con++;
  95.         if(DefRegenStep == 2)
  96.         {
  97.             DefRegenStep = 0;
  98.             Def++;
  99.         }
  100.         else
  101.             DefRegenStep++;
  102.     }
  103.    
  104.     // Override Methods
  105.     @Override
  106.     public void act()
  107.     {
  108.         if (getGrid() == null)
  109.             return;
  110.         ArrayList<Actor> actors = getActors();
  111.         processActors(actors);
  112.        
  113.         if(getLocation() == null) return; // Apparently stuff goes wrong when you remove an actor from the grid in the middle of stuff?
  114.        
  115.         ArrayList<Location> moveLocs = getMoveLocations();
  116.         Location loc = selectMoveLocation(moveLocs);
  117.         makeMove(loc);
  118.     }
  119.  
  120.     @Override
  121.     public ArrayList<Actor> getActors()
  122.     {
  123.         return getGrid().getNeighbors(getLocation());
  124.     }
  125.  
  126.     @Override
  127.     public void processActors(ArrayList<Actor> actors)
  128.     {
  129.         canAttack = true;
  130.        
  131.         if(Def == dDef && Con == dCon)
  132.         {
  133.             // We're ready, let's attack if we can
  134.            
  135.             ArrayList<GenomeBase> genomeList = new ArrayList<GenomeBase>();
  136.             for(Actor a : actors)
  137.             {
  138.                 if(a instanceof GenomeBase) // Get all other genomes around so we can attack them
  139.                 {
  140.                     genomeList.add((GenomeBase) a);
  141.                 }
  142.             }
  143.            
  144.             int num = genomeList.size();
  145.            
  146.             if(num > 0) // There are other actors around us, let's attack
  147.             {
  148.                 GenomeBase toAttack = genomeList.get(((int) Math.random() * num));
  149.                 attack(toAttack);
  150.             }
  151.         }
  152.         else
  153.         {
  154.             regen(); // Spend a turn regenerating Con and Def
  155.         }
  156.     }
  157.  
  158.     @Override
  159.     public ArrayList<Location> getMoveLocations()
  160.     {
  161.         return getGrid().getEmptyAdjacentLocations(getLocation());
  162.     }
  163.  
  164.     @Override
  165.     public Location selectMoveLocation(ArrayList<Location> locs)
  166.     {
  167.         int n = locs.size();
  168.         if (n == 0)
  169.             return getLocation();
  170.         int r = (int) (Math.random() * n);
  171.         return locs.get(r);
  172.     }
  173.  
  174.     @Override
  175.     public void makeMove(Location loc)
  176.     {
  177.         setDirection(getLocation().getDirectionToward(loc));
  178.         super.makeMove(loc);
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement