自己写的HTML5 Canvas + Javascript五子棋

看到一些曾经只会灌水的网友,在学习了前端之后,已经能写出下载量几千几万的脚本、样式,帮助大众,成为受欢迎的人,感觉满羡慕的。我也想学会前端技术,变得受欢迎呀。于是心血来潮,开始学习前端知识,并写下了这个小练习。

基本思路是这样的:

  1. 使用Canvas绘制棋盘、棋子。
  2. 用二维数组保存棋盘状态。
  3. 设置一个flag,用以标识落子顺序。
  4. 点击时,在数组中检测当前点击位置是否存在棋子,若存在,则不落子;如游戏已结束,亦不落子。
  5. 落子时,更新数组,并将当前落子所在的行、列、左上-右下列、左下-右上列四个方向的棋盘状态写入到一维数组中,用以判断新落子是否形成了五子连珠。
  6. 若形成了五子连珠,提示胜利,并结束游戏;反之,则交换顺序,继续进行游戏。

  效果图:

  

代码如下:

  1 <!DOCTYPE html>  2 <html lang="zh-CN">  3 <meta charset="utf-8">  4 <head><title>五子棋</title></head>  5 <body>  6 <canvas id="myCanvas" width="560" height="560" style="border:3px solid black;">  7 您的浏览器不支持 HTML5 canvas 标签。</canvas>  <br/>  8 <button id="reset" onclick="controller.init(ctx)">重置</button>  9 </body> 10 <script> 11 var controller = { 12     round:true, 13     color:"black", 14     whiteTable:new Array(), 15     blackTable:new Array(), 16     row:0, 17     col:0, 18     over:false, 19     trans:function() {        
 20         this.round = !this.round; 21         if (!this.round) { 22             this.blackTable[this.row][this.col] = 1; 23             this.ifWin(this.blackTable) 24             this.color = "white"; 25         } 26         else { 27             this.whiteTable[this.row][this.col] = 1; 28             this.ifWin(this.whiteTable)            
 29             this.color = "black"; 30         } 31     }, 32     ifWin:function(table) {    
 33         var arr1 = new Array(); 34         var arr2 = new Array(); 35         var arr3 = new Array(); 36         var arr4 = new Array(); 37         var n = 0;    
 38         for(x = 0; x<= lineNums; x++) {         
 39             for(y = 0; y <= lineNums; y++) 
 40             { 
 41                 var x1 = this.row - n; 42                 var x2 = this.row + n; 43                 var y1 = this.col - n; 44                 var y2 = this.col + n; 45                 if(y == this.col) { 46                     arr1[x] = table[x][y]; 47                 } 48                 if(x == this.row) { 49                     arr2[y] = table[x][y]; 50                 } 51             } 52             if(this.inBounds(x1) && this.inBounds(y2)) { 53                 arr3[x1] = table[x1][y2]; 54             } 55             if(this.inBounds(x1) && this.inBounds(y1)) { 56                 arr4[x1] = table[x1][y1];                
 57             } 58             if(this.inBounds(x2) && this.inBounds(y1)) { 59                 arr3[x2] = table[x2][y1]; 60             } 61             if(this.inBounds(x2) && this.inBounds(y2)) { 62                 arr4[x2] = table[x2][y2];                
 63             } 64             n = n + 1; 65         } 
 66         this.getSum(arr1, this.row); 67         this.getSum(arr2, this.col); 68         this.getSum(arr3, this.row); 69         this.getSum(arr4, this.row); 70     }, 71     inBounds:function(i) { 72         if(i>=0 && i<=15){ 73             return true; 74         } 75         else{ 76             return false;    
 77         }    
 78     }, 79     getSum:function(array, pos) { 80         num = 5; 81         posr = pos + 1; 82         while(num > 0){ 83             if(array[pos]>0  && this.inBounds(pos)) { 84                 num = num - 1; 85                 pos = pos - 1; 86             } 87             else{ 88                 break; 89             } 90         } 91         while(num > 0){ 92             if(array[posr]>0 && this.inBounds(pos)) { 93                 num  = num - 1; 94                 posr = posr + 1; 95             } 96             else { 97                 break; 98             }        
 99         }100         if(num == 0) {101             this.over = true;102             this.gameOver();103         }    
104     },105     exist:function(x, y) {106         this.row = x / ratio;107         this.col = y / ratio;108         var nums = this.whiteTable[this.row][this.col] + this.blackTable[this.row][this.col];109         if( nums > 0 {110             return true;111         }112         else{113             return false;114         }115     },116     gameOver:function() {117         ctx.font="30px Arial";118         ctx.fillStyle = "#FF0000";119         if(this.round) {120             ctx.fillText("白棋胜利",240,240);121         }122         else {123             ctx.fillText("黑棋胜利",240,240);124         }125     },126     init:function() {127         this.round = true;128         this.color = "black";129         this.over  = false;130         this.drawBoard();131         for(i = 0; i<= lineNums; i++) { 
132             this.whiteTable[i]=new Array();133             this.blackTable[i]=new Array();134             for(n = 0; n <= lineNums; n++) { 
135                 this.whiteTable[i][n]=0;  
136                 this.blackTable[i][n]=0;137             } 
138         } 
139     },140     drawBoard:function() {141         ctx.beginPath();142         ctx.clearRect(0,0,width,width);143         ctx.fillStyle = "#FFBB00";144         ctx.fillRect(0,0,width,width);145         for(var i = 1; i < (lineNums - 1); i++) {146             ctx.moveTo(i * ratio, 0);147             ctx.lineTo(i * ratio, width);148             ctx.stroke();149             ctx.moveTo(0, i * ratio);150             ctx.lineTo(width, i * ratio);151             ctx.stroke();152         }153     },154     drawPiece:function(posX, posY) {155         ctx.beginPath();156         ctx.arc(posX, posY, ratio/2, 0, 2*Math.PI);157         ctx.fillStyle = this.color;158         ctx.fill();159         ctx.stroke();                    
160     }161 }; 
162 //获取点击位置163 function getMousePos(canvas, evt) { 
164    var rect = canvas.getBoundingClientRect(); 
165    return { 
166      x: evt.clientX - rect.left * (canvas.width / rect.width),167      y: evt.clientY - rect.top * (canvas.height / rect.height)168    }169  }170     171 function getNode(pos) {172     return ((pos / ratio).toFixed()) * ratio;173 }174 175 var canvas = document.getElementById("myCanvas");176 var ctx = canvas.getContext("2d");177 var lineNums = 15;178 var ratio = 40;179 var width = (lineNums - 1) * ratio;180 181 controller.init();     
182 183  canvas.addEventListener("click", function (evt) { 
184     var mousePos = getMousePos(canvas, evt); 
185     mousePos.x = getNode(mousePos.x);186     mousePos.y = getNode(mousePos.y);187       var exist = controller.exist(mousePos.x, mousePos.y);188     if (!exist && !controller.over) {189       controller.drawPiece(mousePos.x, mousePos.y); 
190       controller.trans();191     } 
192  }, false);     
193 </script>194 </html>

