gpt4 book ai didi

java - 如何检查击键序列

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:14:32 25 4
gpt4 key购买 nike

我正在制作游戏并想实现像 Konami 代码这样的作弊代码。

但是我如何检查击键序列呢?

我希望它能正常工作,以便玩家只要输入代码就会触发。

提前致谢!

最佳答案

下面是一个检查 Konami 代码的类,包括“UP、UP、UP、DOWN 等情况。”

这应该适用于任何给定的序列。

import java.util.Map;
import java.util.TreeMap;

public class Konami {

static private int[] code =
{UP, UP, DOWN, DOWN, LEFT, RIGHT, LEFT, RIGHT, B};
static private Map<Integer, Integer>[] graph;
static private int currentNode = 0;

public static void main(String args[]) {
//Create graph
graph = generateSequenceMap(code);

//Call checkKonami(key) whenever a key is pressed
}


static public boolean checkKonami(int keyPressed) {
Integer nextNode = graph[currentNode].get(keyPressed);

//Set currentNode to nextNode or to 0 if no matching sub-sequence exists
currentNode = (nextNode==null ? 0 : nextNode);

return currentNode == code.length-1;
}


static private Map<Integer, Integer>[] generateSequenceMap(int[] sequence) {

//Create map
Map<Integer, Integer>[] graph = new Map[sequence.length];
for(int i=0 ; i<sequence.length ; i++) {
graph[i] = new TreeMap<Integer,Integer>();
}

//i is delta
for(int i=0 ; i<sequence.length ; i++) {
loop: for(int j=i ; j<sequence.length-1 ; j++) {
if(sequence[j-i] == sequence[j]) {
System.out.println("If at Node "+j+" you give me seq["+(j-i+1)
+ "] OR " + (sequence[j-i+1]) + " , goto Node " + (j-i+1));

//Ensure that the longest possible sub-sequence is recognized
Integer value = graph[j].get(sequence[j-i+1]);
if(value == null || value < j-i+1)
graph[j].put(sequence[j-i+1], j-i+1);
}
else
break loop;
}
}
return graph;
}
}

关于java - 如何检查击键序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7217037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com