示例#1
0
    public void Populate()
    {
        //Setup Cells Grid and Paint Texture
        for (int y = 0; y < _controller.sizeY; y++)
        {
            for (int x = 0; x < sizeX; x++)
            {
                int        range = Random.Range(0, 100);
                Cell.State state = Cell.State.Dead;
                if (range < 50)
                {
                    state = Cell.State.Alive;
                    texture2D.SetPixel(x, y, _controller.cellColor);
                }
                else
                {
                    state = Cell.State.Dead;
                    texture2D.SetPixel(x, y, Color.black);
                }
                cells[x, y] = new Cell(state, new Vector3Int(x, y, 0), texture2D, _controller.cellColor);
            }
        }

        for (int y = 0; y < _controller.sizeY; y++)
        {
            for (int x = 0; x < sizeX; x++)
            {
                cells[x, y].GetNeigbors(cells);
            }
        }
        texture2D.Apply();
    }
示例#2
0
        private void setState(Cell.State state, Position position)
        {
            void Set(Cell.State s)
            {
                getCell(position).state = s;
            }

            var currentState = getState(position);

            switch (state)
            {
            case DUST when currentState == JEWEL:
            case JEWEL when currentState == DUST:
                Set(DUST_AND_JEWEL);
                break;

            case EMPTY:
            case DUST:
            case JEWEL:
            case DUST_AND_JEWEL:
                Set(state);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(state), state, null);
            }
        }
示例#3
0
文件: Grid.cs 项目: zzskm/Mage-Gen
 public void Fill(Cell.State value)
 {
     for (int i = 0; i < m_grid.Length; ++i)
     {
         m_grid[i] = value;
     }
 }
示例#4
0
    public Cell Hunt()
    {
        for (int i = 0; i < m_grid.Length; ++i)
        {
            Cell curr = m_grid.GetCell(i);

            if (m_grid[i] != 0)
            {
                continue;
            }

            SetCurr(curr);
            m_owner.TakeSnapshot();

            Cell next = m_grid.ChoiceNeighbor(curr, st => st != Cell.State.None);

            if (!next.IsValid())
            {
                continue;
            }

            Cell.State dir = Cell.GetDir(curr, next);

            m_grid.AddState(curr, dir);
            m_grid.AddState(next, Cell.ToOppositeDir(dir));

            curr.state = m_grid[curr];

            m_owner.TakeSnapshot();

            return(curr);
        }

        return(Cell.invalid);
    }
示例#5
0
    public override Grid Generate(int w, int h)
    {
        base.Generate(w, h);

        m_frontiers.Clear();

        Mark(m_grid.GetRandom());

        while (m_frontiers.Count > 0)
        {
            int index = m_frontiers[m_rnd.Next(m_frontiers.Count)];
            m_frontiers.Remove(index);

            Cell curr = m_grid.GetCell(index);
            Cell next = m_grid.ChoiceNeighbor(curr, st => (st & Cell.State.Visited) != 0);

            if (next.IsValid())
            {
                Cell.State dir = Cell.GetDir(curr, next);

                m_grid.AddState(curr, dir);
                m_grid.AddState(next, Cell.ToOppositeDir(dir));

                m_owner.TakeSnapshot();
            }

            Mark(curr);
        }

        SetCurr(Cell.invalid);
        m_owner.TakeSnapshot();

        return(m_grid);
    }
示例#6
0
        //-------------private methods----------------//

        private bool CheckIfALineOfSameMarksIsCreated(IEnumerable <Cell> cells, Cell.State mark)
        {
            for (int i = 0; i < TicTacToeConstants.LINE_TABLE_DEF.GetLength(0); i++)
            {
                Cell[] cellsInOneLine = GetCellsInOneLine(cells, i);

                int sameCellsCount = 0;
                foreach (Cell cell in cellsInOneLine)
                {
                    if (cell.CellState == mark)
                    {
                        sameCellsCount++;
                    }
                    else
                    {
                        break;
                    }
                }

                if (sameCellsCount == 3)
                {
                    return(true);
                }
            }

            return(false);
        }
