简体   繁体   中英

Rock Paper Scissors Game Using AI Java

For My Assignment, I am supposed to create a Rock, Paper, Scissors game using java. However, there is an added twist. The computer should select the weapon most likely to beat the user, based on the user's previous choice of weapons. For instance, if the user has selected Paper 3 times but Rock and Scissors only 1 time each, the computer should choose Scissors as the weapon most likely to beat Paper, which is the user's most frequent choice so far. Here is what I've got so 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 is the classic Rock, Paper, Scissors game everyone has grown to know and love. The \nrules are the same. Paper beats rock, rock beats scissors, scissors beats paper. Good 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;
        }

        return weapon;
    }

    public static String playerChoice()
    {      
        Scanner kb = new Scanner(System.in);

        String input = "";

        System.out.println();
        System.out.print("Please Choose Your Weapon: ");
        input = kb.next();
        String inputLower = input.toLowerCase();
        return 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;

      do
      {
           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 and the Computer Both Chose Rock. It's a Tie!");
                ties++;
                userRock++;
            }
            else if(player.equals("paper")&&comp.equals("paper"))
            {
                System.out.println("You and the Computer Both Chose Paper. It's a Tie!");
                ties++;
                userPaper++;
            }
            else if(player.equals("scissors")&&comp.equals("scissors"))
            {
                System.out.println("You and the Computer Both Chose Scissors. It's a Tie!");
                ties++;
                userScissors++;
            }

            else if (player.equals("rock") && comp.equals("scissors"))
            {
                System.out.println("You Chose Rock and the Computer Chose Scissors. You Win!");
                playerWins++;
                userRock++;
            }
            else if(comp.equals("rock") && player.equals("scissors"))
            {
               System.out.println("You Chose Scissors and the Computer Chose Rock. You Lose!");                
               compWins++;
               userScissors++;
            }
            else if(player.equals("scissors")&& comp.equals("paper"))
            {
               System.out.println("You Chose Scissors and the Computer Chose Paper. You Win!");   
               playerWins ++;
               userScissors++;
            }
            else if(comp.equals("scissors") && player.equals("paper"))
            {
               System.out.println("You Chose Paper and the Computer Chose Scissors. You Lose!");
               compWins++;
               userPaper++;
            }
            else if(player.equals("paper") && comp.equals("rock"))
            {
               System.out.println("You Chose Paper and the Computer Chose Rock. You Win!");     
               playerWins++;
               userPaper++;
            }
            else if(comp.equals("paper")&& player.equals("rock"))
            {
               System.out.println("You Chose Paper and the Computer Chose Rock. You Lose!");
               compWins++;
               userRock++;
            }
            else
            {
               System.out.println("Invalid Input. Please Re-Enter. ");
               System.out.println();
            }

        }while(!(player.equals("quit")));

                    System.out.println("Here are the 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 Rock Chosen: "+userRock);
                    System.out.println("Times Paper Chosen: "+userPaper);
                    System.out.println("Times Scissors Chosen: "+userScissors);

                    return;


         }//end
}

I've got no idea how to make the computer select the weapon most likely to beat the user. I've heard an AI might work, but I've never used one before. How would I go about doing that?

There is no way a computer may succeed better at guessing than a human if computer's opponent chooses rock, paper or scissors at random. However, as a human rarely does anything completely at random, there may be approaches to weigh a likeliness of an outcome given previous outcomes. So I think you could go with pattern recognition. For example, for each combination or rock, paper or scissors of length n (so there would be 3^n of those), you could remember how often has it appeared in the sequence produced by human player. So on each turn you remember n-1 previous turns, and after each turn you increment the counter (one of 3^n counters) associated with a combination of outcomes in the last n turns. You can easily see that the time and space required to solve the problem grow exponentially with n, so I suggest choosing a small n, like 4 or 5. So start off with your program guessing at random (33.3% chance for choosing each option), and then, after certain amount of statistics has been collected by playing against a human, start biasing each of three possible outcomes by consulting your counters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM