public Tuple<bool, ActionEnum> MyTurn(int[,] eatMatrix, Creature[,] cellsMatrix) { if (_store < K || _turns >= T) return Tuple.Create(false, ActionEnum.Die); _store -= K; _turns++; if (_store >= C) { var directions = new List<ActionEnum>(); var points = ActionEx.GetPoints(_position.X, _position.Y); foreach (var item in points) { if (isValid(item, eatMatrix) && cellsMatrix[item.X, item.Y] == null) { directions.Add(ActionEx.ActionByPoint(_position, item)); } } if (directions.Count != 0) { _store -= C; return Tuple.Create(true, directions.ElementAt(0)); } } var state = ActionEx .GetPoints(_position.X, _position.Y) .ToDictionary(x => ActionEx.DirectionByPoint(_position, x), x => (isValid(x, eatMatrix) && eatMatrix[x.X, x.Y] != 0) ? 4 : 0); var result = _executor.Execute(_commands, new MyExecutorToolset(_random, state)); var parsedResult = int.Parse(result); ActionEnum action; switch (parsedResult) { case 0: action = ActionEnum.Stay; if (eatMatrix[_position.X, _position.Y] >= M) { _store += M; eatMatrix[_position.X, _position.Y] -= M; } break; case 1: action = ActionEnum.Up; Stats.Up++; break; case 2: action = ActionEnum.Right; Stats.Right++; break; case 3: action = ActionEnum.Down; Stats.Down++; break; case 4: action = ActionEnum.Left; Stats.Left++; break; default: throw new Exception(); } return Tuple.Create(false, action); }
public static Point PointByAction(ActionEnum actionEnum, Point start, Creature[,] cellsMatrix) { switch (actionEnum) { case ActionEnum.Die: { cellsMatrix[start.X, start.Y] = null; throw new CreatureIsDeadException(start); } case ActionEnum.Left: return new Point(start.X - 1, start.Y); case ActionEnum.Right: return new Point(start.X + 1, start.Y); case ActionEnum.Up: return new Point(start.X, start.Y - 1); case ActionEnum.Down: return new Point(start.X, start.Y + 1); default: throw new ArgumentOutOfRangeException(nameof(actionEnum), actionEnum, null); } }
private void MakeTurn(Creature simpleCreature, int[,] eat, int i, int j) { if (simpleCreature == null) return; var resultTuple = simpleCreature.MyTurn(eat, Cells); var result = resultTuple.Item2; if (resultTuple.Item1) { var newPosition = ActionEx.PointByAction(result, new Point(i, j), Cells); Cells[newPosition.X, newPosition.Y] = simpleCreature.MakeChild(newPosition); return; } if (result == ActionEnum.Stay) return; if (result == ActionEnum.Die) Cells[i, j] = null; else { var newPosition = ActionEx.PointByAction(result, new Point(i, j), Cells); simpleCreature.SetPosition(newPosition); Cells[i, j] = null; Cells[newPosition.X, newPosition.Y] = simpleCreature; } }