Navigation

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

    Looking for help with array and randomness

    Help
    6
    20
    825
    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.
    • K
      kendog400 F last edited by pianofire

      Here is a Deck of cards, it flips out the next card in a sprite sheet. I'm looking to place the deck in an array and flip out a random card till the deck is finished. Can anyone Help ? Thanks in advance....

      Here is the program at Present :

      // View a deck of cards
      // Press "A" to scrool through the deck
      sprite = createsprite ( )
      image = loadimage ("Kenney/boardgames")
      tiles = numtiles(image)
      schale = 3
      setspritelocation (sprite, gwidth ( ),/ 2, gheight ( ) / 2)
      setspriteanimation (sprite, 0, tiles, 0)
      tile = 15      // the 1st card is the 2 of clubs
      backcolor = darkGraan
      
      loop
      clear (darkGreen)
      c = controls ( 0 )
      if c.y and tile > 0 then   // the "Y" key moves to previous card
        tile - = 1
          sleep (.2)
             endif
      if c.a and tile < tiles -1 then  // the "A" key moves to next card
        tile + = 1
          sleep ( .2 )
            endif
      
      setspriteschale (sprite, schale, schale )
      setspriteanimframe (sprite, tile )
      
      if tile > 58 then tile = 15  // once deck gets to the King of spades go back to the begining
       
      size = tilesize (image, tile)
      width = int (size.x)
      height = int (size.y)
      drawsprites ( )
      update ( ) 
      repeat
      
      pianofire 1 Reply Last reply Reply Quote 0
      • pianofire
        pianofire Fuze Team @kendog400 last edited by pianofire

        @kendog400 What you need is a routine to shuffle the pack into a random order. There are lots of ways you could do this but a simple way is to pick two cards at random and swap them over. Then call this a lot of times to mix them up. Something like this:

        // assuming pack is in array called cards
        function shufflePack()
          for i = 0 to 1000 loop
            cardIndex1 = random(52)
            cardIndex2 = random(52)
            temp = cards[cardIndex1]               // save current card at index1
            cards[cardIndex1] = cards[cardIndex2]  // set card at index1 to card at index2
            cards[cardIndex2] = temp               // set card at index2 to the one that was at index1 
          repeat
        return void
        
        1 Reply Last reply Reply Quote 1
        • K
          kendog400 F last edited by

          Thanks, but can i squzze this into the program I have above ? If I can, what line or where can I squzze this function ?
          As you can see, this program is from a sprite view program you've uploaded...I just made it so you can only see the cards...

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

            @kendog400 Have you still got the original program? It will work with that. You need to store the cards in an array first to hold the pack. Then you can scroll through the array to display each card in turn.

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

              I have re-shared the program with the shuffle routine added called "Card Shuffle"

              1 Reply Last reply Reply Quote 1
              • R
                rdurbin F last edited by rdurbin

                What ill generally do for card games is have 2 arrays. Array 1 has the card #(1-52). array 2 controls if number has not been used. I default array 2 to all 1s(true). start off with a random #. say 11 is drawn. Set array2[11] to 0 and set array1[1] to 11. Now for future cards. Do the random # again. Lets say you get 15. Set array2[15] to 0 and array1[2] to 15. Now lets say 11 gets picked again. You check if array2[11]=1. Which is untrue. What i do from here is just increment the random # til it finds one that can be used. Basicially repeat this for each drawn card. Now you need to make it loop around so if the checks get to 52. It will loop around to 1. Its actually easier than it sounds. This can all be accomplished with arrays,if,while statements.

                array deck[53]//1-52
                array used[53]//1-52
                
                for z=1 to 53 loop
                     used[z]=1//makes all cards usable
                repeat
                
                for z=1 to 53 loop
                     a=int(rnd(1)*52+1)
                
                     while used[a]!=1 loop//makes sure not used
                          a+=1//increments rand num
                          if a>52 then
                               a=0
                          endif
                     repeat
                
                     used[a]=0//sets number as used
                     deck[z]=a//places card in deck
                repeat
                
                1 Reply Last reply Reply Quote 2
                • K
                  kendog400 F last edited by pianofire

                  I think I forgot to put this in a function :
                  Take a Peek (rdurbin)

                  card = createsprite ()
                  deck = loadimage ("Kenneyboardgames")
                  tiles = numtiles (deck)
                  setspritelocation(card, 0,67)
                  tile = 15 // start at 2 of clubs
                  
                  // here's where I should put the function....
                  function deck & shuffle ()
                  array deck [53]
                  array used [53]
                  
                  for z = 1 to 53 loop
                      used [z] = 1
                  repeat
                  
                  for z = 1 to 53 loop
                      a += (rnd (1) + 52 + 1)  
                         while used [a] !     // it is here the error msg says Fx not available
                            a += 1
                  if a > 52 then 
                      a = 0
                    endif
                  repeat
                  
                  used [a] = 0
                    deck [z]
                  repeat
                  
                  loop
                     clear (darkGreen)
                       c = controls (0)
                       if c.y and tile > 0 then
                           tile -= 1
                      endif
                      if c.a and tile < tiles -1 then
                         tiles + = 1
                      endif
                      setspritescale (card, 3, 3)
                      setspriteframe (card,tile)
                      if tile > 67 then tile = 15
                      drawsprites ()
                  update ()
                  repeat
                  
                  1 Reply Last reply Reply Quote 0
                  • R
                    rdurbin F last edited by rdurbin

                    while used [a] ! // it is here the error msg says Fx not

                    In while loop its needs to be !=1. 1 means its not used yet, 0 used

                    You should be able to also use

                    while !(used[a]) loop
                    

                    I would also place the function after main loop but call it before main loop. Best to divide individual tasks into functions. Easier to read and understand

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

                      I think this should be:

                      while !used[a] loop

                      meaning keep looping while used[a] is equal to 0

                      Unless i've missed something, which is entirely possible!

                      Personally for legibility i would use:

                      while used[a] == false loop

                      Purely force of habit so i do get told off sometimes by the youngsters!

                      1 Reply Last reply Reply Quote 1
                      • K
                        kendog400 F last edited by pianofire

                        OK, thanks, I finally got that PGM working. But I have another question : I have here a program that displays cards, along the "X" axis, then after 13 cards it moves down the "Y" axis to start another row, but every time it displays a card, the previous card disappears, I'm trying to figure out how to make the previous card stay on the screen. I would like to get all 52 cards on the screen....I tried to setSpriteVisibility (card, true), but nothing seems to work, Heres a example of the program :

                        //..................................................................
                        card = createsprite ( )
                        deck = loadImage ("Kenneyboardgames")
                        tiles = numtiles (deck)
                        setspriteimage (card, deck)
                        x = 150
                        y = 200
                        setspritelocation (card, x, y)
                        setspriteanimation (card, 0, 64)
                        tile = 15
                        loop
                          clear (darkGreen)
                          c = controls (0)
                          if c.a and tile < tiles -1 then
                            tile += 1    
                        setspriteanimation (card, 0, 67)
                          x = x += 80                           // move along the "x" axis
                          setspritelocation (card, x, y)
                            if x > gwidth() - 180 then    // once you go 13 cards across
                              x = 70
                              y = y += 100                     // move down the "y" axis to start a new row
                                endif                               // end the 2nd if statment
                        updatesprites ( )
                        drawsprites ( )
                        update  ( )
                        endif                                       // end the 1st if statment
                        setspriteschale (card, 1.5, 1.5)
                        setspriteanimframe (card, tile)
                        
                        // at this next line I would like to end the program with the end ( ) function, instead it goes back to the beginning...
                          if tile > 67 then tile = 15 
                            endif
                          if tile == 54 then
                            tile += 1                      // skip the joker
                              endif
                        drawsprites ( )
                        update ( )
                        repeat                             // end of program....
                        //...........................................................
                        
                        // here i have an example of an end Function....It worked with other Programs, but not this one....
                        function end ( )
                          clear(lime)
                          drawtext (120, 250, 180, black, "End of Deck")
                          update()
                          sleep(3)
                        return void
                        
                        pianofire 1 Reply Last reply Reply Quote 0
                        • K
                          kendog400 F last edited by

                          Does Fuze have a Drop Shadow effect, like Photoshop ? (Just wondering)

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

                            @kendog400 Your program only has a single sprite. When you change the animation and location of that sprite it just moves and changes that single sprite. If you are going to use sprites you need to have a sprite for each card in the pack. The easiest way to do that is to use an array of sprites. I have shared an example of this.

                            The end function will not terminate your program it will just be called every time around the game loop. Instead you need to make your game loop conditional. Something like

                            finished = false
                            while !finished loop
                              // set finished = true to end program
                            repeat
                            
                            clear(lime)
                            drawtext (120, 250, 180, black, "End of Deck")
                            update()
                            sleep(3)
                            
                            1 Reply Last reply Reply Quote 1
                            • K
                              kendog400 F last edited by

                              OK, thanks, I'LL try this to see if I could get the program to end after the deck of cards is used up...

                              1 Reply Last reply Reply Quote 0
                              • K
                                kendog400 F last edited by

                                As for a drop shadow effect, mabey i could put a rectangle under the sprite, re-set the opacity, then add a blur... (?)

                                1 Reply Last reply Reply Quote 2
                                • K
                                  kendog400 F last edited by

                                  I tried, but it seems I dont know exactly where to put the while loop......I tried to replace the for loop with the command while....It must be some sloppiness on my part.....could you give me a hint ?......

                                  pianofire 2 Replies Last reply Reply Quote 0
                                  • pianofire
                                    pianofire Fuze Team @kendog400 last edited by

                                    @kendog400 Sorry very busy at the moment. I will take a look on Wednesday

                                    1 Reply Last reply Reply Quote 2
                                    • Dave
                                      Dave Fuze Team last edited by

                                      @kendog400 My friend code is 5139-4176-1902

                                      Please add me as a Nintendo friend, share your project and I'll take a look today if I can!

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

                                        @kendog400 OK I think that it is worth you looking at the 3 main different types of loop in Fuze: https://fuzearena.com/help/view/loop

                                        1. Infinite (loop ... repeat) - this doesn't end until it is interrupted (as in your program)
                                        2. Conditional (while condition loop ... repeat) - ends when the condition is met (e.g. the game has finished)
                                        3. For (for index = start to end loop ... repeat) - ends when index > end

                                        You wouldn't normally use 1) as it can only be ended by stopping the program.
                                        You would use 2) when you want something to carry on until something happens
                                        You would use 3) when you have a fixed amount of work to do

                                        In your example you could use 3) because you have a fixed amount of work - you deal the cards and then your program finishes.

                                        In reality your program will probably want to do something else after dealing the cards so you would have loops inside loops

                                        Here is an example of a simple game loop (and possibly the worlds dullest game)

                                        finished = false
                                        
                                        while !finished loop
                                          gameScreen()
                                          endScreen()   
                                        repeat
                                        
                                        function gameScreen()
                                          c = controls(0)
                                          while !c.a loop
                                            clear()
                                            c = controls(0)
                                            printAt(0, 0, "Game Screen. Press A to exit")  
                                            update()
                                          repeat
                                        return void
                                        
                                        function endScreen()
                                          c = controls(0)
                                          while !c.x and !c.y loop
                                            clear()
                                            c = controls(0)
                                            printAt(0, 0, "End Screen. Press Y to play again and X to exit")  
                                            update()
                                          repeat
                                          finished = c.x
                                        return void
                                        
                                        QIsLegit 1 Reply Last reply Reply Quote 3
                                        • QIsLegit
                                          QIsLegit F @pianofire last edited by

                                          @pianofire
                                          Is there no way to break out of an 'infinite loop' using code?

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

                                            @QIsLegit OK yes there is. You can use the break command but it is not good practice https://fuzearena.com/help/view/break

                                            loop
                                              c = controls(0)
                                              if c.a then
                                                break
                                              endif
                                              update()
                                            repeat
                                            
                                            1 Reply Last reply Reply Quote 1
                                            • First post
                                              Last post