.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Code Snippets >> Javascript >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Master Mind using Javascrip

Posted By:Kemal AL GAZZAH       Posted Date: June 01, 2016    Points: 40    Category: Javascript    URL: http://www.dotnetspark.com  

Here the version V1.0 of Master Mind game using Javascript
 

<html>
<head>
<title>Master Mind</title>
<style>
.ref
{
color:white;
}

</style>


<script>

/*
Master Mind Version 1.0
using javascript
By Kamel Gazzah
kamelgazzah@gmail.com
*/




var level=9;
var ends=0;


function setlevel()
{
/*var level=0;*/
if (document.getElementById("rad5").checked) {level=5};
if (document.getElementById("rad6").checked) {level=6};
if (document.getElementById("rad7").checked) {level=7};
if (document.getElementById("rad8").checked) {level=8};
if (document.getElementById("rad9").checked) {level=9};
if(level >0)
{
document.getElementById("slevel").innerHTML=level;
document.getElementById("blevel").style.visibility = "hidden";
var i;
for (i=5;i<10;i++)
{
document.getElementById("rad"+i).style.visibility = "hidden";
document.getElementById("slevel"+i).style.visibility = "hidden";
}
document.getElementById("sellevel").style.visibility = "hidden";
document.getElementById("mylevel").style.visibility = "visible";


firstrow();
}


}




function firstrow()
{
fill();
var h=1*document.getElementById("hc").value;if(h==0){addrow();}
}

function score(id)
{
var mp=0;
var i=0;
var j=0;
var id0="";
var ctr;

var scorebp=0;
for (i=1;i<6;i++)
{
id0="id"+i+id;
id0=id0.replace(/ /g,"");


if (typeof document.getElementById(id0) != 'undefined') 
{

if(1*document.getElementById(id0).value==1*document.getElementById('r'+i).innerHTML)
{scorebp = scorebp +1;}
}
}
document.getElementById('bpl'+id).value=scorebp ;






var scoremp=0;

for(i=1;i<6;i++)
{
for(j=1;j<6;j++)
{
if(1*document.getElementById("id"+i+id).value==1*document.getElementById('r'+j).innerHTML && i!=j  )
{scoremp = scoremp +1;}
                }


}

document.getElementById('mpl'+id).value=scoremp ;


}


function itexists(v)
{
var i=1;
var exist=0;
for(i=1;i<6;i++)
{
if(v==document.getElementById('r'+i).innerHTML) {exist=1;};
}
return exist;
}


function fill()
{
var i=1;
var id="";
var r=0;
var j=0;
var exist=1;


for(i=1;i<6;i++)
{
id="r"+i;
document.getElementById(id).innerHTML=rundomn(level);
}
}

function rundomn(a)
{
var n=0;
var exist=0;

while (n==0 || exist ==1)
{
n=Math.floor((Math.random() * 10) + 1);
/*n=n&6;*/

if(n> a){n=n-a};

exist=itexists(n);
/*alert(n);*/
}

return n;
}


function removerow(ctr)
{
var table = document.getElementById("myTable");
alert(ctr.id);
var h=1*document.getElementById("hc").value;
alert(h);
alert(h-1*ctr.id);
table.deleteRow(h-1*ctr.id);
}




