"Using electron to make Tetris game" follow-up article, smart Program play Tetris game.
Background
Not long ago with ES6 completed the basic Tetris game, today has completed a preliminary intelligent algorithm, you can automatically play Tetris, so that their ideas towards the realization of a step closer.
First run, eliminate 1398 lines, Steal Hi!
Program Structure
The main concern of intelligent algorithm, structure simplification, all put in the index.js.
Using Timers to drive games
function autoPlayClick(){ isAutoPlay = document.getElementById(‘autoPlay‘).checked; if(isAutoPlay){ clearInterval(interval) interval = setInterval( autoTick, 1 ); //自动算法入口 }else{ clearInterval(interval) interval = setInterval( tick, TICKVAL ); //自动下落入口 } document.getElementById(‘speedShow‘).focus();}
Variable definition
Analog manual operation, a block in three steps: rotation, left or right to move, fall to the end.
Const MOVEOPS = [ ' MoveLeft ', ' MoveRight '] //left, Right move definition var oplist = [], Besteva; //pending operation queue class Opration{//Operation details Constructor (op,num) {this.op = op; //operation method this.num = num; //Operations}} class evaluation{//situation evaluation function result definition Constructor (R,x,eva) {THIS.R = r; //number of rotations this.x = x; //block horizontal position this.eva = Eva; //Evaluation Result}}
Intelligent algorithm Scheduling
function autoTick(){ if(opList.length == 0){ //上个方块已经处理完毕 getStrategy(); //策略算法,生成下一个方块的操作方法策略序列 }else{ let op = opList.shift(); //取操作方法 for(let i=0; i<op.num; i++){ //执行既定策略 tetris[op.op](); if(op.op == ‘moveDown‘) //是下落操作,取下一块方块 generateNext(); } }}
Strategy algorithm
This is the core of the algorithm and determines how each block is operated.
functionGetstrategy(){Let Max =0;Optimal evaluation value tetris.erase ();Let TMP =New Tetris (Tetris.shape,tetris.ctx,tetris.x,tetris.y,' RGB (1,1,1,1) ',' RGB (111,111,111) ')Generate a block for testingForLet i =0; I <4; i++) {Keep the test block consistent with the real block, as each of my blocks is generated with a random number of rotationsForLet j =0; J <4; J + +) {Tmp.data[i][j] = Tetris.data[i][j];}}ForLet R =0; R <4; r++) {Each block, rotated four times, respectively, to evaluate the situation tmp.erase (); tmp.x = tetris.x; TMP.Y = Tetris.y;if (R >0) tmp.rotate ();while (Tmp.moveleft ());From the leftmost test to the rightwhile (Tmp.movedown ());Fall to the endwhile (Rightover (TMP)) {To the right to end the assessment of this patternLet score = Evaluate (TMP);Situation Assessmentif (Score > Max) {Save optimal results max = score; Besteva =New Evaluation (R,tmp.x,max)}if (!tmp.moveright ()) {Right Shift failedif (!tmp.moveup ()) {Move up, bypass the barrier max =1000;The move fails, the description fills the void, and the block puts this Besteva =New Evaluation (R,tmp.x,max)Break } }else{while (Tmp.movedown ());After the right move succeeds, fall to the end}}Let score = Evaluate (TMP);Last positionif (Score > Max) {max = score; Besteva =new Evaluation (R,tmp.x,max)}} Tmp.erase (); //Console.log (max) Oplist.push (new opration (' rotate ', BESTEVA.R)); //Rotary operation let moveact = besteva.x-tetris.x > 0? 1: 0; ///Horizontal position difference translates to left or right shift operation let actnum = math.abs (besteva.x-tetris.x) Oplist.push (new Opration (moveops[ Moveact],actnum)); //left or right shift operation Oplist.push (new Opration (' MoveDown ',1)); //Drop operation}
evaluation function
Now only a few basic parameter evaluations have been done and need to be optimized. A more in-depth approach is to add machine learning algorithms for self-feedback learning.
functionEvaluate(t) {let ct = t.y;The bigger the debug, the better.ForLet i =0; I <4; i++) {See the four neighbors of each small squareForLet j =0; J <4; J + +) {if (T.data[i][j]) {if (T.cansee (T.x +i, T.y + j +1))Below is the hollow ct-=5;ForLet k=0; k<4; k++) {Switch (k) {Case0:ct + = T.cansee (t.x + i +1, T.y + j)?0:1;Rightbreak; case 1:ct + = T.cansee (t.x + i-1, T.y + j)? 0: 1; //left break; case 2:ct + = T.cansee (T.x + i, T.y + j + 1)? 0: 1; //break; case 3:ct + = T.cansee (T.x + i, T.y + J- 1)? 0: 1; //break;} }}} return ct;}
Source:
clone https://git.oschina.net/zhoutk/Tetris.git或者:git clone https://github.com/zhoutk/Tetris
Summary
Opened my intelligent algorithm learning path, this is just one of the simplest automatic program, are not any intelligence, but for me is a new direction of the beginning, refueling!
Tetris Game Automaton