示例#7
0
    public void CarvePassages(Cell curr)
    {
        SetCurr(curr);
        m_owner.TakeSnapshot();

        Cell[] neighbors = m_grid.GetNeighbors(curr);

        if (neighbors.Length > 0)
        {
            for (int i = 0; i < neighbors.Length; ++i)
            {
                Cell next = neighbors[i];

                if (m_grid[next] != 0)
                {
                    continue;
                }

                Cell.State dir = Cell.GetDir(curr, next);

                m_grid.AddState(curr, dir);
                m_grid.AddState(next, Cell.ToOppositeDir(dir));

                CarvePassages(next);
            }
        }
    }
示例#8
0
        // 周囲の生きているセルの数から次代の生死を判定する
        internal Cell.State GetNextState(Cell.State self, IList <Cell> neighbours)
        {
            int  alives    = neighbours.Count(cell => cell.Current == Cell.State.Alive);
            bool willBorn  = alives == 3;
            bool isSurvive = alives == 2 && self == Cell.State.Alive;

            return((willBorn || isSurvive) ? Cell.State.Alive : Cell.State.Dead);
        }
示例#9
0
        private void display(Cell.State state, Position position, bool displayRobot = false)
        {
            var sprite = getSprite(state, position, displayRobot);

            Grid.SetColumn(sprite, position.x);
            Grid.SetRow(sprite, position.y);
            gridDisplay.Children.Add(sprite);
        }
示例#10
0
        private Cell InstantiateCell(Coords coords, Cell.State state)
        {
            var cellObject = Instantiate(config.cellPrefab, new Vector3(coords.x, 0, coords.z), Quaternion.identity,
                                         transform);

            cellObject.gameObject.SetActive(Cell.State.ALIVE.Equals(state));

            return(new Cell(coords, state, cellObject));
        }
示例#11
0
        // Génère et affiche un objet dans un endroit aléatoire de la grille.
        private void generate(Cell.State state)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                var randomPosition = new Position(
                    random(MIN_X, MAX_X),
                    random(MIN_Y, MAX_Y)
                    );

                setState(state, randomPosition);
                display(randomPosition);
            });
        }
示例#12
0
        private Game.State ToggleGameStateForNextTurn(Cell.State mark)
        {
            if (mark == Cell.State.CROSS)
            {
                return(Game.State.NAUGHT_TURN);
            }
            else if (mark == Cell.State.NAUGHT)
            {
                return(Game.State.CROSS_TURN);
            }

            throw new TicTacToeStateException(mark);
        }
示例#13
0
        public Game.State GetGameStateWon(Cell.State mark)
        {
            if (mark == Cell.State.CROSS)
            {
                return(Game.State.CROSS_WON);
            }
            else if (mark == Cell.State.NAUGHT)
            {
                return(Game.State.NAUGHT_WON);
            }

            throw new TicTacToeStateException(mark);
        }
示例#14
0
        private Image getSprite(Cell.State state, Position position, bool displayRobot = false)
        {
            var image       = new Image();
            var bitmapImage = new BitmapImage();
            var uri         = getUri(state, position, displayRobot);

            bitmapImage.BeginInit();
            bitmapImage.UriSource = new Uri(uri, UriKind.Relative);
            bitmapImage.EndInit();

            image.Source = bitmapImage;
            return(image);
        }
示例#15
0
    public void MoveRow(int rowIndex)
    {
        RowData[] rowInfo = new RowData[(int)columns];

        for (int j = 0; j < (int)columns; j++)
        {
            Cell.State st    = cells[rowIndex, j].GetComponent <Cell>().state;
            Vector2    index = cells[rowIndex, j].GetComponent <Cell>().GetCellIndex;
            rowInfo[j] = new RowData(index, st);
        }

        for (int j = 0; j < (int)columns; j++)
        {
            if (rowInfo[j].state == Cell.State.NotAssigned)
            {
                if (rowIndex - 1 != -1)
                {
                    if (cells[rowIndex - 1, j].GetComponent <Cell>().GetState == Cell.State.Assigned)
                    {
                        blocks[rowIndex - 1, j].GetComponent <Block>().ChangePosition(cells[rowIndex, j].transform.position, new Vector2(rowIndex, j));
                        blocks[rowIndex, j] = blocks[rowIndex - 1, j];
                        cells[rowIndex - 1, j].GetComponent <Cell>().state = Cell.State.NotAssigned;
                        cells[rowIndex, j].GetComponent <Cell>().state     = Cell.State.Assigned;
                        //blocks[rowIndex - 1, j].GetComponent<Block>().ChangeState(Block.State.Moving);
                        isDone = false;
                        if (rowIndex == 9)
                        {
                            blocks[rowIndex, j].GetComponent <Block>().state = Block.State.Fallen;
                            blocks[rowIndex, j].GetComponent <Block>().UnSubscribeInputController();
                            blocks[rowIndex, j].GetComponent <Block>().CheckCollision();
                        }
                        else if (rowIndex != 9)
                        {
                            if (cells[rowIndex + 1, j].GetComponent <Cell>().GetState == Cell.State.Assigned && blocks[rowIndex + 1, j].GetComponent <Block>().state == Block.State.Fallen)
                            {
                                blocks[rowIndex, j].GetComponent <Block>().state = Block.State.Fallen;
                                blocks[rowIndex, j].GetComponent <Block>().UnSubscribeInputController();
                                blocks[rowIndex, j].GetComponent <Block>().CheckCollision();
                            }
                        }
                    }
                }
            }
            else
            {
                blocks[rowIndex, j].GetComponent <Block>().state = Block.State.Fallen;
                blocks[rowIndex, j].GetComponent <Block>().UnSubscribeInputController();
                blocks[rowIndex, j].GetComponent <Block>().CheckCollision();
            }
        }
    }
        private char EncodeState(Cell.State state)
        {
            switch (state)
            {
            case Cell.State.ALIVE:
                return(ALIVE);

            case Cell.State.DEAD:
                return(DEAD);

            default:
                return('x');
            }
        }
示例#17
0
    public void SetState(Cell.State state)
    {
        this.state = state;
        switch (this.state)
        {
        case State.Dead:
            texture2D.SetPixel(gridPos.x, gridPos.y, deadColor);
            break;

        case State.Alive:
            texture2D.SetPixel(gridPos.x, gridPos.y, liveColor);
            break;

        default:
            break;
        }
    }
示例#18
0
    public void Walk(Cell curr)
    {
        Cell next = m_grid.ChoiceNeighbor(curr);

        while (next.IsValid())
        {
            Cell.State dir = Cell.GetDir(curr, next);

            m_grid.AddState(curr, dir);
            m_grid.AddState(next, Cell.ToOppositeDir(dir));

            curr = next;
            next = m_grid.ChoiceNeighbor(curr);

            SetCurr(curr);
            m_owner.TakeSnapshot();
        }
    }
示例#19
0
        public Game.State CalculateGameState(TicTacToe ticTacToe, Cell.State mark)
        {
            if (mark == Cell.State.BLANK)
            {
                throw new TicTacToeStateException(mark);
            }

            bool isTicTacToeWon = CheckIfALineOfSameMarksIsCreated(ticTacToe.Cells, mark);

            if (isTicTacToeWon)
            {
                return(GetGameStateWon(mark));
            }

            bool isDraw = CheckIfBoardIsAlreadyFull(ticTacToe.Cells);

            if (isDraw)
            {
                return(Game.State.DRAW);
            }

            return(ToggleGameStateForNextTurn(mark));
        }
示例#20
0
        private string getUri(Cell.State state, Position pos, bool displayRobot = false)
        {
            // Est-ce que l'on souhaite afficher le sprite du robot en plus de la cellule courante ?
            bool DisplayRobot()
            {
                return(!displayRobot &&
                       pos.x == robotPosition.x &&
                       pos.y == robotPosition.y);
            }

            switch (state)
            {
            case DUST:
                return(DisplayRobot()
                        ? "images/wall-e-and-dust.jpg"
                        : "images/dust.jpg");

            case JEWEL:
                return(DisplayRobot()
                        ? "images/wall-e-and-jewels.jpg"
                        : "images/jewels.jpg");

            case DUST_AND_JEWEL:
                return(DisplayRobot()
                        ? "images/wall-e-and-dust-and-jewels.jpg"
                        : "images/dust-and-jewels.jpg");

            case EMPTY:
                return(DisplayRobot()
                        ? "images/wall-e.jpg"
                        : "images/empty.png");

            default:
                throw new ArgumentOutOfRangeException(nameof(state), state, null);
            }
        }
