// scissors_paper_rock.js // Help out scissors_paper_rock.html // RJM Programming // October, 2016 // Object literals used var myGame = { name : "Scissors, Paper, Rock", names : ["Player1", "Player2"], numplayers : 2, scores : [0, 0], choices : ["Scissors", "Paper", "Rock"], modes : ["Keyboard", "Mouse/Touch"], goes : 0, focus : -1, methodology : "Object literal", selection : [-1, -1], modeselection : ["", ""], getselection : function(which) { return this.selection[which]; }, getmodeselection : function(which) { return this.modeselection[which]; }, getchoices : function(which) { return this.choices[which]; }, setselection : function(which,valis) { if (which >= 0) { this.selection[which]=valis; //alert(which + ' became ' + valis); } }, whowon : function() { if (this.selection[0] == -1 || this.selection[1] == -1) { return -1; } //alert('maybe'); if (this.selection[0] == 0 && this.selection[1] == 1) { this.goes++; this.scores[0]++; this.selection[0]=-1; this.selection[1]=-1; alert("Congratulations, " + document.getElementById('player1name').value + ". Try again?"); document.getElementById('player1score').innerHTML=this.scores[0] + '/' + this.goes; document.getElementById('player2score').innerHTML=this.scores[1] + '/' + this.goes; myGame.init(); if (this.focus >= 0) document.getElementById('taplayer' + eval(1 + this.focus)).focus(); return 0; } else if (this.selection[1] == 0 && this.selection[0] == 1) { this.goes++; this.scores[1]++; this.selection[0]=-1; this.selection[1]=-1; document.getElementById('player1score').innerHTML=this.scores[0] + '/' + this.goes; document.getElementById('player2score').innerHTML=this.scores[1] + '/' + this.goes; alert("Congratulations, " + document.getElementById('player2name').value + ". Try again?"); myGame.init(); if (this.focus >= 0) document.getElementById('taplayer' + eval(1 + this.focus)).focus(); return 1; } else if (this.selection[0] == 1 && this.selection[1] == 2) { this.goes++; this.scores[0]++; this.selection[0]=-1; this.selection[1]=-1; document.getElementById('player1score').innerHTML=this.scores[0] + '/' + this.goes; document.getElementById('player2score').innerHTML=this.scores[1] + '/' + this.goes; alert("Congratulations, " + document.getElementById('player1name').value + ". Try again?"); myGame.init(); if (this.focus >= 0) document.getElementById('taplayer' + eval(1 + this.focus)).focus(); return 0; } else if (this.selection[1] == 1 && this.selection[0] == 2) { this.goes++; this.scores[1]++; this.selection[0]=-1; this.selection[1]=-1; document.getElementById('player1score').innerHTML=this.scores[0] + '/' + this.goes; document.getElementById('player2score').innerHTML=this.scores[1] + '/' + this.goes; alert("Congratulations, " + document.getElementById('player2name').value + ". Try again?"); myGame.init(); if (this.focus >= 0) document.getElementById('taplayer' + eval(1 + this.focus)).focus(); return 1; } else if (this.selection[0] == 2 && this.selection[1] == 0) { this.goes++; this.scores[0]++; this.selection[0]=-1; this.selection[1]=-1; document.getElementById('player1score').innerHTML=this.scores[0] + '/' + this.goes; document.getElementById('player2score').innerHTML=this.scores[1] + '/' + this.goes; alert("Congratulations, " + document.getElementById('player1name').value + ". Try again?"); myGame.init(); if (this.focus >= 0) document.getElementById('taplayer' + eval(1 + this.focus)).focus(); return 0; } else if (this.selection[1] == 2 && this.selection[0] == 0) { this.goes++; this.scores[1]++; this.selection[0]=-1; this.selection[1]=-1; document.getElementById('player1score').innerHTML=this.scores[0] + '/' + this.goes; document.getElementById('player2score').innerHTML=this.scores[1] + '/' + this.goes; alert("Congratulations, " + document.getElementById('player2name').value + ". Try again?"); myGame.init(); if (this.focus >= 0) document.getElementById('taplayer' + eval(1 + this.focus)).focus(); return 1; } alert("It's a draw. Try again."); this.selection[0]=-1; this.selection[1]=-1; myGame.init(); if (this.focus >= 0) document.getElementById('taplayer' + eval(1 + this.focus)).focus(); return -9; }, startGame : function() { if (this.selection[0] == -1 || this.selection[1] == -1) { return -1; } return this.whowon(); }, init : function() { //this.numplayers = 3; var dealwith = 0, ans="", focusdone=false; this.names[0]=document.getElementById('player1name').value; this.names[1]=document.getElementById('player2name').value; this.modeselection[0]=document.getElementById('player1mode').value; this.modeselection[1]=document.getElementById('player2mode').value; document.getElementById('player1score').innerHTML=this.scores[0] + '/' + this.goes; document.getElementById('player2score').innerHTML=this.scores[1] + '/' + this.goes; while (dealwith < 2) { while (this.modeselection[dealwith] == "" || (dealwith > 0 && this.modeselection[0].indexOf('Mouse') != -1 && this.modeselection[1].indexOf('Mouse') != -1)) { if (dealwith == 1) { if (dealwith > 0 && this.modeselection[0].indexOf('Mouse') != -1) { ans=this.modes[0]; } else { ans=" "; while (ans.toLowerCase().indexOf(this.modes[0].substring(0,1).toLowerCase()) != 0 && ans.toLowerCase().indexOf(this.modes[1].substring(0,1).toLowerCase()) != 0) { ans=prompt("Welcome to " + this.name + " Game ... " + this.names[dealwith] + ". Your opponent " + this.names[eval(1 - dealwith)] + " has chosen to use " + this.modes[0] + " as their mode of playing. The modes of playing choices are " + this.modes[0] + " and " + this.modes[1] + ". Which mode of play do you want, " + this.names[dealwith] + "?", ""); if (ans == null) ans=" "; } } } else { while (ans.toLowerCase().indexOf(this.modes[0].substring(0,1).toLowerCase()) != 0 && ans.toLowerCase().indexOf(this.modes[1].substring(0,1).toLowerCase()) != 0) { ans=prompt("Welcome to " + this.name + " Game ... " + this.names[dealwith] + " and to you too, " + this.names[eval(1 - dealwith)] + ". The modes of playing are " + this.modes[0] + " and " + this.modes[1] + " or both to use " + this.modes[0] + ". Which mode of play do you want, " + this.names[dealwith] + "?", ""); if (ans == null) ans=" "; } } if (ans.toLowerCase().indexOf(this.modes[0].substring(0,1).toLowerCase()) == 0) { this.modeselection[dealwith]=this.modes[0]; document.getElementById('player' + eval(1 + dealwith) + 'mode').value=this.modes[0]; if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) { //alert(904); document.getElementById('mkbrdplayer' + eval(1 + dealwith)).style.display='block'; } else { document.getElementById('taplayer' + eval(1 + dealwith)).style.display='block'; } } else { this.modeselection[dealwith]=this.modes[1]; document.getElementById('player' + eval(1 + dealwith) + 'mode').value=this.modes[1]; document.getElementById('divplayer' + eval(1 + dealwith)).style.visibility='visible'; } } if (this.modeselection[dealwith] == this.modes[1]) { document.getElementById('divplayer' + eval(1 + dealwith)).style.visibility='visible'; } else { if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) { document.getElementById('mkbrdplayer' + eval(1 + dealwith)).style.display='block'; //alert(94 + ' ... ' + dealwith); } else { document.getElementById('taplayer' + eval(1 + dealwith)).style.display='block'; if (!focusdone) { if (this.focus < 0) { document.getElementById('taplayer' + eval(1 + dealwith)).focus(); focusdone=true; this.focus=dealwith; } else if (dealwith == this.focus) { document.getElementById('taplayer' + eval(1 + dealwith)).focus(); focusdone=true; } } } } dealwith++; } this.names[0]=document.getElementById('player1name').value; this.names[1]=document.getElementById('player2name').value; moreWaiting(); } } function afterWait() { if ((document.getElementById('player1name').value + '~').indexOf(' ~') != -1) { document.getElementById('player1name').value=(document.getElementById('player1name').value + '~').replace(' ~','').replace('~',''); myGame.init(); } else if (document.getElementById('player1name').value != 'Player1' || document.getElementById('player1mode').value != '') { document.getElementById('player1name').value+=' '; setTimeout(afterWait, 9000); } else { myGame.init(); } } function moreWaiting() { var retval=myGame.startGame(); //document.title+=' ' + retval; if (retval < 0) { setTimeout(moreWaiting, 1000); } else { myGame.init(); } } function initGame() { setTimeout(afterWait, 9000); } window.onload = initGame;