function addrow()
{

var h0=document.getElementById("hc").value;








var ok=0;
var ok2=1;
if(h0==0) {ok=1};


if (h0>0)
{

if(isNaN(document.getElementById('id1'+h0).value) || document.getElementById('id1'+h0).value=='') {ok=0;ok2=0};
if(isNaN(document.getElementById('id2'+h0).value) || document.getElementById('id2'+h0).value=='') {ok=0;ok2=0};
if(isNaN(document.getElementById('id3'+h0).value) || document.getElementById('id3'+h0).value=='') {ok=0;ok2=0};
if(isNaN(document.getElementById('id4'+h0).value) || document.getElementById('id4'+h0).value=='') {ok=0;ok2=0};
if(isNaN(document.getElementById('id5'+h0).value) || document.getElementById('id5'+h0).value=='') {ok=0;ok2=0};

if(ok2 ==1){score(h0);};


var id='bpl'+h0;
if(document.getElementById(id).value !=5) {ok=1};
if(document.getElementById(id).value ==5) {ok=0};
if(ok2==0){ok=0};


if(document.getElementById(id).value ==5 && ends==0) 
{
var table = document.getElementById("tabmessage");    
var row = table.insertRow(0);  
var cell1 = row.insertCell(0);
cell1.innerHTML="Bravo !";
ends=1;
}


}

if(ok==1)  

{





    var h=1*document.getElementById("hc").value;
    var id1="id1" +h ;
    var id2="id2" +h ;  
    var id3="id3" +h ;
    var id4="id4" +h ;
    var id5="id5" +h ;
    
    
/*    
try
{
    if(typeof document.getElementById(id1) != 'undefined'){var v1=document.getElementById(id1).value;};
    if(typeof document.getElementById(id2) != 'undefined'){var v2=document.getElementById(id2).value;};
    if(typeof document.getElementById(id3) != 'undefined'){var v3=document.getElementById(id3).value;};
    if(typeof document.getElementById(id4) != 'undefined'){var v4=document.getElementById(id4).value;}:
    if(typeof document.getElementById(id5) != 'undefined'){var v5=document.getElementById(id5).value;}; 
}   
catch(err){alert(err.message);}

*/


   
    h=h+1;
    var table = document.getElementById("myTable");    
    var row = table.insertRow(h-1);    
    document.getElementById("hc").value=h;

    

    var element = document.createElement("input");   
    element.setAttribute("type", "text"); 
    element.setAttribute("value", h);
    element.setAttribute("id", "id0"+h);
    element.setAttribute("size", "1");
    var cell1 = row.insertCell(0);  
    cell1.appendChild(element);
    cell1.setAttribute("width", "10%");



    element = document.createElement("input");  
    element.setAttribute("type", "text");
    element.setAttribute("id", "id1"+h);
    element.setAttribute("size", "1");
    element.setAttribute("value", "");
    var cell2 = row.insertCell(1);
    cell2.appendChild(element);
    cell2.setAttribute("width", "11.25%");
 

    element = document.createElement("input");
    element.setAttribute("type", "text");
    element.setAttribute("id", "id2"+h);
    element.setAttribute("size", "1");
    element.setAttribute("value", "");
    var cell3 = row.insertCell(2);
    cell3.appendChild(element);
    cell3.setAttribute("width", "11.25%");

    element = document.createElement("input");
    element.setAttribute("type", "text");
    element.setAttribute("id", "id3"+h);
    element.setAttribute("size", "1");
    element.setAttribute("value", "");
    var cell4 = row.insertCell(3);
    cell4.appendChild(element);
    cell4.setAttribute("width", "11.25%");

    element = document.createElement("input");
    element.setAttribute("type", "text");
    element.setAttribute("id", "id4"+h);
    element.setAttribute("size", "1");
    element.setAttribute("value", "");  
    var cell5 = row.insertCell(4);
    cell5.appendChild(element);
    cell5.setAttribute("width", "11.25%");

    element = document.createElement("input");
    element.setAttribute("type", "text");
    element.setAttribute("id", "id5"+h);
    element.setAttribute("size", "1");
    element.setAttribute("value", "");
    var cell6 = row.insertCell(5);
    cell6.appendChild(element);
    cell6.setAttribute("width", "11.25%");

    

   
   

    element = document.createElement("input");
    element.setAttribute("type", "button");
    element.setAttribute("value", "Try");
    element.setAttribute("onclick", "addrow()");
    element.setAttribute("id", "b"+h);
    element.setAttribute("size", "1");
    var cell8 = row.insertCell(6);
    cell8.appendChild(element);
    cell8.setAttribute("width", "11.25%");


    element = document.createElement("input");
    element.setAttribute("type", "text"); 
    element.setAttribute("value", "");
    element.setAttribute("id", "bpl"+h);
    element.setAttribute("size", "1");
    var cell9 = row.insertCell(7);
    cell9.appendChild(element);
    cell9.setAttribute("width", "11.25%");


    element = document.createElement("input");
    element.setAttribute("type", "text"); 
    element.setAttribute("value", "");
    element.setAttribute("id", "mpl"+h);
    element.setAttribute("size", "1");
    var cell10 = row.insertCell(8);
    cell10.appendChild(element);
    cell10.setAttribute("width", "11.25%");
 
}

}
</script>
</head>
<body>

