Ejemplo n.º 1
0
        public int ValueFromCellState(ECellState state)
        {
            var value = 0;

            switch (state)
            {
            case ECellState.Vide:
                value = 0;
                break;

            case ECellState.Occupe:
                value = 1;
                break;

            case ECellState.Plouf:
                value = 2;
                break;

            case ECellState.Touche:
                value = 3;
                break;

            case ECellState.Coule:
                value = 4;
                break;
            }

            return(value);
        }
Ejemplo n.º 2
0
        private void DealWithHealthyHitCoordinates(ECellState[,] board, int x, int y, ICollection <Ship> ships)
        {
            foreach (var ship in ships)
            {
                if (ship.HealthyCoords.Any(p => p.SequenceEqual(new[] { x, y })))
                {
                    ship.HitCoords.Add(ship.HealthyCoords.Find(p => p.SequenceEqual(new[] { x, y })));
                    ship.HealthyCoords.Remove(ship.HealthyCoords.Find(p => p.SequenceEqual(new[] { x, y })));

                    if (ship.HealthyCoords.Count == 0)
                    {
                        foreach (var hitCoord in ship.HitCoords)
                        {
                            board[hitCoord[0], hitCoord[1]] = ECellState.Sunk;
                        }
                        LatestMoveResult = ECellState.Sunk;
                        ship.IsSunk      = true;
                    }
                    else
                    {
                        board[x, y] = ECellState.Hit;
                    }

                    break;
                }
            }
        }
Ejemplo n.º 3
0
    /// <summary>
    /// Checks the neighbor states and calculates own state
    /// </summary>
    public void CheckNeighbors()
    {
        #region Debugging
        // Debug for checking correct neighbor assignments
        if (m_list_neighbors.Count != 8)
        {
            Debug.Log(m_ArrPos.x + " " + m_ArrPos.y);
        }
        #endregion

        int neighborsAlive = 0;     // Total number of living neighbors

        // Calculate total number of living neighbors
        foreach (CCellBehaviour neighbor in m_list_neighbors)
        {
            if (neighbor.State == ECellState.ALIVE)
            {
                neighborsAlive += 1;
            }
        }

        if (State == ECellState.DEAD && neighborsAlive == m_settings.Resurrect)      // Revive a if dead and rules are met
        {
            State = ECellState.ALIVE;
        }
        else if (neighborsAlive < m_settings.DieLowerLimit || neighborsAlive > m_settings.DieUpperLimit && State == ECellState.ALIVE)    // Die according to set rules
        {
            State = ECellState.DEAD;
        }
    }
Ejemplo n.º 4
0
 public void InsertBoat(ECellState shipType, int quantity)
 {
     for (int i = 0; i < quantity; i++)
     {
         BattleShip.Ships1.Add(shipType);
         BattleShip.Ships2.Add(shipType);
     }
 }
Ejemplo n.º 5
0
 public Ship(ECellState shipType, int shipSize, List <int[]> healthyCoords)
 {
     ShipType      = shipType;
     ShipSize      = shipSize;
     IsSunk        = false;
     HealthyCoords = healthyCoords;
     HitCoords     = new List <int[]>();
 }
