npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zl_3take3_game

v0.0.3

Published

一个3*3的#字游戏算法,添加了些脚本命令,添加了查看算法代码的脚本命令

Downloads

2

Readme

zl_3take3_game

一个functiuon:能返回一个 3 x 3 的井字棋游戏中,玩家下一步可能获胜的几种方式

假设我们现在有一个 3 x 3 的井字棋游戏,我们用一个二维数组代表棋盘,’x’ 代表玩家 X 下的棋子,’o’ 代表玩家 O 下的棋子,’e’ 代表该格没有棋子。例如:

一个空白的棋盘以下面的二维数组表示

[ [‘e’, ‘e’, ‘e’],

[‘e’, ‘e’, ‘e’],

[‘e’, ‘e’, ‘e’] ]

如果玩家 X 在第一行第一列下了一步棋,玩家 O 在第二行第二列下了一步棋,则表示如下:

[ [‘x’, ‘e’, ‘e’],

[‘e’, ‘o’, ‘e’],

[‘e’, ‘e’, ‘e’] ]

现在需要一个 function,接受一个已有任意棋子的棋盘(和上面二维数组一样的格式),和玩家的标志(’x’ 或 ‘o'),返回该玩家下一步有几种可能的获胜方式(获胜方式以数组表示,[0, 0] 代表在第一行第一列下一步棋即可获胜,[2, 2] 代表在第三行第三列下一步棋即可获胜)。例如:

someFunction(

‘x’,

[ [‘o’, ‘e’, ‘e’],

[‘o’, ‘x’, ‘o’],

[‘x’, ‘x’, ‘e’] ]

)

// return [ [2, 2], [0, 1], [0, 2] ]

someFunction(

‘x’,

[ [‘x’, ‘o’, ‘o’],

[‘x’, ‘x’, ‘e’],

[‘e’, ‘o’, ‘e’] ]

)

// return [ [2, 2], [1, 2], [2, 0] ]

someFunction(

‘x’,

[ [‘x’, ‘x’, ‘o’],

[‘e’, ‘e’, ‘e’],

[‘e’, ‘e’, ‘e’] ]

)

// return [ ]

someFunction(

‘o’,

[ [‘o’, ‘o’, ‘o’],

[‘e’, ‘e’, ‘e’],

[‘e’, ‘e’, ‘e’] ]

)

// return [ ]

/*

算法: 由于三3 x 3 的井字棋的胜利路径能很简单的推算出来:即横三,数三,斜三,所以,可以采取遍历法。 具体过程: 先提前把所有胜利的路径坐标给出,然后在用当前棋盘中棋子去匹配坐标,根据胜利路径坐标来返回结果。

*/

//传入参数为棋手与棋盘,棋手为x或o表示,棋盘已一个二维数组表示

let getSuccessResult=(player,board)=>{
successWays=[ //声明一个三维数组来存储所有可能的胜利结果:共8种, [[0,0],[0,1],[0,2]], //横三胜利路径坐标:3种 [[1,0],[1,1],[1,2]], [[2,0],[2,1],[2,2]], [[0,0],[1,0],[2,0]], //竖三胜利路径坐标: 3种 [[0,1],[1,1],[2,1]], [[0,2],[1,2],[2,2]], [[0,0],[1,1],[2,2]], //斜三胜利路径坐标: 3种 [[2,0],[1,1],[0,2]], ] //声明一个结果数组 let results=[]; //一个循环得到所有的结果 for(let i in successWays) { let res=0;//标志每条成功路径,已落的棋子数目,或1或2或3 let empty=[]; //获取值每条胜利路径中的空缺点坐标 for(let j in successWays[i]) { //获取每条胜利路径中每个点的坐标 let x=successWays[i][j][0]; let y=successWays[i][j][1]; //开始具体的判断:判断当前坐标是否已经有棋手落子 if(board[x][y]==player) res++; //,若有,标志变量res就加1 else{ if(board[x][y]=='e')empty=[x,y];//若无,再判断此位置是否为空,如果为空,就将这个点的坐标放入到empty中去 }
} //开始判断当前路径是否能够胜利:如果在每条可能的胜利路径下,有两个点都已经落子,且第三个点是可以落子的空位。 //那么就说明这个空位点坐标是我们想要的结果之一。 if(res==2&&empty.length==2) results.push(empty); } //返回最后的结果 return results; }

返回值

[[1, 2],[2,0],[2,2]]