public static float GetFitnessForMatrix(CAMatrix matMatrix) { float dTotalFitness = 0; for (int x = 0; x < matMatrix.Width; x++) { for (int y = 0; y < matMatrix.Width; y++) { CACell celCurrCell = matMatrix.GetCell(x, y); // If the cell has nothing in it, skip it.. if (celCurrCell == null) { continue; } if (celCurrCell.clrTeamColor == PLAYER_COLOR) { dTotalFitness += 10 + celCurrCell.fAvaliableEnergy; } } } return(dTotalFitness); }
public static void DisplayMatrix(CAMatrix matMatrix, Graphics grpGraphics) { for (int x = 0; x < matMatrix.Width; x++) { for (int y = 0; y < matMatrix.Width; y++) { CACell celCurrCell = matMatrix.GetCell(x, y); // If the cell has nothing in it, skip it.. if (celCurrCell == null) { grpGraphics.FillRectangle(new SolidBrush(Color.Black), x * 10, y * 10, 10, 10); continue; } Color clrTeamColor = celCurrCell.clrTeamColor; float fEnergy = celCurrCell.fAvaliableEnergy; int nWantedRed = (int)((clrTeamColor.R / 4) + (fEnergy * 30)); int nWantedGreen = (int)((clrTeamColor.G / 4) + (fEnergy * 30)); int nWantedBlue = (int)((clrTeamColor.B / 4) + (fEnergy * 30)); int nRed = Math.Min(nWantedRed, 255); int nGreen = Math.Min(nWantedGreen, 255); int nBlue = Math.Min(nWantedBlue, 255); Color clrCombinedWithEnergy = Color.FromArgb(nRed, nGreen, nBlue); grpGraphics.FillRectangle(new SolidBrush(clrCombinedWithEnergy), x * 10, y * 10, 10, 10); } } }
public static float Look(CACell celCurrCell, Direction dirDirection) { int nNextCellX = celCurrCell.x + dirDirection.xDir; int nNextCellY = celCurrCell.y + dirDirection.yDir; // Test if the location is valid if (!(celCurrCell.matParentMatrix.IsLocationValid(nNextCellX, nNextCellY))) { return(2); } // Find out if there's a cell there CACell celNeighbourCell = celCurrCell.matParentMatrix.GetCell(nNextCellX, nNextCellY); if (celNeighbourCell == null) { return(3); } // Is it the same team as mine? or is the neighbour cell on the other team? if (celCurrCell.clrTeamColor == celNeighbourCell.clrTeamColor) { return(1); } else { return(0); } }
public static float ConstructNewCell(CACell celCurrCell, Direction dirDirection) { // If there is no energy left, don't do it. if (celCurrCell.fAvaliableEnergy < ENERGY_NEEDED_FOR_CONSTRUCTION) { return(3); } int nNextCellX = celCurrCell.x + dirDirection.xDir; int nNextCellY = celCurrCell.y + dirDirection.yDir; // Test if the location is valid if (!(celCurrCell.matParentMatrix.IsLocationValid(nNextCellX, nNextCellY))) { return(2); } // Test if there's an already cell there : if (celCurrCell.matParentMatrix.GetCell(nNextCellX, nNextCellY) != null) { return(0); } // Clone the current program, assigning it the same "TeamID" : // The new cell would have zero energy. BaseProgram progClonedProgram = (BaseProgram)celCurrCell.progProgram.Clone(); celCurrCell.matParentMatrix.CreateCell(nNextCellX, nNextCellY, progClonedProgram, celCurrCell.clrTeamColor, 0); // Substract the total energy the cell has. celCurrCell.fAvaliableEnergy -= ENERGY_NEEDED_FOR_CONSTRUCTION; return(1); }
public static float SellExistingCell(CACell celCurrCell, Direction dirDirection) { int nNextCellX = celCurrCell.x + dirDirection.xDir; int nNextCellY = celCurrCell.y + dirDirection.yDir; // Test if the location is valid if (!(celCurrCell.matParentMatrix.IsLocationValid(nNextCellX, nNextCellY))) { return(2); } CACell celNeighbourCell = celCurrCell.matParentMatrix.GetCell(nNextCellX, nNextCellY); // Find out if there's a cell there if (celNeighbourCell == null) { return(0); } // Assign an energy transfer from deleted cell to the new cell EnergyTransfer trnTransfer = new EnergyTransfer(); trnTransfer.celFrom = celNeighbourCell; trnTransfer.celTo = celCurrCell; trnTransfer.dEnergy = ENERGY_GIVEN_BY_SELLING; celCurrCell.matParentMatrix.lstCellEnergyTransfersOne.Add(trnTransfer); // Assign a cell delete celCurrCell.matParentMatrix.lstCellDeletesForNextTime.Add( new Point(nNextCellX, nNextCellY)); return(1); }
public override float action(Node[] arrNodes, TreeLanguageEvolute.TreeProgram progOwner) { float fDummy = arrNodes[0].ReturnValue(progOwner); CACell caCurrCell = ((CACell)progOwner.AdditionalInformation); return(caCurrCell.fAvaliableEnergy); }
public CAMatrix() { matMatrix = new CACell[WIDTH][]; for (int i = 0; i < WIDTH; i++) { matMatrix[i] = new CACell[HEIGHT]; } }
public override float action(Node[] arrNodes, TreeLanguageEvolute.TreeProgram progOwner) { CACell caCurrCell = ((CACell)progOwner.AdditionalInformation); float dEnergyToSubstract = (int)arrNodes[0].ReturnValue(progOwner); // Substract from the current cell and give to the other one. float dResult = SharedEvolutionFunctions.SendEnergy(caCurrCell, SharedEvolutionFunctions.DIRECTION_RIGHT, dEnergyToSubstract); return(dResult); }
public void CreateCell(int x, int y, BaseProgram progProgram, Color clrTeamColor, float fAvaliableEnergy) { CACell celCell = new CACell(); celCell.progProgram = progProgram; celCell.clrTeamColor = clrTeamColor; celCell.fAvaliableEnergy = fAvaliableEnergy; celCell.x = x; celCell.y = y; celCell.matParentMatrix = this; lstNewCellsForNextTime.Add(celCell); }
public static void RunTimeStep(CAMatrix matMatrix) { int nWidth = matMatrix.Width; int nHeight = matMatrix.Height; for (int x = 0; x < nWidth; x++) { for (int y = 0; y < nHeight; y++) { CACell celCurrCell = matMatrix.GetCell(x, y); // If the cell has nothing in it, skip it.. if (celCurrCell == null) { continue; } // The cell has a program in it. run it. celCurrCell.progProgram.Run(celCurrCell); } } // Deleting cells for (int i = 0; i < matMatrix.lstCellDeletesForNextTime.Count; i++) { matMatrix.DestroyCell(matMatrix.lstCellDeletesForNextTime[i].X, matMatrix.lstCellDeletesForNextTime[i].Y); } matMatrix.lstCellDeletesForNextTime.Clear(); // Applying new cells waiting to creation for (int i = 0; i < matMatrix.lstNewCellsForNextTime.Count; i++) { matMatrix.PutCell(matMatrix.lstNewCellsForNextTime[i]); } matMatrix.lstNewCellsForNextTime.Clear(); // Transferring energies for (int i = 0; i < matMatrix.lstCellEnergyTransfersOne.Count; i++) { EnergyTransfer trnTransfer = matMatrix.lstCellEnergyTransfersOne[i]; if (trnTransfer.celFrom.fAvaliableEnergy >= trnTransfer.dEnergy) { trnTransfer.celFrom.fAvaliableEnergy -= trnTransfer.dEnergy; trnTransfer.celTo.fAvaliableEnergy += trnTransfer.dEnergy; } } matMatrix.lstCellEnergyTransfersOne.Clear(); }
public static float SendEnergy(CACell celCurrCell, Direction dirDirection, float fWantedEnergy) { float fTransferringEnergy = fWantedEnergy; // Check that the energy to transmit is above 0. if (fWantedEnergy <= 0) { return(3); } // If there is no energy left, then spend all the energy you can to send // The wanted energy. if (celCurrCell.fAvaliableEnergy < fWantedEnergy) { fTransferringEnergy = celCurrCell.fAvaliableEnergy; } int nNextCellX = celCurrCell.x + dirDirection.xDir; int nNextCellY = celCurrCell.y + dirDirection.yDir; // Test if the location is valid if (!(celCurrCell.matParentMatrix.IsLocationValid(nNextCellX, nNextCellY))) { return(2); } CACell celNeighbourCell = celCurrCell.matParentMatrix.GetCell(nNextCellX, nNextCellY); // Test if there's a cell there. if (celNeighbourCell == null) { return(0); } // Assign an energy transfer from cell one to the new cell EnergyTransfer trnTransfer = new EnergyTransfer(); trnTransfer.celFrom = celCurrCell; trnTransfer.celTo = celNeighbourCell; trnTransfer.dEnergy = fTransferringEnergy; celCurrCell.matParentMatrix.lstCellEnergyTransfersOne.Add(trnTransfer); return(1); }
public void PutCell(CACell celCell) { celCell.matParentMatrix = this; matMatrix[celCell.x][celCell.y] = celCell; }