๐ŸŽจ math game

kirin.logยท2020๋…„ 11์›” 22์ผ
2

https://kirin059.github.io/my_website/game.html

๐ŸŽ… html

 <form name="math">
    <table>
      <!-- caption์œผ๋กœ ์ œ๋ชฉ๋‹ฌ๊ธฐ (<th> ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ) -->
       <caption>๐Ÿ”ถ๐Ÿ”น Math Game ๐Ÿ”ธ๐Ÿ”ท</caption>
       <tr>
         <td colspan="2">   <!-- colspan์„ ๊ฐ€๋กœ 2์นธ์„ ํ•ฉ์นจ(๊ฐ€๋กœ ๋ณ‘ํ•ฉ) -->
         <input type="button" value="New Game" onClick="location.reload();" style="width:100%">
         </td>
       </tr>
-
       <tr>
         <td colspan="2">
         <input type="button" value="Game Start" size="5" onClick="startgame()" style="width:100%">
         </td>
       </tr>
     <!--text type์„ ํ†ตํ•ด ํƒ€์ด๋จธ ์„ค์ •(js์—ฐ๊ฒฐ)-->
       <tr>
         <td> <strong>rest of time</strong> </td>
         <td>
         <input type="text" name="timer" size="10">
         </td>
       </tr>
     <!--๋ฌธ์ œ ์ œ์‹œ (๋žœ๋ค) -->
       <tr>
         <td><strong>Question</strong></td>
         <td>
         <input type="text" name="firstnum" size="7">
         <input type="text" name="operator" size="2">  <!--์—ฐ์‚ฐ์ž(+,-,x,/) -->
         <input type="text" name="secondnum" size="7">
         </td>
       </tr>
      <!-- ๋‹ต ์ œ์ถœ -->
       <tr>
         <td><strong>Answer</strong></td>
         <td>
         <input type="text" name="answer" size="7">
         </td>
       </tr>
       <tr class="answer">
         <td colspan="2" style="text-align:center">
         <input type="button" value="Submit and Check Answer" onClick="answerit();">
         </td>
       </tr>
      <!--์ ์ˆ˜ ํ‘œ๊ธฐ -->
       <tr>
         <td><strong>Score</strong></td>
         <td>
         <input type="text" name="points" size="7" value="0">
         </td>
       </tr>
    </table>
</form>
๐ŸŽ… css

table{
    border:0; 
    width:500px; 
    height:300px; 
    margin:auto;
    font-weight: var( --weight-bold);
    font-size: 20px;
    color:var(--point-color);
    padding-top:30px;
}
caption{
    font-size:35px;
    text-align:center;
    color:whites;
    margin-top:30px;
    padding:5px;
    background-color:  #054d65;
    border-radius: 30px;
}
td {
    text-align:center;
    padding: 8px;
}
.answer input{
    width:100%; 
    background-color: aquamarine;
    font-weight: var( --weight-bold);
}
๐ŸŽ… javascript

"use strict";

/* number game */
var time=5; // ๊ฒŒ์ž„ ์‹œ๊ฐ„ 50์ดˆ
var started=0;  

/* timer */
function CountDown() {
    if(time>0) {
	document.math.timer.value=time;   
      //form submit ํ•  ๋•Œ ์„œ๋ฒ„์—์„œ name๋ช…์œผ๋กœ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.(id์ฒ˜๋Ÿผ ๊ธฐ๋Šฅ) (name=timer),
      //input์œผ๋กœ ๊ฐ’์„ ์ „๋‹ฌ(submit)ํ• ๋•Œ name์€ ์ „๋‹ฌ ๋ฐ›๋Š” ํŽ˜์ด์ง€ ์ž…์žฅ์—์„œ ๊ฐ’(value)์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋Š” ํ‚ค์ด๋‹ค. 
      //๋”ฐ๋ผ์„œ document.math.timer.value=time;์€ time(50์œผ๋กœ ์„ค์ •, time-1๋กœ ์„ค์ •)์˜ ๊ฐ’(value)์„, ๋ณธ๋ฌธ math์•ˆ์˜ name์ด timer์ธ ๊ณณ์œผ๋กœ ์ „๋‹ฌ
	time=time-1;    
      var gameTimer=setTimeout("CountDown()", 1000)
    }
    else if (time==0) {   
	document.math.timer.value="0";
	alert('Time Over'); // ์‹œ๊ฐ„์ด ๋๋‚˜๊ณ (time=0) ๋‚œ ํ›„ ๋‚˜ํƒ€๋‚˜๋Š” ๋ฉ”์„ธ์ง€
	document.math.firstnum.value="";   //time์ด 0์ด๋˜๋ฉด ๋ชจ๋“  ๊ฐ’์„ ์ดˆ๊ธฐํ™”, ๋นˆ๊ฐ’("")์œผ๋กœ ์„ค์ •
	document.math.operator.value="";
	document.math.secondnum.value="";
	document.math.answer.value="";
	}
}

/* game start */
function startgame() {
    if (started!=0) {    //var started=0;์œผ๋กœ ์„ค์ •ํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์—,  0์ด์•„๋‹Œ๊ฒŒ(started!=0) true๋ฉด alert๊ฐ€ ๋œฌ๋‹ค
        alert('์ด๋ฏธ ๊ฒŒ์ž„์„ ํ•˜์…จ์Šต๋‹ˆ๋‹ค.\n\n์ƒˆ๊ฒŒ์ž„ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด์‹œ๊ณ  ๋‹ค์‹œ ํ•ด์ฃผ์„ธ์š”.');
    }
    else {
        started=1;     //??์ด๊ฑธ ์ง€์šฐ๋ฉด if๋ฌธ ๋‚ด์˜ alert๊ฐ€ ๋œจ์ง€ ์•Š๋Š”๋‹ค. ์ด๊ฒƒ์˜ ์˜๋ฏธ๋Š” ๋ฌด์—‡์ผ๊นŒ
        CountDown();
        getProb();  //get problem (๋ฌธ์ œ๋ฅผ ์–ป๋‹ค) ์˜๋ฏธ์˜ ํ•จ์ˆ˜
    }
}

/* question */
function randnum(min,max) {
    var num=Math.round(Math.random()*(max-min))+min;
    return num;
}
    var choose, rightanswer

function getProb() {
    choose=randnum(1,4);  //
    if (choose=="1") {	
        document.math.operator.value="+";
            var choose1=randnum(0,50);
            var choose2=randnum(0,50);
                document.math.firstnum.value=choose1;
                document.math.secondnum.value=choose2;
            rightanswer=choose1 + choose2;
    }
    if (choose=="2") {
        document.math.operator.value="-";
        var choose2=randnum(0,50);
        var choose1=randnum(choose2,50);   //์‚ฌ์šฉ์ž๊ฐ€ ์ ๋Š” ์ •๋‹ต์ด ์ •์ˆ˜๋งŒ ๋‚˜์™€์•ผ ํ•˜๋‹ˆ๊น
            document.math.firstnum.value=choose1;
            document.math.secondnum.value=choose2;
        rightanswer=choose1 - choose2;
    }
    if (choose=="3") {
        document.math.operator.value="x";
        var choose1=randnum(0,10);
        var choose2=randnum(0,10);
            document.math.firstnum.value=choose1;
            document.math.secondnum.value=choose2;
        rightanswer=choose1 * choose2;
    }
    if (choose=="4"){
        document.math.operator.value="/";
        var choose2=randnum(1,10);
        var choose1=choose2 * randnum(0,10);   //์‚ฌ์šฉ์ž๊ฐ€ ์ ๋Š” ์ •๋‹ต์ด ์ •์ˆ˜๋งŒ ๋‚˜์™€์•ผ ํ•˜๋‹ˆ๊น
            document.math.firstnum.value=choose1;
            document.math.secondnum.value=choose2;
        rightanswer=choose1 / choose2;
    }
}

