当前位置:网站首页>JS——70行完成五子棋布局
JS——70行完成五子棋布局
2022-04-21 09:26:00 【华为云】
五子棋是全国智力运动会竞技项目之一,是一种两人对弈的纯策略型棋类游戏。双方分别使用黑白两色的棋子,下在棋盘直线与横线的交叉点上,先形成五子连珠者获胜。
笔者相信很多小伙伴都对五子棋情有独钟,想不想拥有属于自己的五子棋呢,今天笔者给大家带来了JavaScript下的五子棋布局(仅有棋盘和下棋换手定义,尚未定义输赢条件)。

代码:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>五子棋棋局</title> <style type="text/css"> canvas{ display: block; margin: 50px auto; box-shadow: -2px -2px 2px #F3F2F2, 5px 5px 5px #6F6767; } </style></head><body> <canvas id="mycanvas" width="450px" height="450px"></canvas> <script type="text/javascript"> var chess = document.getElementById("mycanvas"); var context = chess.getContext('2d'); var me = true; var chessBox = [];//用于存放棋盘中落子的情况 for(var i=0;i<15;i++){ chessBox[i]=[]; for(var j=0;j<15;j++){ chessBox[i][j]=0;//初始值为0 } } function drawChessBoard(){ for(var i=0;i<15;i++){ context.strokeStyle="#D6D1D1"; context.moveTo(15+i*30,15);//垂直方向画15根线,相距30px; context.lineTo(15+i*30,435); context.stroke(); context.moveTo(15,15+i*30);//水平方向画15根线,相距30px;棋盘为14*14; context.lineTo(435,15+i*30); context.stroke(); } } drawChessBoard();//绘制棋盘 function oneStep(i,j,k){ context.beginPath(); context.arc(15+i*30,15+j*30,13,0,2*Math.PI);//绘制棋子 var g=context.createRadialGradient(15+i*30,15+j*30,13,15+i*30,15+j*30,0);//设置渐变 if(k){ //k=true是黑棋,否则是白棋 g.addColorStop(0,'#0A0A0A');//黑棋 g.addColorStop(1,'#636766'); }else { g.addColorStop(0,'#D1D1D1');//白棋 g.addColorStop(1,'#F9F9F9'); } context.fillStyle=g; context.fill(); context.closePath(); } chess.onclick=function(e){ var x = e.offsetX;//相对于棋盘左上角的x坐标 var y = e.offsetY;//相对于棋盘左上角的y坐标 var i = Math.floor(x/30); var j = Math.floor(y/30); if( chessBox[i][j] == 0 ) { oneStep(i,j,me); if(me){ chessBox[i][j]=1; }else{ chessBox[i][j]=2; } me=!me;//下一步白棋 } } </script></body></html>
小伙伴们快去试一试呀!
版权声明
本文为[华为云]所创,转载请带上原文链接,感谢
https://bbs.huaweicloud.com/blogs/348899
边栏推荐
- CC00043.CloudJenkins—————————————
- 报告解读下载 | 首份《中国数据库行业分析报告》重磅发布!
- OpenCV——分离颜色通道,图像对比度,亮度调整,离散傅里叶变换(10)
- Surfaceview high performance rendering (V) code practice - let the drawn picture move
- 1149: combine two of three digits
- Buuctf [geek challenge 2019] easysql
- 1164: 字符串加密
- 头大毕设论文模板适配指南
- In 2017, I also started to write CSDN blog (Sina Netease moved to CSDN)
- I use ehcache local cache to improve the query performance by 100 times. It's really fragrant!
猜你喜欢

Multithreaded copy set (New 4)

Drafting and Revision: Laplacian Pyramid Network for Fast High-Quality Artistic Style Transfer--T Li

Handler asynchronous message passing mechanism (I) common basic usage of handler
![[MySQL] Based on linux-centos7 9 detailed installation tutorial](/img/25/7947dc6d8b5fa99124b69a90a70a27.png)
[MySQL] Based on linux-centos7 9 detailed installation tutorial
![Buuctf [geek challenge 2019] easysql](/img/35/84422c6aea0c96f6cbcf8354729604.png)
Buuctf [geek challenge 2019] easysql

CC00043.CloudJenkins—————————————

Penetration practice - no echo rce thinkphp5 getshell

Pys1: Overview

Pipy MQTT 代理之(三)Logging

Pyinstaller package exe (detailed tutorial)
随机推荐
Penetration practice - dig a school site vulnerability (APP vulnerability)
1149: 组合三位数之二
Pipy MQTT 代理之(三)Logging
性能分析思路
Maya basic tutorial and basic operation explanation
目标检测入门常见问题(深度学习 / 图像分类)
Open3d reads and writes ply point cloud files
Actf2020 freshman tournament upload 1
Integrating sdl2 with ffmpeg to realize texture rendering of random blocks
CC00000. CloudJenkins—————————————
JS prototype and prototype chain
Transaction isolation level and mvcc
极客大挑战 2019 Upload 1
1171: encryption (pointer topic)
Common computer shortcut keys + common DOS commands
Handler asynchronous message passing mechanism (I) common basic usage of handler
Open3d读写pcd点云文件
1151: large integer addition
Multithreaded copy set (New 4)
[paper reading] [iccv 2021] rpnet: learning inner group relations on point clouds