/// <summary> /// Checks if some shape matches at a given location. This is done by checking the next few rows and columns from the /// given location and checking they all match the shape. /// </summary> /// <param name="s">The shape to check</param> /// <param name="row">The row of the top left cell to look at</param> /// <param name="col">The column of the top left cell to look at</param> /// <returns>True if all cells match the given colours of the shape and false otherwise. The colour -1 is used to represent a wildcard</returns> private bool cellMatch(Shape s, int row, int col) { Cell[][] checkCells = new Cell[s.sizeRow()][]; for (int i = 0; i < s.sizeRow(); i++) { checkCells[i] = new Cell[s.sizeCol()]; for (int j = 0; j < s.sizeCol(); j++) { checkCells[i][j] = cells[i + row][j + col]; } } bool isMatch = true; int sizeCol = s.sizeCol(); int sizeRow = s.sizeRow(); for (int compCol = 0; compCol < sizeCol; compCol++) { for (int compRow = 0; compRow < sizeRow; compRow++) { if(s.getColour(compRow, compCol) == -1) { //do nothing, -1 represents the wildcard } else if(s.getColour(compRow, compCol) == checkCells[compRow][compCol].getDomColour()) { //do nothing, the cells match } else { isMatch = false; //Otherwise they don't match so the shape doesn't match at this location } } } #if DEBUG if (isMatch) { } #endif return isMatch; }
/// <summary> /// Matches a given shape across the entire genome, adding the necessary posMods and negMods /// </summary> /// <param name="s">The shape to match across the whole genome</param> private void patternMatch(Shape s) { for (int i = 0; i <= cells.Length - s.sizeRow(); i++) { for (int j = 0; j <= cells[i].Length - s.sizeCol(); j++) { if (cellMatch(s, i, j)) { posMods.AddRange(s.getPosMods()); negMods.AddRange(s.getNegMods()); } } } }
/// <summary> /// Parses all the shapes from the specified files into a List of Shape objects, that can be used by the Genes to set up their parameters /// </summary> /// <param name="fileLocation">The location of the file to parse the shapes from</param> private void parseShapes(String fileLocation) { try { StreamReader sr = new StreamReader(fileLocation); recognisedShapes = new List<Shape>(); string s = sr.ReadLine(); while (!s.Equals("### BEGIN ###")) //parser starts at ### BEGIN ### { s = sr.ReadLine(); } s = sr.ReadLine(); while (s != null) { string[] dims = s.Split('x'); //each shape begins with a description of its dimensions in the form AxB; int dimX = int.Parse(dims[0]); int dimY = int.Parse(dims[1]); s = sr.ReadLine(); int[][] cells = new int[dimX][]; List<ParamToken> pMods = new List<ParamToken>(); List<ParamToken> nMods = new List<ParamToken>(); for(int i = 0; i < dimY; i++) { cells[i] = new int[dimY]; char[] c = s.ToCharArray(); for(int j = 0; j < cells[i].Length; j++) { if(c[j].Equals('A')) //A stands for any value, represented by -1 in the shape text { cells[i][j] = -1; } else { cells[i][j] = (int)char.GetNumericValue(c[j]); cells[i][j]--; } } s = sr.ReadLine(); } s = sr.ReadLine(); while(!s.Equals("###")) //param input ends at ### { char[] split = s.ToCharArray(); char[] t = new char[] { split[0], split[1], split[2] }; string ident = new string(t); ParamToken p = ParamToken.STRENGTH; if(ident.Equals("STR")) //first work out what parameter we're dealing with { p = ParamToken.STRENGTH; } else if (ident.Equals("SPD")) { p = ParamToken.SPEED; } else if (ident.Equals("AWA")) { p = ParamToken.AWARE; } else if (ident.Equals("DEF")) { p = ParamToken.DEFENCE; } else if (ident.Equals("ENG")) { p = ParamToken.ENERGY; } else if (ident.Equals("HTH")) { p = ParamToken.HEALTH; } else if(ident.Equals("STL")) { p = ParamToken.STEALTHVAL; } else { throw new Exception("Unrecognised parameter ident in shapes text"); } for (int c = 3; c < split.Length; c++) { if(split[c].Equals('+')) { pMods.Add(p); } else if (split[c].Equals('-')) { nMods.Add(p); } else { throw new Exception("Non +/- character used as incrementor/decrementor in shapes text"); } } s = sr.ReadLine(); } Shape shape = new Shape(cells, pMods, nMods); recognisedShapes.Add(shape); s = sr.ReadLine(); } } catch(Exception e) { Console.Beep(); Console.WriteLine(e.Message); } }