Creating a randomized set of numbers in an array without the same number repeating three times in a row












0















Hi so i have to basically create a game like candy crush saga and i created a 2d array to start and i was able to create the game board with the corresponding pictures based on the indices of my array. However i have to make sure that to start off i cant have 3 of the same pictures or candy or whatever in a row. To create the array in the first place i used:



board = [[random.randint(1,6) for i in range(7)] for j in range(9)]


to create a 9x7 board and basically each number from 1-6 corresponds to a specific picture but i cant figure out how to iterate through and check if there are three of the same numbers in a row. Any ideas? Thanks










share|improve this question

























  • When you say "in a row" do you literally mean in a row? Or do you mean rows/columns/possibly diagonals?

    – ggorlen
    Nov 25 '18 at 4:20













  • sorry for not clarifying, i mean like in a row either vertically or horizontally no diagonals

    – shehab
    Nov 25 '18 at 4:21











  • row_counts = [collections.Counter(row) for row in board] will give you number of occurences for each item.

    – Mateen Ulhaq
    Nov 25 '18 at 4:27











  • This will tell you which ones have three or more: [[k for k, v in row.items() if v >= 3] for row in row_counts]

    – Mateen Ulhaq
    Nov 25 '18 at 4:28











  • 3 in a row?? like 3 beside each other? or 3 in the actual row cause i need to change if there are 3 beside each other (in a row) either horizontally or vertically

    – shehab
    Nov 25 '18 at 4:30
















0















Hi so i have to basically create a game like candy crush saga and i created a 2d array to start and i was able to create the game board with the corresponding pictures based on the indices of my array. However i have to make sure that to start off i cant have 3 of the same pictures or candy or whatever in a row. To create the array in the first place i used:



board = [[random.randint(1,6) for i in range(7)] for j in range(9)]


to create a 9x7 board and basically each number from 1-6 corresponds to a specific picture but i cant figure out how to iterate through and check if there are three of the same numbers in a row. Any ideas? Thanks










share|improve this question

























  • When you say "in a row" do you literally mean in a row? Or do you mean rows/columns/possibly diagonals?

    – ggorlen
    Nov 25 '18 at 4:20













  • sorry for not clarifying, i mean like in a row either vertically or horizontally no diagonals

    – shehab
    Nov 25 '18 at 4:21











  • row_counts = [collections.Counter(row) for row in board] will give you number of occurences for each item.

    – Mateen Ulhaq
    Nov 25 '18 at 4:27











  • This will tell you which ones have three or more: [[k for k, v in row.items() if v >= 3] for row in row_counts]

    – Mateen Ulhaq
    Nov 25 '18 at 4:28











  • 3 in a row?? like 3 beside each other? or 3 in the actual row cause i need to change if there are 3 beside each other (in a row) either horizontally or vertically

    – shehab
    Nov 25 '18 at 4:30














0












0








0








Hi so i have to basically create a game like candy crush saga and i created a 2d array to start and i was able to create the game board with the corresponding pictures based on the indices of my array. However i have to make sure that to start off i cant have 3 of the same pictures or candy or whatever in a row. To create the array in the first place i used:



board = [[random.randint(1,6) for i in range(7)] for j in range(9)]


to create a 9x7 board and basically each number from 1-6 corresponds to a specific picture but i cant figure out how to iterate through and check if there are three of the same numbers in a row. Any ideas? Thanks










share|improve this question
















Hi so i have to basically create a game like candy crush saga and i created a 2d array to start and i was able to create the game board with the corresponding pictures based on the indices of my array. However i have to make sure that to start off i cant have 3 of the same pictures or candy or whatever in a row. To create the array in the first place i used:



board = [[random.randint(1,6) for i in range(7)] for j in range(9)]


to create a 9x7 board and basically each number from 1-6 corresponds to a specific picture but i cant figure out how to iterate through and check if there are three of the same numbers in a row. Any ideas? Thanks







python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 4:24









Mateen Ulhaq

11.3k114792




11.3k114792










asked Nov 25 '18 at 4:14









shehabshehab

153




