生存游戏

Posted on 2006-11-27 22:15 近似凯珊卓 阅读(245) 评论(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 © 近似凯珊卓