protected override IEnumerable <IKlopCell> FindAvailableCells(KlopCell baseCell) { return(Cells.Where( cell => (cell.State == ECellState.Free || (cell.State == ECellState.Alive && cell.Owner != CurrentPlayer)) && GetNeighborCells(cell).Any(x => x.Owner == CurrentPlayer))); }
/// <summary> /// Resets the game to initial state /// </summary> public void Reset() { lock (_syncroot) { _defeatedPlayers.Clear(); RaiseDefeatedPlayersChanged(); for (var x = 0; x < FieldWidth; x++) { for (var y = 0; y < FieldHeight; y++) { if (_cells[x, y] == null) { _cells[x, y] = new KlopCell(x, y); } var cell = _cells[x, y]; cell.Available = false; cell.Flag = false; cell.Owner = null; cell.State = ECellState.Free; } } foreach (var player in Players) { var cell = _cells[player.BasePosX, player.BasePosY]; cell.Owner = player; cell.State = ECellState.Base; } RemainingKlops = TurnLength; CurrentPlayerIndex = 0; UpdateCellsAvailableAndFlagStatus(); } }
/// <summary> /// Initializes a new instance of the <see cref="KlopModel"/> class. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="players">The players.</param> /// <param name="turnLenght">The turn lenght.</param> public KlopModelBase(int width, int height, IEnumerable <IKlopPlayer> players, int turnLenght) { _fieldWidth = width; _fieldHeight = height; _players = players.ToArray(); TurnLength = turnLenght; if (width < 10 || height < 10) { throw new ArgumentException("Width and height must be greater than 9"); } if (_players.Length < 2) { throw new ArgumentException("Need two or more players"); } if (_players.Any(player => !CheckCoordinates(player.BasePosX, player.BasePosY))) { throw new ArgumentException("Player base is outside of field!"); } // initialize field _cells = new KlopCell[width, height]; _history = new Stack <KlopCell>(); Reset(); // initialize players foreach (var player in _players) { player.SetModel(this); } }
/// <summary> /// Mark cells where turn is possible as available /// </summary> /// <param name="baseCell">The base cell.</param> protected virtual IEnumerable <IKlopCell> FindAvailableCells(KlopCell baseCell) { if (baseCell.Owner != CurrentPlayer || baseCell.Flag) { yield break; } baseCell.Flag = true; foreach (KlopCell cell in GetNeighborCells(baseCell)) { if (cell.Flag) { continue; } if (cell.Owner == CurrentPlayer) { // Continue tree search foreach (var c in FindAvailableCells(cell)) { yield return(c); } continue; } if (cell.State != ECellState.Free && cell.State != ECellState.Alive) { continue; } // Can go to free cell or eat enemy klop cell.Flag = true; yield return(cell); } }