查找条件为

时间:2016-12-01 10:17:38

标签: javascript jquery arrays

由于我正在制作一个拉姆纸牌游戏,因此在融合卡片时我有三个条件需要检查

  1. 找到具有相同诉讼的纯序列的组。 (强制)
  2. 找到一个具有相同诉讼的纯粹或不纯的序列的团体(没有或与小丑一起)
  3. 查找具有纯粹或不纯序列的组或具有不同诉讼的相同价值卡。
  4. 纯序列表示:3,4,5,6 不纯的顺序意味着:3,4,Joker,6(这里Joker可以被认为是5)

    让我们在这里处理案例1.

    我的群组是:

    var arr4 = [{"value":2,"suit":"hearts"},{"value":5,"suit":"hearts"},{"value":3,"suit":"hearts"},{"value":4,"suit":"spades"}];
    
    var arr5 = [{"value":5,"suit":"hearts"},{"value":7,"suit":"hearts"},{"value":20,"suit":"joker"},{"value":8,"suit":"hearts"}];
    
    var arr6 = [{"value":1,"suit":"hearts"},{"value":2,"suit":"hearts"},{"value":4,"suit":"hearts"},{"value":3,"suit":"hearts"}];
    
    var arr7 = [{"value":1,"suit":"diams"},{"value":4,"suit":"hearts"},{"value":4,"suit":"hearts"},{"value":3,"suit":"hearts"}];
    

    这里,arr6是一个纯序列,因为每个项目都有相同的套装。 arr4不包含所有相同的诉讼,因此它不是

    案例1

    我尝试使用纯粹的序列找到一个没有这样的套装的数组。

    
    
        var arr1 = ["4", "1", "3", "5"];
        var arr2 = ["5", "4", "3", "6"];
        var arr3 = ["1", "5", "3", "6"];
    
        function inSeq(arr) {
          return arr.sort(function(a, b) { return a - b; }) 
            .every(function(n, i, arr) {
              return i === 0 || n - arr[i - 1] === 1;
            });
        }
        
        function findArrWithSeq(arrs) {
          for(var i = 0; i < arrs.length; i++) {
            if(inSeq(arrs[i])) {
               return i;
            }
          }
          
          return null;
        }
        
        var result = [arr1, arr2, arr3].some(inSeq);
        var result1 = findArrWithSeq([arr1, arr2, arr3]);
        
        if(result){
          console.log(result);  // To get true or false
          console.log(result1); // To get the array
     
    
          }
    &#13;
    &#13;
    &#13;

    但是,我无法用arr4,arr5,arr6和arr7来实现它,因为同样的诉讼,它只会为arr6返回true。

    我想摆脱阵列,然后在接下来的步骤中继续使用其余3个阵列。我不知道应该以哪种方式进行。

    因此,在接下来的步骤中,如果有某种循环来检查所有数组,则不会考虑arr6

    注意:此处还有另一个问题。我将卡A(Ace)的值取为1.但是,如果有一个J, Q, K, A -> 11, 12, 13, 1的序列;然后它也会被认为是一个正确的序列,因为如果Ace在K之后出现,那么它将作为数字14,否则作为数字1.因此,Q, K, A有效且A, 2, 3也是有效的。

2 个答案:

答案 0 :(得分:1)

我的2美分将是使用单个汇总函数来处理卡阵列以处理所有场景:

&#13;
&#13;
function getSummary(cards){
	cards.sort((c1,c2)=>c1.value-c2.value); //sort cards by their value  
  let jokerCount = 0,jokersUsed =0, res;  
  for(let i = cards.length -1; i >= 0 ; i--) //start from end to handle jokers first (note this is assuming they will also have the larger value of 20)
  {
  		let {suit,value:val} = cards[i]; //destructure suit and value properties into variables
      if(suit==="joker")
      	jokerCount++; //handle jokers separately
			else	{
      	if(res===undefined){ //first non joker card -> init values
        	res = {uniqueSuit: suit, startOfSequence: val}; 
          if(jokerCount) res.jokersUsed =true;          
        }
        else {
        	if(suit !== res.uniqueSuit) //check if there are more suits
        		delete res.uniqueSuit; //no unique suit -> remove the property (alternative: set the prop to 'none')

          if(res.startOfSequence && --res.startOfSequence != val){ //check if the sequence is intact          	
          	if(val === 1 && i === 0 && res.startOfSequence ===10) //Ace
            	res.startOfSequence++; //Ace is used as 14
          	else {
            	let gap = res.startOfSequence - val;              
              if(gap > 0 &&jokerCount >= gap){ //if enough jokers remaining, use them (sequence still intact)
                jokerCount-=gap;
                jokersUsed+=gap;
                res.startOfSequence=val;
              }
              else
                delete res.startOfSequence; //no sequence -> remove property from res
            }
          }
       }
     }
  }
  
  if(res === undefined){
  	  //only jokers (is this possible?) -> create sequence or matching values
    	res={};
      //loop through suits or do what's needed to create best score
      return res;
  }
  
  if(res.startOfSequence) //Sequence found => all cards handled => return result
  	return res;
  
  if(jokersUsed) 		//jokers were used for sequence, but sequence was not complete
     	jokerCount += jokersUsed; //reclaim jokers
	
  const addSameValue = (suit,val)=>{ //helper function to add cards of same value to collection
		if(!res.sameValues)res.sameValues = {};    
    res.sameValues[val] = (res.sameValues[val] | 1) +1; //offset always 1 because adding is done from prev. value
  };
  
  //search for same values
  let prev, cnt =cards.length - jokerCount;
  for(let i=0 ; i < cnt ; i++){
  		let val = cards[i].value;
      if(prev == val)
      	addSameValue(cards[i].suit,val);
      else
      	prev = val;
  }

  if(jokerCount){
    	//use for highest value or add to existing sequences?
      //for example, add to highest value: (might want to check for unused suits)
      addSameValue('joker', cards[cards.length-jokerCount].value);    
  }  
  return res;
}