153













  • When you say "in a row" do you literally mean in a row? Or do you mean rows/columns/possibly diagonals?

    – ggorlen
    Nov 25 '18 at 4:20













  • sorry for not clarifying, i mean like in a row either vertically or horizontally no diagonals

    – shehab
    Nov 25 '18 at 4:21











  • row_counts = [collections.Counter(row) for row in board] will give you number of occurences for each item.

    – Mateen Ulhaq
    Nov 25 '18 at 4:27











  • This will tell you which ones have three or more: [[k for k, v in row.items() if v >= 3] for row in row_counts]

    – Mateen Ulhaq
    Nov 25 '18 at 4:28











  • 3 in a row?? like 3 beside each other? or 3 in the actual row cause i need to change if there are 3 beside each other (in a row) either horizontally or vertically

    – shehab
    Nov 25 '18 at 4:30



















  • When you say "in a row" do you literally mean in a row? Or do you mean rows/columns/possibly diagonals?

    – ggorlen
    Nov 25 '18 at 4:20













  • sorry for not clarifying, i mean like in a row either vertically or horizontally no diagonals

    – shehab
    Nov 25 '18 at 4:21











  • row_counts = [collections.Counter(row) for row in board] will give you number of occurences for each item.

    – Mateen Ulhaq
    Nov 25 '18 at 4:27











  • This will tell you which ones have three or more: [[k for k, v in row.items() if v >= 3] for row in row_counts]

    – Mateen Ulhaq
    Nov 25 '18 at 4:28











  • 3 in a row?? like 3 beside each other? or 3 in the actual row cause i need to change if there are 3 beside each other (in a row) either horizontally or vertically

    – shehab
    Nov 25 '18 at 4:30

















When you say "in a row" do you literally mean in a row? Or do you mean rows/columns/possibly diagonals?

– ggorlen
Nov 25 '18 at 4:20







When you say "in a row" do you literally mean in a row? Or do you mean rows/columns/possibly diagonals?

– ggorlen
Nov 25 '18 at 4:20















sorry for not clarifying, i mean like in a row either vertically or horizontally no diagonals

– shehab
Nov 25 '18 at 4:21





sorry for not clarifying, i mean like in a row either vertically or horizontally no diagonals

– shehab
Nov 25 '18 at 4:21













row_counts = [collections.Counter(row) for row in board] will give you number of occurences for each item.

– Mateen Ulhaq
Nov 25 '18 at 4:27





row_counts = [collections.Counter(row) for row in board] will give you number of occurences for each item.

– Mateen Ulhaq
Nov 25 '18 at 4:27













This will tell you which ones have three or more: [[k for k, v in row.items() if v >= 3] for row in row_counts]

– Mateen Ulhaq
Nov 25 '18 at 4:28





This will tell you which ones have three or more: [[k for k, v in row.items() if v >= 3] for row in row_counts]

– Mateen Ulhaq
Nov 25 '18 at 4:28













3 in a row?? like 3 beside each other? or 3 in the actual row cause i need to change if there are 3 beside each other (in a row) either horizontally or vertically

– shehab
Nov 25 '18 at 4:30





3 in a row?? like 3 beside each other? or 3 in the actual row cause i need to change if there are 3 beside each other (in a row) either horizontally or vertically

– shehab
Nov 25 '18 at 4:30












1 Answer
1






active

oldest

votes


















0














