vue2.0 초간소화 코드로 퍼즐 맞추기

4642 단어
초간소화 코드로 이루어진 퍼즐 게임의 직접 코드






export default {
    data(){return{
        arr:[],
        isAcive:false,
        txt:'    ',
    }},

    methods:{
        pause(){
            this.isAcive=!this.isAcive;
            if(this.isAcive){
                this.txt='    '
            }else{
                this.txt="    "
            }
        },

        //       
        is_finish(){
            let bool=this.arr.every((item,i)=>{
                return item==i+1  //       
            })
            if(bool){
                this.isAcive=true;
                this.$refs.item[15].style.opacity=1;
            }
        },

        //     
        change_img(item,index,e){
            let reg=/\d+$/
            
            let blank_left=this.$refs.item[15].offsetLeft;
            let blank_top=this.$refs.item[15].offsetTop;

            let left=e.target.offsetLeft;
            let top=e.target.offsetTop;

            let div_left=Math.abs(blank_left-left);
            let div_top=Math.abs(blank_top-top);
            
            if( (div_left==100&&blank_top==top) || (div_top==100&&blank_left==left) ){
                this.$set(this.arr,index,this.arr[15])
                this.$set(this.arr,15,item)
            }

            this.is_finish()
        },

        //     
        play(){
            this.isAcive=false;
            this.class_num();
        },
 
        //       
        class_num(){
            this.arr.splice(0);//    
            for (let i = 0; i <15; i++) {
                this.arr.push(i+1);
            }
            while(1) {
                this.arr.sort(()=>0.5-Math.random())//    
                if (this.check_class_num(this.arr)) {
                    break;
                }
            }
            //            
            this.arr.push(16)
        },

        //          
        check_class_num(arr) {
            let count = 0;
            for (let i = 0; i < 15; i++) {
                for (let j = i+1; j < 15; j++) {
                    if (arr[j] < arr[i]) {
                        count++;
                    }
                }
            }
            return count%2===0;
        }
     
    },

    mounted(){
        this.class_num();
    }
}


좋은 웹페이지 즐겨찾기