Created
October 30, 2019 09:21
-
-
Save siddharthdeore/f76ed9a0e9a6e5de5737a5b052f9c713 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title></title> | |
| <style> | |
| #p1, #p2, #p3,#num { | |
| text-align: right; | |
| width: 8em; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div id="container"> | |
| <table> | |
| <tr> | |
| <td>Game Type</td><td></td> <td><input type="radio" name="gtype" value="dumb" id="dgt" checked=true>Dumb</td><td><input type="radio" name="gtype" value="smart" id="dst">Smart</td> | |
| </tr> | |
| <tr> | |
| <td>Number of Maches</td> <td></td> <td><input type="number" value="1000" id="num"></td> | |
| <td><input type="button" value="Play" onclick="PlayGame(parseInt(document.getElementById('num').value));"></td> | |
| </tr> | |
| <tr> | |
| <td>Player 1 </td><td></td><td><input type="text" value="0" id="p1"></td> | |
| </tr> | |
| <tr> | |
| <td>Player 2 </td><td></td><td><input type="text" value="0" id="p2"></td> | |
| </tr> | |
| <tr> | |
| <td>Draw </td><td></td><td><input type="text" value="0" id="p3"></td> | |
| </tr> | |
| </table> | |
| </div> | |
| </body> | |
| <script type="text/javascript"> | |
| // Dom elements | |
| var txtP1 = document.getElementById("p1"); | |
| var txtP2 = document.getElementById("p2"); | |
| var txtDRW = document.getElementById("p3"); | |
| // The Player Prototype class | |
| function Player() { | |
| this.a=Math.round(Math.random()); | |
| this.b=Math.round(Math.random()); | |
| this.c=Math.round(Math.random()); | |
| this.val=parseInt(""+this.a+""+this.b+""+this.c, 2); | |
| this.handsWin=0; | |
| }; | |
| Player.prototype.selRand = function() { | |
| this.a=getRandomBit(); | |
| this.b=getRandomBit(); | |
| this.c=getRandomBit(); | |
| this.val=parseInt(""+this.a+""+this.b+""+this.c, 2); | |
| this.handsWin=0; | |
| }; | |
| Player.prototype.check = function(seq) { | |
| return seq==this.val; | |
| }; | |
| Player.prototype.selSmart = function(tmp) { | |
| this.a=tmp.b==1?0:1; | |
| this.b=tmp.a; | |
| this.c=tmp.b; | |
| this.val=parseInt(""+this.a+""+this.b+""+this.c, 2); | |
| }; | |
| function getRandomInt(max) { | |
| return Math.floor(Math.random() * Math.floor(max)); | |
| }; | |
| function getRandomBit() { | |
| return Math.round(Math.random()); | |
| }; | |
| /* | |
| Main Game Algorithm | |
| */ | |
| function PlayGame(no_of_games){ | |
| Pwin=0; | |
| Swin=0; | |
| hand=0; | |
| gdrw=0; | |
| drw_flag=false; | |
| for (var j = 0; j < no_of_games; j++) { | |
| // create two player | |
| var p=new Player(); | |
| var s=new Player(); | |
| while(s.val==p.val) { | |
| s.selRand(); | |
| } | |
| if(document.getElementById("dst").checked){ | |
| s.selSmart(p); // Smart game when dom elemet is selected | |
| } | |
| // generate stack | |
| // var temp_stack=["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"]; | |
| var temp_stack=[]; | |
| var card_deck=[]; | |
| for (var i = 0; i < 26; i++) { | |
| temp_stack.push(1);temp_stack.push(0); | |
| } | |
| for (var i = 0; i < 52; i++) { | |
| card_deck.push(temp_stack.splice(getRandomInt(temp_stack.length),1)); | |
| } | |
| var a=0;var b=0; var c=0; | |
| var cur_cards=""; | |
| hand=3; | |
| drw_flag=false; | |
| while(card_deck.length>0 && p.handsWin+s.handsWin+card_deck.length<53) | |
| { | |
| if(!drw_flag) { | |
| if(card_deck.length>2) { | |
| a=card_deck.pop(); b=card_deck.pop(); c=card_deck.pop(); | |
| cur_cards=""+a+""+b+""+c; | |
| hand=3; | |
| } | |
| } else { | |
| d=card_deck.pop(); | |
| a=b; b=c; c=d; // swap bits | |
| cur_cards=""+a+""+b+""+c; | |
| hand++; | |
| } | |
| var n=parseInt(cur_cards, 2); | |
| if(s.check(n)) { | |
| s.handsWin = s.handsWin+hand; // s won the hand | |
| hand=3; | |
| drw_flag=false; | |
| } | |
| else if(p.check(n)){ | |
| p.handsWin = p.handsWin+hand; // s won the hand | |
| hand=3; | |
| drw_flag=false; | |
| } else { | |
| drw_flag=true; // increment hand | |
| } | |
| } | |
| if(p.handsWin > s.handsWin) | |
| Pwin++; | |
| else if (p.handsWin < s.handsWin) | |
| Swin++; | |
| else | |
| gdrw++; | |
| } | |
| txtP1.value=100 * Pwin/no_of_games + " %"; | |
| txtP2.value=100 * Swin/no_of_games + " %"; | |
| txtDRW.value=100 * gdrw/no_of_games + " %"; | |
| } | |
| </script> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment