生命游戏的循环函数问题

huangapple 未分类评论57阅读模式
英文:

The game of life recurrent function probelm

问题

以下是翻译好的内容:

我在Java中实现我的生命游戏时遇到了问题。

在图形用户界面中,我有一些按钮,用于决定游戏的初始状态(振荡器、滑翔机等),然后使用动作监听器设置第一个棋盘并显示它。

然后我有一个函数,用于计算细胞的邻居并设置细胞的颜色。
但是当我想重复进行n次游戏时,我遇到了问题,因为我不知道如何设置时间间隔。

目前我看不到游戏的每一步,只能看到最后一步。

以下是我的动作监听器:

  1. private ActionListener buttonsListener = new ActionListener() {
  2. @Override
  3. public void actionPerformed(ActionEvent e) {
  4. Object source = e.getSource();
  5. if (source == button1) area = setButton1(getBoardWidth(), getBoardHeight());
  6. if (source == glider) area = setGlider(getBoardWidth(), getBoardHeight());
  7. if (source == oscilator) area = setOscilator(getBoardWidth(), getBoardHeight());
  8. setBoard(area, board);
  9. }
  10. };

函数setBoard()接受一个由0和1组成的整数数组,并将其转换为带有颜色的JButton[][]数组。

我尝试使用重写的方法run(),其中包括startTheGame()函数,该函数检查邻居并设置整数数组。我需要多次执行此操作,但我无法设置时间间隔。

  1. @Override
  2. public void run() {
  3. startTheGame(area);
  4. setBoard(area, board);
  5. }
英文:

I have a problem with implementation of my Game of Life in Java.

In GUI I have buttons which decide of inicial state of the game (oscilator, glider etc), then using Action Listener I set the first board and show it.

Then I have a function that count neighbours of my cell and set colors of my cells.
But I have a problem when I want to repeat game n times, because I don't know how to set time interval.

At this moment I don't see every step of game, but just the last one.

Below is my ActionListener:

  1. private ActionListener buttonsListener = new ActionListener() {
  2. @Override
  3. public void actionPerformed(ActionEvent e) {
  4. Object source = e.getSource();
  5. if (source == button1)area = setButton1(getBoardWidth(), getBoardHeight());
  6. if (source == glider) area = setGlider(getBoardWidth(), getBoardHeight());
  7. if (source == oscilator) area = setOscilator(getBoardWidth(), getBoardHeight());
  8. setBoard(area, board);
  9. }
  10. };

Function setBoard() takes array of ints with 0 and 1, and convert it to JButton[][] array with colors.

I tried to use Overrided method run() that include startTheGame() function, which checks neighbourhood and set array of ints. I need to do this multiple times but I can't set time intervals.

  1. @Override
  2. public void run() {
  3. startTheGame(area);
  4. setBoard(area, board);
  5. }

答案1

得分: 1

你应该使用这个定时器schedule

所以你需要定义一个类似这样的自定义TimerTask

  1. import java.util.TimerTask;
  2. public class UserTimerTask extends TimerTask {
  3. @Override
  4. public void run() {
  5. startTheGame(area);
  6. setBoard(area, board);
  7. }
  8. }

然后将你的代码包装成这样:

  1. // daemon表示后台线程。如果每个任务都是守护任务,虚拟机将会退出
  2. Boolean isDaemon = false;
  3. Timer timer = new Timer("nameOfThread", isDaemon);
  4. TimerTask task = new UserTimerTask();
  5. // 安排指定的任务以重复的固定延迟执行,延迟指定时间后开始
  6. // 两个参数都以毫秒为单位
  7. timer.schedule(task, 0, 1000);
英文:

You should use this timer schedule.

So you have to define a custom TimerTask like this:

  1. import java.util.TimerTask;
  2. public class UserTimerTask extends TimerTask{
  3. @Override
  4. public void run() {
  5. startTheGame(area);
  6. setBoard(area, board);
  7. }
  8. }

And then wrapping your code like this:

  1. // daemon means, background. If every task is a demon task, the VM will exit
  2. Bolean isDaemon = false;
  3. Timer timer = new Timer("nameOfThread",isDaemon);
  4. TimerTask task = new UserTimerTask();
  5. // Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay
  6. // both parameters are in milliseconds
  7. timer.schedule(task,0,1000);

huangapple
  • 本文由 发表于 2020年4月6日 00:00:22
  • 转载请务必保留本文链接:https://java.coder-hub.com/61045384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定