2017-08-30 46 views
0

我正在制作一种游戏,有一种岩石剪刀系统,只有更复杂,使用“价值观”,如3摇滚节拍2摇滚,其中一玩家可以在一回合中选择他想要使用的岩石数量。这个游戏可以/必须处理2个以上的玩家,但也可以有2个玩家。我只能这样做与价值,使用此代码:NodeJS - 排序像岩石纸剪刀 - 动态玩家人数

 array.push({ "player": player, "score": parseInt(cardInformation.value), "Seat": i, element: cardInformation.element}); 

     array.sort(function (a, b) { 
      return b.score - a.score; 
     }); 

     var rank = 1; 
     var lastWinner = -1; 
     for (var i = 0; i < array.length; i++) { 
      if (i > 0 && array[i].score < array[i - 1].score) { 
       rank++; 
      } 
      array[i].rank = rank; 
      array[i].winStatus = loss; 
      if(rank == 1) { 
       if(lastWinner != -1) { 
        array[lastWinner].winStatus = tie; 
        array[i].winStatus = tie; 
       } else 
        array[i].winStatus = win; 
       lastWinner = i; 
      } 
     } 

我到处寻找像剪刀石头布的系统,但所有我能找到的2名球员,我不确定如何让它更多。如果你有时间,请帮助我。

+0

您将有更多的描述了一下,如果有类似**定时轮流**和其他游戏逻辑(如检测的玩家或类似的东西总数.. 。)它是在实时,所有在同一时间,每** x **秒?我们需要更多信息来帮助您:P – EMX

+0

@EMX感谢您的回复!游戏是动态的,它可以处理大量的用户,但它也可以处理2. 2是最小的。没有定时轮换,但要获得玩家总数,我只需要array.length。这是实时的,他们都在同一时间挑选。 – JadBalout

+0

我想你也使用'socket.io',对吧? – EMX

回答

0

希望这有助于:

const TURN_INTERVAL = 10//seconds 
const GAME_STATE = { 
    status:1, // 1:player action is allowed/0:player action is frozen 
    turnData:{ // temp storage for playerIDs grouped by value chosen 
    1:[], //paper 
    2:[], //scissors 
    3:[] //rock 
    }, 
    versus:{ //what kills what 
    1:3, 
    2:1, 
    3:2 
    } 
} 

function GlobalTurn(){ 
    console.log('[Round]','started') 
    GAME_STATE.status = 0; // players must wait for turn end response 
    for(var i=1; i<4;i++){ // notify each group of how many they killed 
    var thisGroupKills = GAME_STATE.versus[i]; //which is the weak group against this one 
    var totalKilled = GAME_STATE.turnData[thisGroupKills].length //how much this group has killed 
    console.log('group',i,'killed a total of',totalKilled) //send to clients instead 
    /* Here you can iterate over each playerID if you need to : 
    for(var j=0; j<GAME_STATE.turnData[i].length){ 
    console.log('player',GAME_STATE.turnData[i],'killed a total of',totalKilled) 
    } 
    */ 
    } 
    EndTurn() 
} 

function EndTurn(){ 
    console.log('Next round will start in',TURN_INTERVAL,'seconds') 
    GAME_STATE.turnData = {1:[],2:[],3:[]} //reset for next round 
    GAME_STATE.status = 1; // players can send actions (their pick) 
    setTimeout(GlobalTurn,TURN_INTERVAL*1000); //prepare next round calculator 
}