Results 1 to 12 of 12

Thread: Bejeweled-Like Algorithm Needed...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    119

    Lightbulb Bejeweled-Like Algorithm Needed...

    As the title states, I am looking for a Bejeweled-type algorithm that will trigger if three adjacent objects in a row or column are all clicked.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,173

    Re: Bejeweled-Like Algorithm Needed...

    Well I would think you could simply check the x and y positions of the objects if the x values are the same then they are in the same row, if the y values are the same then they are in the same column. You may also need to take into account the size of the object and make sure that the X or Y does not jump more than the size of one object.

    In the case of bejeweled though doesn't it activate all adjacent like objects that match the one you clicked on if there are 3 or more?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    119

    Re: Bejeweled-Like Algorithm Needed...

    I see what you're saying, but thinking about it is one thing, actually programming it is another. You can check the x and y coords, but you also have to take into account that if another item is clicked outside the row or column, it deselects what they've selected and selects the new item. I'm actually looking for the algorithm that does this. Going on three days straight here and can't figure it out.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,173

    Re: Bejeweled-Like Algorithm Needed...

    There is no algorithm that does this. It would need to be coded specifically for the objects you are using.

    The concept would still be the same, check the x and y and if they differ more than they should then you clear your selections and select the clicked object.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    119

    Re: Bejeweled-Like Algorithm Needed...

    Okay, if I have a 3x3 board, I could see how easy it would be to use the x and y coords. I, however, am using a 7x7 board. I could use the y coord to make sure they have chosen everything in a column, but the object of the game is to choose only 3. Therefore, they could choose everything in one column, skipping rows, which defeats the purpose of the game. They could choose row 1/column 1, row 2/column 1, and row 5/column 1. Those are all on the same y coord, but still not adjacent, which is what I need.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,432

    Re: Bejeweled-Like Algorithm Needed...

    In that case in addition to checking that the column is the same, you need to check that the rows are adjacent to each other (so row 3 is adjacent to rows 2 and 4, but not adjacent to row 5). There are a variety of ways you could do that, probably the simplest is to find the lowest and highest row numbers of the selection (the highest should be 2 bigger than the lowest).

    Note that the amount of rows and columns on your board does not matter... the same condition applies to the minimum board size (3x3) too, the only difference is that for the minimum size the condition can always be assumed to be true.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    119

    Re: Bejeweled-Like Algorithm Needed...

    I understand, yet it proves to be more difficult than just that when programming it. Perhaps you could try it yourself with a simple 4x4 board and see what I mean?

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,173

    Re: Bejeweled-Like Algorithm Needed...

    Well you have really given us no idea of how you are trying to do this, what your objects are nor how they are positioned.

    Assuming that the objects take up the same amount of space and are placed at the same distance apart it really should be pretty simple

    For example if the object is 20x20 then if the user clicked on an object which is located at 100x200 the next object would need to be either 100x180 or 100x220 or it would be 80x200 or 120x 200 and so on
    It is just a matter of checking the 2 values of each object. If you want the objects to all be in a row or column and adjacent then either the X or the Y of all of them needs to be the same which would be your first test
    If that test is true then you check the value of the other coordinate and see that there is a difference of 20 from the others.

    What I might do would be to save the x y of the last item clicked, then compare to the next item clicked if either x or y are equal then check the difference in the other value = 20 if yes then that becomes the last object clicked and we add it to a list of objects to do something with [you did not tell us what you want to do with it once 3 are clicked] If the test is false then that becomes the last object clicked and you would remove the objects from the list then add the newly selected object.

    You may also want to use some kind of highlighting on the objects that are selected for the user to see but again you did not tell us anything other than wanting to know how to tell if the 3 objects are in the same row or column and are next to each other which the simple x/y test will tell you.

    If you are still having trouble show us the code you have tried.

  9. #9
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Bejeweled-Like Algorithm Needed...

    I don't think this is too difficult if you go back to basics,unless I've misunderstood the question.

    For each square there are 4 directions to search vis Up, Down, Left and Right. There are 'special' squares (i.e. the corners) but they can be disregarded for the sake of a simplistic algorithm.

    Choose a direction to go in and see if the adjcent square is selected, if it is then move in that direction again and check if the that square is selected. If it is then do whatever you want to do when 3 adjecent squares have been selected. If the first square is not selected (or a square doesn't exist because you've hit an edge) then select another direction for the starting square and repeat. If the first square is selected but the second isn't then move in the opposite direction and check if that square is selected. Continue until you either find 3 adjcent squares or you've run out of directions in which to search.

    That should give you a basic algorithm.

    When it comes to implementation I think I'd create 4 Functions; LookLeft, LookRight, LookUp and LookDown. Each would have arguments of X and Y (the square to check) and return True if it is selected, or False if it isn't or it doesn't exist.


    Assuming Xn, Yn is Selected, an unoptimised implementation might look like this:
    Code:
     
    If LookLeft(Xn + 1, Yn) Then
        If LookLeft(Xn + 2, Yn) Then
           ThreeSelected
           Exit Sub
        Else
            If LookRight(Xn - 1, Yn) Then
                ThreeSelected
                Exit Sub
            End If
        End If
    End If
    If LookRight(Xn - 1, Yn) Then
        If LookRight(Xn - 2, Yn) Then
            ThreeSelected
            Exit Sub
        Else
            If LookLeft(Xn - 1, Yn) Then
                ThreeSelected
                Exit Sub
            End If
        End If
    End If
    If LookUp(Xn, Yn - 1) Then
        If LookUp(Xn, Yn - 2) Then
            ThreeSelected
            Exit Sub
        Else
            If LookDown(Xn, Yn + 1) Then
                ThreeSelected
                Exit Sub
            End If
        End If
    End If
    If LookDown(Xn, Yn + 1) Then
        If LookDown(Xn, Yn + 2) Then
            ThreeSelected
            Exit Sub
        Else
            If LookUp(Xn, Yn - 1) Then
                ThreeSelected
                Exit Sub
            End If
        End If
    End If
    I'm not a particular fan of 'Exit Sub' but without reversing the logic or having a lot of nested If statements, making it a bit difficiult to follow the logic, I can't immediately see a way of avoiding them in this particular instance.Perhaps 'Select Case True' might be better.

    The Functions themselves are probably trivial to code, but as DM has mentioned, we don't have any idea of what your 'objects' are, how their positions are defined and how selection is indicated / tested.

    EDIT: On reflection,I don't think this can be described as an Algorithm, just a block of raw logic.

    EDIT2: Having had another Cup of Tea and thought some more, you don't actually need 4 Different Functions, one would do, since the direction is implied by the values of the arguments.
    Last edited by Doogle; Sep 16th, 2013 at 03:16 AM.

  10. #10
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Bejeweled-Like Algorithm Needed...

    Maybe it's no directly related but the code meight be helpful: Some time ago i've created a Bejeweled Blitz autoplayer, it played bejeweled alone at super speed. This code first search for moves that would create a 3 jewel line and using come mouse API it makes the move, i remember it also worked with classic Bejeweled for me but for some reason it didn't work for some people, i assume it was related to mouse speeds or something like that. Here is the Link.

    The code does something similar to what Doogle posted, lookright, lookdown..etc.

    BTW @Doogle: Happy birthday! today is my birthday too

  11. #11
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Bejeweled-Like Algorithm Needed...

    Quote Originally Posted by jcis View Post
    BTW @Doogle: Happy birthday! today is my birthday too
    Same to you - sorry it's a day late!

  12. #12
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,491

    Re: Bejeweled-Like Algorithm Needed...

    OK, before I get cursed, sworn at, beaten up, stabbed, shot, shot twice, burnt, thrown with acid, get shouted at, I know this is the VB 6 forum, but I *might* have some input as well.

    Have a look at this article ( as said, I know this is the VB 6 forum and not .NET ) it is a very basic tile matching game I created :

    http://www.codeguru.com/columns/vb/c...b.net-2012.htm

    There might be some useful tips in there ( especially in the creation of the game board ). I have found that if we create the game board with the use of the tag properties and a control array, the rest will be easy.
    Then, we can break down the board into rows and columns
    Then, matching up the items is a breeze.

    I hope this gives you some more ideas and insight and that I have been helpful
    VB.NET MVP 2008 - Present

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width