Java 爪哇西蒙说
我目前为simon says游戏制作了GUI,唯一的问题是实现游戏逻辑(我当前的代码将生成一个序列并显示用户输入,但不会保存生成的序列,也不会将其与输入进行比较)。我知道我必须使用队列或堆栈,但我不知道如何实现这两种方法来制作一个有效的游戏 有人能帮我吗,这是我到目前为止得到的: 司机:Java 爪哇西蒙说,java,stack,queue,Java,Stack,Queue,我目前为simon says游戏制作了GUI,唯一的问题是实现游戏逻辑(我当前的代码将生成一个序列并显示用户输入,但不会保存生成的序列,也不会将其与输入进行比较)。我知道我必须使用队列或堆栈,但我不知道如何实现这两种方法来制作一个有效的游戏 有人能帮我吗,这是我到目前为止得到的: 司机: import javax.swing.JFrame; public class Location { public static void main (String[] args) {
import javax.swing.JFrame;
public class Location
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("Location");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new LocationPanel());
frame.pack();
frame.setVisible(true);
}
}
“位置面板”(西蒙说游戏逻辑):
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入java.awt.event.MouseListener;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseMotionListener;
导入java.util.Random;
公共类LocationPanel扩展了JPanel
{
私有最终整型宽度=300,高度=300;//窗口尺寸
私有int[]xLoc,yLoc;//目标框的位置
/* 0 1
2 3 */
private int timer;//用于显示用户单击的计时器
private int xClick,yClick;//用户单击的位置
private int sTimer;//用于显示目标的计时器
私有int[]目标;//选择目标
私人目标;
私有整数目标;
私有JButton button;//播放下一个序列的按钮
公共场所小组()
{
xLoc=新整数[4];
yLoc=新整数[4];
xLoc[0]=100;yLoc[0]=100;
xLoc[1]=200;yLoc[1]=100;
xLoc[2]=100;yLoc[2]=200;
xLoc[3]=200;yLoc[3]=200;
定时器=0;
刺激值=0;
xClick=-100;yClick=-100;
目标=新整数[10];
currTarget=-1;
按钮=新按钮(“下一个序列”);
addActionListener(new ButtonListener());
添加(按钮);
addMouseListener(新的MouseHandler());
setPreferredSize(新尺寸(宽度、高度));
挫折地面(颜色:白色);
}
公共组件(图形页)
{
super.paintComponent(第页);
//绘制四个目标区域框
page.setColor(Color.black);
对于(int i=0;i0)
{
page.setColor(Color.blue);
页面填充(xLoc[target[currTarget]]-15,yLoc[target[currTarget]]-15,30,30);
刺激器--;
重新油漆();
}
如果(刺激=0)
{
如果(currTarget0)
{
页面设置颜色(颜色为红色);
第页:圆角(xClick-15,yClick-15,30,30);
计时器--;
重新油漆();
}
}
//按下按钮时,程序当前选择一个
//随机目标位置,并在短时间内显示目标
私有类ButtonListener实现ActionListener
{
已执行的公共无效操作(操作事件)
{
Random gen=新的Random();
numTargets=target.length;
for(int i=0;i
}我会将生成的序列保存在
列表中。然后获取此列表的迭代器。
(*)现在您只需将用户输入与迭代器指向的列表项进行比较。如果不同:中止,如果相同:在迭代器上调用next()
,并在*处重复该过程,直到浏览完完整列表。
正如gmoore所写,您也可以使用队列。原理保持不变,而是使用迭代器从队列中调用remove()
或poll()
。检查Javadoc中的队列
首先,将生成的序列保存在按钮列表中。在按钮列表中创建队列
:
Queue<Integer> generatedSequence = new Queue<Integer>();
Queue generatedSequence=new Queue();
生成时,使用随机数填充队列
Queue<Integer> generatedSequence = new Queue<Integer>();
private List<Integer> targets = new ArrayList<Integer>();
targets.add(new Random().nextInt(4));
int targetNum = targets.get(currTarget);
targets.clear();
public static class SimonSays {
private final Random r;
private final int simonNR = 4;
private final ArrayList<Integer> nrs = new ArrayList<Integer>();
private final Queue<Integer> hits = new LinkedList<Integer>();
public SimonSays(Random r, int nr) {
this.r = r;
for (int i = 0; i < nr - 1; i++)
this.nrs.add(r.nextInt(this.simonNR));
}
private void next() {
this.nrs.add(r.nextInt(this.simonNR));
this.hits.clear();
this.hits.addAll(this.nrs);
}
public boolean hasTargets() {
return hits.size() > 0;
}
public Integer[] targets() {
return nrs.toArray(new Integer[nrs.size()]);
}
public boolean hit(Integer i) {
Integer val = hits.poll();
if (val.compareTo(i) == 0) return true;
return false;
}
public void play() {
Scanner sc = new Scanner(System.in);
boolean b = true;
do {
this.next();
System.out.println("Simon says: " +
Arrays.toString(this.targets()));
while (hasTargets()) {
System.out.print("You say: ");
if ((b = hit(sc.nextInt())) == false) break;
}
} while (b);
System.out.println("\nYou made to size: " + this.nrs.size());
}
}