康威的生命游戏更新(下一代) [英] Conway's Game of Life Update(Next Generation)

查看:104
本文介绍了康威的生命游戏更新(下一代)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Conway的生活游戏java代码,我正在与我的更新方法(也称为下一代创建者)进行斗争。我将发布我到目前为止编写的代码,请让我知道如何修复更新方法。

I am working on Conway's game of life java code and I am having a struggle with my update method also known as the next generation creator. I will post my code I have written so far and please let me know what I can do to fix the update method.

如果没有时间单元格生成T
1并且其中三个邻居还活着。

A cell is born if there was none at time T 1 and exactly three of its neighbors were alive.

如果在时间T
1时,现有的单元格仍然存在,则有两个或三个邻居

An existing cell remains alive if at time T 1 there were either two or three neighbors

如果在时间T
1时,一个单元格会因隔离而死亡,那么邻居少于两个。

A cell dies from isolation if at time T 1 there were fewer than two neighbors.

如果时间T
1有超过三个邻居,则单元格因过度拥挤而死亡。

A cell dies from overcrowding if at time T 1 there were more than three neighbors.

public class GameOfLife {

    private char [][] grid;
    private int rows;
    private int columns;

    public GameOfLife(int rows, int columns) {
        grid=new char[rows][columns];
        for(int i=0;i<grid.length;i++)
        {
            for(int j=0;j<grid[i].length;j++)
                grid[i][j]=' ';
        }

    }

    public int numberOfRows() {
         int countRows=0;
          for(int i=0;i<grid.length;i++){
             countRows++;
             rows=countRows;
          }
          return rows;

    }

    public int numberOfColumns() {
        int countColumns=0;
          for(int i=0;i<1;i++){
             for(int j=0;j<grid[i].length;j++)
                countColumns++;
                columns=countColumns;
          }
          return columns;
    }

    public void growCellAt(int row, int col) {
        for(int i=0;i<grid.length;i++){
            for(int j=0;j<grid[i].length;j++) 
                   grid[row][col]='O';
        }
    }

    public boolean cellAt(int row, int col) {
        for(int i=0;i<grid.length;i++){
            for(int j=0;j<grid[i].length;j++)
                if(grid[row][col]=='O')
                    return true;
        }
        return false;
    }

    public String toString() {
        String result="";
        for(int i=0;i<rows;i++){
            for(int j=0;j<columns;j++)
                result+=grid[i][j];
        }
        return result;
    }

    public int neighborCount(int row, int col) {
        int count=0;
        int i=row;
        int j=col;
        int left;
        int right;
        int up;
        int down;
        if(i > 0)
            up = i-1;
        else
            up = grid.length-1;

        if(i < (grid.length-1))
            down = i+1;
        else
            down = 0;

        if(j > 0) 
            left = j-1;
        else
            left = grid[i].length - 1;

        if(j < (grid[i].length-1))
            right = j+1;
        else
            right = 0;

        if(grid[up][left] == 'O')
            count++;

        if(grid[up][j] == 'O')
            count++;

        if(grid[up][right] == 'O')
            count++;

        if(grid[i][left] == 'O')
            count++;

        if(grid[i][right] == 'O')
            count++;

        if(grid[down][left] == 'O')
            count++;

        if(grid[down][j] == 'O')
            count++;

        if(grid[down][right] == 'O')
            count++;

        return count;
    }

    public void update() {

        for(int i=0;i<grid.length;i++){
            for(int j=0;j<grid[i].length;j++){
                if(grid[i][j]==' ' && neighborCount(i,j)==3)
                    grid[i][j]='O';
                if(neighborCount(i,j)<2 || neighborCount(i,j)>3)
                    grid[i][j]= ' ';
                if(grid[i][j]=='O' && neighborCount(i,j)==2 || neighborCount(i,j)==3)
                    grid[i][j]='O';
            }
        }
    }
}

好的关于在更新方法中创建一个新数组,这是否需要完成?另外,我如何进行更新方法的断言测试?

Ok regarding making a new array in the update method, is this all that needs to be done? Also, how would I go about making assertion tests for the update method?

public void update() {
    char[][] newGrid = new char[grid.length][grid[0].length];
    for(int i=0;i<grid.length;i++){
        for(int j=0;j<grid[i].length;j++){
            if(grid[i][j]==' ' && neighborCount(i,j)==3)
                newGrid[i][j]='O';
            if(neighborCount(i,j)<2 || neighborCount(i,j)>3)
                newGrid[i][j]= ' ';
            if(grid[i][j]=='O' && neighborCount(i,j)==2 || neighborCount(i,j)==3)
                newGrid[i][j]='O';
        }
    }
}


推荐答案

看起来您正在尝试修改正在循环的同一网格。当您遍历网格时,应根据网格的上一个状态进行更改。尝试构建一个新的网格,而不是写一个旧网格。

It looks like you are trying to modify the same grid you are looping through. As you loop through your grid, changes should be made based on the previous state of the grid. Try constructing a new grid instead of writing over the old one.

这篇关于康威的生命游戏更新(下一代)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