{"id":22455,"date":"2014-03-09T17:33:43","date_gmt":"2014-03-09T22:33:43","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=22455"},"modified":"2014-03-09T21:27:02","modified_gmt":"2014-03-10T02:27:02","slug":"experimenting-with-threes","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=22455","title":{"rendered":"Experimenting with Threes!"},"content":{"rendered":"<p>NOTE: This post is a little less thought-out than my usual programming posts. This was written pretty much on the fly as I was experimenting with stuff and not after I&#8217;d reflected on it. I&#8217;m not even sure it will make sense. Give it a try.<\/p>\n<p>Today we&#8217;re going to be talking about Threes!, an iOS game. I haven&#8217;t played that version, but I&#8217;ve played <a href=\"http:\/\/threesjs.com\/\">this web-based clone<\/a>. For the purposes of this discussion, you should probably go play the game, get addicted for a few days (everyone does) and then come back here once the mania passes. It will be easier to follow the discussion that way. <\/p>\n<p>After doing that, you might want to read this article from Touch Arcade that talks about how <a href=\"http:\/\/toucharcade.com\/2014\/02\/20\/mame-developer-builds-threes-ai\/\">someone who wrote an AI to play the game<\/a>, which revealed some interesting things about the mechanics.<\/p>\n<p>If you don&#8217;t have that kind of time, then here&#8217;s a basic run-down of the gameplay:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/threes.jpg' class='insetimage'   alt='threes.jpg' title='threes.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>You play using the arrow keys. Tiles will attempt to move in the given direction. If a blue slides into a red (or vice-versa) they merge to form a 3. From there it follows a simple pattern of matching like with like. 3+3=6. 6+6=12. 12+12=24. 24+24=48. And so on. The trick is that every time you move, a new tile is added to the board. If I shift the pieces up, then a new tile slides in on the bottom row. The game ends when the board fills up such that no more moves are possible.<\/p>\n<p>So your apparent objective is to keep merging tiles to make ever-larger numbers. But the actual challenge is to simply merge tiles faster than they appear to keep the board from filling in. If you play a couple of times, you&#8217;ll probably get a score of a few hundred. <\/p>\n<p>You normally expect your scores to go up as you play a game. Over time, your skill improves and you&#8217;re able to do better. Except, that&#8217;s not quite how things went for me. Sure, I repeatedly broke my high score, eventually playing a game all the way to about 7,500<span class='snote' title='1'>It&#8217;s been reported that scores in excess of 21,000 are possible.<\/span>. But mixed in there were still a lot of 150-point games. When that kind of thing happened I always assumed that I had stopped paying attention. But this kept happening, no matter how hard I &#8220;tried&#8221;. Some games dead-ended early and some went a long way, and my results didn&#8217;t seem to line up with how much effort I was putting in.<\/p>\n<p>This makes me think that the game has a huge element of luck. I wanted to play around with this idea, so I decided to make my own version of the game so I could explore the mechanics.<\/p>\n<p><!--more--><br \/>\n<table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/twon7.jpg' class='insetimage' width='600' alt='To start with, we just re-create the basic game.' title='To start with, we just re-create the basic game.'\/><\/td><\/tr><\/table><\/p>\n<p>(All of this is written in C++ using old-school OpenGL. It&#8217;s overkill for an afternoon project like this, but I&#8217;ve already got the boilerplate code handy and using that is faster than learning Python or whatever you kids use for your prototyping work these days.)<\/p>\n<p>First off, this business with the red and blue tiles is kind of suspect. The player needs even numbers of red and blue tiles in order to combine them. So if I get four red tiles in succession they will eat up a quarter of my play area and I&#8217;ll have no way to get rid of them<span class='snote' title='2'>According to the Touch Arcade article, the RNG compensates for this, but it takes several moves for the needed pieces to show up. Enough time to kill a game.<\/span>. That can doom a game through no fault of the user. Having four more of one color than the other is rare<span class='snote' title='3'>For varying definitions of &#8220;rare&#8221;.<\/span> at any particular moment. But in the course of a game that lasts 100 or so moves it starts to become likely. Actually, it&#8217;s worse than that. It&#8217;s &#8220;likely&#8221; in the sense that it will happen in some games and not others. I suspect that my long-running games are ones where this sort of thing &#8211; against the odds &#8211; <em>didn&#8217;t<\/em> happen, thus letting me squeak through those tough points in the game where you&#8217;ve got a lot of high-value pieces on the board that aren&#8217;t quite ready to combine. <\/p>\n<p>If this were done the other way with a series of direct combinations, then this randomness would be mitigated. If 1+1=2 and (humor me here) 2+2=3, then there wouldn&#8217;t be any combination of four low-value tiles it could throw at the player that would be mutually inert. <em>Something<\/em> would be able to combine.<\/p>\n<p>It&#8217;s entirely possible the original designer had a good reason for setting things up this way, but I don&#8217;t know what it was. Maybe the concern was that it would be too easy to &#8220;solve&#8221; the game without this randomness. Maybe everyone would end up with about the same score without it. (If this is true, then it means this is a game of luck where you use skill to reach as much of your determined-by-luck potential as possible. That&#8217;s not bad or anything. Lots of games work that way.)<\/p>\n<p>I don&#8217;t know. But I&#8217;m going to build an alternate rule set for my version.  In my rules, I&#8217;m going to use direct progression using powers of two. I know my powers of two (and more importantly, my square roots) a lot better than I know all these multiples of three, which will make it easier to wrap my head around the game. So 1+1=2, 2+2=4, 4+4=8, 8+8=16, and so on. <\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/twon2.jpg' class='insetimage' width='600' alt='I&#8217;ll explain the information on the left a little later on. Let&#8217;s just get the basics down first.' title='I&#8217;ll explain the information on the left a little later on. Let&#8217;s just get the basics down first.'\/><\/td><\/tr><tr><td class='insetcaption'>I&#8217;ll explain the information on the left a little later on. Let&#8217;s just get the basics down first.<\/td><\/tr><\/table><\/p>\n<p>Basing things on powers of two avoids the obviously ridiculous business of having 2+2=3. It also lets us use a cool shorthand for high-value tiles. 1,024 can be 1k and 1,048,576 can be 1M. (kilobyte and megabyte, respectively. It&#8217;s educational!)<\/p>\n<p>So now I&#8217;m going to build an AI to play the game for me.  It&#8217;s not very bright. It only looks at the next move and doesn&#8217;t attempt to plan several moves in advance. It just attempts to keep the board as clear as possible. Barring that, it will try to move combine-able tiles into place next to each other. For scoring, we don&#8217;t don&#8217;t actually care about &#8220;points&#8221;. We&#8217;re just interested in how long a game lasts. <\/p>\n<p>So, I&#8217;ll have my AI play a round of 32 games. First it will play according to the original rules where the first two tiles must combine to make the third. Then I&#8217;ll do another run where the first tile combines with itself to make the second, and the second combines with itself to make the third, etc.<\/p>\n<p>The results? Kind of a surprise:<\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/twon1.jpg' class='insetimage' width='600' alt='A run of 32 games, as played by the same AI, using the same pseudo-random sequence, under the two rule sets.' title='A run of 32 games, as played by the same AI, using the same pseudo-random sequence, under the two rule sets.'\/><\/td><\/tr><\/table><\/p>\n<p>This is a run of 32 games, as played by the same AI, using the same pseudo-random sequence, under the two rule sets. The red line represents the game according to my rules. The blue one is the original rules. The higher the line, the longer the games. So in the very first game the AI &#8211; playing under the original rules &#8211; lost the game just before turn 100.  Then playing the exact same game under my rules, the AI ended somewhere past 300 turns.<\/p>\n<p>You can see my rules are quite a bit easier. (The games are longer overall.) But what I didn&#8217;t expect is that both rule sets are still incredibly random. My rules allow the AI to score anywhere from 150 to 525. The original rules have games that run from 50 to about 225 or so. Which one is &#8220;more random&#8221;? Original rules have a lower delta between the top and bottom of the range, although the delta is a larger portion of the average. Roughly: <\/p>\n<ol>\n<li>The best Original-rule games scored five times higher than the worst ones, while the top Shamus-rule games only scored about three times higher than the worst.\n<p>HOWEVER:<\/p>\n<li>The best Original-rule games were about 175 higher than the worst, and the best Shamus-rule games were ~375 higher.\n<\/ol>\n<p>I think I&#8217;d need the help of statistics nerds to explore this further. There are a lot of ways to look at this data. The point is, I don&#8217;t know which one of these counts as &#8220;less random&#8221; in the totally subjective sense of feeling more fair to the player. <\/p>\n<p>Now let&#8217;s see what happens when we make the play area larger. We&#8217;ll do the same run, comparing Original and Shamus games on a 5&#215;5 grid instead of a 4&#215;4.<\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/twon4.jpg' class='insetimage' width='600' alt='A game I played myself. (No AI.) Here we&#8217;re pretty close to the end.' title='A game I played myself. (No AI.) Here we&#8217;re pretty close to the end.'\/><\/td><\/tr><tr><td class='insetcaption'>A game I played myself. (No AI.) Here we&#8217;re pretty close to the end.<\/td><\/tr><\/table><\/p>\n<p>In case you&#8217;re curious about the text: (Some of which is debugging info.)<\/p>\n<ol>\n<li>Score: The scoring system used by the original game is a little mysterious. For my program, I&#8217;m just adding up all the tiles currently in play.\n<li>Moves: The real measure of success, in terms of appraising your strategy.\n<li>Ruleset: Original or Shamus.\n<li>Highest: This is used when figuring out what the next tile will be. In my game, it halves the exponent of the highest piece on the board. So if your highest tile is 256, that&#8217;s 2<sup>8<\/sup>. Halving the exponent gives us 2<sup>4<\/sup>, which is 16.  So the &#8220;Next Tile&#8221; will give us 1, 2, 4, 8, or 16. Without this, games can take bloody ages before you start running out of room.\n<li>AI Rating: This is how much the AI &#8220;likes&#8221; this particular board layout. More empty space=better. More combine-able pieces next to each other=better. This is just for my own debugging purposes.\n<li>AI Movement: This number just tells me which direction[s] the AI can move.  Again, debugging.\n<li>Filled: What percent of the board is filled. The game begins with mostly empty space, but quickly rises to about 60% full. It then plateaus in the 60-70 range for the course of the game. Once you hit 85%+, you hit a tipping point where the lack of movement options leads to having even less options, and the game usually ends.\n<li>Playtime: This is how long the current game would take if played by a human that made a move every 1.5 seconds or so. This is important later.\n<\/ol>\n<p>So if we make the game area 5&#215;5, the outcomes look like this:<\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/twon3.jpg' class='insetimage' width='600' alt='A run of 32 games, as played by the same AI, using the same pseudo-random sequence, under the two rule sets, on a 5&#215;5 board.' title='A run of 32 games, as played by the same AI, using the same pseudo-random sequence, under the two rule sets, on a 5&#215;5 board.'\/><\/td><\/tr><\/table><\/p>\n<p>Okay, so let&#8217;s try it again on a 7&#215;7 board:<\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/twon5.jpg' class='insetimage' width='600' alt='A run of 32 games, as played by the same AI, using the same pseudo-random sequence, under the two rule sets, on a 7&#215;7 board.' title='A run of 32 games, as played by the same AI, using the same pseudo-random sequence, under the two rule sets, on a 7&#215;7 board.'\/><\/td><\/tr><\/table><\/p>\n<p>For the record, a game of 11,000 moves or so would take you right around 5 hours, assuming you averaged a second and a half per move. (It takes my AI about 4 seconds to play through the same game.) <\/p>\n<p>Well, it looks like I was wrong. My rule set is somehow more random, not less. I don&#8217;t know how. Maybe I&#8217;ve got a bug or design flaw in my AI that&#8217;s keeping it from performing properly. In the end, this was less illuminating than I&#8217;d hoped. <\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/twon6.jpg' class='insetimage' width='600' alt='An AI game in progress. Note that the &#8220;playtime&#8221; is how long a human would take. The AI had only been playing for about two minutes at this point.' title='An AI game in progress. Note that the &#8220;playtime&#8221; is how long a human would take. The AI had only been playing for about two minutes at this point.'\/><\/td><\/tr><tr><td class='insetcaption'>An AI game in progress. Note that the &#8220;playtime&#8221; is how long a human would take. The AI had only been playing for about two minutes at this point.<\/td><\/tr><\/table><\/p>\n<p>Still, we did learn a few interesting things:<\/p>\n<ol>\n<li>As you might expect, making the board larger adds dramatically to the length of the game. A 4&#215;4 takes a few minutes. A 6&#215;6 takes about half an hour. An 8&#215;8 takes about 5 hours. 10&#215;10 is a couple of days. 12&#215;12 is about ten days. (Again: This is assuming non-stop rapid-fire movements.)\n<li>For anything larger than 5&#215;5, I think the game needs a little something else. Some special pieces or a powerup or something.\n<li>On larger boards, a lot of the interesting activity happens in the very last stages of the game. It&#8217;s kind of like starting a game of Tetris at level zero. You&#8217;ve got half an hour of really boring play. Then three minutes of of challenge, then a minute of sheer chaos where it all falls apart. But unlike Tetris, we can&#8217;t just &#8220;start&#8221; the player near the endgame, because how they fare in the endgame is a measure of how careful and disciplined they have been at managing the board during the &#8220;boring&#8221; parts. This probably means the ideal board size is 6&#215;6 or less. Anything larger, and it just takes too dang long before you can see the results of your efforts.\n<li>There are a lot of interesting things you can do with the &#8220;next tile&#8221; logic. You could have it only give you 1&#8217;s and 2&#8217;s, which would make the game stupidly long and boring. But maybe my approach is too conservative. Maybe instead of 2<sup>n\/2<\/sup>, it would be more interesting to use 2<sup>n-2<\/sup>, or just 2<sup>n<\/sup>. The latter would mean that once you get a 256, then it will start randomly giving you 256&#8217;s. That would make the difficulty ramp up quickly. It might also make the game more random.\n<p>Then again, this post proves I&#8217;m probably bad at intuiting how &#8220;random&#8221; a system is.\n<\/ol>\n<p>I offer this post as an example of why the constant &#8220;cloning&#8221; of mobile games isn&#8217;t necessarily a bad thing. Threes! is a dead-simple game, but here I&#8217;ve stumbled on several interesting variants of number-combining that are thus far left totally unexplored. I&#8217;m sure there are other variations you could play with. You could have a half dozen of these games on iOS and each one of them would be unique and worthwhile. Or someone could put out a mindless re-skin with identical mechanics. A lot of it depends on who is making the game and why they&#8217;re doing it. <\/p>\n<p>It goes back to the &#8220;We make games to make money&#8221; vs. &#8220;We make money to make games&#8221; problem. If all you want is money and you don&#8217;t care about games, then you&#8217;ll look at what&#8217;s selling and do a straight-up clone. If you love thinking about and exploring mechanics then you would probably find direct cloning to be tedious and boring. You&#8217;ll be driven to make something different &#8211; something you want to play that doesn&#8217;t already exist &#8211; and you&#8217;ll put it up for sale as a way of getting paid for your efforts. <\/p>\n<p>In any case, this is a gem of a game. Lots of neat stuff to think about. Do <a href=\"http:\/\/threesjs.com\/\">give it a try<\/a> if you haven&#8217;t already.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NOTE: This post is a little less thought-out than my usual programming posts. This was written pretty much on the fly as I was experimenting with stuff and not after I&#8217;d reflected on it. I&#8217;m not even sure it will make sense. Give it a try. Today we&#8217;re going to be talking about Threes!, an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[343],"class_list":["post-22455","post","type-post","status-publish","format-standard","hentry","category-programming","tag-threes"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/22455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=22455"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/22455\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=22455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=22455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=22455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}