U E D R , A S I H C RSS

Java Script/2011년스터디/URL Hunter

URLHunter를 만들어보자!!

정진경


http://joojis.net/urlhunter.php
<!doctype html>
<html>
	<head>
		<meta charset="utf-8"/>
		<script>
			var maintimer=null;
			var mop=new Array(40);
			var hunter=20;
			var time=0
			var str;

			function redraw(){
				str="|||";
				for(i=1; i<=40; i++){
					if(mop[i]==1 || hunter==i){
						if(mop[i]==1 && hunter==i){
							str=str+'@';
						}else if(mop[i]==1){
							str=str+'a';
						}else{	 // hunter==i
							str=str+'O';
						}
					}else{
						str=str+'-';
					}
				}
				location.href="./urlhunter.php#"+str+"||| time:"+(30-time);
			}
			function spawn(){
				var temp=Math.floor(Math.random()*40)+1
				
				if(mop[temp]!=0 && hunter!=temp){
					mop[temp]=1;
				}
			}

			function gametimer(){
				var temp=Math.floor(Math.random()*10)+1;
				
				if(temp<9){
					spawn();
				}

				time++;
 				if(time<31){
					redraw();
				}else{
					location.href="./urlhunter.php#Game Over";
					pause();
				}
			}
			function init(){
				if(maintimer==null){
					maintimer=setInterval("gametimer();", 1000);
				}else{
					alert("이미 실행중입니다.");
				}
				redraw();
			}
			
			function pause(){
				clearInterval(maintimer);
				maintimer=null;
			}
			function keyevent(){
				switch(event.keyCode){
					case 37:	// Left
						if(hunter>1){
							hunter--;
						}else if(hunter==1){
							hunter=40;
						}
						break;
					case 39:	// Right
						if(hunter<40){
							hunter++;
						}else if(hunter==40){
							hunter=1;
						}
						break;
					case 32:	// Space Bar
						if(mop[hunter]==1){
							mop[hunter]=0;
						}
						break;
				}

				if(time<31){
					redraw();
				}
			}
		</script>
	</head>
	<body onload="init();" onkeydown="keyevent();">
		<input type="button" value="pause" onclick="pause();"/>
		<br/>
		Hello, PHP On CentOS6!	</body>
</html>

박정근

  • The Hunter.html

<heml>
    <head>
        <title>The Hunter</title>
        <script language ="Javascript" src ="URLHunterV1.js"></script>
    </head>
    <body onload ="replash()">
	You should kill all the monsters.<br>
	Your gun point is 'O' and Others are Monsters.
	
    </body>
</html>

  • URLHunterV1.js

MapLength = 50;

var replash = function(){
    setInter = setInterval("URLChange();",100);
}
var CrtURL = (document.URL.indexOf('#') == -1)? document.URL+'#': document.URL.slice(0,document.URL.indexOf('#')+1);
var URLChange = function(){
    if(map.peace){
	clearInterval(setInter);
	location.href = CrtURL + "  You caught \'" + map.r.join('') + "\'";
	alert("Press F5 to play again");
    }else{
	timer.templus();
	map.makeS();
        location.href = CrtURL + "  "+timer.t +"|" + map.s.join('') + "|"+timer.t + "  You caught \'" + map.r.join('') + "\'";
    }
}
document.onkeydown = KeyInput;
function KeyInput(e){
    var code = (window.event)? window.event.keyCode: e.which;
    if(code == 39)  map.H.right();
    else if (code == 37) map.H.left();
    else if (code == 32) map.kill();
    
}

function Timer(g){
    this.t = g;
    this.temp = 0;
    this.templus = function(){
	this.temp++;
	if(this.temp%10 == 0)
	    this.t--;
	if(this.t <= 0)
	    map.peace = true;
    }
}
var timer = new Timer(30);

function Creature(){
    this.right = function(){
	this.where = (this.where+1)%MapLength;
    }
    this.left = function(){ 
	this.where = (this.where-1<0)? (MapLength-1): this.where-1;
    }
}
function Monster(w){
    this.where = w;
    this.alive = true;
    this.randomMove = function(){
	if (this.alive){
	    var t = (Math.floor(Math.random()*100)%3);
	    if(t==0)
		this.right();
	    else if(t==1)
		this.left();
	}else{
	    this.where = -1;
	}
    }
}
function Hunter(w){
    this.where = w;
}

Monster.prototype = new Creature();
Hunter.prototype = new Creature();
function Map(){
    this.peace = false;
    this.a1 = new Monster(Math.floor(Math.random()*MapLength));
    this.a2 = new Monster(Math.floor(Math.random()*MapLength));
    this.a3 = new Monster(Math.floor(Math.random()*MapLength));
    this.a4 = new Monster(Math.floor(Math.random()*MapLength));
    this.a5 = new Monster(Math.floor(Math.random()*MapLength));
    this.H = new Hunter(Math.floor(Math.random()*MapLength));
    this.r = new Array(5);
    this.s = new Array(MapLength);
    this.makeS = function(){
	if((this.a1.where == -1)&&(this.a2.where == -1)&&(this.a3.where == -1)&&(this.a4.where == -1)&&(this.a5.where == -1))  this.peace = true;
	
	this.a1.randomMove();
	this.a2.randomMove();
	this.a3.randomMove();
	this.a4.randomMove();
	this.a5.randomMove();
	
	if (this.a1.alive)	this.s[this.a1.where] = 'l';
	if (this.a2.alive)	this.s[this.a2.where] = 'i';
	if (this.a3.alive)	this.s[this.a3.where] = 'n';
	if (this.a4.alive)	this.s[this.a4.where] = 'u';
	if (this.a5.alive)	this.s[this.a5.where] = 's';

        if(this.a1.alive)	this.r[0] = '_';	else	this.r[0] = 'l';
	if(this.a2.alive)	this.r[1] = '_';	else	this.r[1] = 'i';
	if(this.a3.alive)	this.r[2] = '_';	else	this.r[2] = 'n';
	if(this.a4.alive)	this.r[3] = '_';	else	this.r[3] = 'u';
	if(this.a5.alive)	this.r[4] = '_';	else	this.r[4] = 's';

	if(this.H.where==this.a1.where)
	    this.s[this.H.where] = 'ⓛ';
	else if(this.H.where==this.a2.where)
	    this.s[this.H.where] = 'ⓘ';
	else if(this.H.where==this.a3.where)
	    this.s[this.H.where] = 'ⓝ';
	else if(this.H.where==this.a4.where)
	    this.s[this.H.where] = 'ⓤ';
	else if(this.H.where==this.a5.where)
	    this.s[this.H.where] = 'ⓢ';
	else
	    this.s[this.H.where] = 'O';
	
	var i=0;
	for(i = 0; i<MapLength;i++){
	    if((i!=this.a1.where)&&(i!=this.a2.where)&&(i!=this.a3.where)&&(i!=this.a4.where)&&(i!=this.a5.where)&&(i!=this.H.where))
		this.s[i] = '-';
	}
    }
    this.deada1 = function(){	this.a1.alive = false;    }
    this.deada2 = function(){	this.a2.alive = false;    }
    this.deada3 = function(){	this.a3.alive = false;    }
    this.deada4 = function(){	this.a4.alive = false;    }
    this.deada5 = function(){	this.a5.alive = false;    }
    this.kill = function(){
	if (this.H.where == this.a1.where)  this.deada1();
	else if(this.H.where == this.a2.where)  this.deada2();
	else if(this.H.where == this.a3.where)  this.deada3();
	else if(this.H.where == this.a4.where)  this.deada4();
	else if(this.H.where == this.a5.where)  this.deada5();
    }
}
var map = new Map()
map.makeS();

김수경

  • url-hunter.html

<html>
    <head>
        <title>URL Hunter</title>
        <script type="text/javascript" src="url-hunter.js"></script>
    </head>
    <body>
    </body>
</html>
  • url-hunter.js

var state = new Array();
var length = 50;
var time = 30;
var monsters = new Array();
var player;
var url;
var timer;

function init(){
    //initialize map
    for(var i = 0; i < length; i++){
    state[i] = 0;
    }
    
    //create monsters
    for(var i = 0; i < 4; i++){
        monsters[i] = new Character((i*20)%length);
    }
    
    //create Player
    player = new Character(length/2);
    
    //getURL
    url = location.href;
    
    //Initialize timer
    timer = setInterval("next()", 1000);
}

//Refactoring 필요
function next(){
    for(var i = 0; i < )
    
    time--;
    draw();
}

function toString(){
    var map = "";
    //lenght 고칠것
    for(var i = 0; i < length; i++){
        switch(state[i]){
            case 0:
                map += "-";
                break;
            case 1:
                map += "a";
                break;
            case 2:
                map += "O";
                break;
            case 3:
                map += "@";
                break;   
        }
    }
    return map;
}

function draw(){
    location.href= url + "#" + time + "|" + toString() + "|" + time;
}

function Character(index){
    this.index = index;
}

Character.prototype.move = function(){
    if(Math.random() > 0.5){
        this.goLeft();
    }else{
        this.goRight();
    }
}

Character.prototype.goLeft = function(){
    this.index = (this.index == 0) ? length - 1 : this.index - 1;
}

Character.prototype.goRight = function(){
    this.index = (this.index + 1) % length;
}
  • url-hunter.css

아직 없음

김광순

정의정

김태진

<html>
    <head>
        <script>
            var arr=new Array(40);
            var hunter=20;
            

            
            
            function firprint()
            {
                var i=0;

                while(i<40)
                {
                    arr[i]='-';
                    i++;
                }

                makea();
                arr[hunter]='O';
                continuePrint();
            }
            
            function continuePrint()
            {
                
                location.href="./URLHunter.html#|||"+arr.join('')+"|||time:";
                movea();
            }
            
            function makea()
            {
                var a;
                for(i=0;i<10;i++)
                {
                    a=Math.floor(Math.random()*40);
                    if(arr[a]=='-') arr[a]='a';
                }
            }
            function keyboard(){

				switch(event.keyCode){
					case 37:	// Left
                    if(hunter>0){
                        hunter--;
                        if(arr[hunter]=='a') arr[hunter]='@';
                        else arr[hunter]='O';
                        
                        if(arr[hunter+1]=='@') arr[hunter+1]='a';
                        else arr[hunter+1]='-';
                    }
                    continuePrint();
                    break;
					case 39:	// Right
                    if(hunter<40){

                        hunter++;
                        if(arr[hunter]=='a') arr[hunter]='@';
                        else arr[hunter]='O';
                        
                        if(arr[hunter-1]=='@') arr[hunter-1]='a';
                        else arr[hunter-1]='-';
                        
                    }
                    continuePrint();
                    break;
					case 32:	// Space Bar
                    if(arr[hunter]=='@'){
                        arr[hunter]='O';
                    }
                    continuePrint();
                    break;
                }
            }
            function movea(){
                var temp;
                i=0;
                while(i<40)
                {
                    if(arr[0]=='a') arr[1]=='a';
                    if(arr[40]=='a') arr[39]=='a';
                    temp=Math.floor(Math.random()*3)-1;
                    if(arr[i]=='a') {
                        if(arr[i-1]!='O'||arr[i]!='O'||arr[i+1]!='O') {
                            arr[i]='-'; arr[temp+i]='a';
                        }
                    }
                    i++;
                }
            }
        
        </script>
    </head>
    <body onload="firprint();" onkeydown="keyboard();">
        <input type="button" value="pause" onclick="pause();"/>
        <br/>
        !!! 완성했다!	</body>
</html>
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:30
Processing time 0.0235 sec