Ejemplo n.º 6
0
 private static string CellString(ECellState cellState)
 {
     return(cellState switch
     {
         ECellState.Empty => "~",
         ECellState.Bomb => "X",
         // ECellState.Boat => "B",
         _ => "-"
     });
Ejemplo n.º 7
0
        private void SwitchFunc(ECellState[,] board, int x, int y, ICollection <Ship> ships)
        {
            switch (board[x, y])
            {
            case ECellState.Carrier:
                LatestMoveResult = ECellState.Hit;
                DealWithHealthyHitCoordinates(board, x, y, ships);
                break;

            case ECellState.Battleship:
                LatestMoveResult = ECellState.Hit;
                DealWithHealthyHitCoordinates(board, x, y, ships);
                break;

            case ECellState.Submarine:
                LatestMoveResult = ECellState.Hit;
                DealWithHealthyHitCoordinates(board, x, y, ships);
                break;

            case ECellState.Cruiser:
                LatestMoveResult = ECellState.Hit;
                DealWithHealthyHitCoordinates(board, x, y, ships);
                break;

            case ECellState.Patrol:
                LatestMoveResult = ECellState.Hit;
                DealWithHealthyHitCoordinates(board, x, y, ships);
                break;

            case ECellState.Empty:
                board[x, y]      = ECellState.Miss;
                LatestMoveResult = ECellState.Miss;
                break;

            case ECellState.Hit:
                LatestMoveResult = ECellState.Miss;
                break;

            case ECellState.Miss:
                LatestMoveResult = ECellState.Miss;
                break;

            case ECellState.Sunk:
                LatestMoveResult = ECellState.Miss;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 8
0
    /** <summary> Simulates the next step. </summary> */
    private SimulationStep SimulateNextStep()
    {
        performance_stopwatch_.Start();

        // Since the simulation is basically a function that outputs a new grid I need a new array to store the new states of the cell
        // in order to avoid interferences on the current state of the game grid if new cells spawn or some die.
        // This array will then become the new game grid.
        ECellState[, ] ret_next_step_game_grid = new ECellState[cell_grid_.GetLength(0), cell_grid_.GetLength(1)];

        // Counts the neighbors of each cell and spawns or kills based on the rules of the game.
        byte local_neighbors_count;

        for (int cycle_row_index = 0; cycle_row_index < cell_grid_.GetLength(0); cycle_row_index++)
        {
            for (int cycle_column_index = 0; cycle_column_index < cell_grid_.GetLength(1); cycle_column_index++)
            {
                local_neighbors_count = getNeighborsCount(cycle_row_index, cycle_column_index);

                // Count == 3 => New cell
                // Count < 2 => Cell dies
                // Count > 3 => Cell dies
                // Else => The same value as the original step is applied.
                if (local_neighbors_count == 3)
                {
                    ret_next_step_game_grid[cycle_row_index, cycle_column_index] = ECellState.ALIVE;
                }
                else if (local_neighbors_count < 2)
                {
                    ret_next_step_game_grid[cycle_row_index, cycle_column_index] = ECellState.DEAD;
                }
                else if (local_neighbors_count > 3)
                {
                    ret_next_step_game_grid[cycle_row_index, cycle_column_index] = ECellState.DEAD;
                }
                else
                {
                    ret_next_step_game_grid[cycle_row_index, cycle_column_index] = cell_grid_[cycle_row_index, cycle_column_index];
                }
            }
        }

        cell_grid_ = ret_next_step_game_grid;

        performance_stopwatch_.Stop();
        float elapsed_time = performance_stopwatch_.ElapsedMilliseconds;

        performance_stopwatch_.Reset();

        return(new SimulationStep(ret_next_step_game_grid, elapsed_time));
    }
Ejemplo n.º 9
0
    private void Awake()
    {
        m_settings = GetComponentInParent <CPlaygroundBehaviour>().Settings;
        m_Sprite   = GetComponent <SpriteRenderer>();

        if (Random.Range(0, 101) <= m_settings.AliveChance)     // Randomly select start state by chance
        {
            State = ECellState.ALIVE;
        }
        else
        {
            State = ECellState.DEAD;
        }
    }
Ejemplo n.º 10
0
    public void OnCellClicked()
    {
        Image cellImage = GetComponent<Image>();

        switch (m_cellState)
        {
            case ECellState.Inactive:
                // do nothing
                break;
            case ECellState.Unchecked:
                // determine randomly to go to Empty or Mine state
                bool DidIWinThisGameOfMinesweepEr = (Random.Range(0, 2) == 0) ? (true) : (false);
                if (DidIWinThisGameOfMinesweepEr) // you won
                {
                    m_cellState = ECellState.Empty;
                    if (cellImage)
                    {
                        cellImage.sprite = EmptySprite;
                    }
                }
                else // you lost
                {
                    m_cellState = ECellState.Mine;
                    if (cellImage)
                    {
                        cellImage.sprite = MineSprite;
                    }
                }
                break;
            case ECellState.Empty:
                break;
            case ECellState.Mine:
                break;
            case ECellState.Flag:
                break;
            default:
                break;
        }
    }
Ejemplo n.º 11
0
 private static string CellString(ECellState cellState)
 {
     return(cellState switch
     {
         ECellState.Empty => "~",
         ECellState.Bomb => "O",
         ECellState.Hit => "X",
         ECellState.Patrol => "P",
         ECellState.Cruiser => "C",
         ECellState.Submarine => "S",
         ECellState.Battleship => "B",
         ECellState.Carrier => "A",
         ECellState.Custom0 => "0",
         ECellState.Custom1 => "1",
         ECellState.Custom2 => "2",
         ECellState.Custom3 => "3",
         ECellState.Custom4 => "4",
         ECellState.Custom5 => "5",
         ECellState.Custom6 => "6",
         ECellState.Custom7 => "7",
         ECellState.Custom8 => "8",
         ECellState.Custom9 => "9",
         _ => "-"
     });
Ejemplo n.º 12
0
		public CCellContent(bool alternatorInitialValue, ECellState state) { mAlternator = alternatorInitialValue; mState = state; }
Ejemplo n.º 13
0
        //PLACING SHIPS
        public bool PlaceShip(int startRow, int endRow, int startCol, int endCol, ICollection <ECellState> playerShips,
                              ECellState[,] board)
        {
            var        lastRowIndex         = _y - 1;
            var        lastColIndex         = _x - 1;
            var        shipLength           = (int)playerShips.ToArray()[0];
            ECellState shipType             = playerShips.ToArray()[0];
            var        desiredPlacementSize = 0;

            for (int i = startRow; i < endRow + 1; i++)
            {
                for (int j = startCol; j < endCol + 1; j++)
                {
                    if (board[i, j] == ECellState.Empty)
                    {
                        desiredPlacementSize += 1;
                    }
                }
            }

            List <int[]> shipCoords2 = new List <int[]>();

            if (desiredPlacementSize.Equals(shipLength))
            {
                var thCellInShip = 0;

                for (int i = startRow; i < endRow + 1; i++)
                {
                    for (int j = startCol; j < endCol + 1; j++)
                    {
                        if (board[i, j] == ECellState.Empty)
                        {
                            var cellPositionOnBoard = CheckEdgeTouching(i, j, lastRowIndex, lastColIndex);

                            var cellsTouched = 0;
                            switch (thCellInShip)
                            {
                            case 0:
                            {
                                if (TouchingRule == ETouchingRule.CanNotTouch)
                                {
                                    cellsTouched = SwitchTouchFunctionNoTouchAllowed(cellPositionOnBoard, board, i, j);
                                }
                                else if (TouchingRule == ETouchingRule.CornersCanTouch)
                                {
                                    cellsTouched =
                                        SwitchTouchFunctionCornersTouchAllowedFirstCell(cellPositionOnBoard, board, i,
                                                                                        j);
                                }
                                else if (TouchingRule == ETouchingRule.CanTouch)
                                {
                                    cellsTouched = 0;
                                }
                                if (cellsTouched == 0)
                                {
                                    board[i, j] = shipType;
                                    shipCoords2.Add(new[] { i, j });
                                    thCellInShip++;
                                }
                                else
                                {
                                    return(false);
                                }
                                break;
                            }

                            case > 0:
                            {
                                if (TouchingRule == ETouchingRule.CanTouch)
                                {
                                    cellsTouched = 0;
                                }
                                else if (TouchingRule == ETouchingRule.CanNotTouch)
                                {
                                    cellsTouched = SwitchTouchFunctionNoTouchAllowed(cellPositionOnBoard, board, i, j);
                                }
                                else if (TouchingRule == ETouchingRule.CornersCanTouch && thCellInShip == shipLength)
                                {
                                    cellsTouched =
                                        SwitchTouchFunctionCornersTouchAllowedFirstCell(cellPositionOnBoard, board, i,
                                                                                        j) + 1;
                                }
                                // this 2 is a bit fishy
                                if (cellsTouched <= 1 || cellsTouched == 2)
                                {
                                    //PLACE
                                    board[i, j] = shipType;
                                    shipCoords2.Add(new[] { i, j });
                                    thCellInShip++;
                                }
                                else
                                {
                                    foreach (var coord in shipCoords2)
                                    {
                                        board[coord[0], coord[1]] =
                                            ECellState.Empty;
                                    }

                                    shipCoords2.Clear();
                                    return(false);
                                }

                                break;
                            }

                            default:
                                return(false);
                            }
                        }

                        thCellInShip++;
                    }
                }

                if (Player1Ships.Count < ShipCountPerPlayer)
                {
                    Ships1 = Ships1.Skip(1).ToArray();
                    Player1Ships.Add(new Ship(shipType, shipLength, shipCoords2));
                    return(true);
                }
                else
                {
                    _nextMoveByPlayer2 = false;
                    Ships2             = Ships2.Skip(1).ToArray();
                    Player2Ships.Add(new Ship(shipType, shipLength, shipCoords2));
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 14
0
 public Cell()
 {
     CellState = ECellState.Dead;
 }
Ejemplo n.º 15
0
 public void OnStatusClicked()
 {
     m_cellState = ECellState.Unchecked;
     Image cellImage = GetComponent<Image>();
     if (cellImage)
     {
         cellImage.sprite = UncheckedSprite;
     }
     Button cellButton = GetComponent<Button>();
     if (cellButton)
     {
         cellButton.interactable = true;
     }
     // TODO -- a game state class attached to a dummy object should also receive this information -- including for wins and losses
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Chỉ có thể thay đổi đc cell state thông qua hàm SetCellState
 /// </summary>
 /// <param name="cellState"></param>
 public void SetCellState(ECellState cellState)
 {
     this.State = cellState;
 }
Ejemplo n.º 17
0
 public Ship(int width)
 {
     Width     = width;
     CellState = GetCellState(width);
     Name      = CreateName(width);
 }
Ejemplo n.º 18
0
    // Use this for initialization
    void Start()
    {
        m_cellState = ECellState.Inactive;

        Button cellButton = GetComponent<Button>();
        if (cellButton)
        {
            cellButton.interactable = false;
        }
    }