l

时间: 2024-08-04 13:14:08

自己写的HTML5 Canvas + Javascript五子棋的相关文章

自己的写的HTML5 Canvas + Javascript五子棋

看到一些曾经只会灌水的网友,在学习了前端之后,已经能写出下载量几千几万的脚本.样式,帮助大众,成为受欢迎的人,感觉满羡慕的.我也想学会前端技术,变得受欢迎呀.于是心血来潮,开始学习前端知识,并写下了这个小练习. 基本思路是这样的: 使用Canvas绘制棋盘.棋子. 用二维数组保存棋盘状态. 设置一个flag,用以标识落子顺序. 点击时,在数组中检测当前点击位置是否存在棋子,若存在,则不落子:如游戏已结束,亦不落子. 落子时,更新数组,并将当前落子所在的行.列.左上-右下列.左下-右上列四个方向的

知名Html5 Canvas Javascript库对比 (转)

知名Html5 Canvas Javascript库对比 声明: 原文链接:http://www.softr.li/blog/2012/06/20/which-html5-canvas-javascript-library-should-i-use 本译文地址:http://jo2.org/html5-canvas-libary-introduction/ 转载请保留.. <我应该选哪个Canvas库?>译文(以下的“我”是指原作者): 我一直在找一个Html5 canvas库,可以让我创建可

