Tuesday, 15 September 2015

methods - Rock Paper Scissors Game Using AI Java -



methods - Rock Paper Scissors Game Using AI Java -

for assignment, supposed create rock, paper, scissors game using java. however, there added twist. computer should select weapon beat user, based on user’s previous selection of weapons. instance, if user has selected paper 3 times stone , scissors 1 time each, computer should take scissors weapon beat paper, user’s frequent selection far. here i've got far:

import java.util.random; import java.util.scanner; public class cscd210hw3 { public static void main(string[] args) { displaygreeting(); computerchoice(); gamecode(); } public static void displaygreeting() { system.out.print("this classic rock, paper, scissors game has grown know , love. \nrules same. paper beats rock, stone beats scissors, scissors beats paper. luck fool!"); system.out.println(); } public static string computerchoice() { random randomgenrator = new random(); int randomnumber = randomgenrator.nextint(3); int cpurock = 0; int cpupaper = 0; int cpuscissors = 0; string weapon = "nothing"; switch(randomnumber) { case 0: weapon = "rock"; cpurock++; break; case 1: weapon = "paper"; cpupaper++; break; case 2: weapon = "scissors"; cpuscissors++; break; } homecoming weapon; } public static string playerchoice() { scanner kb = new scanner(system.in); string input = ""; system.out.println(); system.out.print("please take weapon: "); input = kb.next(); string inputlower = input.tolowercase(); homecoming inputlower; } public static void gamecode() { int ties = 0; int playerwins = 0; int compwins = 0; int userscissors = 0; int userrock = 0; int userpaper = 0; string player; string comp; { player = playerchoice(); if(player == "scissors") { userscissors++; } else if(player == "rock") { userrock++; } else if(player == "paper") { userpaper++; } comp = computerchoice(); if(player.equals("rock")&&comp.equals("rock")) { system.out.println("you , computer both chose rock. it's tie!"); ties++; userrock++; } else if(player.equals("paper")&&comp.equals("paper")) { system.out.println("you , computer both chose paper. it's tie!"); ties++; userpaper++; } else if(player.equals("scissors")&&comp.equals("scissors")) { system.out.println("you , computer both chose scissors. it's tie!"); ties++; userscissors++; } else if (player.equals("rock") && comp.equals("scissors")) { system.out.println("you chose stone , computer chose scissors. win!"); playerwins++; userrock++; } else if(comp.equals("rock") && player.equals("scissors")) { system.out.println("you chose scissors , computer chose rock. lose!"); compwins++; userscissors++; } else if(player.equals("scissors")&& comp.equals("paper")) { system.out.println("you chose scissors , computer chose paper. win!"); playerwins ++; userscissors++; } else if(comp.equals("scissors") && player.equals("paper")) { system.out.println("you chose paper , computer chose scissors. lose!"); compwins++; userpaper++; } else if(player.equals("paper") && comp.equals("rock")) { system.out.println("you chose paper , computer chose rock. win!"); playerwins++; userpaper++; } else if(comp.equals("paper")&& player.equals("rock")) { system.out.println("you chose paper , computer chose rock. lose!"); compwins++; userrock++; } else { system.out.println("invalid input. please re-enter. "); system.out.println(); } }while(!(player.equals("quit"))); system.out.println("here results: "); system.out.println("ties: " + ties); system.out.println("computer wins: " + compwins); system.out.println("player wins: " + playerwins); system.out.println(); system.out.println("times stone chosen: "+userrock); system.out.println("times paper chosen: "+userpaper); system.out.println("times scissors chosen: "+userscissors); return; }//end }

i've got no thought how create computer select weapon beat user. i've heard ai might work, i've never used 1 before. how go doing that?

there no way computer may succeed improve @ guessing human if computer's opponent chooses rock, paper or scissors @ random. however, human @ random, there may approaches weigh likeliness of outcome given previous outcomes. think go pattern recognition. example, each combination or rock, paper or scissors of length n (so there 3^n of those), remember how has appeared in sequence produced human player. on each turn remember n-1 previous turns, , after each turn increment counter (one of 3^n counters) associated combination of outcomes in lastly n turns. can see time , space required solve problem grow exponentially n, suggest choosing little n, 4 or 5. start off programme guessing @ random (33.3% chance choosing each option), , then, after amount of statistics has been collected playing against human, start biasing each of 3 possible outcomes consulting counters.

java methods artificial-intelligence

No comments:

Post a Comment