<table width="30%">
<tr>
<td width="70%">
<h2>Master Mind using Javascript by Kamel Gazzah</h2>
</td>
<td width="20%"><span id="mylevel" style="visibility:hidden;">Level</span></td><td><span id="slevel"></span></td>
</tr>
</table>

<table width="30%">
<tr><td><span id="sellevel">Level: Number from 1 to </span></td>




<td>
<form id="f1">
<input id="rad5" type="radio" value="5" name="level"><span id="slevel5">5</span>
<input id="rad6" type="radio" value="6" name="level"><span id="slevel6">6</span>
<input id="rad7" type="radio" value="7" name="level"><span id="slevel7">7</span>
<input id="rad8" type="radio" value="8" name="level"><span id="slevel8">8</span>
<input id="rad9" type="radio" value="9" name="level"><span id="slevel9">9</span>
</form>
</td>
<td><input id="blevel" type="button" value="Ok" onclick="setlevel()"></td>
</tr>
</table>


<input type="hidden" id="hc" value="0">
<!--<input type="button" value="add" onclick="addrow()" />-->

<table width="30%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="10%"><span id="r0"></span></td>
<td width="11.25%" ><span id="r1"class="ref"></span></td>
<td width="11.25%"><span id="r2" class="ref"></span></td>
<td width="11.25%"><span id="r3" class="ref"></span></td>
<td width="11.25%"><span id="r4" class="ref"></span></td>
<td width="11.25%"><span id="r5" class="ref"></span></td>
<td width="11.25%"><span id="r6" class="ref"></span></td>
<td width="11.25%"><span id="r7" class="ref"></span></td>
<td width="11.25%"><span id="r8" class="ref"></span></td>
</tr>
</table>



<table id="myTable" cellspacing="0" cellpadding="0" width="30%" border="1">
</table>
<br>
<table id="tabmessage" width="30%">

</table>

<object type="text/html" >
<p>
<html>
<head>
<title>Master Mind</title>
<style>
.ref
{
color:white;
}

</style>


<script>

/*
Master Mind Version 1.0
using javascript
By Kamel Gazzah
kamelgazzah@gmail.com
*/




var level=9;
var ends=0;


function setlevel()
{
/*var level=0;*/
if (document.getElementById("rad5").checked) {level=5};
if (document.getElementById("rad6").checked) {level=6};
if (document.getElementById("rad7").checked) {level=7};
if (document.getElementById("rad8").checked) {level=8};
if (document.getElementById("rad9").checked) {level=9};
if(level >0)
{
document.getElementById("slevel").innerHTML=level;
document.getElementById("blevel").style.visibility = "hidden";
var i;
for (i=5;i<10;i++)
{
document.getElementById("rad"+i).style.visibility = "hidden";
document.getElementById("slevel"+i).style.visibility = "hidden";
}
document.getElementById("sellevel").style.visibility = "hidden";
document.getElementById("mylevel").style.visibility = "visible";


firstrow();
}


}




function firstrow()
{
fill();
var h=1*document.getElementById("hc").value;if(h==0){addrow();}
}

function score(id)
{
var mp=0;
var i=0;
var j=0;
var id0="";
var ctr;

var scorebp=0;
for (i=1;i<6;i++)
{
id0="id"+i+id;
id0=id0.replace(/ /g,"");


if (typeof document.getElementById(id0) != 'undefined') 
{

if(1*document.getElementById(id0).value==1*document.getElementById('r'+i).innerHTML)
{scorebp = scorebp +1;}
}
}
document.getElementById('bpl'+id).value=scorebp ;






var scoremp=0;

for(i=1;i<6;i++)
{
for(j=1;j<6;j++)
{
if(1*document.getElementById("id"+i+id).value==1*document.getElementById('r'+j).innerHTML && i!=j  )
{scoremp = scoremp +1;}
                }


}

document.getElementById('mpl'+id).value=scoremp ;


}