var arr4 = [{"value":2,"suit":"hearts"},{"value":5,"suit":"hearts"},{"value":3,"suit":"hearts"},{"value":4,"suit":"spades"}];
var arr5 = [{"value":5,"suit":"hearts"},{"value":7,"suit":"hearts"},{"value":20,"suit":"joker"},{"value":8,"suit":"hearts"}];
var arr6 = [{"value":1,"suit":"hearts"},{"value":2,"suit":"hearts"},{"value":4,"suit":"hearts"},{"value":3,"suit":"hearts"}];
var arr7 = [{"value":1,"suit":"diams"},{"value":4,"suit":"hearts"},{"value":4,"suit":"hearts"},{"value":3,"suit":"hearts"}];
var arr8 = [{"value":11,"suit":"hearts"},{"value":12,"suit":"hearts"},{"value":13,"suit":"hearts"},{"value":1,"suit":"hearts"}]; 
var arr9 = [{"value":8,"suit":"hearts"},{"value":8,"suit":"diams"},{"value":9,"suit":"hearts"},{"value":9,"suit":"spades"}]; 


for(let arr of [arr4,arr5,arr6,arr7, arr8,arr9])	
  console.log(getSummary(arr)); //test output
&#13;
&#13;
&#13;

生成的对象将包含相关信息。如果存在序列,则结果对象将包含属性startOfSequence,该属性指示存在序列并保存该序列的第一个值。如果有独特的套装,则在uniqueSuit属性中设置。最后,如果使用joker创建序列,则添加属性jokersUsed(以指示不纯的序列?)

修改的 改变了获取具有相同值的卡的代码。如果找到,则结果将包含一个对象sameValues,其中的属性包含随着它们出现次数而找到的值。

答案 1 :(得分:0)

我建议你使用underscore.js吗?

var arr4 = [{"value":2,"suit":"hearts"},{"value":5,"suit":"hearts"},{"value":3,"suit":"hearts"},{"value":4,"suit":"spades"}];

var arr5 = [{"value":5,"suit":"hearts"},{"value":7,"suit":"hearts"},{"value":20,"suit":"joker"},{"value":8,"suit":"hearts"}];

var arr6 = [{"value":1,"suit":"hearts"},{"value":2,"suit":"hearts"},{"value":4,"suit":"hearts"},{"value":3,"suit":"hearts"}];

var arr7 = [{"value":1,"suit":"diams"},{"value":4,"suit":"hearts"},{"value":4,"suit":"hearts"},{"value":3,"suit":"hearts"}];

var arr8 = [{"value":1,"suit":"diams"},{"value":12,"suit":"diams"},{"value":11,"suit":"diams"},{"value":13,"suit":"diams"}]; // another catch condition

function inSeq(arr) { // identical to your inSeq() method
  //console.log(JSON.stringify(arr));
  return _.every(arr, function(n, i, arr) {
    //console.log(JSON.stringify(n) + ":" + i);
    return i === 0 || n.value - arr[i - 1].value === 1 || (arr[0].value == 1 && (n.value == 11 || n.value == 12)); // checking for 'another catch'
  });
}

function checkPureSeq(arr, suit){
  var sCards = _.where(arr, {suit: suit}); // get all the cards of specific suit
  //console.log(sCards.length);
  if(sCards.length < 3 || sCards.length != arr.length) { // if same suit cards are less than 3 or the length doesn't match the original array length return false ... meaning you don't need to check if the cards are in sequence
    return false;
  }

  sCards = _.sortBy(sCards, 'value'); // sort all the cards of same suit based on 'value'
  return inSeq(sCards);
}

function log(str){ // just a util function. I'm displaying it on screen. You can put it on console
  //console.log(str);
  $('<div>').html(str).appendTo('#output');
}

$(function(){
  var arrs = [arr4, arr5, arr6, arr7, arr8]; // array of arrays, just for ease of processing, similar to the way you had created [arr1, arr2, arr3] in your question
  var suits = ['hearts', 'diams', 'spades', 'clubs'];
  _.each(arrs, function(arr, i){ // process each array
    //console.log(JSON.stringify(arr));
    _.each(suits, function(suit){ // for each suit
    //console.log(suit);
      if(checkPureSeq(arr, suit)){ 
        log('<h4>Found pure sequence of ' + suit + ': ' + JSON.stringify(arr) + '</h4>');
      } else {
        log('Pure sequence of ' + suit + ' not found: ' + JSON.stringify(arr));
        // you may push current `arr` to another array for further processing of case 2 and case 3
      }
    });
  });
});

可以找到工作演示here

注意:它只处理第一种情况(包括你已经定义的另一种情况)。