- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果您不知道康威的生命游戏是什么,请查看此内容,这是一个零玩家游戏,并且是一种细胞自动化。 https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
我需要帮助调试代码中的以下问题。如果有必要,我可以编辑这个问题以包含我的程序的旧版本,这些版本不打算扩展。
我还将接受改进我的程序的提示,包括(但不限于)时间效率(但是偏离主题,因为这不是主要问题)以及现有方法的错误使用。 (我的paint()方法)
Problems:
Examine Glider A in image 1. (Expected behavior) The whole point of the program was to make the cells shrink in the JFrame and add another row or column of dead cells to the
ArrayList<ArrayList<Boolean>> grid
. This glider, unfortunately does not cause this to happen. Instead, the result (Unexpected behavior) was that it simply "flattened into a square" as shown in image 2 (the square marked with an A).Examine Glider B in image 1. (Expected behavior) Seeing the results Glider A in image 1 made me think that glider B would end the same. However, this is not remotely true. Look at glider B in image 2. It, for some reason, does not even reach the border. (Unexpected)
这个程序的用途和目的:康威的生命游戏是在无限的 2D 平面上,所以我想复制这个。我的其他程序使用固定的数组大小,但这个使用嵌套的ArrayList来扩展,并且真正是无限的(直到内存限制到来)
不幸的是,这个扩展程序根本无法扩展边界。 (边框实际上比看起来要小。基本上,(假装我的 ArrayList> 网格是一个数组)grid[0][0 to grid.length-1]
包括、grid[grid.length-1][0 to grid.length-1]
包括、grid[1 to grid.length-1][0]
包括、grid[1 to grid.length-1][grid.length-1]
包括。)而不是执行指定的操作为了腾出空间,图 1 中的滑翔机 A 压平了边框。 (边框位于单元格网格下方,其中不存在灰线)
这些图像相互对应;图 1 中的滑翔机 A 标题产生了图 2 中的正方形,同时标记为 A。如果您在代码注释中看到任何描述“第 74 行”、指向“第 74 行”的单词或类似“UP”(没有任何其他含义)的单词,请告诉我,以便我删除它们。这是一个单独的、已经修复的错误。
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.Timer;
public class Expansion {
static int delay = 50;
static SquarePaint sp = new SquarePaint();
static int K = 75; // An integer used below to create the starting setup for the grid.
static{
// 001 101 011
boolean[][] array = new boolean [300][200];
// array[30][30]
array[K][K+2] = true;
array[K+1][K] = true;
array[K+1][K+2] = true;
array[K+2][K+1] = true;
array[K+2][K+2] = true;
sp.define(array); // Uses array to make first setup
}
public static void main (String[] args){
// It is possible to put this information in a constructor instead
JFrame j = new JFrame();
j.add(sp); // Must add a custom paint component overriding the paint component,
//a class could and will do this
j.setVisible(true);
j.setSize(2000, 1000);
j.setResizable(false);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static class SquarePaint extends Component{
static boolean VALUE = false;
public static final int screenX = 1000, screenY = 500;
static int cellSize = 5;
int cellsX = screenX / cellSize, cellsY = screenY / cellSize, frames = 0;
ArrayList<ArrayList<Boolean>> grid = new ArrayList<ArrayList<Boolean>>(); ////
Timer timer = new Timer(delay, new ActionListener(){
@Override
public void actionPerformed(ActionEvent A) {
//UPDATE FUNCTION
frames++;
boolean [][] oldGrid = new boolean[cellsX][cellsY];
for(int i=0; i<cellsX && i<grid.get(1).size(); i++){
for(int j=0; j<cellsY && j<grid.get(1).size(); j++){
oldGrid[i][j] = grid.get(i).get(j); // <<Line 74>><<Line 74>><<Line 74>>
}
}
/* These are notes
* How would I:
* Expand LinkedLists, for example
(Grid in a picture, 0 = variable, + = new cell)
0 0 0 +
0 0 0 +
0 0 0 +
0 0 0 +
* Adjust existing variables (and poorly coded methods if they exist) to suit this new change
*/
for(int i=1; i<cellsX-2; i++) {
for(int j=1; j<cellsY-2; j++) { // TODO
// System.out.println(i + " " + j);
int nearbyCells = nearbyCells(oldGrid, i, j);
/////////////////////////////////////////////
if(oldGrid[i][j]){
// If the cell is alive
// and is beside the no interact border, expand it by 1
// (Basically, to prevent ArrayOutOfBoundsException s,
// (the ArrayList equivalent) only cells from 1 to cellsX-1(-1)/cellsY-1(-1)
// are counted,)
// meaning that a whole row/column of cells is ignored and is not interacted with,
// until the grid expands.
ArrayList <Boolean> l = new ArrayList<Boolean>();
for(int k=0; k<cellsX; k++){
l.add(false);
}
if(i <= 1){ // You may notice that the above comments are wrong.
// This is because I thought this might be a small chance for it to succeed
// while I was frustrated this was not working.
grid.add(0, l);
cellSize = screenX / (cellSize + 1);
cellsX++;
VALUE = true;
repaint();
System.out.println("i<=5");
}
if(i >= cellsX-1){
grid.add(l);
cellSize = screenX / (cellSize + 1);
cellsX++;
VALUE = true;
repaint();
System.out.println("i>=cellsX");
}
if(j <= 1) {
for(int k=0; k<cellsY; k++){
grid.get(k).add(false);
}
cellSize = screenY / (cellSize + 1);
cellsY++;
VALUE = true;
System.out.println("j<=5");
repaint();
}
if(j >= cellsY-1) {
for(int k=0; k<cellsY; k++){
grid.get(k);
}
cellSize = screenY / (cellSize + 1);
cellsY++;
VALUE = true;
System.out.println("j>cellsY");
repaint();
}
//corner cases aren't a problem, it'll add to both sides
// && (i == 1 || i == cellsX-1 || j == 1 || j == cellsY-1)
/* i.e.
* 0 = dead cells
* 1 = annoying cells in the corner
* + = new cells
* First, it'll do this
* + + +
* 0 1 1
* 0 1 1 // Note that the square is a "still life form" meaning that it will
* 0 0 0 // simply sit there and do nothing for infinite generations if
* // untouched
* But the if statements above will return true more than once, so
* 0 0 0 +
* 0 1 1 +
* 0 1 1 +
* 0 0 0 +
*/
}
/////////////////////////////////////////////
if (oldGrid[i][j] && !(nearbyCells == 2 || nearbyCells == 3)){
// if it is alive, sustain rules
grid.get(i).set(j, false);
}
else if (!oldGrid[i][j] && nearbyCells == 3){ // if it is dead, birth rules
grid.get(i).set(j, true);
}
}
}
repaint(); // never erase, note that in bigger applications I assume that they
// draw to pictures that get slapped onto the screen so the paint function doesn't
// get called 9999 times and slow the fps down
}
});
int nearbyCells(boolean[][] oldGrid, int i, int j) { // A method that calculates how many cells are
//alive near it, i.e.
/* 0 = Dead
* 1 = Alive
* "+" = center
* 1 0 0
* 0 + 1
* 0 0 1
* If you called nearbyCells on the center in the example, then it would return 3.
*/
int nearbyCells = 0;
if(oldGrid[i+1][j+1]) nearbyCells++;
if(oldGrid[i+1][j]) nearbyCells++;
if(oldGrid[i+1][j-1]) nearbyCells++;
if(oldGrid[i][j+1]) nearbyCells++;
if(oldGrid[i][j-1]) nearbyCells++; // i, j is where the to-be-changed cell is, skip it
if(oldGrid[i-1][j+1]) nearbyCells++;
if(oldGrid[i-1][j]) nearbyCells++;
if(oldGrid[i-1][j-1]) nearbyCells++;
return nearbyCells;
}
public SquarePaint(boolean[][] grid){
define(grid);
timer.start();
}
public SquarePaint(){
timer.start();
}
public void define (boolean[][] grid){ // define cannot be called twice, not intended
for(int i=0; i<cellsX; i++){
ArrayList<Boolean> l = new ArrayList<Boolean>();
for(int j=0; j<cellsY; j++){
l.add(grid[i][j]);
}
this.grid.add(l);
}
}
@Override
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, screenX, screenY); // Warning: Color must be set
// each time if the paint command is ever to be called again
for(int i=0; i<cellsX-1; i++){
for(int j=0; j<cellsY-1; j++){
try{
if(grid.get(i).get(j)){
g.setColor(Color.WHITE);
g.fillRect(i * cellSize, j * cellSize, cellSize, cellSize);
} // Basically, 2 for loops
}// Nested for loops draw each cell on the potentially expanding ArrayList "grid" of cells
catch(java.lang.IndexOutOfBoundsException e){
System.out.println(i + " " + j);
e.getStackTrace();
}
g.setColor(Color.GRAY);
g.drawRect(i * cellSize, j * cellSize, cellSize, cellSize);
}
}
}
}
}
最佳答案
我不知道你的应用程序中间的逻辑应该做什么。不应该那么复杂。这在另一个线程中运行模拟,因此可能有助于解决性能问题。
public class GameOfLife extends JFrame {
private static final long serialVersionUID = 1L;
private Thread thread;
private SimulationThread simulationThread;
public GameOfLife() {
super("GameOfLife");
}
class MyPanel extends JPanel {
private static final long serialVersionUID = 1L;
public MyPanel() {
setBorder(BorderFactory.createLineBorder(Color.black));
}
public Dimension getPreferredSize() {
return new Dimension(1000, 1000);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
simulationThread.drawScreenItems(g2d);
}
}
/**
* Create the GUI and show it. For thread safety, this method should be invoked
* from the event-dispatching thread.
*/
public void createAndShowGUI() {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyPanel myPanel = new MyPanel();
add(myPanel);
// Display the window.
pack();
setLocationRelativeTo(null);
setVisible(true);
// Create and set up the window.
// create thread with genetic reproduction callback code.
simulationThread = new SimulationThread(myPanel);
thread = new Thread(simulationThread);
thread.start();
}
public static void main(String[] args) {
final GameOfLife swingGa = new GameOfLife();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
swingGa.createAndShowGUI();
}
});
}
}
public class SimulationThread implements Runnable {
private MyPanel myPanel;
public boolean run = true;
static final int GAME_SIZE = 200;
boolean[][] cells;
boolean[][] newCells;
public SimulationThread(MyPanel myPanel) {
this.myPanel = myPanel;
cells = new boolean[GAME_SIZE][GAME_SIZE];
for (int i = 1; i < cells.length - 1; i++) {
for (int j = 1; j < cells[0].length - 1; j++) {
cells[i][j] = Math.random() > 0.5;
}
}
newCells = new boolean[GAME_SIZE][GAME_SIZE];
}
@Override
public void run() {
while (run) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Iterate through the array, follow game of life rules
for (int i = 1; i < cells.length - 1; i++) {
for (int j = 1; j < cells[0].length - 1; j++) {
int surrounding = 0;
if (cells[i - 1][j - 1]) surrounding++;
if (cells[i - 1][j]) surrounding++;
if (cells[i - 1][j + 1]) surrounding++;
if (cells[i][j - 1]) surrounding++;
if (cells[i][j + 1]) surrounding++;
if (cells[i + 1][j - 1]) surrounding++;
if (cells[i + 1][j]) surrounding++;
if (cells[i + 1][j + 1]) surrounding++;
newCells[i][j] = false;
if (cells[i][j]) {
// Cell is alive, Can the cell live? (2-3)
if ((surrounding == 2) || (surrounding == 3)) {
newCells[i][j] = true;
}
} else {
// Cell is dead, will the cell be given birth? (3)
if (surrounding == 3) {
newCells[i][j] = true;
}
}
}
}
synchronized(cells) {
for (int i = 1; i < cells.length - 1; i++) {
for (int j = 1; j < cells[0].length - 1; j++) {
cells[i][j] = newCells[i][j];
}
}
}
myPanel.repaint();
}
}
public void drawScreenItems(Graphics2D g2d) {
synchronized(cells) {
for (int i = 1; i < cells.length - 1; i++) {
for (int j = 1; j < cells[0].length - 1; j++) {
if (cells[i][j])
g2d.fillRect(i * (1000 / GAME_SIZE), j * (1000 / GAME_SIZE), 1000 / GAME_SIZE, 1000 / GAME_SIZE);
}
}
}
}
}
关于java - 复制康威生命游戏的尝试无法扩展或正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58037057/
我是 magento 的新手,目前我在 magento 安装期间遇到“必须加载 PHP 扩展 curl ”错误。你能帮帮我吗? 最佳答案 如果您的服务器上没有安装 curl,您可以键入以下命令之一来安
我在 macOS Mojave/macOS Big Sur/macOS Monterey/macOS Ventura 上使用最新的 php 版本 7.2 并收到类似错误 $composer requ
这个问题已经有答案了: Why generic type is not applicable for argument extends super class for both? (5 个回答) 已关
我正在使用 NightWatch.js 并进行一些 UI 测试,我想用一些额外的 desiredCapabilities 启动默认浏览器实例(即启用扩展并应用一些特定值)。 p> 注意:我可以执行这些
有人知道为什么我在 java 8 中使用此代码时没有服务器扩展名称吗: try { URL url = new URL(urlString); URLC
扩展提供给我的类(class)。为现有的类提供新功能。或扩展现有的mixin s 或虚拟类,任何东西都可以工作。 也许是这样的: class FlatButton {} // maybe no
我有一个关于使用 c 代码和 mod_wsgi 扩展 python 的问题。 我在 apache 服务器中有一个 django 应用程序,它查询 postgresql 数据库以生成报告。在某些报告中,
testcafe支持在Chrome浏览器中加载crx扩展吗? 如果是这样,请告诉我需要尝试什么方法。 我尝试了下面的代码,但没有成功 await t.eval(new Function(fs.read
这个问题已经有答案了: What is a raw type and why shouldn't we use it? (16 个回答) 已关闭 3 年前。 有什么区别: // 1 class A c
我正在编写一个 chrome 扩展来记录单击开始按钮后触发的请求。 这是我的文件:1. list .json { "manifest_version": 2, "name": "recorde
扩展是将较短的文本,例如一组提示或主题列表,输入到大型语言模型中,让模型生成更长的文本。我们可以利用这个特性让大语言模型生成基于某个主题的电子邮件或小论文。通过这种方式使用大语言模型,可以为工作与生活
我每天都在使用 vim 和 perforce 现在我的问题是,如果我想查看 perforce 文件修订版,则从命令模式下的 vim :!p4 打印文件#1 vim 试图让我获得缓冲区 #1。有没有办法
大家好,我有一个关于 NUnit 扩展(2.5.10)的问题。 我想做的是向 数据库。为此,我使用 Event 创建了 NUnit 扩展 听众。 我遇到的问题是公共(public)无效 TestFin
我有弹出窗口,而不是模态窗口。 如何通过单击页面的其他部分(不在窗口中)来关闭此窗口? 最佳答案 像这样的东西: function closeWin(e, t) { var el = win.
我通常非常谨慎地使用扩展方法。当我确实觉得有必要编写一个扩展方法时,有时我想重载该方法。我的问题是,您对调用其他扩展方法的扩展方法有何看法?不好的做法?感觉不对,但我无法真正定义原因。 例如,第二个
扩展 Ant Ant带有一组预定义的任务,但是你可以创建自己的任务,如下面的例子所示。 定制Ant 任务应扩展 org.apache.tools.ant.Task 类,同时也应该拓展 execut
我想要一个重定向所有请求的扩展: http://website.com/foo.js 到: http://localhost/myfoo.js 我无法使用主机文件将主机从 website.com 编辑
对于为什么 QChartView 放在 QTabWidget 中时会扩展,我有点迷惑。 这是 QChartView 未展开(因为它被隐藏)时应用程序的图片。 应用程序的黑色部分是 QOpenGLWid
如果在连接条件中使用 OR 运算符,如何优化以下查询以避免 SQL 调优方面的 OR 扩展? SELECT t1.A, t2.B, t1.C, t1.D, t2.E FROM t1 LEFT J
一旦加载插件的问题得到解决(在 .NET 中通过 MEF 的情况下),下一步要解决的是与它们的通信。简单的方法是实现一个接口(interface),使用插件实现,但有时插件只需要扩展应用程序的工作方式
我是一名优秀的程序员,十分优秀!