function itexists(v)
{
var i=1;
var exist=0;
for(i=1;i<6;i++)
{
if(v==document.getElementById('r'+i).innerHTML) {exist=1;};
}
return exist;
}


function fill()
{
var i=1;
var id="";
var r=0;
var j=0;
var exist=1;


for(i=1;i<6;i++)
{
id="r"+i;
document.getElementById(id).innerHTML=rundomn(level);
}
}

function rundomn(a)
{
var n=0;
var exist=0;

while (n==0 || exist ==1)
{
n=Math.floor((Math.random() * 10) + 1);
/*n=n&6;*/

if(n> a){n=n-a};

exist=itexists(n);
/*alert(n);*/
}

return n;
}


function removerow(ctr)
{
var table = document.getElementById("myTable");
alert(ctr.id);
var h=1*document.getElementById("hc").value;
alert(h);
alert(h-1*ctr.id);
table.deleteRow(h-1*ctr.id);
}




function addrow()
{

var h0=document.getElementById("hc").value;








var ok=0;
var ok2=1;
if(h0==0) {ok=1};


if (h0>0)
{

if(isNaN(document.getElementById('id1'+h0).value) || document.getElementById('id1'+h0).value=='') {ok=0;ok2=0};
if(isNaN(document.getElementById('id2'+h0).value) || document.getElementById('id2'+h0).value=='') {ok=0;ok2=0};
if(isNaN(document.getElementById('id3'+h0).value) || document.getElementById('id3'+h0).value=='') {ok=0;ok2=0};
if(isNaN(document.getElementById('id4'+h0).value) || document.getElementById('id4'+h0).value=='') {ok=0;ok2=0};
if(isNaN(document.getElementById('id5'+h0).value) || document.getElementById('id5'+h0).value=='') {ok=0;ok2=0};

if(ok2 ==1){score(h0);};


var id='bpl'+h0;
if(document.getElementById(id).value !=5) {ok=1};
if(document.getElementById(id).value ==5) {ok=0};
if(ok2==0){ok=0};


if(document.getElementById(id).value ==5 && ends==0) 
{
var table = document.getElementById("tabmessage");    
var row = table.insertRow(0);  
var cell1 = row.insertCell(0);
cell1.innerHTML="Bravo !";
ends=1;
}


}

if(ok==1)  

{





    var h=1*document.getElementById("hc").value;
    var id1="id1" +h ;
    var id2="id2" +h ;  
    var id3="id3" +h ;
    var id4="id4" +h ;
    var id5="id5" +h ;
    
    
/*    
try
{
    if(typeof document.getElementById(id1) != 'undefined'){var v1=document.getElementById(id1).value;};
    if(typeof document.getElementById(id2) != 'undefined'){var v2=document.getElementById(id2).value;};
    if(typeof document.getElementById(id3) != 'undefined'){var v3=document.getElementById(id3).value;};
    if(typeof document.getElementById(id4) != 'undefined'){var v4=document.getElementById(id4).value;}:
    if(typeof document.getElementById(id5) != 'undefined'){var v5=document.getElementById(id5).value;}; 
}   
catch(err){alert(err.message);}

*/


   
    h=h+1;
    var table = document.getElementById("myTable");    
    var row = table.insertRow(h-1);    
    document.getElementById("hc").value=h;

    

    var element = document.createElement("input");   
    element.setAttribute("type", "text"); 
    element.setAttribute("value", h);
    element.setAttribute("id", "id0"+h);
    element.setAttribute("size", "1");
    var cell1 = row.insertCell(0);  
    cell1.appendChild(element);
    cell1.setAttribute("width", "10%");



    element = document.createElement("input");  
    element.setAttribute("type", "text");
    element.setAttribute("id", "id1"+h);
    element.setAttribute("size", "1");
    element.setAttribute("value", "");
    var cell2 = row.insertCell(1);
    cell2.appendChild(element);
    cell2.setAttribute("width", "11.25%");
 

    element = document.createElement("input");
    element.setAttribute("type", "text");
    element.setAttribute("id", "id2"+h);
    element.setAttribute("size", "1");
    element.setAttribute("value", "");
    var cell3 = row.insertCell(2);
    cell3.appendChild(element);
    cell3.setAttribute("width", "11.25%");

    element = document.createElement("input");
    element.setAttribute("type", "text");
    element.setAttribute("id", "id3"+h);
    element.setAttribute("size", "1");
    element.setAttribute("value", "");
    var cell4 = row.insertCell(3);
    cell4.appendChild(element);
    cell4.setAttribute("width", "11.25%");

    element = document.createElement("input");
    element.setAttribute("type", "text");
    element.setAttribute("id", "id4"+h);
    element.setAttribute("size", "1");
    element.setAttribute("value", "");  
    var cell5 = row.insertCell(4);
    cell5.appendChild(element);
    cell5.setAttribute("width", "11.25%");

    element = document.createElement("input");
    element.setAttribute("type", "text");
    element.setAttribute("id", "id5"+h);
    element.setAttribute("size", "1");
    element.setAttribute("value", "");
    var cell6 = row.insertCell(5);
    cell6.appendChild(element);
    cell6.setAttribute("width", "11.25%");

    

   
   

    element = document.createElement("input");
    element.setAttribute("type", "button");
    element.setAttribute("value", "Try");
    element.setAttribute("onclick", "addrow()");
    element.setAttribute("id", "b"+h);
    element.setAttribute("size", "1");
    var cell8 = row.insertCell(6);
    cell8.appendChild(element);
    cell8.setAttribute("width", "11.25%");


    element = document.createElement("input");
    element.setAttribute("type", "text"); 
    element.setAttribute("value", "");
    element.setAttribute("id", "bpl"+h);
    element.setAttribute("size", "1");
    var cell9 = row.insertCell(7);
    cell9.appendChild(element);
    cell9.setAttribute("width", "11.25%");


    element = document.createElement("input");
    element.setAttribute("type", "text"); 
    element.setAttribute("value", "");
    element.setAttribute("id", "mpl"+h);
    element.setAttribute("size", "1");
    var cell10 = row.insertCell(8);
    cell10.appendChild(element);
    cell10.setAttribute("width", "11.25%");
 
}

}
</script>
</head>
<body>

