public bool Move() { int @event; var isFoodInFront = Observe(out @event); var transition = Ant.Transitions[_currentState, @event]; if (transition.EndState >= 0 && transition.EndState < Ant.StatesNumber) { var action = (ArtificialAnt.AntActions)transition.Output; _currentState = transition.EndState; switch (action) { case ArtificialAnt.AntActions.Left: CurrentDirection = GetLeftDirection(CurrentDirection); break; case ArtificialAnt.AntActions.Right: CurrentDirection = GetRightDirection(CurrentDirection); break; case ArtificialAnt.AntActions.Move: CurrentCell = CurrentCell.Next(CurrentDirection, _currentField.FieldWidth, _currentField.FieldHeight); CurrentField[CurrentCell.X, CurrentCell.Y] = false; break; } return(isFoodInFront && (action == ArtificialAnt.AntActions.Move)); } throw new Exception(String.Format("ends fail: {0}", transition.EndState)); }
private bool Observe(out int @event) { var nextCell = CurrentCell.Next(CurrentDirection, _currentField.FieldWidth, _currentField.FieldHeight); var isFoodInFront = CurrentField[nextCell.X, nextCell.Y]; @event = 0; if (ViewRadius == 1) { @event = isFoodInFront ? 1 : 0; } else { var left = GetLeftDirection(CurrentDirection); var right = GetRightDirection(CurrentDirection); nextCell = CurrentCell; for (int i = 0; i < ViewRadius - 1; i++) { nextCell = nextCell.Next(left, _currentField.FieldWidth, _currentField.FieldHeight); } Cell startCell = nextCell; int currCollLength = ViewRadius * 2 - 1; int cellsElapsed = 0; for (int i = 0; i < ObservableCells; i++) { if (nextCell.X == CurrentCell.X && nextCell.Y == CurrentCell.Y) { nextCell = nextCell.Next(right, _currentField.FieldWidth, _currentField.FieldHeight); cellsElapsed++; } @event |= _currentField[nextCell.X, nextCell.Y] ? 1 << i : 0; cellsElapsed++; if (cellsElapsed >= currCollLength) { cellsElapsed = 0; currCollLength -= 2; startCell = startCell.Next(right, _currentField.FieldWidth, _currentField.FieldHeight).Next(CurrentDirection, _currentField.FieldWidth, _currentField.FieldHeight); nextCell = startCell; } else { nextCell = nextCell.Next(right, _currentField.FieldWidth, _currentField.FieldHeight); } } } return(isFoodInFront); }