生存游戲

          Posted on 2006-11-27 22:15 近似凱珊卓 閱讀(263) 評論(0)  編輯  收藏 所屬分類: 我的作業
          Spiel des Lebens,生存游戲?如果這么翻譯恰當的話。這么一個小游戲,花了我不少心思。雖然已經交了,可是不滿意原來笨笨的算法,不甘心,又改了重交一遍,也不管Tutor會不會嫌煩了。第一次做像樣的小游戲,居然對它還蠻有感情的,放上來吧,留個紀念。在此特別感謝酷酷的T同學,聰明又耐心的P同學,以及來晚了想幫忙沒幫上的M同學,盡管你們看不見也看不懂,呵呵^^


          游戲規則:
          任意輸入需要的行數和列數,選取“活著”的格子,程序按照“生死規則”計算下一步的“生死”情況,一直點“weiter”(下一步),一直到全部死光或者“活著”的格子按規則無法死掉。
          ??????? 生死規則:原來“活著”的格子,若周圍八個格子(邊緣的五個,角上的三個)有兩個或三個格子也是“活著”的,則下一步仍然活著,否則,死去;原來“死”的格子,若周圍的格子正好有三個格子是活的,則下一步復活,否則,仍然是死的。

          程序的變量名稱都是德語的=)
          public class Spiel_des_Lebens extends SDL{
          ?public static void main(String[] args){
          ??int m=0,n=0;
          ??boolean flag=true;
          ??????? write("Geben Sie die Anzahl der Zeilen ein.");
          ??while(m==0){
          ????? m=read();
          ????? if(m<=0){
          ?????? write("Nur positive Eingabe erlaubt.");
          ?????? m=0;
          ????? }
          ??}
          ??????? write("Geben Sie die Anzahl der Spalten ein.");
          ??while(n==0){
          ????? n=read();
          ????? if(n<=0){
          ?????? write("Nur positive Eingabe erlaubt.");
          ?????? n=0;
          ????? }
          ??}
          ??boolean[][] arena=new boolean[m][n];
          ??update(arena);
          ??while(flag){
          ???flag=false;
          ???arena=veraendern(arena,n);
          ???update(arena);
          ???for(int i=0;i<arena.length;i++)
          ????for(int j=0;j<arena[i].length;j++){
          ?????if(arena[i][j]){
          ??????flag=true;
          ??????break;??
          ?????}
          ????}???????????????? ?
          ??}
          ?}
          ?public static boolean[][] veraendern(boolean[][] arena,int n){
          ??????? boolean[][] speicher=new boolean[arena.length][n];
          ??for(int i=0;i<arena.length;i++)
          ???for(int j=0;j<arena[i].length;j++){
          ??????????????? speicher[i][j]=berechnen(arena,i,j);
          ???}
          ??return(speicher);
          ?}

          ?public static boolean berechnen(boolean[][] arena,int i,int j){
          ???? int count=0;
          ??? for(int u=i-1;u<=i+1;u++){
          ????? if(u>=0&&u<=arena.length-1){
          ??? ?? for(int v=j-1;v<=j+1;v++){
          ??? ??? if(v>=0&&v<=arena[i].length-1&&arena[u][v]==true&&(u==i&&v==j)==false)
          ??? ???? count++;
          ????}
          ???}
          ??}
          ??if(arena[i][j]){
          ???if(count==2||count==3)
          ????return(true);
          ???else
          ????return(false);
          ??}
          ??else{
          ???if(count==3)
          ????return(true);
          ???else
          ????return(false);
          ??}???
          ?}
          }

          布陣源代碼 SDL:
          import java.awt.*;
          import java.awt.event.*;
          import javax.swing.*;
          public class SDL extends MiniJava {
          ?private static final Object syncObj = new Object();
          ?private static final int w = 20;
          ?private static class But extends JButton {
          ??boolean[][] arena;
          ??int i,j;
          ??But(boolean[][] _arena, int _i, int _j) {
          ???arena = _arena;
          ???i = _i;
          ???j = _j;
          ???setLabel();
          ???addActionListener(new ActionListener(){
          ????public void actionPerformed(ActionEvent e) {
          ?????arena[i][j] = !arena[i][j];
          ?????setLabel();
          ????}
          ???});
          ???setLocation(w*j,w*i);
          ??}
          ??void setLabel() {
          ???if (arena[i][j])
          ????setBackground(Color.RED);
          ???else
          ????setBackground(Color.BLUE);
          ???setSize(w,w);
          ??}
          ?}
          ?private static class UpdateArenaFrm extends JFrame {
          ??UpdateArenaFrm(boolean[][] arena) {
          ???JPanel panel = new JPanel(null);
          ???panel.setPreferredSize(new Dimension(400,400));
          ???panel.setMinimumSize(new Dimension(400,400));
          ???JButton okBut = new JButton("Weiter!");
          ???getContentPane().add(BorderLayout.SOUTH, okBut);
          ???okBut.addActionListener(new ActionListener(){
          ????public void actionPerformed(ActionEvent e) {
          ?????dispose();
          ?????synchronized(syncObj) {
          ??????syncObj.notifyAll();
          ?????}
          ????}
          ???});
          ???addWindowListener(new WindowAdapter() {
          ????public void windowClosing(WindowEvent e) {
          ?????System.exit(0);
          ????}
          ???});
          ???getContentPane().add(panel);
          ???for (int i=0; i<arena.length; i++)
          ????for (int j=0; j<arena[i].length; j++) {
          ?????panel.add(new But(arena,i,j));
          ????}
          ???pack();
          ??}
          ?}
          ?private static JFrame frm;
          ?public static void updateArena(boolean[][] arena) {
          ??UpdateArenaFrm frm = new UpdateArenaFrm(arena);
          ??frm.setVisible(true);
          ??synchronized (syncObj) {
          ???try {
          ????syncObj.wait();
          ???} catch(Exception e) {
          ????//nichts zu tun
          ???}
          ??}
          ?}
          ?public static void update(boolean[][] arena) {
          ??updateArena(arena);
          ?}
          }

          MiniJava源代碼:
          import javax.swing.JFrame;
          import javax.swing.JOptionPane;
          public class MiniJava {
          ??? // textuelle Eingabe
          ??? public static String readString(String text) {
          ??????? JFrame frame = new JFrame();
          ??????? String s = JOptionPane.showInputDialog(frame, text);
          ??????? frame.dispose();
          ??????? // frame.dispose() ist unter Java1.4 notwendig, um den Dialog
          ??????? // korrekt zu l?schen. Ansonsten terminiert das Programm nicht.
          ??????? if (s == null)
          ??????????? System.exit(0);
          ??????? return s;
          ??? }
          ??? public static String readString() {
          ??????? return readString("Eingabe:");
          ??? }
          ??? // Ganzzahlige Eingabe
          ??? public static int readInt(String text) {
          ??????? JFrame frame = new JFrame();
          ??????? String s = JOptionPane.showInputDialog(frame, text);
          ??????? frame.dispose();
          ??????? int x = 0;
          ??????? if (s == null)
          ??????????? System.exit(0);
          ??????? try {
          ??????????? x = Integer.parseInt(s.trim());
          ??????? } catch (NumberFormatException e) {
          ??????????? x = readInt(text);
          ??????? }
          ??????? return x;
          ??? }
          ??? public static int readInt() {
          ??????? return readInt("Geben Sie eine ganze Zahl ein:");
          ??? }
          ??? public static int read(String text) {
          ??????? return readInt(text);
          ??? }
          ??? public static int read() {
          ??????? return readInt();
          ??? }
          ??? // Flie?komma Eingabe
          ??? public static double readDouble(String text) {
          ??????? JFrame frame = new JFrame();
          ??????? String s = JOptionPane.showInputDialog(frame, text);
          ??????? frame.dispose();
          ??????? double x = 0;
          ??????? if (s == null)
          ??????????? System.exit(0);
          ??????? try {
          ??????????? x = Double.parseDouble(s.trim());
          ??????? } catch (NumberFormatException e) {
          ??????????? x = readDouble(text);
          ??????? }
          ??????? return x;
          ??? }
          ??? public static double readDouble() {
          ??????? return readDouble("Geben Sie eine Zahl ein:");
          ??? }
          ??? //
          ??? // Ausgabe
          ??? //
          ??? public static void write(String output) {
          ??????? JFrame frame = new JFrame();
          ??????? JOptionPane.showMessageDialog(frame, output, "Ausgabe", JOptionPane.PLAIN_MESSAGE);
          ??????? frame.dispose();
          ??? }
          ??? public static void write(int output) {
          ??????? write("" + output);
          ??? }
          ??? public static void write(double output) {
          ??????? write("" + output);
          ??? }
          }

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          posts - 9, comments - 0, trackbacks - 0, articles - 0

          Copyright © 近似凱珊卓

          主站蜘蛛池模板: 沈阳市| 台东县| 繁昌县| 兴化市| 新昌县| 沿河| 锡林郭勒盟| 兴山县| 汾西县| 宁明县| 义马市| 句容市| 平顺县| 中超| 九龙坡区| 瓦房店市| 佳木斯市| 保德县| 江阴市| 大城县| 德庆县| 临汾市| 西充县| 九台市| 三台县| 五大连池市| 合阳县| 仙游县| 聂拉木县| 阿拉善盟| 凤凰县| 西畴县| 普兰店市| 鸡西市| 广汉市| 渝北区| 合江县| 萨迦县| 古丈县| 乐业县| 沙田区|