简体   繁体   English

使用AI Java的剪刀石头布游戏

[英]Rock Paper Scissors Game Using AI Java

For My Assignment, I am supposed to create a Rock, Paper, Scissors game using java. 对于“我的作业”,我应该使用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. 例如,如果用户选择了Paper 3次,而Rock和Scissors分别只有1次,则计算机应选择Scissors作为最有可能击败Paper的武器,这是用户迄今为止最常见的选择。 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. 我听说过AI可能会起作用,但是我以前从未使用过。 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. 例如,对于每种组合或长度为n(因此会有3 ^ n)的石头,纸或剪刀,您可能还记得它在人类玩家产生的序列中出现的频率。 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. 因此,在每个回合中,您都要记住n-1个先前的回合,而在每个回合中,您需要增加与最后n个回合中的结果组合相关的计数器(3 ^ n个计数器之一)。 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. 您可以轻松地看到解决问题所需的时间和空间随n呈指数增长,因此我建议选择一个较小的n,例如4或5。因此,请从程序随机猜测开始(选择每个选项的机会为33.3%) ,然后在通过与人类对战收集到一定数量的统计数据之后,开始咨询您的计数器,开始对三种可能结果中的每一种产生偏见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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