This solution will not win a beauty contest, but it generates the board in one pass, which should offer significant performance advantages over generating entire boards and running counts on them until you manage to get a lucky board (there's about a 10% chance a random board will be valid).



def generate_board(rows=9, cols=7):
board =

for i in range(rows):
board.append([0] * cols)

for j in range(cols):
while not board[i][j]:
c = random.randint(1, 6)

if (i < 2 or board[i-2][j] != c or board[i-1][j] != c) and
(j < 2 or board[i][j-2] != c or board[i][j-1] != c):
board[i][j] = c

return board


A few sample output boards:



[6, 1, 3, 3, 2, 5, 5] [3, 5, 6, 5, 2, 1, 1] [2, 2, 1, 5, 4, 6, 6] [4, 1, 1, 5, 2, 6, 2]
[4, 2, 5, 5, 3, 5, 3] [4, 2, 2, 1, 4, 3, 1] [5, 3, 6, 2, 3, 4, 5] [1, 3, 6, 1, 2, 4, 2]
[5, 2, 6, 5, 1, 4, 6] [2, 4, 1, 4, 3, 3, 6] [4, 1, 4, 1, 4, 1, 3] [4, 3, 4, 3, 1, 6, 3]
[5, 6, 2, 6, 3, 6, 6] [4, 5, 5, 3, 4, 5, 5] [5, 5, 3, 3, 1, 6, 3] [2, 4, 1, 5, 5, 1, 5]
[2, 1, 4, 2, 4, 3, 5] [4, 6, 5, 1, 5, 3, 4] [1, 3, 2, 3, 6, 2, 4] [4, 6, 4, 3, 6, 4, 2]
[1, 3, 4, 1, 6, 3, 1] [6, 2, 1, 5, 1, 2, 1] [6, 3, 3, 2, 4, 6, 6] [2, 3, 3, 6, 2, 1, 5]
[2, 1, 1, 3, 2, 5, 4] [6, 5, 5, 4, 5, 3, 3] [2, 4, 2, 2, 3, 6, 3] [2, 1, 3, 6, 1, 2, 6]
[3, 1, 2, 3, 3, 5, 1] [5, 3, 6, 1, 5, 4, 1] [4, 2, 1, 6, 5, 3, 6] [3, 6, 5, 2, 1, 5, 5]
[2, 6, 3, 1, 5, 6, 4] [6, 6, 4, 2, 3, 4, 2] [5, 4, 6, 6, 4, 2, 6] [5, 3, 1, 6, 3, 6, 2]


Try it!






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464602%2fcreating-a-randomized-set-of-numbers-in-an-array-without-the-same-number-repeati%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This solution will not win a beauty contest, but it generates the board in one pass, which should offer significant performance advantages over generating entire boards and running counts on them until you manage to get a lucky board (there's about a 10% chance a random board will be valid).



    def generate_board(rows=9, cols=7):
    board =

    for i in range(rows):
    board.append([0] * cols)

    for j in range(cols):
    while not board[i][j]:
    c = random.randint(1, 6)

    if (i < 2 or board[i-2][j] != c or board[i-1][j] != c) and
    (j < 2 or board[i][j-2] != c or board[i][j-1] != c):
    board[i][j] = c

    return board


    A few sample output boards:



    [6, 1, 3, 3, 2, 5, 5] [3, 5, 6, 5, 2, 1, 1] [2, 2, 1, 5, 4, 6, 6] [4, 1, 1, 5, 2, 6, 2]
    [4, 2, 5, 5, 3, 5, 3] [4, 2, 2, 1, 4, 3, 1] [5, 3, 6, 2, 3, 4, 5] [1, 3, 6, 1, 2, 4, 2]
    [5, 2, 6, 5, 1, 4, 6] [2, 4, 1, 4, 3, 3, 6] [4, 1, 4, 1, 4, 1, 3] [4, 3, 4, 3, 1, 6, 3]
    [5, 6, 2, 6, 3, 6, 6] [4, 5, 5, 3, 4, 5, 5] [5, 5, 3, 3, 1, 6, 3] [2, 4, 1, 5, 5, 1, 5]
    [2, 1, 4, 2, 4, 3, 5] [4, 6, 5, 1, 5, 3, 4] [1, 3, 2, 3, 6, 2, 4] [4, 6, 4, 3, 6, 4, 2]
    [1, 3, 4, 1, 6, 3, 1] [6, 2, 1, 5, 1, 2, 1] [6, 3, 3, 2, 4, 6, 6] [2, 3, 3, 6, 2, 1, 5]
    [2, 1, 1, 3, 2, 5, 4] [6, 5, 5, 4, 5, 3, 3] [2, 4, 2, 2, 3, 6, 3] [2, 1, 3, 6, 1, 2, 6]
    [3, 1, 2, 3, 3, 5, 1] [5, 3, 6, 1, 5, 4, 1] [4, 2, 1, 6, 5, 3, 6] [3, 6, 5, 2, 1, 5, 5]
    [2, 6, 3, 1, 5, 6, 4] [6, 6, 4, 2, 3, 4, 2] [5, 4, 6, 6, 4, 2, 6] [5, 3, 1, 6, 3, 6, 2]


    Try it!






    share|improve this answer






























      0














      This solution will not win a beauty contest, but it generates the board in one pass, which should offer significant performance advantages over generating entire boards and running counts on them until you manage to get a lucky board (there's about a 10% chance a random board will be valid).



      def generate_board(rows=9, cols=7):
      board =

      for i in range(rows):
      board.append([0] * cols)

      for j in range(cols):
      while not board[i][j]:
      c = random.randint(1, 6)

      if (i < 2 or board[i-2][j] != c or board[i-1][j] != c) and
      (j < 2 or board[i][j-2] != c or board[i][j-1] != c):
      board[i][j] = c

      return board


      A few sample output boards:



      [6, 1, 3, 3, 2, 5, 5] [3, 5, 6, 5, 2, 1, 1] [2, 2, 1, 5, 4, 6, 6] [4, 1, 1, 5, 2, 6, 2]
      [4, 2, 5, 5, 3, 5, 3] [4, 2, 2, 1, 4, 3, 1] [5, 3, 6, 2, 3, 4, 5] [1, 3, 6, 1, 2, 4, 2]
      [5, 2, 6, 5, 1, 4, 6] [2, 4, 1, 4, 3, 3, 6] [4, 1, 4, 1, 4, 1, 3] [4, 3, 4, 3, 1, 6, 3]
      [5, 6, 2, 6, 3, 6, 6] [4, 5, 5, 3, 4, 5, 5] [5, 5, 3, 3, 1, 6, 3] [2, 4, 1, 5, 5, 1, 5]
      [2, 1, 4, 2, 4, 3, 5] [4, 6, 5, 1, 5, 3, 4] [1, 3, 2, 3, 6, 2, 4] [4, 6, 4, 3, 6, 4, 2]
      [1, 3, 4, 1, 6, 3, 1] [6, 2, 1, 5, 1, 2, 1] [6, 3, 3, 2, 4, 6, 6] [2, 3, 3, 6, 2, 1, 5]
      [2, 1, 1, 3, 2, 5, 4] [6, 5, 5, 4, 5, 3, 3] [2, 4, 2, 2, 3, 6, 3] [2, 1, 3, 6, 1, 2, 6]
      [3, 1, 2, 3, 3, 5, 1] [5, 3, 6, 1, 5, 4, 1] [4, 2, 1, 6, 5, 3, 6] [3, 6, 5, 2, 1, 5, 5]
      [2, 6, 3, 1, 5, 6, 4] [6, 6, 4, 2, 3, 4, 2] [5, 4, 6, 6, 4, 2, 6] [5, 3, 1, 6, 3, 6, 2]


      Try it!






      share|improve this answer




























        0












        0








        0







        This solution will not win a beauty contest, but it generates the board in one pass, which should offer significant performance advantages over generating entire boards and running counts on them until you manage to get a lucky board (there's about a 10% chance a random board will be valid).



        def generate_board(rows=9, cols=7):
        board =

        for i in range(rows):
        board.append([0] * cols)

        for j in range(cols):
        while not board[i][j]:
        c = random.randint(1, 6)

        if (i < 2 or board[i-2][j] != c or board[i-1][j] != c) and
        (j < 2 or board[i][j-2] != c or board[i][j-1] != c):
        board[i][j] = c

        return board


        A few sample output boards:



        [6, 1, 3, 3, 2, 5, 5] [3, 5, 6, 5, 2, 1, 1] [2, 2, 1, 5, 4, 6, 6] [4, 1, 1, 5, 2, 6, 2]
        [4, 2, 5, 5, 3, 5, 3] [4, 2, 2, 1, 4, 3, 1] [5, 3, 6, 2, 3, 4, 5] [1, 3, 6, 1, 2, 4, 2]
        [5, 2, 6, 5, 1, 4, 6] [2, 4, 1, 4, 3, 3, 6] [4, 1, 4, 1, 4, 1, 3] [4, 3, 4, 3, 1, 6, 3]
        [5, 6, 2, 6, 3, 6, 6] [4, 5, 5, 3, 4, 5, 5] [5, 5, 3, 3, 1, 6, 3] [2, 4, 1, 5, 5, 1, 5]
        [2, 1, 4, 2, 4, 3, 5] [4, 6, 5, 1, 5, 3, 4] [1, 3, 2, 3, 6, 2, 4] [4, 6, 4, 3, 6, 4, 2]
        [1, 3, 4, 1, 6, 3, 1] [6, 2, 1, 5, 1, 2, 1] [6, 3, 3, 2, 4, 6, 6] [2, 3, 3, 6, 2, 1, 5]
        [2, 1, 1, 3, 2, 5, 4] [6, 5, 5, 4, 5, 3, 3] [2, 4, 2, 2, 3, 6, 3] [2, 1, 3, 6, 1, 2, 6]
        [3, 1, 2, 3, 3, 5, 1] [5, 3, 6, 1, 5, 4, 1] [4, 2, 1, 6, 5, 3, 6] [3, 6, 5, 2, 1, 5, 5]
        [2, 6, 3, 1, 5, 6, 4] [6, 6, 4, 2, 3, 4, 2] [5, 4, 6, 6, 4, 2, 6] [5, 3, 1, 6, 3, 6, 2]


        Try it!






        share|improve this answer















        This solution will not win a beauty contest, but it generates the board in one pass, which should offer significant performance advantages over generating entire boards and running counts on them until you manage to get a lucky board (there's about a 10% chance a random board will be valid).



        def generate_board(rows=9, cols=7):
        board =

        for i in range(rows):
        board.append([0] * cols)

        for j in range(cols):
        while not board[i][j]:
        c = random.randint(1, 6)

        if (i < 2 or board[i-2][j] != c or board[i-1][j] != c) and
        (j < 2 or board[i][j-2] != c or board[i][j-1] != c):
        board[i][j] = c

        return board


        A few sample output boards:



        [6, 1, 3, 3, 2, 5, 5] [3, 5, 6, 5, 2, 1, 1] [2, 2, 1, 5, 4, 6, 6] [4, 1, 1, 5, 2, 6, 2]
        [4, 2, 5, 5, 3, 5, 3] [4, 2, 2, 1, 4, 3, 1] [5, 3, 6, 2, 3, 4, 5] [1, 3, 6, 1, 2, 4, 2]
        [5, 2, 6, 5, 1, 4, 6] [2, 4, 1, 4, 3, 3, 6] [4, 1, 4, 1, 4, 1, 3] [4, 3, 4, 3, 1, 6, 3]
        [5, 6, 2, 6, 3, 6, 6] [4, 5, 5, 3, 4, 5, 5] [5, 5, 3, 3, 1, 6, 3] [2, 4, 1, 5, 5, 1, 5]
        [2, 1, 4, 2, 4, 3, 5] [4, 6, 5, 1, 5, 3, 4] [1, 3, 2, 3, 6, 2, 4] [4, 6, 4, 3, 6, 4, 2]
        [1, 3, 4, 1, 6, 3, 1] [6, 2, 1, 5, 1, 2, 1] [6, 3, 3, 2, 4, 6, 6] [2, 3, 3, 6, 2, 1, 5]
        [2, 1, 1, 3, 2, 5, 4] [6, 5, 5, 4, 5, 3, 3] [2, 4, 2, 2, 3, 6, 3] [2, 1, 3, 6, 1, 2, 6]
        [3, 1, 2, 3, 3, 5, 1] [5, 3, 6, 1, 5, 4, 1] [4, 2, 1, 6, 5, 3, 6] [3, 6, 5, 2, 1, 5, 5]
        [2, 6, 3, 1, 5, 6, 4] [6, 6, 4, 2, 3, 4, 2] [5, 4, 6, 6, 4, 2, 6] [5, 3, 1, 6, 3, 6, 2]


        Try it!







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 25 '18 at 5:34

























        answered Nov 25 '18 at 4:41









        ggorlenggorlen

        7,1703825




        7,1703825






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464602%2fcreating-a-randomized-set-of-numbers-in-an-array-without-the-same-number-repeati%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)