Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question 3x4 Slot Machine - win condition algorithm

Discussion in 'Scripting' started by Cloky, Aug 18, 2022.

  1. Cloky

    Cloky

    Joined:
    Sep 6, 2019
    Posts:
    4
    Hey,

    as a fun side project, I want to build a slot machine. This slot machine has a 3x4 space with a random symbol in each cell.
    Each symbol is saved into an array of 12 variables (0 is the bottom left slot, 11 is the top right slot)

    Maybe this sc helps understand my setup (ignore the symbol in the middle):
    upload_2022-8-18_10-28-39.png
    upload_2022-8-18_10-39-26.png

    For weeks I'm trying to wrap my head around an algorithm to check for prizes.

    Of course, it's easy to check if a green gems neighbor is also green. But how would I go about checking all green gems that are connected in any way (horizontally, vertically, diagonally) and get an integer of "x green gems are connected"?

    Is the problem maybe my setup of saving all locations in an array? Should I go with 3 arrays, one for each row, to make it easier?

    Please understand that this is just a hobby and I am not an expert by any means :)
     
  2. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Probably loads of ways. For a grid this size any algorithm will work since it can be done in at most 12 iterations. Floodfill comes to mind from each point on the grid, count all of the connected colours and stop once no more are found, you shouldn't need to re-visit any previously noted connected colours either for a nice optimisation.
    That's a solution if you wanted just a raw "the number of connected colours in any direction".

    If you're looking for something more specific, like horizontal only, vertical only, and diagonal only, (which you did mention in your post), then honestly, just write a piece of code that counts them on each row and column for the horizontal and vertical axis, and for diagonal just do the same thing but on both axis combined.
     
    Cloky likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Weeks?!!!! Why not just go to Youtube and work through the first three slots tutorials? You're not the first guy to think of this stuff!

    Screen Shot 2022-08-18 at 6.55.58 AM.png

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors...

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
  4. Cloky

    Cloky

    Joined:
    Sep 6, 2019
    Posts:
    4
    @Zalosath
    Thanks for your answer, I will look into this "Floodfill" approach.
    What do you mean by "count them on each row" - how does that help me?

    @Kurt-Dekker thanks for your idea of actually googling my problem. The first video from your screenshot shows a 1x3 slot machine. The second one is a weird random number generator (?). If you have any actual, helpful ideas please let me know! :)
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Are you suggesting that the algorithm to evaluate is different just because the count of cells is different??

    If so then I would suggest you haven't understood your problem space fully.

    If the Youtube approach eludes you, go to the asset store and download any one of the full slots game packages and unpack it yourself to figure out how it works.

    Hint: this stuff is really really really simple.

    Disclaimer: I have written dozens of slots games professionally.
     
  6. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    This should be a simple for loop for each axis, one for x and one for y.

    Your loop will increment from 0 to 3 on the x axis for example.
    When you start at 0, note the colour of the tile at that location (this assumes y is constant for horizontal checking), when you go to check index 1 (which is actually [1,0], 1 for x, 0 for y), if the colour at index 1 is the same as 0, then you have a chain of 2, continue this process until you reach the final index of 3.

    Same applies for the vertical check but alternate the axis.

    Diagonal has a little more to it, you'll have to loop both axis simultaneously, applying the same principles as the horizontal approach.
     
    Cloky likes this.