Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Bear Bibeault
• Jeanne Boyarsky
• paul wheaton
Sheriffs:
• Junilu Lacar
• Paul Clapham
• Knute Snortum
Saloon Keepers:
• Stephan van Hulst
• Ron McLeod
• Tim Moores
• salvin francis
• Carey Brown
Bartenders:
• Tim Holloway
• Frits Walraven
• Vijitha Kumara

# Matching Algorithm for tiles on a triangular matrix

Greenhorn
Posts: 13
Hello,

I have a triangular matrix with triangles that are facing upwards and downwards (I'm trying to make this)

Becks Lore
Greenhorn
Posts: 13
I accidentally pressed the Enter button while trying to insert an image of the board.
It's a Spectrangle board and I want to write an algorithm that checks if it's a valid move by matching the tiles. They have to be the same color to match.
However the upwards tiles have different array indexes than the downwards and I can't get it to work, especially when a player chooses to rotate the tiles.
Any help would be greatly appreciated.

Sheriff
Posts: 13353
221
I wonder if Pascal's Triangle would be of any help here... Just a thought that occurred to me, haven't tried it out but might be an interesting idea to explore.

Marshal
Posts: 63781
209
I think we would need to see a diagram please. Please show us the array indices, too.

Junilu Lacar
Sheriff
Posts: 13353
221
A Spectrangle board looks something like this:

Which is why I'm wondering whether a Pascal's Triangle might be something you could use to assign values to the points that each Tile's vertices touch on the Board. If two tiles touch the same two points on the board, then they will adjacent to each other on that edge.

Junilu Lacar
Sheriff
Posts: 13353
221
On second thought, Pascal's Triangle would probably not be the simplest thing you could possibly do. I think just indexing the points on Board that the vertices will touch will be enough. Again, if two tiles touch the same two points, then that would mean they're adjacent along the edge defined by those two points.

This is actually an interesting exercise in assigning responsibilities to objects and following the Tell, Don't Ask Principle

Campbell Ritchie
Marshal
Posts: 63781
209
The lengths of the rows appear to increase by two each time, and Pascal's Triangle increases by one at a time, so those look different.

Maybe?? Consider determining the maximum size of the board in advance: make your longest array equal to that, and make the shorter arrays by blocking the end indices. Maybe??

Junilu Lacar
Sheriff
Posts: 13353
221
I would index the points where the vertices of the tiles touch, not the spaces on which the tiles are placed, which is probably what you're thinking, Campbell. If you look at the points, Pascal's Triangle fits fine. However, I still think that's overkill.

Campbell Ritchie
Marshal
Posts: 63781
209
Agreed, Junilu.

Becks Lore
Greenhorn
Posts: 13
Wow, thank you guys for the ideas. I'm just a beginner and not really good at math, so I just thought I'd hardcode everything... But, when a player chooses to rotate a tile to match the existing ones, the array gets messed up and if the field is upwards, then the left color should match the right color. Ahhh... too much. But thank you.

Junilu Lacar
Sheriff
Posts: 13353
221
This seems like a really good exercise in object-oriented programming. You could write a procedural solution to this but I think an object-oriented solution will be much better organized and easier to understand. I don't know if you are equipped to be able to come up with an OO solution though. Seems like most students are taught to think in a very procedural-minded way even when they are learning how to program with Java.

Becks Lore
Greenhorn
Posts: 13
Yeah, I agree... I've now created methods that first check if a field has a neighbor and then different methods that return that neighbor.
Then, I will create more methods to check if each color matches their neighbor's individually and after rotation, as breaking down things is easier for me to understand. I know this is a noob approach but I'm just trying my best. If you think this is a really bad thing to do, I'm very open to any different approaches.

Junilu Lacar
Sheriff
Posts: 13353
221
It will be easier to help you if you show us the code you're working on.

Becks Lore
Greenhorn
Posts: 13
This is for finding neighbors. So far, it's working, I've ran a couple tests. The tricky part is to check if it matches after rotation as well.

This is the code for checking if the right neighbor matches:

Sheriff
Posts: 5791
150
• 1

* Don't double-space you line.  Adding a black line here or there is fine.
* When you are tempted to write something like this:

* When you find yourself writing...

* If you find yourself writing:

Rancher
Posts: 3111
110
I would probably stick to a Field[][] array, with 9 rows and each row[i] being of length 2 * i + 1. A Field with even column coordinate would be in BaseDown state, else BaseUp.
My Tile class would have a List of neighbor Fields, all determined at initialization.

Just a quick question:
I just read on Wiki that there are 35 colored Tiles (called trangs) plus a white one, consisting of all the permutaions of three colors out of five. If I calculate what that means, I get:

1) there are 10 permutations of 3 out of 5 colors. Each permutation comes in 6 variants (1,2,3), (1, 3, 2) et cetera. But three of these can be obtained by rotating the tile, so in fact there are ony two different parmutations: 1, 2, 3 and 1, 3, 2. But these can be put on both sides of a Tile, so we need 10 of these Tiles alltogether.

2) there are 20 permutations of two identical and one different colors (112, 113, ..., 554), 20 of them, making 30 Tiles so far

3) and there are 5 Tiles that have three equal colors: 111, 222, ..., 555.

Am I right? If so, it means that the permutations 123 and 132 are mutually exclusive, since you can use a Tile only once in the game, with either front or back facing up.

Final question: can you show us your Tile class?

Junilu Lacar
Sheriff
Posts: 13353
221
I was wondering along the same lines as Piet. It's not actually permutations because that would mean order matters. In reality, you have 35 combinations of 5 colors taken 3 at a time. The 36th trang is the all-white "joker" trang which will match any tile(s) it is placed next to.

5 colors: Red, Green, Blue, Magenta, Yellow

5 trangs that have all three segments of the same color

5 x 4 trangs that have two color: one color for the base and another color for the two sides

# combinations for 5 colors taken 3 at a time: 10

So, 5 + 20 + 10 = 35

Again, the 36th tile is the all-white joker trang.

See http://www.clicksandclicks.com/spectrangle/abo_page.shtml

 If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad: global solutions you can do in your home or backyard https://coderanch.com/t/708587/global-solutions-home-backyard