'이것도 연습'에 해당되는 글 1건

  1. 2011.04.16 발코드 놀이.
제2 외국어 영역2011.04.16 10:31

$(document).ready(function(){
	var amIworking = false,
	    mainCfg ={
			cardNubmers : [1,2,3,4,5,6,7,8],
			widthBoxCount : 4,
			heightBoxCount : 4,
			eachBoxWidth : 84,
			eachBoxHeight : 104
		},
		firstFaceUpCardNumber = -1,
		faceUpCardIdxs = [],
		totalBoxCount = (mainCfg.widthBoxCount*mainCfg.heightBoxCount),
		blackBox = {},
		triedCount = 0,
		totalScore = 0,
		initGame = function(){
			var cardSets = arraySuffle(mainCfg.cardNubmers);
			cardSets = $.merge(cardSets,arraySuffle(cardSets.slice()));
			
			$('#card-part li').each(function(idx,item){
				$(item).css({'left': (Math.floor(idx % mainCfg.widthBoxCount) * mainCfg.eachBoxWidth),
						     'top' : (Math.floor(idx / mainCfg.heightBoxCount) * mainCfg.eachBoxHeight)
						    });	
			});
			
			for(var i = 0; i < totalBoxCount;i++){
				blackBox[i] = {};
				blackBox[i].cardNubmer = cardSets[i];
				blackBox[i].isRemove = false;
			}
			faceUpCardIdxs = [];
			amIworking = false;
			firstFaceUpCardNumber = -1;
			triedCount = 0;
			totalScore = 0;
			resetCardFaceDown();
			$('#entry-box').focus();
		},
		movingBox = function(currentIdx, direction){
			if(currentIdx === undefined){
				return false;
			}
			currentIdx = parseInt(currentIdx,10);
			var nextIdx = 0,
			    leftLimtIdx = 0,
				lastIdx = totalBoxCount-1;
			
			switch (direction) {
				case 37: //left
					nextIdx = currentIdx-1;
					if(leftLimtIdx > nextIdx){
						nextIdx = lastIdx;	
					}
					break;
				case 38: //up
					if(currentIdx === leftLimtIdx){
						nextIdx = lastIdx;
					}else{
						nextIdx = currentIdx-mainCfg.heightBoxCount;
						if(0 > nextIdx){
							nextIdx = lastIdx + nextIdx;
						}						
					}
					break;
				case 39: //right
					nextIdx = currentIdx+1;
					if(lastIdx < nextIdx){
						nextIdx = leftLimtIdx;	
					}
					break;
				case 40: //down
					if(currentIdx === lastIdx){
						nextIdx = leftLimtIdx;
					}else{
						nextIdx = currentIdx+mainCfg.heightBoxCount;
						if(lastIdx < nextIdx){
							nextIdx = nextIdx - lastIdx;
						}
					}
					break;
			}
			if(blackBox[nextIdx].isRemove){
				movingBox(nextIdx,direction);
			}else{
				seleteBox(nextIdx);
			}
		},
		
		toggleCard = function(){
			var selectedCard = $('#card-part li.selected'),
			    idx = parseInt(selectedCard.attr('data-idx'),10),
			    secondFaceUpCardNumber;
			if(faceUpCardIdxs.length > 0){
				if($.inArray(idx,faceUpCardIdxs) === 0){
					amIworking = false;
					return false;
				}else{
					triedCount++;
					secondFaceUpCardNumber = blackBox[selectedCard.attr('data-idx')].cardNubmer;
					selectedCard.attr('data-num',secondFaceUpCardNumber);
					if(firstFaceUpCardNumber === secondFaceUpCardNumber){
						blackBox[faceUpCardIdxs[0]].isRemove = true;
						blackBox[idx].isRemove = true;
						$('#card-part li[data-idx="'+faceUpCardIdxs[0]+'"]').remove();
						$('#card-part li[data-idx="'+idx+'"]').remove();
						faceUpCardIdxs = [];
						totalScore++;
						amIfinished(idx);
						amIworking = false;
					}else{
						if(totalScore > 0){
							totalScore--; 
						}
						setTimeout(function(){
							resetCardFaceDown(faceUpCardIdxs[0]);
							resetCardFaceDown(idx);
							faceUpCardIdxs = [];
							amIworking = false;
						},500);
					}
				}
			}else{
				firstFaceUpCardNumber = blackBox[selectedCard.attr('data-idx')].cardNubmer;
				selectedCard.attr('data-num',firstFaceUpCardNumber);
				faceUpCardIdxs[0] = idx;
				amIworking = false; 
			}
		},
		
		arraySuffle = function(arr){
	      var s = [];
	      while (arr.length){
	        s.push(arr.splice(Math.random() * arr.length, 1));
	      }
	      while (s.length){
	        arr.push(s.pop()[0]);
	      }
	      return arr;
	    },
		
		resetCardFaceDown = function(idx){
			if(idx !== undefined){
				$('#card-part li[data-idx="'+idx+'"]').attr('data-num',0);
			}else{
				for(var i = 0 ; i < totalBoxCount ; i++){
					$('#card-part li[data-idx="'+i+'"]').attr('data-num',0);
				}
				seleteBox(0);
			}
		},
		amIfinished = function(currentIdx){
			var firstAliveBoxIdx;
			$.each(blackBox,function(idx,item){
				if(!item.isRemove){
				  if(firstAliveBoxIdx === undefined || currentIdx < idx){
						firstAliveBoxIdx = idx;
						if(currentIdx < idx ){
							return false;
						}
					}
				}
			});
			if(firstAliveBoxIdx === undefined){
				alert('triedCount ==>'+triedCount+'\n totalScore ==>'+totalScore);
				initGame();
			}else{
				seleteBox(firstAliveBoxIdx);
			}
		},
		seleteBox = function(idx){
			$('#card-part li.selected').removeClass('selected');
			$('#card-part li[data-idx="'+idx+'"]').addClass('selected');
		};
	
	$('#entry-box').keydown(function(e){
		if(e.keyCode === 37|| 
		   e.keyCode === 38||
		   e.keyCode === 39||
		   e.keyCode === 40 
		  ){
		  	if(amIworking){
				return false;
			}
			e.preventDefault();
			amIworking = true;
			movingBox($('#card-part li.selected').attr('data-idx'),e.keyCode);
			amIworking = false;
		}else if(e.keyCode === 13){
			if(amIworking){
				return false;
			}
			e.preventDefault();
			amIworking = true;
			toggleCard();
		}
	});
	
	initGame();
});
생각없이 발로 만든 코드.. ;; 머 하는 녀석인가?;
Posted by is윤군

댓글을 달아 주세요