/// <summary> /// Clones the specified cell. /// </summary> /// <param name="cell">The cell.</param> /// <returns></returns> public static Cell Clone(this Cell cell) { var clone = new Cell(); foreach (Human human in cell.Humans) { clone.Humans.Add(human.Clone()); } return clone; }
/// <summary> /// Gets the cell image. /// </summary> /// <param name="cell">The cell.</param> /// <returns></returns> private Image GetCellImage(Cell cell) { Image image = null; var assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); switch(cell.State) { case CellState.Man: image = Image.FromFile(Path.Combine(assemblyPath, "Resources/man.png")); break; case CellState.Woman: image = Image.FromFile(Path.Combine(assemblyPath, "Resources/woman.png")); break; case CellState.Couple: image = Image.FromFile(Path.Combine(assemblyPath, "Resources/couple.png")); break; } return image; }
/// <summary> /// Gels the color of the cell back. /// </summary> /// <param name="cell">The cell.</param> /// <returns></returns> private Color GelCellBackColor(Cell cell) { Color backColor = Color.Silver; switch(cell.State) { case CellState.Man: backColor = Color.FromArgb(0,0,240); break; case CellState.Woman: backColor = Color.FromArgb(0,240,0); break; case CellState.Couple: backColor = Color.FromArgb(240,0,0); break; } return backColor; }
private void InitializeEmptyCells() { for (int i = 0; i < m_size; i++) { for (int j = 0; j < m_size; j++) { m_Cells[i, j] = new Cell(); } } }
public Tuple<int, int> FindCellIndexes(Cell cell) { for (int i = 0; i < m_size; i++) { for (int j = 0; j < m_size; j++) { if (m_Cells[i, j] == cell) { return new Tuple<int, int>(i, j); } } } return null; }
/// <summary> /// Calculates the state of the next board. /// </summary> /// <param name="board">The board.</param> /// <returns></returns> public Board CalculateNextBoardState(Board board) { var nextGenerationBoard = new Board(board.Size); for (int i = 0; i < board.Size; i++) { for (int j = 0; j < board.Size; j++) { Cell currGenerationCell = board[i, j]; Cell nextGenerationCell = nextGenerationBoard[i, j]; if (nextGenerationCell.State != CellState.Empty /*Already calculated*/ || currGenerationCell.State == CellState.Empty) continue; if (currGenerationCell.State != CellState.Couple) //A single cell, try to find spouses { Cell spouseCell = FindSpouseForSingleHumanCell(i, j, board, nextGenerationBoard); if (spouseCell != null) // Found a spouse, create a couple { Human spouse = spouseCell.GetOppositeSexHuman(currGenerationCell.Humans[0].Sex); Human human = currGenerationCell.Humans[0].Clone(); if (m_enableMemory) //If memory is enable, save spouses memory { human.BestCharacterDiffMemory = Math.Abs(spouse.Character - human.Character); } nextGenerationCell.Humans.Add(human); nextGenerationCell.Humans.Add(spouse.Clone()); Tuple<int, int> spouseIndexes = board.FindCellIndexes(spouseCell); if (spouseCell.State == CellState.Couple) //Create the single human cell in the next state board { spouseCell.Humans.Remove(spouse); nextGenerationBoard[spouseIndexes.Item1, spouseIndexes.Item2].Humans.Add(spouseCell.Humans[0].Clone()); } board[spouseIndexes.Item1, spouseIndexes.Item2] = new Cell(); board[i, j] = new Cell(); } else //Didn't find a spouse, try to move the human { MoveCell(board, nextGenerationBoard, i, j); } } else //The cell has a couple { Cell spouse = FindBetterSpouseForCoupleCell(i, j, board); if (spouse != null) //Found a better spouse, generate the new couple and new single { Tuple<int, int> spouseIndexes = board.FindCellIndexes(spouse); if (spouse.State == CellState.Man) { //The new couple nextGenerationBoard[spouseIndexes.Item1, spouseIndexes.Item2].Humans.Add(spouse.Male.Clone()); nextGenerationBoard[spouseIndexes.Item1, spouseIndexes.Item2].Humans.Add(board[i, j].Female.Clone()); //The new single nextGenerationBoard[i, j].Humans.Add(board[i, j].Male.Clone()); } else //Woman { //The new couple nextGenerationBoard[spouseIndexes.Item1, spouseIndexes.Item2].Humans.Add(spouse.Female.Clone()); nextGenerationBoard[spouseIndexes.Item1, spouseIndexes.Item2].Humans.Add(board[i, j].Male.Clone()); //The new single nextGenerationBoard[i, j].Humans.Add(board[i, j].Female.Clone()); } //Clear the couple and the new spouse cells from the original board board[i, j] = new Cell(); board[spouseIndexes.Item1, spouseIndexes.Item2] = new Cell(); } else //Didn't find a better spouse, try to move the couple { MoveCell(board, nextGenerationBoard, i, j); } } } } return nextGenerationBoard; }
/// <summary> /// Randomly Moves a cell on the board5. /// </summary> /// <param name="board">The board.</param> /// <param name="nextGenBoard">The next state board.</param> /// <param name="i">The i.</param> /// <param name="j">The j.</param> private void MoveCell(Board board, Board nextGenBoard, int i, int j) { Tuple<int, int> coordinates = GenerateRandomMoveCoordinates(i, j, board, nextGenBoard); if (coordinates != null) { nextGenBoard[coordinates.Item1, coordinates.Item2] = board[i, j].Clone(); } else { nextGenBoard[i, j] = board[i, j].Clone(); ; } board[i, j] = new Cell(); }