作者PsMonkey (痞子军团团长)
看板java
标题[GWT] 来写个生命游戏吧!
时间Tue Dec 22 16:59:39 2009
如遇转载本文,请保留完整作者资讯以及签名档
(尤其某 j2_e.info 网站,咱家的文章不是给你拿来乱用的)
写一个 JavaScript 版的生命游戏(Game of life)
要花多久? 有多难呢?
嗯? Java 版不是说好不提 JavaScript 吗?
好好好,那拿出以前交作业的 Java 程式吧! [咦?]
首先包个「细胞」class
状态只有两种,要嘛活着要嘛死掉
不过得多个变数预知下一代会生会死
public class Cell{
private boolean now;
private boolean next;
public Cell(){}
public Cell(boolean alive) {now = alive;}
public boolean isAlive(){return now;}
public void setNextAlive(boolean alive){
this.next = alive;
}
public void nextTurn() {now = next;}
}
然後再来包个「世界」的 class
里头有一堆细胞,所以用个二维阵列来表示
至於 width, 跟 height 这两个 field 纯粹只是为了懒惰好看 [逃]
public class World{
private int width;
private int height;
private Cell[][] cell;
public World(int w, int h){
width = w;
height = h;
cell = new Cell[w][h];
for(int i=0; i<w; i++){
for(int j=0; j<h; j++){
cell[i][j] = new Cell(Random.nextBoolean());
//Random 是什麽? 不要问,很恐怖... [误]
}
}
}
public void nextTurn(){
for(int w=0; w<width; w++){
for(int h=0; h<height; h++){
checkCell(w, h);
}
}
for(int w=0; w<width; w++){
for(int h=0; h<height; h++){
cell[w][h].nextTurn();
}
}
}
private void checkCell(int w, int h) {
int tmpW;
int tmpH;
int sum=0;
for(int i=-1; i<2; i++){
tmpW = w+i;
if( tmpW<0 || tmpW==width){
continue;
}
for(int j=-1; j<2; j++){
tmpH = h+j;
if( tmpH<0 || tmpH==height){
continue;
}
if(tmpH==h && tmpW==w){
continue;
}
if(getCell(tmpW, tmpH).isAlive()){
sum++;
}
}
}
switch(sum){
case 0:
case 1:
case 4:
case 5:
case 6:
case 7:
case 8:
getCell(w, h).setNextAlive(false);
break;
case 3:
getCell(w, h).setNextAlive(true);
break;
}
}
public Cell getCell(int w, int h){
//TODO w, h 的范围值判断
return cell[w][h];
}
}
好了,物件都包好了,那麽现在该来解决画面了
这次不用 Swing、也不用 SWT,试试传说中的 GWT [奸笑]
「监视器」本身是个 panel,至於 vertical 在干麽就跳过。
监视器要监看生命游戏的世界,还要有一个棋盘来显示状况
所以 field 大概就是这样啦~
public class Monitor extends VerticalPanel{
private World world;
private Grid board;
private int width = 10, height = 10;
}
constructor 要把「世界」跟「棋盘」正式建立起来
「棋盘」要放在「监视器」上头,还要有个按钮让人家可以点下一步
所以...
public Monitor(){
world = new World(width, height);
board = new Grid(width, height);
this.add(board);
Button next = new Button("下一世代");
//点下一步要干些什麽好事?
next.addClickHandler(new ClickHandler(){
@Override
public void onClick(ClickEvent event) {
nextTurn();
}
});
this.add(next);
//重新整理画面
refresh();
}
这边先跑出来两个新的 method
nextTurn() 没啥好讲的,就是要 world 过个年,然後重新整理画面
public void nextTurn(){
world.nextTurn();
refresh();
}
至於 refresh(),我打算让活着的细胞用「X」来表示
死掉的细胞的空空白白的什麽都没有(其实是全形空白)
private void refresh() {
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (world.getCell(i, j).isAlive()) {
board.setText(i, j, "X");
} else {
board.setText(i, j, " ");
}
}
}
}
剩下的就是找到启动的地方
在 GWT 里头是 EntryPoint 这个家族的 class
然後这样写:
public class Enter implements EntryPoint {
public void onModuleLoad() {
RootPanel.get().add(new Monitor());
}
}
好啦~ 恭喜你,只剩下 compile
以及找个网页空间放 .js 档
JavaScript 版的生命游戏就... 写 完 啦... \囧/
轻不轻松? 开不开心? [扭扭]
以前的 Java 程式码可以直接拿来用...
阿? 什麽?
GWT 的开发环境要怎麽安装?
程式码要放在哪个目录下?
怎麽指定 EntryPoint?
ㄜ... 请参照课本第 43 页就可以了,好! 下课! [逃]
----
以 GWT 教学系列来说,应该先讲环境建制之类 blahblah
不过还是先跳过那些繁文缛节,先来讲比较实在的部份
如果这样子写 JavaScript 没办法引起兴趣
还是会想要用 jQuery 之类的方法来写程式
那开发环境安装起来再快(例如 XAMPP [大笑]),也没啥意义 :P
要试试看程式执行起来的感觉,可以到这来看看:
http://www.psmonkey.org/gwt-product.html
--
钱锺书:
说出来的话
http://www.psmonkey.org
比不上不说出来的话
Java 版 cookcomic 版
只影射着说不出来的话
and more......
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.20.183.90
2F:→ PsMonkey:理论上可以,实际上... 我不会 Orz 12/22 20:28
3F:→ TonyQ:会js的人直接写就好了 , 跟你兜一圈写java干麽 XDDD 12/22 21:20
4F:→ PsMonkey:我也会 JS 阿... 可是我不想写...... 12/22 21:37
5F:推 neversay:You can write javascript with jQuery 12/22 21:40
7F:→ PsMonkey:现在变成出题大赛了 T_T 12/22 21:52
8F:推 tkcn:我比较想要 javascript 版的 starcraft XD 12/22 21:59
9F:→ PsMonkey:StarCraft... 你... 你好样的... 12/22 22:25
10F:推 zanyking:还要可以出Broodwar 资料片 12/24 13:11
11F:推 coronach:写个Diablo III吧 XD 12/25 00:30