/* answer submit */    
function answerit() {
    if (started==0) {    //์ „์—ญ๋ณ€์ˆ˜ var started=0; ์ด๊ธฐ๋•Œ๋ฌธ์— true
        // ์‹œ์ž‘ํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ๋‹ต๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„๋•Œ
        alert("Click the 'Game Start' button");
    }
    else {   //Game Start๋ฒ„ํŠผ ๋ˆ„๋ฅธ ํ›„ answer๋ฅผ ์ œ์ถœํ•˜๋Š” ๊ฒฝ์šฐ
        if (time==0) {   //ํƒ€์ž„์˜ค๋ฒ„๋œ ์ผ€์ด์Šค
            alert('Time Over');
        }
        else {    //์‚ฌ์šฉ์ž๊ฐ€ submitํ•˜๋Š” ์ผ€์ด์Šค
            var theiranswer=eval(document.math.answer.value); //eval()ํ•จ์ˆ˜๋Š” ์ธ์ž๋กœ์„œ ๋ฌธ์ž์—ด์„ ๋ฐ›์•„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๊ฒฐ๊ณผ๊ฐ’์„ ๋Œ๋ ค์ค€๋‹ค
            var theirpoints=eval(document.math.points.value);
            if (theiranswer==null) {
            // ๊ฒŒ์ž„์ค‘ ๋‹ต๋ž€์— ๋‹ต์„ ์ ์ง€ ์•Š๊ณ  ๋‹ต๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„๋•Œ ๋‚˜ํƒ€๋‚˜๋Š” ๋ฉ”์„ธ์ง€
                alert("After type on your answer about this question, click on the 'answer' button");
                document.math.answer.select();  //select()ํ•จ์ˆ˜๋Š” input text๋‚˜ textarea ์š”์†Œ๋ฅผ ๋“œ๋ž˜๊ทธ ํ•œ ๋“ฏ์ด ์„ ํƒํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜ (์ฆ‰, answer text์ฐฝ์— ์“ฐ์—ฌ์ง„ ์ด์ „ ๋ฌธ์ œ์˜ ๋‹ต์ด ๋“œ๋ž˜๊ทธํ•œ ๋“ฏ ์ปค์„œ๊ฐ€ ์„ ํƒ๋œ ๋ชจ์Šต)
          
            }
            else {   //์‚ฌ์šฉ์ž๊ฐ€ ๋‹ต์„ ์ ์—ˆ์„ ์ผ€์ด์Šค
               if (theiranswer==rightanswer) {
               // ๋ฌธ์ œ์˜ ๋‹ต์ด ๋งž์•˜์„๊ฒฝ์šฐ ๋‚˜ํƒ€๋‚˜๋Š” ๋ฉ”์„ธ์ง€
                   alert('Right Answer!');
                   theirpoints++;
                   document.math.points.value=theirpoints;
                   }
                else {
                // ๋ฌธ์ œ์˜ ๋‹ต์ด ํ‹€๋ ธ์„ ๊ฒฝ์šฐ ๋‚˜ํƒ€๋‚˜๋Š” ๋ฉ”์„ธ์ง€
                // theiranswer๋Š” ํ‹€๋ฆฐ๋‹ต์„ ๋‚˜ํƒ€๋‚ด๊ณ  rightanswer๋Š” ์ •ํ™•ํ•œ ๋‹ต์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
                    alert(theiranswer+ " is wrong answer.\n\nThe right answer is "+rightanswer+".")
                    }           // ๋ฐฑ์Šฌ๋ž˜์‰ฌ(\) + n ์€ ์ค„๋ฐ”๊ฟˆ
                    document.math.answer.select();  //์ด์ „ ๋ฌธ์ œ์— ์“ด ๋‹ต์€ ๋“œ๋ž˜๊ทธ ์ปค์„œ ์ฒ˜๋ฆฌ
                    getProb();  //์ƒˆ๋กœ์šด ๋ฌธ์ œ ์–ป๋Š”๋‹ค
            }
        }
    }
}

โ— .select() ๋ฉ”์†Œ๋“œ(ํ•จ์ˆ˜)๋ฅผ ์ ์šฉํ•˜๋ฉด input text๋‚˜ textarea ์š”์†Œ๋ฅผ ๋“œ๋ž˜๊ทธ ํ•œ ๋“ฏ์ด ์„ ํƒ๋œ๋‹ค.

profile
boma91@gmail.com

0๊ฐœ์˜ ๋Œ“๊ธ€