This article shares the specific code of JS Canvas to implement Gozi Chess game for your reference. The specific content is as follows
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Gozi Chess</title> <style type='text/css'> canvas { display: block; margin: 50px auto; box-shadow: -2px -2px 2px #efefef, 5px 5px 5px #b9b9b9; cursor: pointer; } .btn-wrap { display: flex; flex-direction: row; justify-content:center; } .btn-wrap div { margin: 0 10px; } div>span { display: inline-block; padding: 10px 20px; color: #fff; background-color: #EE82EE; border-radius: 5px; cursor: pointer; } span { background: #D6D6D4; color: #adacaa; } #result-wrap {text-align: center;} </style> </head> <body> <h3 >--益智Gozi Chess--</h3> <canvas width="450px" height="450px"></canvas> <div class="btn-wrap"> <div id='restart' class="restart"> <span>restart</span> </div> <div id='goback' class="goback unable"> <span>Regret chess</span> </div> <div id='return' class="return unable"> <span>撤销Regret chess</span> </div> </div> <script type="text/javascript" charset="utf-8"> var over = false; var me = true; //I var _nowi = 0, _nowj = 0; //Record the coordinates of your own chess var _compi = 0, _compj = 0; //Record the current coordinates of the computer chess var _myWin = [], _compWin = []; //Record me, the computer wins var backAble = false, returnAble = false; var resultTxt = ('result-wrap'); var chressBord = [];//checkerboard for(var i = 0; i < 15; i++){ chressBord[i] = []; for(var j = 0; j < 15; j++){ chressBord[i][j] = 0; } } //The winning method statistics array var myWin = []; var computerWin = []; //Winning method array var wins = []; for(var i = 0; i < 15; i++){ wins[i] = []; for(var j = 0; j < 15; j++){ wins[i][j] = []; } } var count = 0; //Total number of winning methods //Horizontal line win method for(var i = 0; i < 15; i++){ for(var j = 0; j < 11; j++){ for(var k = 0; k < 5; k++){ wins[i][j+k][count] = true; } count++; } } //Permanent line win method for(var i = 0; i < 15; i++){ for(var j = 0; j < 11; j++){ for(var k = 0; k < 5; k++){ wins[j+k][i][count] = true; } count++; } } //Forward slash win method for(var i = 0; i < 11; i++){ for(var j = 0; j < 11; j++){ for(var k = 0; k < 5; k++){ wins[i+k][j+k][count] = true; } count++; } } //Backslash win method for(var i = 0; i < 11; i++){ for(var j = 14; j > 3; j--){ for(var k = 0; k < 5; k++){ wins[i+k][j-k][count] = true; } count++; } } // debugger; for(var i = 0; i < count; i++){ myWin[i] = 0; _myWin[i] = 0; computerWin[i] = 0; _compWin[i] = 0; } var chess = ("chess"); var context = ('2d'); = '#bfbfbf'; //Border color var backbtn = ("goback"); var returnbtn = ("return"); = function(){ drawChessBoard(); // Draw the chessboard } ("restart").onclick = function(){ (); } // I, play chess = function(e){ if(over){ return; } if(!me){ return; } // The chess function is available = ( new RegExp( "(\\s|^)unable(\\s|$)" )," " ); var x = ; var y = ; var i = (x / 30); var j = (y / 30); _nowi = i; _nowj = j; if(chressBord[i][j] == 0){ oneStep(i,j,me); chressBord[i][j] = 1; //I have taken the position for(var k = 0; k < count; k++){ // Add 1 to any possible winning situations if(wins[i][j][k]){ // debugger; myWin[k]++; _compWin[k] = computerWin[k]; computerWin[k] = 6;//The opponent cannot win at this position if(myWin[k] == 5){ // ('You win'); = 'Congratulations, you won! '; over = true; } } } if(!over){ me = !me; computerAI(); } } } // Regret chess = function(e){ if(!backAble) { return;} over = false; me = true; // = 'o(╯□╰)o, regret it in chess'; // Undo the regret chess function is available = ( new RegExp( "(\\s|^)unable(\\s|$)" )," " ); // I regret chess chressBord[_nowi][_nowj] = 0; //I, it has taken up a place to restore minusStep(_nowi, _nowj); //Destroy the chess pieces for(var k = 0; k < count; k++){ // Reduce all possible wins by 1 if(wins[_nowi][_nowj][k]){ myWin[k]--; computerWin[k] = _compWin[k];//The opponent may win in this position } } // Computer corresponding regret chess chressBord[_compi][_compj] = 0; //Computer, already occupied a location Restore minusStep(_compi, _compj); //Destroy the chess pieces for(var k = 0; k < count; k++){ // Reduce all possible wins by 1 if(wins[_compi][_compj][k]){ computerWin[k]--; myWin[k] = _myWin[i];//The opponent may win in this position } } = '--Puzhi Goji--'; returnAble = true; backAble = false; } // Undo the regret chess = function(e){ if(!returnAble) { return; } // I, cancel the regret chess chressBord[_nowi][_nowj] = 1; //I have taken the position oneStep(_nowi,_nowj,me); for(var k = 0; k < count; k++){ if(wins[_nowi][_nowj][k]){ myWin[k]++; _compWin[k] = computerWin[k]; computerWin[k] = 6;//The opponent cannot win at this position } if(myWin[k] == 5){ = 'Congratulations, you won! '; over = true; } } // The computer cancels the corresponding regret chess chressBord[_compi][_compj] = 2; //The computer has occupied a location oneStep(_compi,_compj,false); for(var k = 0; k < count; k++){ // Reduce all possible wins by 1 if(wins[_compi][_compj][k]){ computerWin[k]++; _myWin[k] = myWin[k]; myWin[k] = 6;//The opponent cannot win at this position } if(computerWin[k] == 5){ = 'o(╯□╰)o,The computer won,Keep working hard!'; over = true; } } += ' '+ 'unable'; returnAble = false; backAble = true; } // Computer chess var computerAI = function (){ var myScore = []; var computerScore = []; var max = 0; var u = 0, v = 0; for(var i = 0; i < 15; i++){ myScore[i] = []; computerScore[i] = []; for(var j = 0; j < 15; j++){ myScore[i][j] = 0; computerScore[i][j] = 0; } } for(var i = 0; i < 15; i++){ for(var j = 0; j < 15; j++){ if(chressBord[i][j] == 0){ for(var k = 0; k < count; k++){ if(wins[i][j][k]){ if(myWin[k] == 1){ myScore[i][j] += 200; }else if(myWin[k] == 2){ myScore[i][j] += 400; }else if(myWin[k] == 3){ myScore[i][j] += 2000; }else if(myWin[k] == 4){ myScore[i][j] += 10000; } if(computerWin[k] == 1){ computerScore[i][j] += 220; }else if(computerWin[k] == 2){ computerScore[i][j] += 420; }else if(computerWin[k] == 3){ computerScore[i][j] += 2100; }else if(computerWin[k] == 4){ computerScore[i][j] += 20000; } } } if(myScore[i][j] > max){ max = myScore[i][j]; u = i; v = j; }else if(myScore[i][j] == max){ if(computerScore[i][j] > computerScore[u][v]){ u = i; v = j; } } if(computerScore[i][j] > max){ max = computerScore[i][j]; u = i; v = j; }else if(computerScore[i][j] == max){ if(myScore[i][j] > myScore[u][v]){ u = i; v = j; } } } } } _compi = u; _compj = v; oneStep(u,v,false); chressBord[u][v] = 2; //The computer occupies the position for(var k = 0; k < count; k++){ if(wins[u][v][k]){ computerWin[k]++; _myWin[k] = myWin[k]; myWin[k] = 6;//The opponent cannot win at this position if(computerWin[k] == 5){ = 'o(╯□╰)o,The computer won,Keep working hard!'; over = true; } } } if(!over){ me = !me; } backAble = true; returnAble = false; var hasClass = new RegExp('unable').test(' ' + + ' '); if(!hasClass) { += ' ' + 'unable'; } } //Drawing chessboard var drawChessBoard = function() { for(var i = 0; i < 15; i++){ (15 + i * 30 , 15); (15 + i * 30 , 435); (); (15 , 15 + i * 30); (435 , 15 + i * 30); (); } } //Draw chess pieces var oneStep = function(i,j,me) { (); (15 + i * 30, 15 + j * 30, 13, 0, 2 * );// Draw a circle (); //Gradial var gradient = (15 + i * 30 + 2, 15 + j * 30 - 2, 13, 15 + i * 30 + 2, 15 + j * 30 - 2, 0); if(me){ (0,'#0a0a0a'); (1,'#636766'); }else{ (0,'#d1d1d1'); (1,'#f9f9f9'); } = gradient; (); } //Destroy the chess pieces var minusStep = function(i,j) { //Erase the circle ((i) * 30, (j) * 30, 30, 30); // Repaint the grid around the circle (); (15+i*30 , j*30); (15+i*30 , j*30 + 30); (i*30, j*30+15); ((i+1)*30 , j*30+15); (); } </script> </body> </html>
More interesting classic game implementation topics, share with you:
Summary of C++ classic games
Summary of classic python games
python Tetris game collection
JavaScript classic game
Summary of classic javascript games
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.