生命游戏的循环函数问题

huangapple go评论97阅读模式
英文:

The game of life recurrent function probelm

问题

以下是翻译好的内容:

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

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

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

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

以下是我的动作监听器:

private ActionListener buttonsListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Object source = e.getSource();
            if (source == button1) area = setButton1(getBoardWidth(), getBoardHeight());
            if (source == glider) area = setGlider(getBoardWidth(), getBoardHeight());
            if (source == oscilator) area = setOscilator(getBoardWidth(), getBoardHeight());
        
            setBoard(area, board);

        }
    };

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

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

@Override
public void run() {
        startTheGame(area);
        setBoard(area, board);
}
英文:

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:

private ActionListener buttonsListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Object source = e.getSource();
            if (source == button1)area = setButton1(getBoardWidth(), getBoardHeight());
            if (source == glider) area = setGlider(getBoardWidth(), getBoardHeight());
            if (source == oscilator) area = setOscilator(getBoardWidth(), getBoardHeight());
        
            setBoard(area, board);

        }
    };

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.

 @Override
    public void run() {
            startTheGame(area);
            setBoard(area, board);
    }

答案1

得分: 1

你应该使用这个定时器schedule

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

import java.util.TimerTask;

public class UserTimerTask extends TimerTask {
    @Override
    public void run() {
        startTheGame(area);
        setBoard(area, board);
    }
}

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

// daemon表示后台线程。如果每个任务都是守护任务,虚拟机将会退出
Boolean isDaemon = false;
Timer timer = new Timer("nameOfThread", isDaemon);

TimerTask task = new UserTimerTask();

// 安排指定的任务以重复的固定延迟执行,延迟指定时间后开始
// 两个参数都以毫秒为单位
timer.schedule(task, 0, 1000);
英文:

You should use this timer schedule.

So you have to define a custom TimerTask like this:

import java.util.TimerTask;

public class UserTimerTask extends TimerTask{
    @Override
	public void run() {
       startTheGame(area);
       setBoard(area, board);
    }
}

And then wrapping your code like this:

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

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

发表评论

匿名网友

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

确定