示例#21
0
        public static Cell.State[][] CreateBaseState(int w, int h)
        {
            var rng    = new Random(Seeder);
            var source = new Cell.State[h][];


            for (int y = 0; y < h; y++)
            {
                source[y] = new Cell.State[w];
                Cell.State[] row = source[y];

                for (int x = 0; x < row.Length; x++)
                {
                    int max = (int)Cell.State.MAX;

                    //if (x < 5 || x > 15 ||
                    //    y < 5 || y > 15)
                    //    max = (int)Cell.State.Dead;

                    row[x] = (Cell.State)rng.Next(max);
                }
            }
            return(source);
        }
示例#22
0
 public void SetState(Cell.State state)
 {
     CellState = state;
 }
示例#23
0
 public RowData(Vector2 index, Cell.State st)
 {
     cellIndex = index;
     state     = st;
 }
示例#24
0
文件: Grid.cs 项目: zzskm/Mage-Gen
 public void SetState(int x, int y, Cell.State state)
 {
     m_grid[Cell.ToIndex(x, y, Width)] = state;
 }
示例#25
0
文件: Grid.cs 项目: zzskm/Mage-Gen
 public void AddState(Cell cell, Cell.State state)
 {
     m_grid[Cell.ToIndex(cell.x, cell.y, Width)] |= state;
 }
示例#26
0
        private bool Process_Group(Group group)
        {
            /* To describe this process, let's use the example group of size 10 with hint '4 3'
             *
             * What we want is to figure out all possible solutions, and then use that to determine
             *
             *
             */

            /* Count how many sections we have
             *
             * Sections are 'on, on, on, on', 'off, on, on, on', 'off', 'off'
             *
             * if a section has an 'on' in it, it's an 'on section'. Otherwise it's an 'off section'.
             *
             * The only wiggle-room we have in this solution is where the two 'off' sections go.
             *
             */

            // Count how many 'on' sections and 'off' sections we have in this group.

            int total_cell_count  = group.Cells.Length;
            int on_section_count  = group.Group_Clue.Group_Sizes.Length;
            int off_section_count = total_cell_count;
            int total_section_count;

            {
                for (int i = 0; i < on_section_count; i++)
                {
                    off_section_count -= group.Group_Clue.Group_Sizes[i];

                    if (i > 0)
                    {
                        off_section_count -= 1;
                    }
                }

                total_section_count = on_section_count + off_section_count;
            }

            // Build the list of possible permutations

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

            {
                int[] permutation = new int[total_section_count];
                for (int i = 0; i < permutation.Length; i++)
                {
                    permutation[i] = (i < off_section_count ? 0 : 1); // TODO: make these 'on' and off' for code readability.
                }

                do
                {
                    permutations.Add(permutation.Copy());
                } while (permutation.Get_Next_Permutation());
            }

            // convert each permutation back to a possibile solution for the group

            List <Cell.State[]> possibilities = new List <Cell.State[]>();

            {
                foreach (int[] permutation in permutations)
                {
                    Cell.State[] possibility = Convert_Permutation_To_Possibility(
                        group.Group_Clue.Group_Sizes,
                        permutation);

                    // But only count possibilities that are valid given the current group state.

                    if (Is_Valid_Possibility(possibility, group.Cells))
                    {
                        possibilities.Add(possibility);
                    }
                }
            }

            // Compute the union of all valid possibilities

            Cell.State[] union_of_possibilities = new Cell.State[total_cell_count];
            {
                for (int cell_index = 0; cell_index < total_cell_count; cell_index++)
                {
                    union_of_possibilities[cell_index] = Cell.State.maybe;

                    bool cell_can_possibly_be_on  = false;
                    bool cell_can_possibly_be_off = false;

                    foreach (Cell.State[] possibility in possibilities)
                    {
                        switch (possibility[cell_index])
                        {
                        case Cell.State.on:
                            cell_can_possibly_be_on = true;
                            break;

                        case Cell.State.off:
                            cell_can_possibly_be_off = true;
                            break;
                        }
                    }

                    if (cell_can_possibly_be_on && !cell_can_possibly_be_off)
                    {
                        union_of_possibilities[cell_index] = Cell.State.on;
                    }
                    else if (cell_can_possibly_be_off && !cell_can_possibly_be_on)
                    {
                        union_of_possibilities[cell_index] = Cell.State.off;
                    }
                    else
                    {
                        union_of_possibilities[cell_index] = Cell.State.maybe;
                    }
                }
            }

            // See if we can change anything in the group with our newfound knowledge

            bool changed_something = false;

            for (int cell_index = 0; cell_index < total_cell_count; cell_index++)
            {
                // TODO detect that the board is wrong! User made a mistake trying to solve something.

                if (union_of_possibilities[cell_index] != Cell.State.maybe &&
                    union_of_possibilities[cell_index] != group.Cells[cell_index].state)
                {
                    group.Cells[cell_index].state = union_of_possibilities[cell_index];

                    changed_something = true;
                }
            }

            return(changed_something);

            /*
             * 1. Build list of all possibilities
             *
             *  collect the cells into sections:
             *      Say hint is '2 3' in a 10x10 grid
             *      Sections would be:
             *          1 instance of: 'on,on'
             *          1 instance of: 'off,on,on,on'
             *          4 instances of: 'off'
             *      So the question now comes down to where to put the 4 free-floating 'off's
             *
             *      to represent this specific case, we're going to try to solve:
             *
             *          list all permutations of '110000'
             *              1 = is one of the sections with an 'on'
             *                  These are all the same for the purposes of permutations
             *                  because we know their order already from the hints.
             *              0 = one of the 'off' sections
             *                  These are all the same because they're identical.
             *
             *      So now we just need to list through the permutations of '110000'
             *          Ex: 000011, 000101, 000110, 001001, 001010, 001100, 010001, etc
             *      In C++, std::next_permutation does this!
             *          https://helloacm.com/the-next-permutation-algorithm-in-c-stdnext_permutation/
             *          https://stackoverflow.com/questions/11483060/stdnext-permutation-implementation-explanation
             *
             *      So implement that, and then transform back from the numbers to the final.
             *          Ex:
             *              We have a permutations: 000101
             *              Converted back to the sections: ['off', 'off', 'off', 'on,on', 'off', 'off,on,on,on']
             *              Final: off,off,off,on,on,off,off,on,on,on
             *
             *
             * 2. Throw out permutations that are impossible based on the current state of the board.
             *      ex: permutation is 'off, on, on, off', but board is 'maybe, maybe, maybe, on'
             *          These permutation isn't possible since the board has 'on' in the last spot.
             *
             * 3. Loop through remaining permutations to compute their 'union'
             *      ex: if the two permutations were:
             *           'off, on, on, off'
             *           'off, off, on, on'
             *      The union is:
             *           'off, maybe, on, maybe'
             *
             * 4. Any non-maybes in the 'union' get set into the board's cells.
             */
        }
