JavaScript實(shí)現(xiàn)五子棋小游戲
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)五子棋小游戲的具體代碼,供大家參考,具體內(nèi)容如下
HTML部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>五子棋</title>
<style>
* {
padding: 0;
margin: 0;
}
body{
padding-top: 100px;
}
.main {
width: 600px;
height: 600px;
margin: 0 auto;
background-color: burlywood;
}
.col {
position: relative;
width: 40px;
height: 40px;
box-sizing: border-box;
border: 1px solid #000;
border-collapse: collapse;
/*border-radius: 20px;*/
}
.row {
position: relative;
display: flex;
height: 40px;
/*background-color: brown;*/
}
.col-action {
background-color: blue;
}
.col-actionA {
/*background-color: white;*/
}
.col-actionB {
/*background-color: black;*/
}
.col-actionA::before{
content: "";
position: absolute;
width: 30px;
height: 30px;
background-color: white;
border-radius: 99px;
top:4.5px;
left:4.5px;
box-shadow: 0 0 2px rgba(0,0,0,0.5);
}
.col-actionB::before{
content: "";
width: 30px;
height: 30px;
background-color: black;
border-radius: 99px;
position: absolute;
top:4.5px;
left:4.5px;
box-shadow: 0 0 2px rgba(128,128,128,0.5);
}
.hq{
width: 600px;
height: 600px;
margin: 0 auto;
}
</style>
<script type="text/javascript" src="js/demo03.js" ></script>
</head>
<body>
<div class="main">
<div class="qipan" id="qipan">
</div>
<div class="hq"><button id="hq">悔棋</button></div>
</div>
</body>
</html>
JavaSrcipt
window.onload = function(){
var busz = new Array();
//div單擊事件
var ansj = function () {
const x = this.getAttribute("col");
const y = this.getAttribute("row");
// console.log(x, y, nowPlayer)
if (nowPlayer) {
qjck[this.getAttribute("row")][this.getAttribute("col")] = 1;
this.classList.add("col-actionA");
nowPlayer = !nowPlayer;
} else {
qjck[this.getAttribute("row")][this.getAttribute("col")] = 2;
this.classList.add("col-actionB");
nowPlayer = !nowPlayer;
}
busz.push(this);
var js = pdsl(y,x);
if(js)
{
setTimeout(function(){
alert("游戲結(jié)束");
location.reload(); //刷新瀏覽器
},50);
}
this.onclick = null;
}
//判斷是否結(jié)束
var pdsl = function(x,y){
var sx=1,zy=1,zs=1,ys=1,t=1;
//上
for(t=1;t<=5;t++){
if(x-t < 0)
break;
console.log("上"+zy);
if(qjck[x-t][y]==qjck[x][y] && qjck[x-t][y]!=0)
sx++;
else
break;
}
//下
for(t=1;t<=5;t++){
if(Number(x)+t >= 10)
break;
console.log("下"+zy);
if(qjck[Number(x)+t][y]==qjck[Number(x)][y] && qjck[Number(x)+t][y]!=0)
sx++;
else
break;
}
//左
for(t=1;t<=5;t++){
if(y-t < 0)
break;
console.log("左"+zy);
if(qjck[x][y-t]==qjck[x][y] && qjck[x][y-t]!=0)
zy++;
else
break;
}
//右
for(t=1;t<=5;t++){
if(Number(y)+t >= 10)
break;
console.log("右"+zy);
if(qjck[x][Number(y)+t]==qjck[x][y] && qjck[x][Number(y)+t]!=0)
zy++;
else
break;
}
//上左
for(t=1;t<=5;t++){
if(x-t < 0)
break;
console.log("上左"+zy);
if(qjck[x-t][y-t]==qjck[x][y] && qjck[x-t][y-t]!=0)
zs++;
else
break;
}
//下右
for(t=1;t<=5;t++){
if(Number(x)+t >= 10 || Number(y)+t >= 10)
break;
console.log("下右"+zy);
if(qjck[Number(x)+t][Number(y)+t]==qjck[x][y] && qjck[Number(x)+t][Number(y)+t]!=0)
zs++;
else
break;
}
//上右
for(t=1;t<=5;t++){
if(x-t < 0 || Number(y)+t >= 10)
break;
console.log("上右"+zy);
if(qjck[x-t][Number(y)+t]==qjck[x][y] && qjck[x-t][Number(y)+t]!=0)
ys++;
else
break;
}
//下左
for(t=1;t<=5;t++){
if(Number(x)+t >= 10 || y-t < 0)
break;
console.log("下右"+zy);
if(qjck[Number(x)+t][y-t]==qjck[x][y] && qjck[Number(x)+t][y-t]!=0)
ys++;
else
break;
}
console.log(sx + " " + zy + " " + zs + " " + ys);
if(sx == 5 || zy==5 || zs==5 || ys==5)
return true;
else
return false;
}
var nowPlayer = 0;
//棋盤數(shù)組
var qjck = Array();
//div
var piece = document.createElement("div");
piece.id = "piece";
//得到div
var qipan = document.getElementById("qipan");
//生成棋盤
for (let r = 0; r < 15; r++) {
let newrow = document.createElement("div");
newrow.id = "row" + r;
newrow.classList.add("row")
let arrCol = Array()
qjck.push(arrCol)
for (let c = 0; c < 15; c++) {
arrCol.push(0)
let newcol = document.createElement("div");
newcol.id = "col" + c;
newcol.classList.add("col");
newcol.setAttribute("row", r);
newcol.setAttribute("col", c)
newrow.appendChild(newcol)
newcol.onclick = ansj;
}
// console.log(newrow)
qipan.appendChild(newrow)
}
//悔棋
var hq = document.getElementById("hq");
hq.onclick = function(){
if(busz.length <= 0)
return;
var divt = busz.pop();
divt.onclick = ansj;
divt.classList.remove("col-actionA");
divt.classList.remove("col-actionB");
qjck[divt.getAttribute("row")][divt.getAttribute("col")] = 0;
nowPlayer = !nowPlayer;
// console.log(qjck);
console.log(divt);
}
}
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
實(shí)現(xiàn)兩個(gè)文本框同時(shí)輸入的實(shí)例
下面小編就為大家?guī)?lái)一篇實(shí)現(xiàn)兩個(gè)文本框同時(shí)輸入的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
基于javascript實(shí)現(xiàn)根據(jù)身份證號(hào)碼識(shí)別性別和年齡
這篇文章主要介紹了基于javascript實(shí)現(xiàn)根據(jù)身份證號(hào)碼識(shí)別性別和年齡的相關(guān)資料,需要的朋友可以參考下2016-01-01
JavaScript程序設(shè)計(jì)之JS調(diào)試
這篇文章主要介紹了JavaScript程序設(shè)計(jì)中的重要環(huán)節(jié):JS調(diào)試,本文通過(guò)一個(gè)加法器,介紹JS如何調(diào)試,感興趣的小伙伴們可以參考一下2015-12-12
一文帶你搞懂JavaScript中的進(jìn)制與進(jìn)制轉(zhuǎn)換
JavaScript 中提供的進(jìn)制表示方法有四種:十進(jìn)制、二進(jìn)制、十六進(jìn)制、八進(jìn)制。本文主要講介紹一下JS中這些進(jìn)制的互相轉(zhuǎn)換,感興趣的可以了解一下2023-02-02
在JavaScript中實(shí)現(xiàn)類的方式探討
在 javascript 中有很多方式來(lái)創(chuàng)建對(duì)象,所以創(chuàng)建對(duì)象的方式使用起來(lái)非常靈活,到底哪一種方式是最恰當(dāng)呢?下面為大家講講2013-08-08
JS+CSS實(shí)現(xiàn)滾動(dòng)數(shù)字時(shí)鐘效果
本篇文章教給大家用JS代碼配合CSS樣式來(lái)實(shí)現(xiàn)滾動(dòng)時(shí)鐘的動(dòng)畫(huà)效果,一起來(lái)學(xué)習(xí)下。2017-12-12
js 實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)示例解析
這篇文章主要為大家介紹了js實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Fundebug支持監(jiān)控微信小程序HTTP請(qǐng)求錯(cuò)誤的方法
這篇文章主要介紹了Fundebug支持監(jiān)控微信小程序HTTP請(qǐng)求錯(cuò)誤的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
找出字符串中出現(xiàn)次數(shù)最多的字母和出現(xiàn)次數(shù)精簡(jiǎn)版
找出字符串中出現(xiàn)次數(shù)最多的字母和出現(xiàn)次數(shù)精簡(jiǎn)版,有需求的朋友可以參考下2012-11-11

