- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试以一种相当简单(但可能是错误的)方式制作生命游戏程序。从技术上讲,它似乎可行,但当我尝试运行一些测试用例时,结果与在线示例不符。
这是主类:
package gameOfLife;import java.applet.*;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import gameOfLife.Cell.State;
public class CreateGame extends Applet implements Runnable, MouseListener, KeyListener {
private enum GameState {
SETTING, START
}
private GameState state = GameState.SETTING;
private Image image;
private Graphics second;
private Cell[][] cells = new Cell[200][120];
private int indexI = 1;
private int indexJ = 1;
@Override
public void init() {
setSize(1000, 600);
setFocusable(true);
Frame frame = (Frame) this.getParent().getParent();
frame.setResizable(false);
frame.setTitle("Game of Life - Settings");
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[0].length; j++) {
cells[i][j] = new Cell(i * 5, j * 5);
}
}
addMouseListener(this);
addKeyListener(this);
super.init();
}
@Override
public void start() {
Thread thread = new Thread(this);
thread.start();
super.start();
}
@Override
public void run() {
while (true) {
if (state == GameState.START) {
for(int i=1; i<cells.length-1; i++){
for(int j=1; j<cells[0].length-1; j++){
update(i, j);
}
}
}
repaint();
try {
Thread.sleep(125);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void update(int i, int j) { //updating the cells into ALIVE/DEAD state
int[] neighbors = new int[8];
int sum = 0;
for (int index = 0; index < 8; index++)
neighbors[index] = 0;
if (cells[i - 1][j - 1].state == State.ALIVE)
neighbors[0] = 1;
if (cells[i][j - 1].state == State.ALIVE)
neighbors[1] = 1;
if (cells[i + 1][j - 1].state == State.ALIVE)
neighbors[2] = 1;
if (cells[i - 1][j].state == State.ALIVE)
neighbors[3] = 1;
if (cells[i + 1][j].state == State.ALIVE)
neighbors[4] = 1;
if (cells[i - 1][j + 1].state == State.ALIVE)
neighbors[5] = 1;
if (cells[i][j + 1].state == State.ALIVE)
neighbors[6] = 1;
if (cells[i + 1][j + 1].state == State.ALIVE)
neighbors[7] = 1;
for (int index = 0; index < 8; index++)
sum += neighbors[index];
if ((sum < 2 || sum > 3) && cells[i][j].state == State.ALIVE)
cells[i][j].state = State.DEAD;
else {
if (sum == 3 && cells[i][j].state == State.DEAD)
cells[i][j].state = State.ALIVE;
}
}
@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Rectangle r;
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[0].length; j++) {
if (cells[i][j].state == State.ALIVE) {
r = cells[i][j].getCell();
g.fillRect((int) r.getX(), (int) r.getY(), (int) r.getWidth(), (int) r.getHeight());
}
}
}
super.paint(g);
}
@Override
public void update(Graphics g) {
if (image == null) {
image = createImage(this.getWidth(), this.getHeight());
second = image.getGraphics();
}
second.setColor(getBackground());
second.fillRect(0, 0, getWidth(), getHeight());
second.setColor(getForeground());
paint(second);
g.drawImage(image, 0, 0, this);
}
@Override
public void mousePressed(MouseEvent e) {
if (state == GameState.SETTING) {
int x = e.getX() - e.getX() % 5;
int y = e.getY() - e.getY() % 5;
cells[x / 5][y / 5].born();
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
if(state == GameState.START)
state = GameState.SETTING;
else
state = GameState.START;
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
我编写的程序的方式是,通过按空格键,您可以从“设置”(单击屏幕以创建暂停游戏的单元格)和“开始”更改游戏状态,这使游戏自行运行。Cell 是我创建的一个类,其中包含 Rectangle 和状态:
package gameOfLife;
import java.awt.Rectangle;
public class Cell {
final int Measure = 5;
public static enum State{
DEAD, ALIVE
}
private Rectangle cell;
private int x, y;
public State state;
public void born(){
state = State.ALIVE;
}
public Cell(int x, int y){
state = State.DEAD;
cell = new Rectangle(x, y, Measure, Measure);
this.x = x;
this.y = y;
}
public Rectangle getCell(){
return this.cell;
}
}
如果有人可以测试它并告诉我程序逻辑出了什么问题,那就太好了,因为总的来说它可以工作,但可能存在一些小问题,我无法指出到底是哪个问题搞砸了。
最佳答案
我已经发现了错误 - 您在迭代网格时刷新了每个单元格的状态,但正确的方法是在迭代期间创建新单元格状态的网格,然后将现有单元格状态设置为如果有意义的话,新的细胞状态会一次性出现。
修改后的代码似乎有效:
public class CreateGame
extends Applet
implements Runnable, MouseListener, KeyListener
{
private enum GameState
{
SETTING, START
}
private GameState state = GameState.SETTING;
private Image image;
private Graphics second;
private Cell[][] cells = new Cell[200][120];
private int indexI = 1;
private int indexJ = 1;
@Override
public void init()
{
setSize(1000, 600);
setFocusable(true);
Frame frame = (Frame) this.getParent().getParent();
frame.setResizable(false);
frame.setTitle("Game of Life - Settings");
for (int i = 0; i < cells.length; i++)
{
for (int j = 0; j < cells[0].length; j++)
{
cells[i][j] = new Cell(i * 5, j * 5);
}
}
addMouseListener(this);
addKeyListener(this);
super.init();
}
@Override
public void start()
{
Thread thread = new Thread(this);
thread.start();
super.start();
}
@Override
public void run()
{
while (true)
{
if (state == GameState.START)
{
Cell[][] newCells = new Cell[200][120];
for (int i = 0; i < newCells.length; i++)
{
for (int j = 0; j < newCells[0].length; j++)
{
newCells[i][j] = new Cell(i * 5, j * 5);
}
}
for (int i = 1; i < cells.length - 1; i++)
{
for (int j = 1; j < cells[0].length - 1; j++)
{
update(newCells, i, j);
}
}
cells = newCells; // update all cell states in one go
}
repaint();
try
{
Thread.sleep(125);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public void update(Cell[][] newCells, int i, int j)
{ // updating the cells into ALIVE/DEAD state
int[] neighbors = new int[8];
int sum = 0;
for (int index = 0; index < 8; index++)
neighbors[index] = 0;
if (cells[i - 1][j - 1].state == State.ALIVE)
neighbors[0] = 1;
if (cells[i][j - 1].state == State.ALIVE)
neighbors[1] = 1;
if (cells[i + 1][j - 1].state == State.ALIVE)
neighbors[2] = 1;
if (cells[i - 1][j].state == State.ALIVE)
neighbors[3] = 1;
if (cells[i + 1][j].state == State.ALIVE)
neighbors[4] = 1;
if (cells[i - 1][j + 1].state == State.ALIVE)
neighbors[5] = 1;
if (cells[i][j + 1].state == State.ALIVE)
neighbors[6] = 1;
if (cells[i + 1][j + 1].state == State.ALIVE)
neighbors[7] = 1;
for (int index = 0; index < 8; index++)
sum += neighbors[index];
if (cells[i][j].state == State.ALIVE)
{
if ((sum < 2 || sum > 3))
{
newCells[i][j].state = State.DEAD;
}
else // sum == 2 or 3
{
newCells[i][j].state = State.ALIVE;
}
}
else if (cells[i][j].state == State.DEAD)
{
if (sum == 3)
{
newCells[i][j].state = State.ALIVE;
}
}
}
@Override
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
Rectangle r;
for (int i = 0; i < cells.length; i++)
{
for (int j = 0; j < cells[0].length; j++)
{
if (cells[i][j].state == State.ALIVE)
{
r = cells[i][j].getCell();
g.fillRect((int) r.getX(), (int) r.getY(), (int) r.getWidth(), (int) r.getHeight());
}
}
}
super.paint(g);
}
@Override
public void update(Graphics g)
{
if (image == null)
{
image = createImage(this.getWidth(), this.getHeight());
second = image.getGraphics();
}
second.setColor(getBackground());
second.fillRect(0, 0, getWidth(), getHeight());
second.setColor(getForeground());
paint(second);
g.drawImage(image, 0, 0, this);
}
@Override
public void mousePressed(MouseEvent e)
{
if (state == GameState.SETTING)
{
int x = e.getX() - e.getX() % 5;
int y = e.getY() - e.getY() % 5;
cells[x / 5][y / 5].born();
}
}
@Override
public void mouseEntered(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_SPACE)
{
if (state == GameState.START)
state = GameState.SETTING;
else
state = GameState.START;
}
}
@Override
public void keyReleased(KeyEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0)
{
// TODO Auto-generated method stub
}
}
关于java - Java 生命游戏程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39406554/
我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding(
我不明白为什么他们不接受这个作为解决方案,他们说这是一个错误的答案:- #include int main(void) { int val=0; printf("Input:- \n
我正在使用基于表单的身份验证。 我有一个注销链接,如下所示: 以及对应的注销方法: public String logout() { FacesContext.getCurren
在 IIS7 应用程序池中有一个设置 Idle-time out 默认是 20 分钟,其中说: Amount of time(in minutes) a worker process will rem
我是一名优秀的程序员,十分优秀!