我正在研究一种算法,该算法有助于预测计算机在剪刀石头布鞋类游戏中可能会做什么。基本上我所做的是我玩了100次并记下了所有动作。我将其移动成字符串。
我正在做的是寻找计算机行为的模式,就像我与一个人一样。在前四轮中,我只是在猜测算法。但是,在前四轮之后,我将计算机的动作与训练数据进行比较。如果可以找到模式,请继续下一步。例如,假设部分数据如下:
R P P S S S P P
我发现计算机说R P P S,我的算法建议我做剪刀(S),因为这是上次做的。
我正在做的是将训练数据另存为字符串,获取输入数据并将其另存为字符串,然后尝试分割训练数据字符串,但这似乎不是工作。
如何使用包含在较大字符串中的较小字符串拆分字符串,因此我可以保存并打印较大字符串中包含较小字符串之后的下一个字符?
Scanner scan = new Scanner(System.in);
Random rand = new Random();
int rock = 50;
int paper = 32;
int sicsors = 18;
//it should pick rock 1/2 the time, paper 1/3 the time, and scissors 1/5 the time
//Look at history and look at past four.
//If you can find a pattern then whatever comes next use
//char[] trainingData = "beep".toCharArray();
//char[] gameInput = "beep".toCharArray();
String userInput = "";
String trainingDataString = "RRRRSPPPSRRRP";
ArrayList<String> trainingData = new ArrayList<String>();
ArrayList<String> gameInput = new ArrayList<String>();
ArrayList<String> testingData = new ArrayList<String>();
System.out.println("Lets start!");
System.out.println("Play 100 roudds of RPC!");
System.out.println("Press 1 to continue");
int x = scan.nextInt();
scan.nextLine();
int rounds = 0;
boolean patternedContained = true;
while (rounds < 100) {
rounds++;
//choose based on overall perctanges
if (rounds <= 4 || patternedContained == false) {
int compChoice = rand.nextInt(100);
//System.out.println(compChoice);
if(compChoice <= 20) {
System.out.println("Use Rock. Enter the robots choice (R P S). ");
userInput = scan.nextLine();
gameInput.add(userInput);
}
else if (compChoice > 20 && compChoice <= 32) {
System.out.println("Use Paper. Enter the robots choice (R P S).");
userInput = userInput + scan.nextLine();
gameInput.add(userInput);
}
else {
System.out.println("Use Scissors. Enter the robots choice (R P S).");
userInput = userInput + scan.nextLine();
gameInput.add(userInput);
}
}
else if (rounds > 4) {
//check if pattern is contained
//if it is
if(trainingDataString.toLowerCase().contains(userInput.toLowerCase())) {
String[] startHere = trainingDataString.split(userInput);
String gotCha = startHere[0];
System.out.println(gotCha);
}
}
}
System.out.println(userInput);
}
}
答案 0 :(得分:0)
使用indexOf
查找大字符串在大字符串中的起始位置:
int littleStringPos = bigString.indexOf(littleString);
然后您可以通过在小字符串的起始位置加上小字符串的长度来找到小字符串之后的字符索引:
int charAfterIdx = littleStringIdx + littleString.length();
然后使用charAt
来选择字符:
char charAfterLittleString = bigString.charAt(charAfterIdx);
您应该为大字符串不包含小字符串的可能性做好准备,这可能由indexOf
返回-1的结果表示。因此,删除中间变量并包括安全检查的正确实现可能是这样的:
int littleStringPos = bigString.indexOf(littleString);
if (littleStringPos >= 0)
System.out.println("The character after thelittle string is: " +
bigString.charAt(littleStringIdx + littleString.length()));
else
System.out.println("The little string isn't there!");