저는 Game of Life 프로그램을 아주 단순한 (그러나 어쩌면 틀린) 방법으로 만들려고 노력해 왔습니다. 기술적으로는 작동하는 것처럼 보이지만 일부 테스트 케이스를 실행하려고하면 결과가 온라인 예제에 적합하지 않습니다.Game of Life in Java 프로그램
다음은 메인 클래스입니다 :
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
}
}
내가 스페이스 키를 눌러 당신이 (SETTING 세포를 만들 수있는 화면을 클릭에서 게임 상태를 변경하는 방식으로 프로그램을 제작하는 게임을 일시 중지합니다.) 그리고 START는 게임 자체를 실행합니다. ,
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;
}
}
사람이 그것을 테스트하고 좋을 것이다 프로그램 로직 뭐가 잘못 됐는지 말해 줄 수 있다면, 때문에 전체의 작업 : 셀은 사각형과 상태를 포함 내가 만든 클래스입니다 하지만 나사를 조이면 안되는 사소한 문제가 있습니다.
이 http://codereview.stackexchange.com/ –
당신은 샘플 입력으로 재현 예를 만드는 경우가 여기에 해당 될 수있다에 더 적합 할 수 있습니다 보인다
개정 코드가 작동하는 예상 출력 설명 및 잘못된 출력 샘플. "여기에 코드가 있습니다. 오류를 찾으십시오."사람들이 일반적으로 여기에 응답하는 것이 아닙니다. –
글쎄, 미안하지만,이 장소가 어떻게 작동하는지 정말로 모르겠다. 나는 내가 한 것처럼 그것을 게시 할 수있을 것이라고 생각했고, 필요한 출력에 대한 설명이나 예가 있으면 주석에 게시 할 것입니다. – user3917631