JavaScript - Rock-paper-scissors Algorithm
I have an assignment to code a java class that plays rock, paper, or scissors. The class can access the history of past gestures played by both itself and its opponent, but nothing else. The class will be played against others in 10,000 matches and the winner will be determined via round robin format. Other than using a greedy algorithm to determine statistically the best choice from prior gestures and basic pattern recognition I have no decent ideas. I'd appreciate any suggestions.
Similar TutorialsOkay I'm writing a JavaScript application that would prompt the user for a number between 0 and 2, generate a random number for the computer, then take that input and display two images (in this case two hands making rock, paper, or scissor symbols at each other) using an array, and displaying text indicating a win, draw, or loss. I'm having a lot of problems getting this to work as I am new to JavaScript and am wondering if anyone could help me fix this problem. I just want the input from the prompt AND the random number to translate to what in the array should be displayed and what text is then shown at the bottom. Any comments/advice/help is greatly appreciated! By the way for some reason it doesn't let the letters H T M L show up together so that is where the ellipses are showing up! Code: <script type = "text/javascript"> selection = prompt("Enter 0 for rock, 1 for paper, and 2 for scissors") userChoice = parseInt(selection) arrayHolder = new Array(); arrayHolder[0] = "rock.jpg"; arrayHolder[1] = "paper.jpg"; arrayHolder[2] = "scissors.jpg"; compchoice = Math.floor(Math.random()*arrayHolder.length) function playGame(idholder1) { document.images["userpic"].src = arrayHolder[userchoice]; document.images["comppic"].src = arrayHolder[compchoice]; if (userChoice == 0 && compchoice == 0) { document.getElementbyId(idholder1).innerHTML = "DRAW!" } else if (userChoice == 0 && compchoice == 1) { document.getElementbyId(idholder1).innerHTML = "Paper beats Rock, you lose!" } else if (userChoice == 0 && compchoice == 2) { document.getElementbyId(idholder1).innerHTML = "Rock beats Scissors, you win!" } else if (userChoice == 1 && compchoice == 0) { document.getElementbyId(idholder1).innerHTML = "Paper beats Rock, you win!" } else if (userChoice == 1 && compchoice == 1) { document.getElementbyId(idholder1).innerHTML = "Draw!" } else if (userChoice == 1 && compchoice == 2) { document.getElementbyId(idholder1).innerHTML = "Scissors beats Paper, you lose!" } else if (userChoice == 2 && compchoice == 0) { document.getElementbyId(idholder1).innerHTML = "Rock beats Scissors, you lose!" } else if (userChoice == 2 && compchoice == 1) { document.getElementbyId(idholder1).innerHTML = "Scissors beat Paper, you win!" } else if (userChoice == 2 && compchoice == 2) { document.getElementbyId(idholder1).innerHTML = "Draw!" } } </script> </head> <body> <p> <center><img src="rock.jpg" width="197" height="171" alt="rock" id="userpic" /><img src="paper.jpg" width="210" height="95" id="comppic" /><br /> <h1 id="gametext" onLoad="playGame('gametext')">ROCK PAPER SCISSORS SHOOT!</h1></center> </p> First of all, I wanna make my code using (while loop, do while loop, int, double, if, else, else if, Math.random, Scanner-- not string or boolean). -Actually the game goes like, after the user and the computer has has selections it is gonna be displayed and then tell who wins and who does not--I dont know how to do it. - When user presses 4, the the computer tells wins, loses and ties. This happens to me after two tries, wheteher i press 4 or not.(actully its |y/n| in mine and i want to change it to just 4 to quit). -The user is only allowed numbers from 1 to 4, but apparently, i add any number and it works. -My code gos like this: import java.util.Scanner; import java.util.Random; public class boobitty { public static void main (String[] args) { int Computer=0,Player=0,tie=0,compic,pscore; String str="y"; Random generate= new Random(); Scanner scan=new Scanner(System.in); while (str.equals("y")) { compic=generate.nextInt(3)+1; System.out.println ("Enter 1 for Rock, 2 for Paper, and 3 for Scissors"); pscore=scan.nextInt(); if (compic==pscore) { System.out.println("Tie Game"); tie++; } else { switch (pscore) { case 1: { if (compic==2) { System.out.println ("Paper beats Rock"); System.out.println ("Computer wins"); Computer++; } else { System.out.println ("Rock beats Scissors"); System.out.println ("Player wins!"); Player++; } break; } case 2: { if (compic==1) { System.out.println ("Paper beats Rock"); System.out.println ("Player wins!"); Player++; } else { System.out.println ("Scissors beat Paper"); System.out.println ("Computer wins!"); Computer++; } break; } case 3: { if (compic==1) { System.out.println ("Rock beats Scissors"); System.out.println ("Computer wins"); Computer++; } else { System.out.println ("Scissors beat Paper"); System.out.println ("Player wins"); Player++; } break; } default: { System.out.println("Enter 1 2 or 3"); break; } } } Scanner scen = new Scanner (System.in); System.out.println ("Play again? y/n"); str = scen.nextLine(); if(!(str.equals("y"))) { System.out.println ("Scores:"); System.out.println ("Ties= "+tie+" Wins= "+Player+" Losses= "+Computer); } else { System.out.println ("playing again"); } } } } SO, thanks for checking this out. If i have 3 text areas for output and an array that can be of size 0 to however big a user passes in values. How would I display the last 3 values of the array in the text areas? Here's what I was thinking psuedoCode, but I just want to know if there is an easier way. thnx! Code: //handling all the cases (ie if i have less than 3 items) var size = array.length display 0 if(size <= 3) if(size == '3'){ display0 = array[0] display1 = array[1] display2 = array[2] if(size == '2') do the same but copy the 2 elements if(size == '1') do the same else var newSize = (size -1) - 1 //to find where the first of the 3 elements will be display0 = array[newsize] display1 = array[newsize+1] display2 = array]newsize+2] Its seems a bit extraneous doing it this method, so i was just wondering if anyone could think of a better way. Thanks again! not sure why this wont sort in asending order... i tend to make little dumb mistakes sorry. :/ Code: <script type="text/javascript"> function sort(nums) { var rangeStart = 0; var rangeEnd = nums.length - 1; var i = 0; var minPosition = rangeStart; while(rangeStart < rangeEnd) { // find minumum for(i = rangeStart; i < rangeEnd; i++) { if(nums[i] <= nums[minPosition+1]) { minPosition = i; } } // swap var temp = nums[rangeStart]; nums[rangeStart] = nums[minPosition]; nums[minPosition] = temp; // change range rangeStart++; } } document.write("<h3>Examples</h3>"); first10 = [2,3,5,7,9,4,8,0,6,1]; document.write("<div>Sorting <tt>["+first10+"]</tt> with current code gives "); sort(first10); document.write("<tt>["+first10+"]</tt></div>"); ages = [19,34,20,66,82,53,88,74,39,13]; document.write("<div>Sorting <tt>["+ages+"]</tt> with current code gives "); sort(ages); document.write("<tt>["+ages+"]</tt></div>"); </script> I've written a function that "condenses" a string if it is too long. Code: function shortenMsg(msg,maxLen){ if (msg.length > maxLen){ var over = msg.length - maxLen, // amount that needs to be trimmed method1 = (msg.match(/,\s/g) || []).length, // amount that method1 can trim method2 = (msg.match(/\]\s\[/g) || []).length; // etc... method3 = (msg.match(/demonstration/gi) || []).length * 6, // etc... if (method1 >= over){ msg = msg.replace(/,\s/g, ","); } else if (method2 >= over){ msg = msg.replace(/\]\s\[/g, "]["); } else if (method3 >= over){ msg = msg.replace(/nstration/gi, ""); } else { // optimal combination of 2+ methods } } return msg; } var longMsg = "...", shortMsg = shortenMsg(longMsg, 50); // example usage Each of the methods 1-3 is the amount that that specific method can trim from the string. I'd like to be able to trim as little as possible. For example, if the string needs 5 characters to be trimmed, and method1 can trim 8 characters, but method3 can trim 6, then method3 should be used. If none of the methods can individually trim the string enough, then I'd like the optimal combination of the methods that will get the job done. I can't figure out what sort of code structure I need for this (besides a ton of if/else statements). Maybe an array that contains each of the methods, arranged in increasing order....? Help would be appreciated! Sorry for posting so much recently Hello, I'm trying to write some javascript that will detect if the page has been loaded because of the refresh button being pressed. I've searched google on how to do this, and several websites recommend something similar to the code I'm implementing below: Code: <script type="text/javascript" language="javascript"> // in head function checkRefresh() { alert("BEFO value = " + document.getElementById("visited").getAttribute("value")); if (document.getElementById("visited").getAttribute("value") == null || document.getElementById("visited").getAttribute("value") == "") { document.getElementById("visited").setAttribute("value", "refreshed") } alert("AFTER: value = " + document.getElementById("visited").getAttribute("value")); } </script> ... <body onload="Javascript:checkRefresh();"> ... <form id="hiddenform"> <input type="hidden" id="visited" value="" /> </form> </body> My variation differs from most of the examples on the internet in a few ways (which may or may not affect its functionality): 1) Most examples access the elements by directly using their names (as in: document.hiddenForm.visited.value). I'm using document.getElementById(...).getAttribute(...) just because that seems to be the safest way to ensure you are in fact getting the elements you want, and setAttribute(...) to ensure you're setting the attribute in the proper way. This entails that I need to set the ID in the form and input elements rather than the name. 2) I'm accessing the input tag directly (rather than going through the form) because I really don't see how this would make a difference. 3) I'm doing all this within asp:content tags which, from what I understand, can affect the behavior of the elements within it. I'm not sure if this works out for other programmers, but for me it doesn't seem to be working. My alert messages in the checkRefresh function tell me that the value of the input element does indeed change as expected, but it seems to get wiped out and reinitialized to the original value of "" when the page is refreshed. Am I doing something wrong? Thanks for any help. |