프로그래밍 언어/JQuery

가위바위보 자바스크립트로

벌게진눈 2016. 1. 28. 12:20
반응형

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>rock-paper-scissors</title>

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>

<script>

///// 가위 0 바위 1 보 2 

function computerChoice(){

var computerChoice = Math.floor(Math.random()*3)

return computerChoice;

}

function yourChoice(){

var yourChoice = $('#inputYourChoice').val();

if(yourChoice=='가위')

return 0;

else if(yourChoice=='바위')

return 1;

else if(yourChoice=='보')

return 2;

else

alert('가위, 바위, 보 셋중 하나를 입력하시오\n다시 입력하시오')

}

function checkwhowin(user, com){

//0 승리 1 패배 2 드로우

var userResult = 0;

if((user==1&&com==0) || (user==2&&com==1) || (user==0&&com==2))

return 0;

else if((user==2&&com==0) || (user==0&&com==1) || (user==1&&com==2))

return 1;

else if((user==0&&com==0) || (user==1&&com==1) || (user==2&&com==2))

return 2;

else

alert('승패 오류');

}

function showimage(user, rusult, com){

if(user==0)

$('#yourChoiceImg').attr('src', 'image/scissors.PNG');

else if(user==1)

$('#yourChoiceImg').attr('src', 'image/rock.PNG');

else if(user==2)

$('#yourChoiceImg').attr('src', 'image/paper.PNG');

if(rusult==0)

$('#resultImg').attr('src', 'image/win.PNG');

else if(rusult==1)

$('#resultImg').attr('src', 'image/lose.PNG');

else if(rusult==2)

$('#resultImg').attr('src', 'image/draw.PNG');

if(com==0)

$('#computerChoiceImg').attr('src', 'image/scissors.PNG');

else if(com==1)

$('#computerChoiceImg').attr('src', 'image/rock.PNG');

else if(com==2)

$('#computerChoiceImg').attr('src', 'image/paper.PNG');

}

function startgame(){

var comChoice = computerChoice();

var youChoice = yourChoice();

var result = checkwhowin(youChoice, comChoice);

showimage(youChoice, result, comChoice);

}

</script>

</head>

<body>

<form>

<input type="text"  value="가위 바위 보 입력하시오." id = "inputYourChoice" name=""/>

<input type="button"  value="도전" onclick="startgame()"/>

</form>

<img src = "image/dontknow.PNG" id = "yourChoiceImg"/>

<img src = "image/dontknow.PNG" id = "resultImg"/>

<img src = "image/dontknow.PNG" id = "computerChoiceImg"/>

</body>

</html>



012


반응형