示例#27
0
    public override Grid Generate(int w, int h)
    {
        base.Generate(w, h);

        m_edges.Clear();

        m_sets = new int[w * h];

        for (int y = 0; y < h; ++y)
        {
            for (int x = 0; x < w; ++x)
            {
                int index = Cell.ToIndex(x, y, w);
                m_sets[index] = index;

                if (y > 0)
                {
                    m_edges.Add(new Cell(x, y, Cell.State.N));
                }

                if (x > 0)
                {
                    m_edges.Add(new Cell(x, y, Cell.State.W));
                }
            }
        }

        // shuffle
        for (int i = 0, count = m_edges.Count; i < count; ++i)
        {
            int index = m_rnd.Next(count - i);

            Cell temp = m_edges[i];
            m_edges[i]     = m_edges[index];
            m_edges[index] = temp;
        }

        while (m_edges.Count > 0)
        {
            Cell curr = m_edges[0];
            Cell next = Cell.invalid;

            switch (curr.state)
            {
            case Cell.State.N:
                next = new Cell(curr.x, curr.y - 1, Cell.State.None);
                break;

            case Cell.State.W:
                next = new Cell(curr.x - 1, curr.y, Cell.State.None);
                break;
            }

            int cSetId = m_sets[Cell.ToIndex(curr, w)];
            int nSetId = m_sets[Cell.ToIndex(next, w)];

            m_edges.RemoveAt(0);

            if (cSetId != nSetId)
            {
                // combine sets
                for (int i = 0; i < m_sets.Length; ++i)
                {
                    if (m_sets[i] == nSetId)
                    {
                        m_sets[i] = cSetId;
                    }
                }

                Cell.State dir = Cell.GetDir(curr, next);

                m_grid.AddState(curr, dir);
                m_grid.AddState(next, Cell.ToOppositeDir(dir));

                SetCurr(curr);
                m_owner.TakeSnapshot();
            }
        }

        SetCurr(Cell.invalid);
        m_owner.TakeSnapshot();

        return(m_grid);
    }