Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Card shuffling algorithm needed

    Coding
    5
    5
    550
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • I
      inbetrieb last edited by

      I need an algorithm in fuze which shuffles a deck of cards (for example 10cards) randomized in order.

      1st card is card #4
      2nd card is card #10
      And so on.

      Thanx for help.

      1 Reply Last reply Reply Quote 0
      • pianofire
        pianofire Fuze Team last edited by pianofire

        This came up on Discord yesterday:

         var sequence = [ 0, 1, 2, 3, 4 ]
        
        var shuffled = shuffle( sequence )
        
        print( shuffled )
        update()
        sleep( 3 )
        
        function shuffle( sequence )
          var result = []
          int sequenceLen = len( sequence )
          int i = 0 int j = 0 int k = 0 int t = 0
          for i = 0 to sequenceLen loop
            result[ len( result ) ] = sequence[ i ]
          repeat
          for i = 0 to 100 loop
            j = rnd( sequenceLen )
            k = rnd( sequenceLen )
            t = result[ j ]
            result[ j ] = result[ k ]
            result[ k ] = t
          repeat
        return result 
        

        So this basically copies a sequence and then randomly swaps two entries 100 times but the principal is the same

        1 Reply Last reply Reply Quote 2
        • PB____
          PB____ last edited by

          The shuffle function that I wrote in my Patience Solitaire game (a cards game where I obviously needed to shuffle) is actually quite similar to the suggestion by Pianofire:

          /* 
          some context about the global functions and variables that I use in this code:
          : g 
          is a global struct from which I refer to all my global variables
          
          : g.game.stock 
          is an array of all card values that need to be shuffled
          
          : g.shuffleAmount 
          is a number describing how intense the deck needs to be shuffled (configurable by the user in my case)
          
          : sng 
          is a function I wrote to return the next number in a seeded sequence of numbers (not exactly random, but for this usage random enough to appear random to the user). The seed in Solitaire is random by default but can be configured by the user as well.
          */
          function game_shuffle_stock()
              int i
              int n
              int buffer
              int count = len(g.game.stock)
              for i = 0 to g.shuffleAmount loop
                  n = sng(0, count)
                  buffer = g.game.stock[n]
                  g.game.stock[n] = g.game.stock[i % count]
                  g.game.stock[i % count] = buffer
              repeat
          return void
          
          1 Reply Last reply Reply Quote 3
          • Gothon
            Gothon F last edited by Gothon

            What you want is called a 'random permutation' https://en.wikipedia.org/wiki/Random_permutation
            They are not difficult to produce. ex:

            // Shuffle Program
            
            // Initialize
            Array Nums[10]
            For I = 0 To Len(Nums) Loop
                Nums[I] = I
            Repeat
            
            // Randomly Permute
            For I = 0 To Len(Nums) Loop
                R = I + Rnd(Len(Nums) - I)
                //Swap positions R and I
                Temp = Nums[R]
                Nums[R] = Nums[I]
                Nums[I] = Temp
            Repeat
            
            // Display Result
            Clear()
            For I = 0 To Len(Nums) Loop
                Print(Nums[I], "\n")
            Repeat
            Loop
                Update()
            Repeat
            
            1 Reply Last reply Reply Quote 1
            • T
              tony7059 last edited by tony7059

              I have something but it's brute force. I use a working copy of the items to shuffle. I use random to grab an index into the items then copy that to the first available position of the result array. The items in the working array are then shifted down to replace the used value and the number of indices given to random is reduce by 1. Once that value gets down to 1 you just copy it and you're done. I did a test one 10 number and it looked like the worst-case scenario would be a total of 30 shifts. Logically, the lower the random result (index) the more shifts have to occur in that iteration of the main loop. It's enough code that I couldn't bring myself to copy it here tonight. All the logic fits nicely into a function so it keeps all these extra variables out of the scope of the main processing, which I have done. I, too, knew that I might need this and had done it this way in another language, so decided to recreate it in Fuze tonight.

              EDIT: Wouldn't it be nice to send a code file to yourself in an email from the Switch so you could just drop a snippet in here! : )

              items = [123456789]
              
              function  shuffle(ref options )
                  itemCnt = len( options )
                  array workItems[itemCnt]
                  array shuffle[itemCnt]
              
                  // Make the working copy...
                  for i  = 0 to len( options ) loop
                      workItems[i] = options[i]
                  repeat
                  
                  shufleLimit = itemCnt
                  for i = 0 to itemCnt loop
                      // If last item, just move it...
                      if shuffleLimit == 1 then
                          shuffle[i] = workItems[0]
                      else
                          // Use random index to get working item
                          // into shuffled result array...
                          pickIdx = random( shuffleLimit )
                          shuffle[i] = workItems[pickIdx]
                           // Shift values down in workingItems
                           // the item was not the last...
                          if pickIdx != shuffleLimit-1 then
                              for j = pickIdx to shuffleLimit-1 loop
                                  workItems[j] = workItems[j+1]
                               repeat
                           endif
                           shuffleLimit-=1
                      endif
                  repeat
              return shuffle
              
              result = shuffle( items )
              loop
                  clear()
                  print( "Original: ", items, "\n" )
                  print( "Shuffled: ", result )
                  update()
              repeat
              
              1 Reply Last reply Reply Quote 3
              • First post
                Last post