這篇文章給大家分享的是有關(guān)JS+canvas如何實(shí)現(xiàn)的五子棋游戲的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供習(xí)水網(wǎng)站建設(shè)、習(xí)水做網(wǎng)站、習(xí)水網(wǎng)站設(shè)計(jì)、習(xí)水網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、習(xí)水企業(yè)網(wǎng)站模板建站服務(wù),10年習(xí)水做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
運(yùn)行效果圖:
html代碼如下:
五子棋 重新開始
style.css代碼如下:
canvas{ display: block; margin: 50px auto; box-shadow: -2px -2px 2px #efefef, 5px 5px 5px #b9b9b9; } .restart{ text-align: center; } .restart>span{ display: inline-block; padding: 10px 20px; color: #fff; background-color: #45c01a; border-radius: 5px; }
script.js代碼如下:
var over = false; var me = true; //我 var chressBord = [];//棋盤 for(var i = 0; i < 15; i++){ chressBord[i] = []; for(var j = 0; j < 15; j++){ chressBord[i][j] = 0; } } //贏法的統(tǒng)計(jì)數(shù)組 var myWin = []; var computerWin = []; //贏法數(shù)組 var wins = []; for(var i = 0; i < 15; i++){ wins[i] = []; for(var j = 0; j < 15; j++){ wins[i][j] = []; } } var count = 0; //贏法總數(shù) //橫線贏法 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++; } } //豎線贏法 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++; } } //正斜線贏法 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++; } } //反斜線贏法 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++; } } for(var i = 0; i < count; i++){ myWin[i] = 0; computerWin[i] = 0; } var chess = document.getElementById("chess"); var context = chess.getContext('2d'); context.strokeStyle = '#bfbfbf'; //邊框顏色 var logo = new Image(); logo.src = 'img/logo.png'; logo.onload = function(){ context.drawImage(logo,0,0,450,450); drawChessBoard(); } document.getElementById("restart").onclick = function(){ window.location.reload(); } chess.onclick = function(e){ if(over){ return; } if(!me){ return; } var x = e.offsetX; var y = e.offsetY; var i = Math.floor(x / 30); var j = Math.floor(y / 30); if(chressBord[i][j] == 0){ oneStep(i,j,me); chressBord[i][j] = 1;//我 for(var k = 0; k < count; k++){ if(wins[i][j][k]){ myWin[k]++; computerWin[k] = 6;//這個(gè)位置對(duì)方不可能贏了 if(myWin[k] == 5){ window.alert('你贏了'); over = true; } } } if(!over){ me = !me; computerAI(); } } } //計(jì)算機(jī)下棋 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; } } } } } oneStep(u,v,false); chressBord[u][v] = 2; for(var k = 0; k < count; k++){ if(wins[u][v][k]){ computerWin[k]++; myWin[k] = 6;//這個(gè)位置對(duì)方不可能贏了 if(computerWin[k] == 5){ window.alert('計(jì)算機(jī)贏了'); over = true; } } } if(!over){ me = !me; } } //繪畫棋盤 var drawChessBoard = function(){ for(var i = 0; i < 15; i++){ context.moveTo(15 + i * 30 , 15); context.lineTo(15 + i * 30 , 435); context.stroke(); context.moveTo(15 , 15 + i * 30); context.lineTo(435 , 15 + i * 30); context.stroke(); } } //畫旗子 var oneStep = function(i,j,me){ context.beginPath(); context.arc(15 + i * 30, 15 + j * 30, 13, 0, 2 * Math.PI);//畫圓 context.closePath(); //漸變 var gradient = context.createRadialGradient(15 + i * 30 + 2, 15 + j * 30 - 2, 13, 15 + i * 30 + 2, 15 + j * 30 - 2, 0); if(me){ gradient.addColorStop(0,'#0a0a0a'); gradient.addColorStop(1,'#636766'); }else{ gradient.addColorStop(0,'#d1d1d1'); gradient.addColorStop(1,'#f9f9f9'); } context.fillStyle = gradient; context.fill(); }
1、js屬于一種解釋性腳本語言;2、在絕大多數(shù)瀏覽器的支持下,js可以在多種平臺(tái)下運(yùn)行,擁有著跨平臺(tái)特性;3、js屬于一種弱類型腳本語言,對(duì)使用的數(shù)據(jù)類型未做出嚴(yán)格的要求,能夠進(jìn)行類型轉(zhuǎn)換,簡(jiǎn)單又容易上手;4、js語言安全性高,只能通過瀏覽器實(shí)現(xiàn)信息瀏覽或動(dòng)態(tài)交互,從而有效地防止數(shù)據(jù)的丟失;5、基于對(duì)象的腳本語言,js不僅可以創(chuàng)建對(duì)象,也能使用現(xiàn)有的對(duì)象。
感謝各位的閱讀!關(guān)于“JS+canvas如何實(shí)現(xiàn)的五子棋游戲”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!