public Cell(int x, int y) { Coords = new CellCoords { X = x, Y = y }; Clear(); }
void TouchCell(Vector3 position) { position = transform.InverseTransformPoint(position); CellCoords coordinates = CellCoords.FromPosition(position); Debug.Log("daddy touched me at " + coordinates.ToString()); }
private void MarkShipAsHit(int x, int y) { MarkKnown(x, y); _lastHit = new CellCoords(x, y); _currentShip.HitCells.Add(_lastHit); }
public GameState Shoot(string gameId, int row, int column) { if (_shots.Count > 100) throw new Exception("Too many shots!"); Debug.Assert(gameId == "42"); var shot = new CellCoords(row, column); Debug.Assert(!_shots.Contains(shot)); _shots.Add(shot); var hitShip = _ships .Where(s => s.IsSunken == false) .FirstOrDefault(s => s.PositionCells.Contains(shot)); if (hitShip == null) { return new GameState { IsFinished = false, LastShot = ShotResult.Miss }; } hitShip.HitCells.Add(shot); var state = new GameState { IsFinished = _ships.Count(s => s.IsSunken == false) == 0, LastShot = hitShip.IsSunken ? ShotResult.HitAndSunk : ShotResult.Hit }; if (state.IsFinished) { CalculateScore(_shots.Count); } return state; }
void CreateCell(int x, int z, int i) { Vector3 position; position.x = x; position.y = 0f; position.z = z; SquareCellScript cell = cells[i] = Instantiate <SquareCellScript>(cellPrefab); cell.transform.SetParent(transform, false); cell.transform.localPosition = position; cell.coordinates = CellCoords.FromCoordinates(x, z); if (x > 0) { cell.SetNeighbor(CellDirection.W, cells[i - 1]); } if (z > 0) { //if ((z & 1) == 0) cell.SetNeighbor(CellDirection.S, cells[i - width]); } Text label = Instantiate <Text>(cellLabelPrefab); label.rectTransform.SetParent(gridCanvas.transform, false); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); label.text = x.ToString() + "\n" + z.ToString(); }
private void on_chained_change(object sender, CellCoords e) { _cache [0].destroy(); _cache [1].destroy(); if (changed != null) { changed(this, e); } }
bool CanFitShip(int shipSize, CellCoords baseCell, Func<CellCoords, int, CellCoords> offsetCell) { for (var i = 0; i < shipSize; ++i) { if (IsCellAlreadyHit(offsetCell(baseCell, i))) return false; } return true; }
public BoardArea(CellCoords topLeft, CellCoords bottomRight) { _top = topLeft.y; _left = topLeft.x; _bottom = bottomRight.y; _right = bottomRight.x; for (var x = _left; x <= _right; ++x) for (var y = _top; y <= _bottom; ++y) _allCells.Add(new CellCoords(x, y)); }
//Converts touch position to cell coordinates via the CellCoords script void TouchCell(Vector3 position) { position = transform.InverseTransformPoint(position); CellCoords coordinates = CellCoords.FromPosition(position); Debug.Log("contact at " + coordinates.ToString()); int index = coordinates.X + coordinates.Y * width; SquareCellScript cell = cells[index]; cell.color = touchedColor; }
public override void OnGUI( Rect position, SerializedProperty property, GUIContent label ) { CellCoords coordinates = new CellCoords( property.FindPropertyRelative("x").intValue, property.FindPropertyRelative("y").intValue ); position = EditorGUI.PrefixLabel(position, label); GUI.Label(position, coordinates.ToString()); }
int WaysShipCanFit(CellCoords cell, int shipSize) { if (shipSize == 1) return IsCellAlreadyHit(cell) ? 0 : 1; int w = 0; for (var i = 0; i < shipSize; ++i) { if (CanFitShip(shipSize, new CellCoords(cell.x - i, cell.y), (c, n) => c.AddHorizontal(n))) w += 1; if (CanFitShip(shipSize, new CellCoords(cell.x, cell.y - i), (c, n) => c.AddVertical(n))) w += 1; } return w; }
internal IList<CellCoords> GetShots(ShipInfo currentShip, CellCoords lastHit, IEnumerable<CellCoords> cellsToAvoid) { var pivot_x = currentShip.HitCells.All(c => c.x == lastHit.x) ? lastHit.x : -1; var pivot_y = currentShip.HitCells.All(c => c.y == lastHit.y) ? lastHit.y : -1; var totalCandidates = currentShip.HitCells .SelectMany(c => c.GetSurroundingCells()) .Where(c => c.x == pivot_x || c.y == pivot_y) .Where(c => cellsToAvoid.Contains(c) == false) .ToList(); Debug.Assert(totalCandidates.Count > 0); var aroundLastHit = lastHit .GetSurroundingCells() .Intersect(totalCandidates) .ToList(); return aroundLastHit.Count > 0 ? aroundLastHit : totalCandidates; }
private static List<CellCoords> CreateRandomShip(int shipSize, int boardSize) { var r = new Random(); var shipCells = new List<CellCoords>(); var direction = r.Next(1) == 0; var initialPos = new CellCoords(r.Next(boardSize), r.Next(boardSize)); for (var i = 0; i < shipSize; ++i) { shipCells.Add(direction ? initialPos.AddHorizontal(i) : initialPos.AddVertical(i)); } return shipCells; }
public RangeCoords(CellCoords firstCell, CellCoords lastCell) { FirstCell = firstCell; LastCell = lastCell; }
private void MarkShipAsSunk(int x, int y) { MarkShipAsHit(x, y); var ship = _ships.First(s => s.Size == _currentShip.HitCells.Count && !s.IsSunken); foreach (var cell in _currentShip.HitCells) { ship.HitCells.Add(cell); foreach (var adjCell in cell.GetSurroundingCells()) { _knownCells.Add(adjCell); } Debug.Assert(_knownCells.Count <= new BoardArea(CellCoords.Min(), CellCoords.Max()).Area); } Debug.Assert(_knownCells.IsProperSupersetOf(_ships.SelectMany(s => s.HitCells))); _lastHit = null; _currentShip.HitCells.Clear(); }
public List<BoardArea> Split(CellCoords pivot) { Debug.Assert(this.Contains(pivot)); var ret = new List<BoardArea>(); // Left if (pivot.x > _left) ret.Add(new BoardArea(TopLeft, new CellCoords(pivot.x - 1, _bottom))); // Above if (pivot.y > _top) ret.Add(new BoardArea(TopLeft, new CellCoords(_right, pivot.y - 1))); // Right if (pivot.x < _right) ret.Add(new BoardArea(new CellCoords(pivot.x + 1, _top), BottomRight)); // Below if (pivot.y < _bottom) ret.Add(new BoardArea(new CellCoords(_left, pivot.y + 1), BottomRight)); return ret; }
public bool Contains(CellCoords cell) { return (cell.x >= _left && cell.x <= _right) && (cell.y >= _top && cell.y <= _bottom); }
public void AddShot(CellCoords cell, ShotResult result) { //Console.Write("[{0}, {1}]:{2} ", cell.x, cell.y, GetShotResult(result)); Console.Write(GetShotResult(result)); }
bool IsCellAlreadyHit(CellCoords cell) { return _knownCells.Contains(cell); }