Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <!-- saved from url=(0044)http://users.etown.edu/m/mcdevittt/play.html -->
- <HTML lang=en-US xml:lang="en-US"
- xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>RPS</TITLE>
- <META content="A study in Rock Paper Scissors" name=description>
- <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
- <SCRIPT type=text/javascript>
- // setting these two to rock is a hack to always make it throw paper first
- var lastThrow = 'rock';
- var twoThrowsAgo = 'rock';
- var count=0;
- var wins = 0;
- var losses = 0;
- var ties = 0;
- var allThrows = "";
- // initialize matrix
- // we do this here, instead of a servlet, so it's easier to track and learn from
- // an individual person
- var AIMatrix;
- function init(){
- AIMatrix = new Object();
- // -------ROCK first---------
- AIMatrix.RR = new Object();
- AIMatrix.RR.R = 68;
- AIMatrix.RR.P = 21;
- AIMatrix.RR.S = 11;
- AIMatrix.RP = new Object();
- AIMatrix.RP.R = 23;
- AIMatrix.RP.P = 32;
- AIMatrix.RP.S = 45;
- AIMatrix.RS = new Object();
- AIMatrix.RS.R = 18;
- AIMatrix.RS.P = 62;
- AIMatrix.RS.S = 20;
- // -----PAPER first-------------
- AIMatrix.PR = new Object();
- AIMatrix.PR.R = 24;
- AIMatrix.PR.P = 38;
- AIMatrix.PR.S = 38;
- AIMatrix.PP = new Object();
- AIMatrix.PP.R = 21;
- AIMatrix.PP.P = 59;
- AIMatrix.PP.S = 20;
- AIMatrix.PS = new Object();
- AIMatrix.PS.R = 36;
- AIMatrix.PS.P = 33;
- AIMatrix.PS.S = 31;
- // -----SCISSORS first-------------
- AIMatrix.SR = new Object();
- AIMatrix.SR.R = 24;
- AIMatrix.SR.P = 65;
- AIMatrix.SR.S = 11;
- AIMatrix.SP = new Object();
- AIMatrix.SP.R = 46;
- AIMatrix.SP.P = 29;
- AIMatrix.SP.S = 25;
- AIMatrix.SS = new Object();
- AIMatrix.SS.R = 08;
- AIMatrix.SS.P = 18;
- AIMatrix.SS.S = 74;
- }
- //given an array of throw probabilities, find response with greatest expected return
- function bestThrow(throwArray){
- // deteremine expected return for each possible throw
- var rReturn = (throwArray.R * 0) + (throwArray.P * -1) + (throwArray.S * 1);
- var pReturn = (throwArray.R * 1) + (throwArray.P * 0) + (throwArray.S * -1);
- var sReturn = (throwArray.R * -1) + (throwArray.P * 1) + (throwArray.S * 0);
- // find best throw
- if((rReturn > pReturn) && (rReturn > sReturn)){
- return 'rock';
- }
- if((pReturn > rReturn) && (pReturn > sReturn)){
- return 'paper';
- }
- if((sReturn > rReturn) && (sReturn > pReturn)){
- return 'scissors';
- }
- // if we get to here, there should be a tie, arbitarily create difference
- // if it's a three way tie, all will be increased twice
- var rinc = 0;
- var pinc = 0;
- var sinc = 0;
- if(rReturn == pReturn){
- rinc += (Math.Random() * 5);
- pinc += (Math.Random() * 5);
- }
- if(pReturn == sReturn){
- pinc += (Math.Random() * 5);
- sinc += (Math.Random() * 5);
- }
- if(sReturn == rReturn){
- sinc += (Math.Random() * 5);
- rinc += (Math.Random() * 5);
- }
- rReturn += rinc;
- pReturn += pinc;
- sReturn += sinc;
- // try to find best throw again
- if((rReturn > pReturn) && (rReturn > sReturn)){
- return 'rock';
- }
- if((pReturn > rReturn) && (pReturn > sReturn)){
- return 'paper';
- }
- if((sReturn > rReturn) && (sReturn > pReturn)){
- return 'scissors';
- }
- // if we get to here, something is wrong, so throw rock arbitrarily(actually, should be random)
- return 'rock';
- }
- function resolveTurn(playerThrow)
- {
- if (count >= 50){
- return;
- }
- // determine AI throw here
- var letters = getLetter(twoThrowsAgo) + getLetter(lastThrow);
- //document.getElementById("debug").innerHTML = ("letters are "+letters);
- var throwArray = AIMatrix[letters];
- var AIThrow = bestThrow(throwArray);
- //resolve round here
- document.getElementById("playerThrow").innerHTML = playerThrow;
- document.getElementById("AIThrow").innerHTML = AIThrow;
- //determine winner here
- count++;
- if(playerThrow.match('rock')){
- if(AIThrow.match('rock')){
- ties++;
- }else if(AIThrow.match('paper')){
- losses++;
- }else if(AIThrow.match('scissors')){
- wins++;
- }else{
- document.getElementById("debug").innerHTML += ("<br /> couldn't resolve AIThrow in 'rock'");
- }
- } else if(playerThrow.match('paper')){
- if(AIThrow.match('rock')){
- wins++;
- }else if(AIThrow.match('paper')){
- ties++;
- }else if(AIThrow.match('scissors')){
- losses++;
- }else{
- document.getElementById("debug").innerHTML += ("<br /> couldn't resolve AIThrow in 'paper'");
- }
- } else if(playerThrow.match('scissors')){
- if(AIThrow.match('rock')){
- losses++;
- }else if(AIThrow.match('paper')){
- wins++;
- }else if(AIThrow.match( 'scissors')){
- ties++;
- }else{
- document.getElementById("debug").innerHTML += ("<br /> couldn't resolve AIThrow in 'scissors'");
- }
- } else{
- document.getElementById("debug").innerHTML += ("<br /> couldn't resolve playerThrow");
- }
- //update matrix
- var playerLetter = getLetter(playerThrow);
- //document.getElementById("debug").innerHTML = ("letter is "+playerLetter);
- AIMatrix[letters][playerLetter]+= 10;
- // update throws state
- twoThrowsAgo = lastThrow;
- lastThrow = playerThrow;
- allThrows += getLetter(playerThrow);
- document.getElementById("wins").innerHTML = wins;
- document.getElementById("ties").innerHTML = ties;
- document.getElementById("losses").innerHTML = losses;
- document.getElementById("gamesLeft").innerHTML = 50-count;
- document.getElementById("debug").innerHTML = allThrows;
- if(count >= 50){
- saveResults();
- }
- }
- function getName(letter){
- if(letter.match('R')) return 'rock';
- if(letter.match('P')) return 'paper';
- if(letter.match('S')) return 'scissors';
- //default
- return 'unknown';
- }
- function getLetter(name){
- if(name.match('rock')) return 'R';
- if(name.match('paper')) return 'P';
- if(name.match('scissors')) return 'S';
- //default
- return 'U';
- }
- // I tried naming the variable throw, but it's a reserved word in JS
- // EDIT: Tim is no longer saving results so the contents of this function are being commented out
- function saveResults()
- {
- // var url=("/rps/AIServlet?playerId=${id}&wins="+wins+"&losses="+losses+"&ties="+ties+"&allThrows="+allThrows);
- // document.getElementById("debug").innerHTML = url;
- // var ajax = new AJAXInteraction(url);
- // ajax.send();
- }
- function postProcess(xml)
- {
- //parse ajax respone here
- var response = xml.getElementsByTagName("response")[0].childNodes[0].nodeValue;
- if(response.match('success')){
- document.getElementById("debug").innerHTML = "Thank you for playing! Your score has been recorded.";
- }
- }
- function AJAXInteraction(url) {
- this.url = url;
- var req = init();
- req.onreadystatechange = processRequest;
- function init() {
- if (window.XMLHttpRequest) {
- return new XMLHttpRequest();
- } else if (window.ActiveXObject) {
- isIE = true;
- return new ActiveXObject("Microsoft.XMLHTTP");
- }
- }
- function processRequest () {
- if (req.readyState == 4) {
- if (req.status == 200) {
- postProcess(req.responseXML);
- }
- }
- }
- this.send = function() {
- req.open("GET", url, true);
- req.send(null);
- }
- }
- </SCRIPT>
- <META content="MSHTML 6.00.6000.16674" name=GENERATOR></HEAD>
- <BODY onload=init();><!--${id} -->
- <H2>You think you got what it takes?</H2><SPAN id=debug></SPAN>
- <P>This is my kung fu, and it is strong. </P>Wins: <SPAN id=wins>0</SPAN>
- Losses: <SPAN id=losses>0</SPAN> Ties: <SPAN id=ties>0</SPAN> <BR><BR>Games
- left: <SPAN id=gamesLeft>50</SPAN> <BR>You threw: <SPAN id=playerThrow>nothing
- </SPAN><BR>Comp threw: <SPAN id=AIThrow>nothing </SPAN><BR>
- <H3><SPAN id=outcome></SPAN></H3><BR>
- <TABLE>
- <TBODY>
- <TR>
- <TD><IMG alt=ROCK src="rock.jpg" width=65> </TD>
- <TD><IMG alt=PAPER src="paper.jpg" width=72> </TD>
- <TD><IMG alt=SCISSORS src="scissors.jpg" width=55> </TD></TR>
- <TR>
- <TD><INPUT onclick="resolveTurn('rock')" type=button value=ROCK name=rock>
- </TD>
- <TD><INPUT onclick="resolveTurn('paper')" type=button value=PAPER name=paper>
- </TD>
- <TD><INPUT onclick="resolveTurn('scissors')" type=button value=SCISSORS name=scissors>
- </TD></TR></TBODY></TABLE></BODY></HTML>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement