简体   繁体   English

JavaScript 箭头函数数组

[英]Array of JavaScript arrow functions

I'm implementing the card game Hearts in JavaScript, and one of the core elements of the game is that you can pass your cards to other players.我正在用 JavaScript 实现纸牌游戏 Hearts,游戏的核心元素之一是您可以将您的纸牌传递给其他玩家。 My game has strictly 4 players, no more, no less.我的游戏有严格的 4 名玩家,不多也不少。

The passing order goes left, right, straight ahead, then no passing.超车顺序是向左、向右、直行,然后不超车。 Therefore, P1 would pass to P2, P4, P3, and then pass to nobody.因此,P1 会传给 P2、P4、P3,然后传给没人。 The cycle loops until the game is over.循环循环直到游戏结束。

I am trying to implement this logic via arrow functions, however, it is not working.我试图通过箭头函数来实现这个逻辑,但是,它不起作用。 What I am trying to do is print out the player, and the player that they should pass to, based on a given index.我想要做的是根据给定的索引打印出玩家以及他们应该传递给的玩家。

Here is my code, I hope it is clear what I am trying to do.这是我的代码,我希望很清楚我要做什么。

 const players = [1, 2, 3, 4]; const passingOrder = 2; const passCards = [ i => (i + 1) % 4, //pass left i => (i - 1 + 4) % 4, //pass right i => (i + 2) % 4, //pass straight i => i //pass to ones self ]; players.forEach((player, index) => { console.log(player + "passes to " + passCards[passingOrder](index)) })

1, 2, 3, 4 are not good numbers to work with when you have to do MOD, which produces something from 0 and up, and you use it to indicate which user.当您必须进行 MOD 时,1、2、3、4 不是很好的数字,它会从 0 开始生成一些内容,然后您用它来指示哪个用户。

Use 0, 1, 2, 3 instead:使用 0, 1, 2, 3 代替:

 const players = [0, 1, 2, 3]; const passingOrder = 2; const passCards = [ i => (i + 1) % 4, //pass left i => (i - 1 + 4) % 4, //pass right i => (i + 2) % 4, //pass straight i => i //pass to ones self ]; players.forEach((player, index) => { console.log(player + "passes to " + passCards[passingOrder](index)) })

Or if you want to keep it 1, 2, 3, 4, or even use names, you need to use the index generated to find the player using players[generatedIndex] , where generatedIndex is passCards[passingOrder](index) :或者,如果您想保留它 1、2、3、4,甚至使用名称,则需要使用生成的索引来查找使用 player players[generatedIndex] ,其中generatedIndexpassCards[passingOrder](index)

 const players = [1, 2, 3, 4]; const passingOrder = 2; const passCards = [ i => (i + 1) % 4, //pass left i => (i - 1 + 4) % 4, //pass right i => (i + 2) % 4, //pass straight i => i //pass to ones self ]; players.forEach((player, index) => { console.log(player + "passes to " + players[passCards[passingOrder](index)]) })

And you can use names if you want:如果需要,您可以使用名称:

 const players = ["Peter", "Paul", "Mary", "Susan"]; const passingOrder = 2; const passCards = [ i => (i + 1) % 4, //pass left i => (i - 1 + 4) % 4, //pass right i => (i + 2) % 4, //pass straight i => i //pass to ones self ]; players.forEach((player, index) => { console.log(player + " passes to " + players[passCards[passingOrder](index)]) })

And all passing methods:以及所有传递方法:

 const players = ["Peter", "Paul", "Mary", "Susan"]; const passCards = [ i => (i + 1) % 4, //pass left i => (i - 1 + 4) % 4, //pass right i => (i + 2) % 4, //pass straight i => i //pass to ones self ]; passCards.forEach((passMethod, iPassMethod) => { console.log(`Game ${iPassMethod + 1}`); players.forEach((player, index) => { console.log(player + " passes to " + players[passMethod(index)]) }) console.log("\\n"); })

Your players a 1 based while the index is 0 based.您的球员以 1 为基础,而指数以 0 为基础。

Try尝试

console.log(player + "passes to " + (passCards[passingOrder](index) + 1))

Or make your players const players = [0, 1, 2, 3];或者让你的玩家const players = [0, 1, 2, 3]; . .

We're going to cheat.我们要作弊。 Rather than waste time trying to solve this in an overly creative way, we're going to make an object that lays out the association between players and their passing direction.与其浪费时间试图以一种过于创造性的方式来解决这个问题,我们将制作一个对象来展示球员与其传球方向之间的关联。 Let's skip the modulus for this one.让我们跳过这个模数。

Doing things this way also happens to keep our code readable, and is more intuitive at first glance.以这种方式做事也恰好保持了我们的代码可读性,并且乍一看更直观。 Notice how we could easily extend this to three dimensions by just adding up and down keys to passingOrder and directions .请注意我们如何通过将updown键添加到passingOrderdirections来轻松地将其扩展到三个维度。

Sometimes it pays off to be lazy!有时候懒惰是有回报的!

 var players = [1, 2, 3, 4]; var passingOrder = ["left", "right", "center"]; var directions = { 1: { left: 2, center: 3, right: 4 }, 2: { left: 3, center: 4, right: 1 }, 3: { left: 4, center: 1, right: 2 }, 4: { left: 1, center: 2, right: 3 } }; players.forEach( player => passingOrder.forEach( pass => console.log(player, "passes to", directions[player][pass]) ) );

I would go a simple solution, each player should remember his passing order, if he can't, he should not join the game .我会去一个简单的解决方案,每个玩家都应该记住他的传球顺序,如果他不能,他就不应该参加比赛

 class Player { constructor(id, passOrder, name = 'Anounymous') { this.id = id this.name = name this.passOrder = passOrder } } let players = [ new Player(1, [2, 4, 3], 'Alex'), new Player(2, [3, 1, 4], 'Billy'), new Player(3, [4, 2, 1]), new Player(4, [1, 3, 2], 'Tom') ] players.forEach((p, i) => { console.log(`#${i+1} Player ${p.name} pass to `) p.passOrder.forEach(np => { console.log(` #${np} player ${players[np-1].name},`) }) console.log('-------------------') })

In your case, you have passCards which takes an index and return an index.在您的情况下,您有 passCards,它接受一个索引并返回一个索引。 You can also consider passTo (just named differently) which takes a player and returns a player .你也可以考虑passTo (只是命名不同)它接受一个player并返回一个player

Below an example in which passTo defines the possible actions下面是 passTo定义可能操作的示例

 const players = [1, 2, 3, 4]; const passingOrder = 2; const passTo = (players => { const inIndexSpace = cb => { return player => { const i = players.indexOf(player) return players[cb(i)] } } return { left: inIndexSpace(i => (i + 1) % 4), right: inIndexSpace(i => (i - 1 + 4) % 4), straight: inIndexSpace(i => (i + 2) % 4), self: inIndexSpace(i => i) } })(players) players.forEach((player, index) => { console.log(player + "passes to " + passTo.straight(player)) })

which in "minimal" form (which you should avoid since dupplicate code) would be以“最小”形式(由于重复代码而应避免)将是

 const players = [1, 2, 3, 4]; const passingOrder = 2; const passTo = [ player => { const i = players.indexOf(player); return players[(i + 1) % 4]}, player => { const i = players.indexOf(player); return players[(i - 1 + 4) % 4]}, player => { const i = players.indexOf(player); return players[(i + 2) % 4]}, player => { const i = players.indexOf(player); return players[i]} ]; players.forEach((player, index) => { console.log(player + "passes to " + passTo[passingOrder](player)) })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM