悉尼大学第一年的 JAVA类似生命游戏的编程,英文题目!!求人,只有一天了!!哭
大概意思就是:这个程序类似于生命游戏。这游戏有三种情况,比如;
牛 草 空。
如果格子是 空的,
邻居有一个是草,那么在下一次就成为草。如果不是,那么还是草。
如果格子是 牛,
如果牛 过多(邻居在大于等于4个以上) 或者 饥饿(四周没有任何草),下一次都死光。
如果格子是 草
邻居相邻一个牛的话,那么下一次就成为牛。
后面这个老师还给了我们要用probalility的,我看不懂,那块不用上也没关系,只想求主要的怎么做。副本在后面
Write a program to simulate a population of grazers and food in a 易做图 2D world, similar
to Conway’s classic Game of Life. For the sake of this assignment, food and grazers are both creatures.
This is an example of using cellular automata.
Each tick of the clock, the next state of the cell will be determined by its current state and the states
of its neighbours, according to the following rules:
1. If the cell is EMPTY:
If it is adjacent to at least one Food, it will become Food next turn; Otherwise it will stay as it was.
2. If the cell is a Grazer:
If the Grazer is overcrowded or starved then it will die next turn (and the cell will become empty);
Otherwise it will stay as it was.
3. If the cell is a Food:
If it is next to a Grazer then it will become a Grazer next turn; Otherwise it will stay the same
Overcrowded means there aremore than 3 adjacent cells of the same kind as this one.
Starved means, for a predator, that there is no food in any adjacent cell.
We will represent food with dots ‘.’ and grazers with ‘@’.
The program must be called LifeAndDeath and must implement the Livable inte易做图ce provided.
NOTE: the LifeAndDeath class does not have to implement the Livable inte易做图ce. TheWorld class must
implement the Livable inte易做图ce. Do not add any methods to the Livable inte易做图ce. You may add any
other methods you like to your program though.
The program must have the class LifeAndDeath.java which contains the main method, and another
class file called World.java which implements the Livable inte易做图ce. You must put all your own
code (not the Livable inte易做图ce) into a package called assignment2.
The World class must have a constructor that takes as its arguments the size of the grid, the probability
(as a double) of each cell initially containing food, and the probability (as a double) of each cell
initially containing a grazer, like this:
1 public World (int size , double probFood , double probGrazer ) {
2 // ... fill in this
3 }
The programmust be able to be run as follows:
java LifeAndDeath 25 0.1 0.2 100
Which would create a 25£25 grid of cells, and each cell would have a probability of 0.1 of being a
grazer or of 0.2 of being food (so probability of 0.7 of being empty). The program should run with the
above arguments for 100 ticks of the clock or until everything dies.
The LifeAndDeath program should print out the grid as above with each grid on a new line (not
side by side as I showed in the last part of the example).
If the program is given another argument, the string “summary” as the last String, then it should
only print out the total number of creatures after each tick of the clock, not the whole grid.
这是inte易做图ce:
/**--------------------编程问答-------------------- MY LADY GAGA,坐等高人! --------------------编程问答-------------------- 這次的題目有看沒啥懂, 好像是要模擬出一個簡單的2D世界人口的草食動物(Grazer)和食品(Food)生死遊戲。
*
*/
package constraints;
/**
* @author mac
*/
public inte易做图ce Livable {
public char getNextCellState(int x, int y);
/**
* TODO Calculate the next state of the cell at position x, y
*
* according to the rules below:
* "lonely" means no neighbours with the same type
* "overcrowded" means at least 4 neighbours of the same type
* A Grazer is "starved" when none of its neighbours are Food
*
* If the cell is EMPTY then
* If it is adjacent to at least one Food,
* it will become Food next turn
* Otherwise it will stay as it was.
*
* If the cell is a Grazer then
* If the Grazer is overcrowded or starved
* then it will die next turn.
* Otherwise it will stay as it was.
*
* If the cell is a Food then
* If it is next to a Grazer
* then it will become a Grazer next turn
* Otherwise it will stay the same
*
*/
public char getContents(int i, int j);
/**
* TODO YOU MUST IMPLEMENT THIS METHOD
*
* Return the char at this cell, representing whether it is empty
* or contains a grazer or food.
*/
public boolean isValidCoordinate(int x, int y);
/**
* TODO YOU MUST IMPLEMENT THIS METHOD
*
* Return true if and only if the (x,y) coordinates fit in the world
*/
public int getNumNeighboursWithType(char type, int i, int j);
/**
* TODO YOU MUST IMPLEMENT THIS METHOD
*
* Return the number of neighbours of the cell at position (i,j)
* which are of the type given.
*/
public int getNumCreatures();
/**
* TODO YOU MUST IMPLEMENT THIS METHOD
*
* count and return the number of creatures in the world
*/
public boolean isEmpty();
/**
* TODO YOU MUST IMPLEMENT THIS METHOD
*
* Return true if and only if the complete world is empty
*/
public void setContents(int i, int j, char ch);
/**
* TODO YOU MUST IMPLEMENT THIS METHOD
*
* Set the contents of the cell (i,j) to be the character ch
* if the coordinates are valid. If they are not,
* throw an IndexOutOfBoundsException.
*/
}
只限於者次作業,這兩者都當做是生物
每個時鐘節拍,旁邊的單元格(Cell)的狀態將決定於當前狀態和相鄰的單元格,根據以下規則:
1。如果單元格為空:
如果是相鄰的至少有一個單元格是食物,它會變成食物在下一回合,如果不是就是保持原樣。
2。如果單元格是一種草食動物:
如果草食動物是人滿或餓死,下一個回合就會死亡(單元格將成為空白),如果不是就是保持原樣。
3。如果單元格是一種食物:
如果它是在一個草食動物的旁邊,它下一回合會成為一個草食動物,如果不是就保持不變
人滿意味著(以自己為主)有超過 3個相鄰的單元格是草食動物,下回合就會死。
餓死是指一個在任何相鄰的草食動物沒有食物的捕食, 下回合就會餓死。
這是以上條件的範例 "."為食物, "@"為草食動物(這是以6X6的格式去表示, 怕會混亂所以是空白的話我用"-"來代表):
Step 0:
012345
0 [ ..----] This is the initial state.
1 [ .-@---] Cells (0,0), (0,1) and (1,0) have food
2 [---@-- ] Cells (1,2), (2,3), (3,2), (3,3) and (3,4) have grazers
3 [--@@@- ]
4 [------ ]
5 [------ ]
Step 1:
0[ .@.--- ] A new grazer has moved in to cell (0,2)一個草食動物加入
1[ ..@--- ] This grazer has food and will spread這部分的食物因為(1,1)是食物,所以這一輪(1,2,)也變成食物
2[ ..---- ] The grazers on rows 2 and 3 have all starved(第二第三的動物因為上一輪旁邊沒有食物, 所以餓死了)
3[------ ]
4[------ ]
5[------ ]
Step 2:
0[ @@@.-- ] The food is spreading, but so are the grazers
1[ @@@.-- ]
2[ .@.--- ]
3[ ...--- ]
4[------ ]
5[------ ]
Step 3:
0[ --@@.- ] Now the grazers are expanding and eating everything
1[ ---@.- ]
2[ @@@..- ]
3[ @@@.-- ] The grazer at (3,1) will die from overcrowding
4[ ....-- ]
5[------ ]
Steps 4:
0[ ---@@. ]
1[ --.@@. ]
2[ ---@@. ]
3[ @-@@.. ] Yes, it's gone despite having food available.
4[ @@@@.- ]
5[ ..... -]
Steps 5-10:
5↓ 6↓ 7↓ 8 ↓ 9↓ 10↓
0[ -..@@@ ] [ .@@@-- ] [ @@-@.- ] [ ---@@. ] [ ----@@ ] [------ ]
1[ -.@--@ ] [ .@@.-- ] [ @--@.- ] [ ---@@. ] [ -----@ ] [------ ]
2[ -..-- @ ] [ .@@.-- ] [ @@@@.- ] [ ----@. ] [ -----@ ] [------ ]
3[ -----@@ ] [ .... -@ ] [ @@@@.- ] [ @--@@. ] [ -----@ ] [------ ]
4[ @--@@. ] [----- @ ] [ .....- ] [ @@@@@. ] [ @@@-@@ ] [ ------]
5[ @@@@@.] [----- @@ ] [------ ] [ ...... ] [ @@@@@@ ] [------ ]
Now everything is dead.(全死光了)
這裡說要做出一個package 裡面有兩個classes LifeAndDeath.java(主要的[main], 可能要重這個class叫World)跟World.java(主要執行Livable inte易做图ce的class)
用這樣執行:
java LifeAndDeath 25 0.1 0.2 100
上面是說做出5X5的表格, 而且一開始的每個單位格(Cell)都會有10%的機率隨機被設為草食動物,20%的機率為食物, 70%為空白
會跑100個節拍或全死光為止, 而輸出會類似像上面的範例(step 可以不用管)
--------------------编程问答-------------------- LZ同病相怜啊!!
你现在做出来了吗!!
我已经完全放弃了!!看来上半学期要挂科了!!
补充:Java , Java相关