8 ms·
I was wondering how a quick random player might fare so I made a simple one. Paste this into your inspector console (or in chrome type "javascript:" into the UR
by codybits 11y ago
I was wondering how a quick random player might fare so I made a simple one. Paste this into your inspector console (or in chrome type "javascript:" into the URL bar and paste it):
function countPieces(){it=b.pieces();for(var e=0;it.current();)e++,it.next();return e}function getRandPiece(){for(var e=b.pieces(),t=Math.floor(Math.random()*countPieces()),n=0;t>n;n++)e.next();return e.current()}function stackRandPiece(){if(!b.isBusy()){if(b.isGameOver())return void clearInterval(randInterval);var e=getRandPiece();b.setTarget({x:e.pos.x+100,y:1500}),b.grab(e),window.setTimeout(function(){b.setTarget({x:200*Math.floor(7*Math.random())+100,y:1500}),window.setTimeout(function(){b.release()},15)},15)}}randInterval=window.setInterval(stackRandPiece,40);
Original code here: https://gist.github.com/CodyWalker/842149b82ed363659678 https://gist.github.com/CodyWalker/842149b82ed363659678
The highest I've seen it manage is 17.
- GolfyMcG 11y agoIf it wasn't so late I might get into it but I think this could be dramatically improved by just detecting whether a piece is currently underneath another piece. Assuming you can eliminate those as possible choices for the random select process, you would avoid the issue of iterating through a bunch of pieces that can't even be moved during the late game!
- codybits 11y agoYeah, that would probably help a lot. It is pretty dumb in its current state. That said, sometimes it can join matching tiles that are next to each other in the bottom of the pile, so that can still be useful. Maybe I'll work on it some later...
- sossles 11y agoThat's very cool. I never imagined I'd see a bot playing Twenty.
- bottled_poe 11y agoThis is pretty great - it scored 16 for me on the first go which is higher than my own first attempt of 14. This script also highlights my main issue with the game. It feels like the rules are not strict enough for this style of game. The real-time nature of the physics combined with the concurrent actions taken by the user results in quite loose game mechanics.
- beyti 11y agoLoved the game, installed for my phone immediately. That said, this script is way more fun though, loved it more, did 17 for me too.
- toast76 11y agoJust made some changes, wins consistently -> https://gist.github.com/alandownie/8e2fe28e9edf356ac966 https://gist.github.com/alandownie/8e2fe28e9edf356ac966 Basically same, but giving weight left<>right based on the value of the number. EDIT: Oooh... just discovered, 20 x 2 is a thing... go twentybot!
- PebblesHD 11y agothis is fascinating code, nicely done!
- ColinWright 11y agoMy background seems to be significantly different from most people here - what you've written there is something I'd like to learn about, but have no idea where to start. I've copied your code, hit "F12" in Firefox, but where do I paste it? Where/how does it run? How can I learn the basics of this stuff without wading through the mindless tutorials intended for people who have never coded? For reference, I've written safety-critical real-time embedded systems in assembler and C, and these days I write number-theory and expert systems code in Python. Where can I start that doesn't patronise me to death?
- teen 11y agoin the console window of the browser tools in chrome or firefox. https://developer.chrome.com/devtools#console https://developer.chrome.com/devtools#console note: javascript always runs in the context of a root object. in the browser, it's the window object. if the webpage has multiple frames, you might have to select the correct frame for the console to execute the js in.
- ColinWright 11y agoThank you - that's got me started - I'll see if I can get more time to explore this later today.
- pbhjpbhj 11y agoI'm a non-programmer [I've only made programs in BASIC, Fortran, C, C++, assembler, smalltalk, javascript, BASH, probably a couple I forgot (does Maple count), and most recently python (lol - I mean to say my level is 'interested amateur')] but I found this [1] quite a nice reintroduction to javascript the other day. It gives a good review of javascript syntax, goes over the intricacies of the loose typing system and variable scope, covers some detail on avoiding memory leaks and some browser specifics (which I think are probably out of date; I didn't review it thoroughly I was playing Minecraft at the time) and gets up to anonymous functions and closures. Aside: Incidentally if someone would like to explain closures to me I'd appreciate it - I don't really understand them. Code Academy's [2] javascript looks very basic but Code School [3] seems to have better beginner stuff - I've done their git/jQuery courses before and they were quite well done. HTH. [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re... [2] Avoid for javascript, very basic: http://www.codecademy.com/courses/getting-started-v2/0/1?curriculum_id=506324b3a7dffd00020bf661 http://www.codecademy.com/courses/getting-started-v2/0/1?cur... [3] https://www.codeschool.com/courses/javascript-best-practices https://www.codeschool.com/courses/javascript-best-practices
- indream 11y agoModified with sorting pieces by value: https://gist.github.com/inDream/c4c1eb78faf2742c5e58 https://gist.github.com/inDream/c4c1eb78faf2742c5e58 Highest score: 20x3
- ddmf 11y agoCan't. Stop. Watching.
- fiatjaf 11y agoDoes anyone else think this proves the game is bad?
- tjbiddle 11y agoIn what context? You can automate anything. Chess has had AI for years; does that make it bad?
- fiatjaf 11y agoYes, of course, in the sense that it is not the best game possible. But it is much less bad than this game. Look at what a Chess AI does and look at this AI.
- Lawtonfogle 11y agoIf this was able to achieve any arbitrary number score or even if it was able to consecutively hit scores beyond what a decent player could get, I would agree. But just because playing random works somewhat doesn't make it bad. I'm sure there are new players who would lose in Go to a computer that only placed pieces randomly in valid positions. But with a little skill, another player (or designed algorithm) should be able to almost always beat such the random algorithm.