Пример #1
0
        public CellState[,] ComputeNextGeneration()
        {
            _nextGenMatrix = (CellState[, ])_matrix.Clone();

            ApplyRules();

            return(_nextGenMatrix);
        }
Пример #2
0
 public void NextTick()
 {
     CellState[,] newField = (CellState[, ])field.Clone();
     RefreshAliveNeighbours();
     Parallel.For(0, Rows, i =>
     {
         Parallel.For(0, Columns, j =>
         {
             int aliveNeighbours = this.aliveNeighbours[i, j];
             if (field[i, j] == CellState.Dead && birthCount.Contains(aliveNeighbours))
             {
                 newField[i, j] = CellState.Alive;
             }
             if (field[i, j] == CellState.Alive && !stayAliveCount.Contains(aliveNeighbours))
             {
                 newField[i, j] = CellState.Dead;
             }
         });
     });
     previousField = (CellState[, ])field.Clone();
     field         = newField;
 }
Пример #3
0
        private static CellState[,] StepForestFire(CellState[,] state, int f, int p)
        {
            /* Clone our old state, so we can write to our new state
             * without changing any values in the old state. */
            var newState = (CellState[, ])state.Clone();

            int height = state.GetLength(0);
            int width  = state.GetLength(1);

            for (int i = 1; i < height - 1; i++)
            {
                for (int o = 1; o < width - 1; o++)
                {
                    /*
                     * Check the current cell.
                     *
                     * If it's empty, give it a 1/p chance of becoming a tree.
                     *
                     * If it's a tree, check to see if any neighbors are burning.
                     * If so, set the cell's state to burning, otherwise give it
                     * a 1/f chance of combusting.
                     *
                     * If it's burning, set it to empty.
                     */
                    switch (state[i, o])
                    {
                    case CellState.Empty:
                        if (Random.Next(0, p) == 0)
                        {
                            newState[i, o] = CellState.Tree;
                        }
                        break;

                    case CellState.Tree:
                        if (IsNeighbor(state, i, o, CellState.Burning) ||
                            Random.Next(0, f) == 0)
                        {
                            newState[i, o] = CellState.Burning;
                        }
                        break;

                    case CellState.Burning:
                        newState[i, o] = CellState.Empty;
                        break;
                    }
                }
            }

            return(newState);
        }
Пример #4
0
    private CellState[,] StepWires(CellState[,] state)
    {
        var newState = (CellState[, ])state.Clone();

        int height = state.GetLength(0);
        int width  = state.GetLength(1);

        for (int i = 1; i < height - 1; i++)
        {
            for (int o = 1; o < width - 1; o++)
            {
                switch (state[i, o])
                {
                case CellState.Wire:
                    int count = CountNeighbor(state, i, o, CellState.Head);
                    if (count == 1 || count == 2)
                    {
                        newState[i, o] = CellState.Head;
                    }
                    break;

                case CellState.Head:
                    newState[i, o] = CellState.Tail;
                    break;

                case CellState.Tail:
                    newState[i, o] = CellState.Wire;
                    break;
                }
            }
        }


        sourceDelay--;
        if (sourceDelay < 0)
        {
            for (int i = 0; i < sources.Count; ++i)
            {
                newState[sources[i].y, sources[i].x] = CellState.Head;
            }

            sourceDelay = int.MaxValue;
        }

        return(newState);
    }
Пример #5
0
        /// <summary>
        /// Returns a copy of this board.
        /// </summary>
        /// <returns>A copy of this board.</returns>
        public Board Copy()
        {
            // Use memberwise clone to do a shallow copy of our board
            // We will have to copy the reference types by hand below
            Board boardCopy = MemberwiseClone() as Board;

            // Copy the move sequence to an array
            Pos[] movesArray = moves.ToArray();

            // Create a new move sequence in the copied board...
            boardCopy.moves = new Stack <Pos>();

            // ...and push the moves in reverse order they appear in the array
            for (int i = movesArray.Length - 1; i >= 0; i--)
            {
                boardCopy.moves.Push(movesArray[i]);
            }

            // Copy the board's contents onto the new board object
            boardCopy.board = board.Clone() as CellState[, ];

            // Return a copy of the board
            return(boardCopy);
        }
Пример #6
0
    IEnumerator RecursiveMiniMaxing(CellState[,] state, CellState player, Action action, Score score)
    {
        Debug.Log("Player " + player + " simulating state: " + PrintState(state));
        yield return(null);

        // If this is a terminal state, return the value.
        if (Wins(state, player))
        {
            score.value = 10;
            yield break;
        }
        else if (Loses(state, player))
        {
            score.value = -10;
            yield break;
        }
        else if (Draws(state, player))
        {
            score.value = 0;
            yield break;
        }

        // Check if mini or maxi
        bool isMaximizing = player == aiPlayer;

        int bestScoreValue = 100000 * (isMaximizing ? -1 : 1);

        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                // Valid action where empty
                if (state[i, j] == CellState.E)
                {
                    // Test that action

                    CellState[,] nextState = (CellState[, ])state.Clone();
                    nextState[i, j]        = player;

                    // Get the next score
                    Score  nextScore  = new Score();
                    Action nextAction = new Action();

                    yield return(StartCoroutine(RecursiveMiniMaxing(nextState, (CellState)((int)player * -1), nextAction, nextScore)));

                    nextScore.value *= -1;

                    // Update the max
                    if (nextScore.value * (isMaximizing ? 1 : -1) > bestScoreValue * (isMaximizing ? 1 : -1))
                    {
                        bestScoreValue  = nextScore.value;
                        action.action_i = i;
                        action.action_j = j;
                    }
                }
            }
        }

        Debug.Log("MAX score: " + bestScoreValue + "\nBest action: " + action);

        score.value = bestScoreValue;
    }
Пример #7
0
    private CellState[,] StepForestFire(CellState[,] state, int f, int p)
    {
        /* Clone our old state, so we can write to our new state
         * without changing any values in the old state. */
        var newState = (CellState[, ])state.Clone();

        int height = state.GetLength(0);
        int width  = state.GetLength(1);

        bool playtick  = false;
        bool playwrong = false;

        for (int i = 1; i < height - 1; i++)
        {
            for (int o = 1; o < width - 1; o++)
            {
                /*
                 * Check the current cell.
                 *
                 * If it's empty, give it a 1/p chance of becoming a tree.
                 *
                 * If it's a tree, check to see if any neighbors are burning.
                 * If so, set the cell's state to burning, otherwise give it
                 * a 1/f chance of combusting.
                 *
                 * If it's burning, set it to empty.
                 */
                switch (state[i, o])
                {
                case CellState.Empty:
                    if (Random.Range(0, p) == 0)
                    {
                        newState[i, o] = CellState.Tree;
                        playwrong      = true;
                    }

                    if (IsNeighbor(state, i, o, CellState.Tree) && Random.Range(0, nearbyTreeChance) == 0)
                    {
                        newState[i, o] = CellState.Tree;
                        playwrong      = true;
                    }
                    break;

                case CellState.Tree:
                    if (IsNeighbor(state, i, o, CellState.Burning) ||
                        Random.Range(0, f) == 0)
                    {
                        newState[i, o] = CellState.Burning;
                        playtick       = true;
                    }
                    break;

                case CellState.Burning:
                    newState[i, o] = CellState.Empty;
                    break;
                }
            }
        }

        if (playtick)
        {
            SHGUI.current.PlaySound(SHGUIsound.tick);
        }
        else if (playwrong)
        {
            SHGUI.current.PlaySound(SHGUIsound.wrong);
        }


        return(newState);
    }