大佬教程收集整理的这篇文章主要介绍了算法连接四个javascript,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
directions = [ { x: 0,y: 1 },// North-South { x: 1,y: 0 },// East-West { x: 1,// Northeast-SouthWest { x: 1,y: -1 } // Southeast-NorthWest ];@H_607_3@然后,循环遍历该对象并循环通过您的“表格”,从最远的边界开始,这块可能有助于获胜.因此,由于您需要连续4件,因此当前放置的部件可以在任何方向上最多赢得3件. @H_404_26@minX = Math.min(Math.max(placedX - (3 * directions[i].X),0),pieces.length - 1); minY = Math.min(Math.max(placedY - (3 * directions[i].y),pieces[0].length - 1); maxX = Math.max(Math.min(placedX + (3 * directions[i].X),pieces.length - 1),0); maxY = Math.max(Math.min(placedY + (3 * directions[i].y),pieces[0].length - 1),0); @H_607_3@为了避免小于和大于(我遇到过)的任何问题,计算循环遍历各个部分之前的步数,而不是使用计算的边界作为条件.
steps = Math.max(Math.abs(maxX - minX),Math.abs(maxY - minY));@H_607_3@最后,遍历项目,保持与最后放置的棋子相匹配的连续棋子的数量.
function isVictory(pieces,placedX,placedY) { var i,j,x,y,maxX,maxY,steps,count = 0,directions = [ { x: 0,// North-South { x: 1,// East-West { x: 1,// Northeast-SouthWest { x: 1,y: -1 } // Southeast-NorthWest ]; // check all directions outerloop: for (i = 0; i < directions.length; i++,count = 0) { // Set up bounds to go 3 pieces forWARD and BACkWARD x = Math.min(Math.max(placedX - (3 * directions[i].X),pieces.length - 1); y = Math.min(Math.max(placedY - (3 * directions[i].y),pieces[0].length - 1); maxX = Math.max(Math.min(placedX + (3 * directions[i].X),0); maxY = Math.max(Math.min(placedY + (3 * directions[i].y),0); steps = Math.max(Math.abs(maxX - X),Math.abs(maxY - y)); for (j = 0; j < steps; j++,x += directions[i].x,y += directions[i].y) { if (pieces[x][y] == pieces[placedX][placedy]) { // Increase count if (++count >= 4) { break outerloop; } } else { // Reset count count = 0; } } } return count >= 4; }
以上是大佬教程为你收集整理的算法连接四个javascript全部内容,希望文章能够帮你解决算法连接四个javascript所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。