<table width="30%">
<tr>
<td width="70%">
<h2>Master Mind using Javascript by Kamel Gazzah</h2>
</td>
<td width="20%"><span id="mylevel" style="visibility:hidden;">Level</span></td><td><span id="slevel"></span></td>
</tr>
</table>

<table width="30%">
<tr><td><span id="sellevel">Level: Number from 1 to </span></td>




<td>
<form id="f1">
<input id="rad5" type="radio" value="5" name="level"><span id="slevel5">5</span>
<input id="rad6" type="radio" value="6" name="level"><span id="slevel6">6</span>
<input id="rad7" type="radio" value="7" name="level"><span id="slevel7">7</span>
<input id="rad8" type="radio" value="8" name="level"><span id="slevel8">8</span>
<input id="rad9" type="radio" value="9" name="level"><span id="slevel9">9</span>
</form>
</td>
<td><input id="blevel" type="button" value="Ok" onclick="setlevel()"></td>
</tr>
</table>


<input type="hidden" id="hc" value="0">
<!--<input type="button" value="add" onclick="addrow()" />-->

<table width="30%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="10%"><span id="r0"></span></td>
<td width="11.25%" ><span id="r1"class="ref"></span></td>
<td width="11.25%"><span id="r2" class="ref"></span></td>
<td width="11.25%"><span id="r3" class="ref"></span></td>
<td width="11.25%"><span id="r4" class="ref"></span></td>
<td width="11.25%"><span id="r5" class="ref"></span></td>
<td width="11.25%"><span id="r6" class="ref"></span></td>
<td width="11.25%"><span id="r7" class="ref"></span></td>
<td width="11.25%"><span id="r8" class="ref"></span></td>
</tr>
</table>



<table id="myTable" cellspacing="0" cellpadding="0" width="30%" border="1">
</table>
<br>
<table id="tabmessage" width="30%">

</table>
</body>
</html>


</p>
</object>



</body>
</html>


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend