




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第JavaScript实现网页五子棋小游戏本文实例为大家分享了JavaScript实现网页五子棋小游戏的具体代码,供大家参考,具体内容如下
设计思路如下:
1.先采用的Math.random()方法决定哪一方先行;
2.设置变量WFLAG=1,BFLAG=0时执白子,WFLAG=0,BFLAG=1时执黑子;
3.设置标志[I]中,当为白子时标志[I]=1,当为黑子时标志[I]=2,且标志[I]不为0时不能落子;
4.设置判断语句,当列5子时,竖5子,斜5子时为胜利,并计分;
5.重新开始-重新初始化变量。
采用的方法如下:
1.Math.random():随机产生0~1的随机数,含0但不包括1;
2.window.addEventListener(type,javascript语句,false):监听器,三个参数,第一个参数为所要监听的事件的类型(mousedown-鼠标按下,keydown-键盘按下等事件),第二个参数为发生事件后所要执行的JavaScript的语句,第三个参数可选,默认为假;
!doctypehtml
html
head
title五子棋/title
metacharset="utf-8"
style
padding:0;
margin:0;
}
#out{
width:610px;
height:610px;
border:5pxsolid;
position:absolute;
margin-left:300px;
margin-top:100px;
}
#play{
width:200px;
height:80px;
position:absolute;
margin-left:500px;
margin-top:0px;
}
#start{
width:100px;
height:45px;
border-radius:25px;
position:absolute;
margin-top:0px;
margin-left:50px;
font-size:20px;
background-color:#CCFF66;
}
span{
width:80px;
height:30px;
position:absolute;
margin-top:50px;
border:1pxsolid;
text-align:center;
}
width:40px;
height:40px;
border-radius:40px;
background-color:#999999;
margin-left:7px;
margin-top:0px;
}
width:40px;
height:40px;
border-radius:40px;
background-color:#000000;
margin-left:7px;
margin-top:0px;
}
tabletr{
height:50px;
}
tabletrtd{
width:50px;
height:50px;
}
.block{
width:50px;
height:50px;
}
#history{
width:100px;
height:50px;
position:absolute;
margin-top:40px;
margin-left:720px;
}
#return{
width:100px;
height:50px;
background-color:#999900;
border-radius:26px;
font-size:17px;
font-family:"LucidaConsole","LucidaSansTypewriter",Monaco,"CourierNew",monospace;
font-style:oblique;
}
.tab{
width:100px;
height:151px;
position:absolute;
margin-top:120px;
}
.head{
width:100px;
height:50px;
position:absolute;
margin-top:0px;
}
.score{
width:100px;
height:100px;
position:absolute;
margin-top:51px;
}
.feshu{
width:50px;
height:100px;
position:absolute;
margin-top:0px;
}
width:100%;
height:100%;
}
/style
script
window.onload=function(){
varwhite=document.getElementById("white");
varblack=document.getElementById("black");
varstart=document.getElementById("start");
varout=document.getElementById("out");
varle1=0,le2=0,ri1=0,ri2=0;
varleft1=document.getElementById("left1");
varleft2=document.getElementById("left2");
varright1=document.getElementById("right1");
varright2=document.getElementById("right2");
varimages=newArray("./images/s0.png","./images/s1.png","./images/s2.png","./images/s3.png","./images/s4.png","./images/s5.png","./images/s6.png","./images/s7.png","./images/s8.png","./images/s9.png");
functiongoal(le2,ri2){
left1.src=images[le1];
left2.src=images[le2];
right1.src=images[ri1];
right2.src=images[ri2];
for(m=0;mm++){
document.getElementsByTagName("td")[m].innerHTML='divid="the'+m+'"/div
}
//flag=1,不能放棋子
varflag=newArray(100);
for(varj=0;jj++){
flag[j]=0;
}
//1-白子先行,2-黑子先行
varwflag=0,bflag=0,lflag=0;
varturn=Math.floor(Math.random()*2);//0,1,如果是turn=0表示白子先行,=1表示黑子先行
start.unction(){
if(turn+1==1){
wflag=1;
bflag=0;
}
else{
wflag=0;
bflag=1;
}
down(wflag,bflag);
lflag=1;
}
//重新开始
document.getElementById("return").aishi;
functionkaishi(){
for(m=0;mm++){
document.getElementsByTagName("td")[m].innerHTML='divid="the'+m+'"/div
}
out.style.cursor="default";
for(varj=0;jj++){
flag[j]=0;//flag重置为0
}
white.innerHTML="";
black.innerHTML="";
wflag=0,bflag=0,lflag=0;
varturn=Math.floor(Math.random()*2);//0,1
start.unction(){
if(turn+1==1){
wflag=1;
bflag=0;
}
else{
wflag=0;
bflag=1;
}
down(wflag,bflag);
lflag=1;
}
win=0;
functiondown(wflag,bflag){
//下棋
if(wflag==0bflag==1){
black.innerHTML="黑方执子";
white.innerHTML="白方等待";
wflag=1;
bflag=0;
out.style.cursor="url(./images/cursor1.cur),auto";
}
else{
white.innerHTML="白方执子";
black.innerHTML="黑方等待";
wflag=0;
bflag=1;
out.style.cursor="url(./images/cursor2.cur),auto";
}
happen(wflag,bflag);
functionhappen(wflag,bflag){
if(wflag==0bflag==1){
//白子
fox("wq",wflag,bflag);
}
//黑子
else{
fox("bq",wflag,bflag);
}
}
functionfox(color,wflag,bflag){
for(vari=0;ii++){
downup(i,color,wflag,bflag);
}
functiondownup(i,color,wflag,bflag){
document.getElementById('the'+i).unction(){
if(flag[i]!=0){alert("禁止放子!");}
else{
document.getElementById('the'+i).className=color;
if(color=="wq"){
flag[i]=1;
}
else{flag[i]=2;}
down(wflag,bflag);
}
}
}
}
varwin=0;
//正5子
functionzheng(){
if(win==0){
for(varp=0;pp++){
if(flag[p]==1flag[p+1]==1flag[p+2]==1flag[p+3]==1flag[p+4]==1){
varh=p+4
if(h!=10h!==20h!==30h!==40h!=50h!==60h!==70h!==80h!==90h!==100){
alert("白方获胜!");
win=1;
le2++;
goal(le2,ri2);
}
else{returnfalse;}
}
elseif(flag[p]==2flag[p+1]==2flag[p+2]==2flag[p+3]==2flag[p+4]==2){
varh=p+4
if(h!=10h!==20h!==30h!==40h!=50h!==60h!==70h!==80h!==90h!==100){
alert("黑方获胜!");
win=1;
ri2++;
goal(le2,ri2);
}
else{returnfalse;}
}
}
}
else{returntrue;}
//竖5子
functionshu(){
if(win==0){
for(varp=0;pp++){
if(flag[p]==1flag[p+10]==1flag[p+20]==1flag[p+30]==1flag[p+40]==1){
alert("白方获胜!");
win=1;
le2++;
goal(le2,ri2);
}
elseif(flag[p]==2flag[p+10]==2flag[p+20]==2flag[p+30]==2flag[p+40]==2){
alert("黑方获胜!");
win=1;
ri2++;
goal(le2,ri2);
}
}
}
else{returntrue;}
}
//左斜5子
functionleft(){
if(win==0){
for(varp=0;pp++){
if(flag[p]==1flag[p+11]==1flag[p+22]==1flag[p+33]==1flag[p+44]==1){
alert("白方获胜!");
win=1;
le2++;
goal(le2,ri2);
}
elseif(flag[p]==2flag[p+11]==2flag[p+22]==2flag[p+33]==2flag[p+44]==2){
alert("黑方获胜!");
win=1;
ri2++;
goal(le2,ri2);
}
}
}
else{returntrue;}
}
//右斜5子
functionright(){
if(win==0){
for(varp=0;pp++){
if(flag[p]==1flag[p+9]==1flag[p+18]==1flag[p+27]==1flag[p+36]==1){
alert("白方获胜!");
win=1;
le2++;
goal(le2,ri2);
}
elseif(flag[p]==2flag[p+9]==2flag[p+18]==2flag[p+27]==2flag[p+36]==2){
alert("黑方获胜!");
win=1;
ri2++;
goal(le2,ri2);
}
}
}
else{returntrue;}
}
window.addEventListener('mousedown',zheng,false);
window.addEventListener('mousedown',shu,false);
window.addEventListener('mousedown',left,false);
window.addEventListener('mousedown',right,false);
window.addEventListener('mousedown',that,false);
/script
/head
body
divid="play"
buttonid="start"START/button
spanid="white"/spanspanid="black"/span
/div
divid="history"buttonid="return"重新开始/button/div
div
divimgsrc="./images/baif.png"//div
div
divimgsrc="./images/s0.png"id="left1"/div
divimgsrc="./images/s0.png"id="left2"/div
/div
/div
divid="bi"imgsrc="./images/bi.png"//div
div
divimgsrc="./images/heif.png"//div
div
divimgsrc="./images/s0.png"id="right1"/div
divimgsrc="./images/s0.png"id="right2"/div
/div
/div
divid="out"
tablewidth="600px"height="600px"border="1px"
trheight="50px"td/tdtd/tdtd/tdtd/tdtd/tdtd/tdtd/tdtd/tdtd/tdtd/td/tr
trheight="50px"td/t
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 医院新员工入职培训体系
- 城市交通规划历史重点基础知识点
- 《矿物质与健康》课件
- 《企业数字化转型之课件解决方案》课件
- 【培训课件】如何提升顾问单位的满意度与服务品质
- 进校陪读协议书范本
- 车站管理协议书范本
- 湖北中考英语试题单选题100道及答案
- 足浴店购销合同协议
- 《膝关节损伤》课件
- 国有企业职场礼仪培训XXXX最新版课件
- 第节-国际直接投资理论PPT优秀资料
- 完美日记营销体系策略深度解析(深度解析完美日记崛起的驱动力)
- 动火许可证(模板)
- 具备履行合同所必需的设备和专业技术能力的证明材料范文五篇
- 预拌混凝土运输单(正本)
- 江苏省生物技术和新医药产业发展规划纲要
- 【学习课件】第七章天然气处理与轻烃回收
- Thermo Form 311 CO2培养箱中文说明书
- 【精华】贝雷梁拆除施工方案9
- 小学二年级环保校本课程教材与绿色同行
评论
0/150
提交评论