当前位置:编程学习 > JS >>

前端js也能写算法

[html] 
<!doctype html> 
<html> 
<head> 
<title>html5 迷宫算法</title> 
<style> 
body{background-color:green;} 
#map{marign:100px auto;width:330px;font-family:"微软雅黑";font-size:12px;} 
#map:after{content:"";clear:both;} 
#map > div{width:60px;line-height:60px;height:60px;margin:2px 0 0 2px;background-color:#ccc;float:left;text-align:center;color:white;font-weight:bold;} 
#map > div.box8{background-color:green;} 
#map > div.box5{background-color:red;} 
#map > div.box1{background-color:#f93;} 
#map > div.step{background-color:green;} 
#map > div.null{background-color:white;} 
</style> 
</head> 
<body> 
<div id="map"> 
</div> 
<script> 
Array.prototype.clone=function(){  
    return [].concat(this);  
}  
var game = function(element,options){ 
    options = options || {number:9}; 
    this.element = this.get(element); 
    this.width = Math.pow(options.number,0.5); 
    this.timer = 1000; 
    this.crossrate = 0.9;//杂交率 
    this.mutationrate = 0.1 //变异率 
    this.veclen =400;//基因组的长度 
    this.zero = null; 
    this.count = 0; 
    this.good = null; 
    this.inerterval = null; 
    this.finished = false; 
    this.map = (function(){   
                    // 随即生成1到100的打乱的数组,这个算法是跟JK学习的,也算是一种洗牌算法,感觉不错,谢谢JK   
                    var i,   
                        len = 9,   
                        oldsource = [1,2,3,4,5,6,7,8,0];   
                    var cpos = 8,tmp;  
                    for(i = 0 ; i < 1000 ; i++){ 
                        var random = Math.floor(Math.random()*5); 
                        if(random == 0){//上 
                            if(cpos < 3){ 
                                continue; 
                            }else{ 
                                tmp = oldsource[cpos]; 
                                oldsource[cpos] = oldsource[cpos-3]; 
                                oldsource[cpos-3] = tmp; 
                                cpos = cpos -3; 
                            } 
                        }else if(random == 1){ 
                            if(cpos >=6){ 
                                continue; 
                            }else{ 
                                tmp = oldsource[cpos]; 
                                oldsource[cpos] = oldsource[cpos+3]; 
                                oldsource[cpos+3] = tmp; 
                                cpos = cpos +3; 
                            } 
                        }else if(random == 2){ 
                            if(cpos % 3 ==0){ 
                                continue; 
                            }else{ 
                                tmp = oldsource[cpos]; 
                            &n
补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,