import java.awt.*; import java.awt.image.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.*; class GeneticPanel extends JPanel { int xwidth = 800, ywidth = 600, tilewidth = 20; BufferedImage img = new BufferedImage(xwidth, ywidth, BufferedImage.TYPE_INT_RGB); Graphics buffer = img.getGraphics(); javax.swing.Timer timer = new javax.swing.Timer(0, new Listener()); int comps = 5; int rmax= 20; int cmax = 30; int grid[][] = new int[rmax][cmax]; int rcomp[]=new int[comps], ccomp[]=new int[comps]; Color color[] = new Color[comps]; int totalalgs = 3; double alg[][] = new double[comps][totalalgs]; int score[] = new int[comps]; boolean done[] = new boolean[comps]; int rend, cend; int rman, cman; double mutspeed = 0.1; double wallprob = 0.55; int turn=-1; int totalrounds = 0; int totalscore[] = new int[comps]; boolean paused = false; boolean player = false; boolean mandone = false; int manscore = -1; public void move(int comp) { int bestrch=0, bestcch=0; double bestfit=calcFit(rcomp[comp],ccomp[comp],alg[comp]); for(int rchange=-1; rchange<=1; rchange++) for(int cchange=-1; cchange<=1; cchange++) { if(rchange==0&&cchange==0) continue; int rnew = rcomp[comp]+rchange, cnew = ccomp[comp]+cchange; if(rnew<0||rnew>=rmax||cnew<0||cnew>=cmax||grid[rnew][cnew]/10==0) continue; double fit = calcFit(rnew, cnew, alg[comp]); if(fit>bestfit){bestfit=fit;bestrch=rchange;bestcch=cchange;} } rcomp[comp]+=bestrch; ccomp[comp]+=bestcch; return; } public double calcFit(int r, int c, double[] alg) { double fit = 0; for(int n=0; n=rmax||cnew<0||cnew>=cmax) continue; if(grid[rnew][cnew]/10!=0) movable = true; } if(!movable) return false; for(int n=0; n<100; n++) { int rchange = (int)(Math.random()*3)-1; int cchange = (int)(Math.random()*3)-1; int rnew = rend+rchange, cnew = cend+cchange; if(rnew<0||rnew>=rmax||cnew<0||cnew>=cmax) continue; if(grid[rnew][cnew]/10!=0) { //moveAlgorithm here moveEndTo(rnew, cnew); //always move return true; } } return false; } public boolean moveEndTo(int r, int c) { if(r<0||r>=rmax||c<0||c>=cmax) return false; rend = r; cend = c; return true; } public boolean manMove(int rchange, int cchange) { int rnew = rman+rchange, cnew = cman+cchange; if(rnew<0||rnew>=rmax||cnew<0||cnew>=cmax) return false; if(grid[rnew][cnew]/10==0) return false; rman = rnew; cman = cnew; endTurn(); return true; } public void drawAll() { buffer.setColor(Color.GREEN); buffer.fillRect(90,90, Math.max(0,xwidth-180), Math.max(0,ywidth-180)); for(int r=0; r=20||grid[r][c]<0) buffer.setColor(Color.GREEN); buffer.fillRect(100+tilewidth*c, 100+tilewidth*r, tilewidth, tilewidth); } for(int n=0; n=97 && key<=105 && player) manMove(1-(key-97)/3, (key-97)%3-1); } } public GeneticPanel() throws IOException // Constructor { init(); for(int n=0; n0) return -1; if(s<0) return 1; return 0; } } public class simple { public static void main(String args[]) throws IOException { JFrame frame = new JFrame("Genetic"); frame.setBackground(Color.BLACK); frame.setForeground(Color.WHITE); frame.setSize(800, 600); frame.setLocation(0, 0); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GeneticPanel app = new GeneticPanel(); frame.setContentPane(app); frame.setVisible(true); } // end of main }