联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> Java编程Java编程

日期:2020-10-27 11:27

CSCI-UA.0101-005: Project 1

Part 1: Warmup 40 points

In this first part you will extend the “game of life” code to support additional features.

(1) (10 points) Refactor the method countAliveNeighs. Instead of having a loop to retrieve

the x, y-coordinates of the 8 neighbors; add a new method int[][] getNeighs(boolean[][]

alive, int i, int j) to retrieve them. This change should affect only the body of countAliveNeighs.

(2) (20 points) Extend the code to support different rules. Change the update method to also

receive two arrays containing the numbers of required neighbors for a cell to survive or die.

Example where a cell is born if it has exactly 3 neighbors, survives if it has 2 or 3 living

neighbors, and dies otherwise:

int [ ] born = {3 };

int [ ] s u r v i v i n g = {2 , 3 };

while ( true )

{

p r i n t ( a l i v e ) ;

update ( a l i v e , born , s u r v i v i n g ) ;

. . .

}

(3) (10 points) Change the program to receive command line arguments to decide the size of the

grid and the rules for a cell to survive or die. For instance, java GOL 10 B3/S23 runs the

game of life on a 10 × 10 grid with the rule B3/S23. That is, a cell is born if it has exactly 3

neighbors and survives if it has 2 or 3 living neighbors.

1

CSCI-UA.0101-005 Project 1 (Page 2 of 3)

Figure 1: Example of an hexahedral grid.

Figure 2: Example of a print of 20 × 20 grid.

Part 2: Hexagonal game of life: 80 points

In this second part you will extend the “game of life” to run on a hexagonal grid. A hexagonal grid

can be stored as a normal grid where every row is “shifted by half” left and right (Figure 1). For

more information visit this page. To start, just copy your current “game of life” code.

(1) (40 points) Change the print code to visualize a hexagonal grid. Use the characters /, \, and

| to visualize the grid and use ? to illustrate if a cell is alive (Figure 2).

(2) (30 points) Change the method int[][] getNeighs(boolean[][] alive, int i, int j)

to retrieve the x, y-coordinates of the 6 neighbors of a cell at position i, j (instead of 8 as in

the square grid).

(3) (10 points) Now your code supports input grid size and rules for both a hexagonal and square

grid. Implement a new class to accept an optional user parameter hex to decide which grid to

run, e.g., java GOL 30 B2/S2 hex will run on an hexagonal grid while java GOL 30 B2/S2

on the square one. Important, the new class must call the main methods of the other two

classes.

CSCI-UA.0101-005 Project 1 (Page 3 of 3)

Question Points Bonus Points Score

Warmup 40 points 40 0

Hexagonal game of life: 80 points 80 0

Total: 120 0


版权所有:留学生编程辅导网 2018 All Rights Reserved 联系方式:QQ:99515681 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。