Mahjong?

Community Forums/General Help/Mahjong?

SLotman(Posted 2010) [#1]
Anyone here like it? Played it?

I have a small doubt: I just discovered how to play it (with Mahjong Titans on Vista), and now I want to write my own.

I managed to correctly draw the board, check for matches and free tiles... but I couldn't figure out yet how to generate the board, in a way that it's always solvable.

I thought sorting pieces in pairs, and laying out them also in pairs in the board - I could make it, but no go so far.

Has anyone tried that? Any hints or clues???

Thanks in advance to everyone ;)


xlsior(Posted 2010) [#2]
I thought sorting pieces in pairs, and laying out them also in pairs in the board - I could make it, but no go so far.


Should work just fine.

Just remember that even with an 'always solvable' board, you can still fail: Even though you guarantee that there is (was) a workable solution, the order that the tiles are picked up in can prevent you from completing a board regardless since there are multiple sets of the same tiles present in a typical Mahjong game.

(For example: You have two pairs of a particular tile. You randomly put down a pair at the following coordinates: (1,1) and (2,1)
then you put down the second pair: (1,1) and (2,2)
Player makes the match (2,1) + (2,2), which is valid... But now the other pair is both stacked on (1,1), and no longer can be picked up and the board is now unsolvable.

If the player had picked up (1,1) + (2,1) and then (1,1) + (2,2) the board would still be winnable at that point.

Anyway, the occasionally painting yourself in the corner is pretty much by design I gather. If you wanted to make it easier to could also check and verify that you're not having any duplicates in a stack, but that make the game too easy... Plus you'd still have the opportunity to have other deadlock combinations where certain tiles you need to unlock a row are stock somewhere in the middle of that very same row.

It's pretty much by design though, not every play will automatically be winnable. You have to consider the odds of getting stuck later when making certain combinations, that's part of the game.


xlsior(Posted 2010) [#3]
Oh, and something else to consider: different mahjong games have different goals.

In some variations your goal is to clear the entire board every time.

In others, all you need to do is unlock two special golden tiles, and once you make that match the level is over regardless of how many tiles are left. (You can reward/penalize with bonus points depending on how many tiles are left at the time you make that match. An example of a series of games that use that Mechanic is MahJong Quest (http://www.gamehouse.com/download-games/mah-jong-quest )

Some of the more recent versions also have other special tiles, which give the player the ability to perform special operations, eg.:
- Choose any tile and remove it (plus the other one forming its pair, which may or may not be visible)
- Remove all occurrences of a particular tile
- Allow the user to swap two tiles of their choice

Having those specials allows the user a way out of a deadlock situation, if they use them carefully. You can of course reward the user for any remaining special abilities at the end of a level, to reward actually thinking ahead.

Plus there's often (a limited number of) undo's, hints, etc. for when people get stuck. It's also helpful if the game shows how many possible moves are on the table at that moment.


TaskMaster(Posted 2010) [#4]
Mahjong is actually a 4-player gambling game.

The game you are referring to uses Mahjong Tiles, but is just a matching game. It is called Mahjong because the first person to create a matching game with the tiles called it Mahjong. I think it is actually called Mahjong Solitaire.

Wikipedia link to the actual Mahjong game:

http://en.wikipedia.org/wiki/Mahjong


_Skully(Posted 2010) [#5]
Ya I played real Mahjong at my brothers place.. his wife is from Hong Kong and taught us how to play... totally different than computer Mahjong.


Ginger Tea(Posted 2010) [#6]
ditto on the its not really mahjong issue, having said that it frees you up from always using the default tile set of chinese symbols and other pieces, you could have dominoes, playing cards or as one blitz game from way back when brightly coloured 8bit computer logos and game icons

the same goes for how the tiles are laid out, you dont have to follow the traditional layouts, you could design your own


SLotman(Posted 2010) [#7]
Thanks everyone. I already am blocking two tiles with same symbol to overlap themselves, and even tried to block them side-by-side, but still, there are deadlocks.

As for it being Mahjong Solitaire, I know, I read the wikipedia article (and other sites) already :)

My first goal is to implement the mode to "remove all tiles" - like in Mahjong Titans - and (I don't know how) on MS version you *can* always win, so there is a way to guarantee the board will always be solvable.

I already have preliminary functions to test combinations, find combinations and show how many are left.

Of course, after I come with a solution I'll implement multiple layouts, multiple tilesets, and even other play modes. But I need to get the basic working right first ;)


TaskMaster(Posted 2010) [#8]
How do you know there are deadlocks? You play it out?

How do you know you are not just making a mistake that ls leading to a deadlock?

Two of the same tiles can be on top of each other, three is what you do not want.

Why are you so worried about whether there will be deadlocked? If a person were to play by themself and actually stack up the tiles, there can be deadlocks.

The best thing to do is score how many tiles are left over, as well as how long it took to get there. Then it does not matter if they deadlock. They can still try to remove more tiles than the person before them or the same number of tiles in a faster time.


_Skully(Posted 2010) [#9]
Well, if tiles on the board layout are placed in pairs one after the other, you should always end up with a solvable board... since removing them in reverse order will solve it. That doesn't mean the player will do that of course, which is why the player may not solve every solvable board ;)


SLotman(Posted 2010) [#10]
I think I got it! Sometimes erasing all code (at least the one which shuffled the board heh) and starting from scratch helps a lot =)

The trick was to avoid the same tile above and below, side by side, and to fill the board from the "borders" to the center... still have more testing to do (like other layouts and so) but I think I got it :)


How do you know you are not just making a mistake that ls leading to a deadlock?


Because I wrote code to test all possible solutions to a given board :)

Why are you so worried about whether there will be deadlocked?

I don't want to create a "no-win" game. Otherwise, I don't think the game would be much fun ;)


Well, if tiles on the board layout are placed in pairs one after the other, you should always end up with a solvable board...

Not always. If you have a small board, and few tiles, it works. But with bigger boards, you can easily lock one "pair" with other two. Specially since the board has up to 5 layers, you can even block a tile below another!

Now I'm going to get more layouts into the game and see if it will work everytime :)

Cheers!


xlsior(Posted 2010) [#11]
Except pretty much all of the MahJong games I've seen (I've played maybe half a dozen of them over the past few years) DO allow side-by-side matches, and two copies of the same tile on top of each other.

Preventing those might actually make it too easy?


_Skully(Posted 2010) [#12]
Not always. If you have a small board, and few tiles, it works. But with bigger boards, you can easily lock one "pair" with other two. Specially since the board has up to 5 layers, you can even block a tile below another!


If you don't allow the pairs to be placed on top of each other then you won't have that problem... as long as the two are placed in different locations... you might end up with two same tile types stacked... but its in a way that the board can still be solved.

Starting the board from the border to the inside tells me how to play your boards to win... always start from the middle and work your way out to the border.


TaskMaster(Posted 2010) [#13]
I think _Skully is right. If you place the tiles in pairs in playable locations, then the reverse of that will clear the board, so you ARE guaranteed to have a winnable board.


SLotman(Posted 2010) [#14]
Placing tiles in pairs guarantees nothing. Look at this for example:

First place two "A" tiles on a 4x2 board:

.A..
A...

Then, add two "B" tiles:
.AB.
A..B

Now, two "C" tiles:
.ABC
AC.B

And last, two "D" tiles:
DABC
ACDB

It's a nice deadlock.

I'm still looking for a good solution for *any* board :(


xlsior(Posted 2010) [#15]
Placing tiles in pairs guarantees nothing. Look at this for example:


It does if you don't fill in random blanks, but work either outwards from the center...


TaskMaster(Posted 2010) [#16]
You placed one of the D's in an unplayable location...

Like I said, if you place the tiles in playable locations, you will be guaranteed to have a winnable board.


SLotman(Posted 2010) [#17]
So, what I'm missing? Look at the example below, it's a valid board (taken from another game), I'm placing pairs from center to border, in "playable positions", but still, the last two pieces end up blocking everything!




TaskMaster(Posted 2010) [#18]
The last 2 were not a valid placement. After placing them, they must be able to be picked up. You have to place them as if they were legally played.


SLotman(Posted 2010) [#19]
I know that. But that's the only place left on the board, so I have nowhere else to put them.

So what could be done to avoid this from happening? I tried everything I can think of so far, but still haven't achieved a good solution for that.


xlsior(Posted 2010) [#20]
If you work from the center outwards, then the last place to put down a tile will always be accessible.

(Of course you can still match different levels, as long as you have a small island on the bottom layer you can start layering on top of those too)


SLotman(Posted 2010) [#21]
I finally solved it. It has nothing to do with placing tiles from center, from borders... in many, many layouts this will always lead to a dead end.

The solution is to actually place all tiles on board (as empty), and then solve it forward, removing tiles by pairs, and keeping a list of removed tiles.

Then it's just a matter of resetting the board, and assigning random "images" to the tiles on the list... it creates one hell of a hard game, and seems to be always solvable for the little tests I've done so far :)


TaskMaster(Posted 2010) [#22]
So, you just play the game, grabbing free tiles at random, keeping a list of the pairs you grab, then put image pairs on the tiles?!?!

That is genius. Probably really fast too.


xlsior(Posted 2010) [#23]
...what would be the benefit of that over automating the lay-down?

It's much more work to generate new boards your way, plus you end up with the exact same end result: as long as you have multiples of some of the tilepairs, you can still end up with a deadlock if the player doesn't match the exact same combination pairs that you did in your trial game.

Just seems like adding more work with no pay-off. :-?


TaskMaster(Posted 2010) [#24]
The player causing a deadlock is OK, in fact it is almost always possible. He just want there to be a possible solution.

I don't see how you think that his solution is generating more work. If there are 144 tiles, he just has to pick 72 pairs, and put them in a list, then assign tile faces to them. Pretty easy in my opinion. PC should be able to do it in a split second.


Chalky(Posted 2010) [#25]
All the versions of the game I have played had 4 of each tile rather than 2. Would that help your situation or make it even more difficult to generate valid levels? If nothing else it would mean you only had to design half the number of tile images... :o)


SLotman(Posted 2010) [#26]
My method is pretty fast, and unlike the "pick a pair, place it from the center/border", it works for every layout I tested so far (30+), as long as it obey to the basic requirement of mahjong solitaire games: having an even number of places for tiles.

You can even use less than 144 tiles, and it will work pretty good.

And it's already placing the tiles pair by pair. I previously sort the images by pairs too, so it's dead simple to generate a board.


TaskMaster(Posted 2010) [#27]
It doesn't matter how many tiles are in a set. Treat them as pairs, not sets of 4 and placing them is not an issue.