public GridItem GetRandomElementByState(GridItemState state) { var gridItems = GetGridItemsByState(state); if (gridItems.Any()) { return GetRandomElementFromCollection(gridItems); } return null; }
/// <summary> /// Determines if the given state is the winning one. /// </summary> /// <param name="session">GameSession object</param> /// <param name="field">Current click position</param> /// <param name="state">Current state (player)</param> /// <param name="size">Size of the grid</param> /// <returns> /// GridItemState.Empty if no winner has been found; /// otherwise GridItemState.Player1 or GridItemState.Player1 /// </returns> public static GridItemState GetWinningState( GameSession session, Field field, GridItemState state, int size) { // TODO: Determine whether the given state is the winning one return GridItemState.Empty; }
/// <summary> /// Run the Place command. Can be overridden. /// </summary> /// <param name="parseCommandResult"></param> /// <returns>CommandResult</returns> protected virtual GenericResult DoPlace(ParseCommandResult parseCommandResult) { GenericResult result = new GenericResult(); // Check if the new coords are within the boundary of the grid. if (_taskGrid.IsInGridBounds(parseCommandResult.Coordinates)) { var newState = new GridItemState() { Coords = parseCommandResult.Coordinates, Facing = parseCommandResult.FacingDirection }; AddItemStateToHistory(newState); result.Success = true; } else { result.Comment = $"That command would position the {GRID_ITEM_TYPE_NAME} off the grid. Try again."; } return(result); }
public void Initialize(InventoryGrid grid) { _grid = grid; State = GridItemState.None; }
public GameSession(int size, string player1Id) { Grid = new GridItemState[size, size]; Player1Id = player1Id; }
private IEnumerable<GridItem> GetGridItemsByState(GridItemState state) { return ArrayExtensions.ToEnumerable<GridItem> (_items).Where(x => x.State== state); }
/// <summary> /// Run/process the command. /// </summary> /// <param name="command"></param> /// <returns>GenericResult</returns> public virtual GenericResult RunCommand(string command) { GenericResult result = new GenericResult(); // parse the command var parseResult = ParseCommand(command); if (!parseResult.Success) { result.Comment = parseResult.Comment; return(result); } var canRunCommand = CheckRunCommandRules(parseResult.Command); if (!canRunCommand.Success) // Command can't be run, due to a rule. { result.Comment = canRunCommand.Comment; return(result); } _currentState = GetCurrentState(); DelegateDoCommand _delegateDoCommand = null; switch (parseResult.Command) { case CommandType.PLACE: _delegateDoCommand = DoPlace; break; case CommandType.MOVE: _delegateDoCommand = DoMove; break; case CommandType.LEFT: _delegateDoCommand = DoLeft; break; case CommandType.RIGHT: _delegateDoCommand = DoRight; break; case CommandType.REPORT: _delegateDoCommand = DoReport; break; default: result.Comment = "Not a valid command. Please try again."; return(result); } if (_delegateDoCommand != null) { var doResult = _delegateDoCommand(parseResult); result = doResult; } else { result.Comment = "Unable to run the command. Please try again."; } return(result); }
/// <summary> /// Add itemState to the state history list at the end. /// </summary> /// <param name="itemState"></param> private void AddItemStateToHistory(GridItemState itemState) { _validStateHistory.Add(itemState); }
/// <summary> /// Shallow clone the GridItemState. /// </summary> /// <param name="gridItem"></param> /// <returns>A deep clone of the item</returns> private GridItemState CloneGridItem(GridItemState gridItem) { return((GridItemState)gridItem.Clone()); }