- Home /
Creating a mahjong game, problem with randomizing tiles and counting points.
Hello! I am creating a riichi mahjong game(japanese poker-style game) where point is to create a hand of four sets of three and a pair. I'm on a good start but I am having problem with randomizing tiles and calculating points. I've tried
var number = Random.Range
but don't really know what to put in as value. (Game has three types of main tiles (1 through 9 of balls, bamboos and characters) as well as dragons (red, green and white) and winds (south, north, east and west) four of each (111122223333444455556666777788889999 Red dragon x4 White dragon x4 Green dragon x4 EastWind x4 WestWind x4 NorthWind x4 SouthWind x4) which in total comes as 136 tiles.
var number = Random.Range(1,137)
doesn't seem to be what I am looking when pulling hands from the wall (tiles are shuffled and placed on 17 tiles wide walls that are 2 tiles high in front of every player and then depending on dealer are drawn from that four at time, but that's not the point.) Any pointers on how I should approach my dilemma?
Then as my next problem comes the scoring system. To win, person first needs at least winning point in his hand. There is set amount of ways to achieve this. (reference Wikipedia: Japanese Mahjong Yaku)
Now, every winning hand is valued differently depending on if the person who gets it is dealer, how many winning points he or she has etcetera. I am struggling insanely with getting program to understand different winning hands adding points together and subtracting them is not a problem, but the way I should handle them. Should I just create a variable for every single possible winning hand there is (which is insane) or is there any more logical way to handle this?
Sorry for messy explanation and I hope you understand what I am looking for with this. For reference I suggest just looking for wikipedia about how points are distributed and stuff if I did not manage to explain them clearly enough in this.
Also, sorry for messy English, not my native language!
Thanks in advance everyone!
Unity Answers is designed so that each question has one specific technical issue. I suggest you break your two issues into two question. In addition, this question is bordering on a discussion question (especially your second issue). UA is not a good format for discussion questions. Unity Forums is the place to post discussion questions.
Answer by Gnometech · Feb 12, 2014 at 09:05 PM
Ok, I give it a try. I have worked on a MahJongg game myself once (in Java, not with Unity), so I know the game and what you are trying to do.
First I would create an array of all possible tiles (length 136). This can then be shuffled. There are several ways to do this, googling will give you several types of shuffling algorithms. One possibility is to create a second array of the same length, fill it with random numbers of a large range (like between 1 and 1000000000) and sort the first array according to the ranom numbers from the second.
About the winning hands: The tricky part is to actually teach the game to recognize a winning hand. Some hands have more than one possible way, so this is harder as it sounds. The best way is to do this recursively. All open combinations can be ignored, they are fixed anyway, just count the number of them. The player needs 4 of these "pictures" (or whatever the term for a Chi, Pon or Kan is) and a twin. So analyze the hidden tiles of the hand, see if you find a picture and analyze the rest. If you manage to find 4 pictures this way (counting the open ones) and the remaining two are a couple => Winning hand (Mah Jongg). If you search recursively, you can do the trick.
The neat thing is that in this way you already split your hand into the pictures you need for calculating the points. There are some corner cases to consider, but with an elaborate data model you are on a good way. But I agree, the last part of the question is better suited for the forums.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133323im_/https://answers.unity.com/themes/thub/images/avi.jpg)