知名Html5 Canvas Javascript库对比 (引用)

由于LS 的数据绑定使用了 :EaselJS The LightSwitch Data Binding Framework Using EaselJS http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/1211/The-LightSwitch-Data-Binding-Framework-Using-EaselJS.aspx 所以,在网上寻找相关的资料,这是一个比较.http://www.tuicool.com/articles/F3

赠书:HTML5 Canvas 2d 编程必读的两本经典

赠书:HTML5 Canvas 2d 编程必读的两本经典 这两年多一直在和HTML5 Canvas 打交道,也带领团队开发了世界首款基于HTML5 Canvas 的演示文档工具---AxeSlide(斧子演示,www.axeslide.com).在这个领域也积累了一些 经验,希望有机会和大家分享.今天是要给大家推荐两本这方面的书,同时会送一本书给大家. 要介绍的第一本书是我学习Canvas开发的入门书——<HTML5 Canvas核心技术:图形.动画与游戏开发>. 此书作者David Gear

HTML5 canvas绘制雪花飘落动画(需求分析、知识点、程序编写分布详解)

看到网上很多展示html5雪花飞动的效果,确实非常引人入胜,我相信大家也跟我一样看着心动的同时,也很好奇,想研究下代码如何实现:虽然哦很多地方也能下载这些源码,不过也不知道别人制作此类动画时的思路及难点分析. 我这几天刚好学习了一下,也趁着此刻有时间从需求分析.知识点.程序编写一步步给大家解剖下,要是在各位关公面前耍大刀了,可别见笑哟. 最终效果图如下: 图1 一.需求分析 1.圆形雪花 本示例中雪花形状使用圆形 2.雪花数量固定 根据图1仔细观察白色雪花数量,飘落过程中,整张图的雪花数量应该是

利用HTML5 Canvas和Javascript实现的蚁群算法求解TSP问题演示

HTML5提供了Canvas对象,为绘图应用提供了便利. Javascript可运行于浏览器中, 而不需要安装特定的编译器: 基于HTML5和Javascript语言, 可随时编写应用, 为算法测试带来便利. 针对TSP问题, 编写了Ant colony algorithm, 用于演示该算法, tsp_ant_colony_algorithm.html代码如下: <html> <head> <meta charset = "utf-8" / > &l

html5 canvas+原生javascript 实时获取文本框内容绘制图片水印

最近有位客户要求在网页图片上加文字水印效果,并且内容是从当前网页的文本输入框实时获取的,研究了一半天,在网上也参考了不少朋友的办法,再加上园子里热心的好哥们帮助终于实现了,先看下效果图: 代码如下: 1 <!DOCTYPE html> 2 <head> 3 <meta charset="UTF-8"> 4 <title>drawing by input text</title> 5 </head> 6 7 <

html5 Canvas颜色渐变(画图很重要)

如果我们想要给图形上色,有两个重要的属性可以做到:fillStyle 和 strokeStyle.    fillStyle = color    strokeStyle = color strokeStyle 是用于设置图形轮廓的颜色,而 fillStyle 用于设置填充颜色.color 可以是表示 CSS 颜色值的字符串,渐变对象或者图案对象.默认情况下,线条和填充颜色都是黑色(CSS 颜色值 #000000). 下面的例子都表示同一种颜色.   // 这些 fillStyle 的值均为 '

HTML5 canvas 绘制精美的图形

HTML5 是一个新兴标准,它正在以越来越快的速度替代久经考验的 HTML4.HTML5 是一个 W3C “工作草案” — 意味着它仍然处于开发阶段 — 它包含丰富的元素和属性,它们都支持现行的 HTML 4.01 版本规范.它还引入了几个新元素和属性,它们适用许多使用 web 页面的领域 — 音频.视频.图形.数据存储.内容呈现,等等.本文主要关注图形方面的增强:canvas. 新的 HTML5 canvas 是一个原生 HTML 绘图簿,用于 JavaScript 代码,不使用第三方工具.跨