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;
?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;
????? }
??}
??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;??
?????}
????}???????????????? ?
??}
?}
??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);
?}
??????? 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 final Object syncObj = new Object();
?private static final int w = 20;
?private static class But extends JButton {
??boolean[][] arena;
??int i,j;
??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);
??}
???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);
??}
?}
???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();
??}
?}
??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
???}
??}
?}
??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;
??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.
??? // 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;
??? }
??????????? System.exit(0);
??????? return s;
??? }
??? public static String readString() {
??????? return readString("Eingabe:");
??? }
??????? return readString("Eingabe:");
??? }
??? // Ganzzahlige Eingabe
??? public static int readInt(String text) {
??????? JFrame frame = new JFrame();
??????? String s = JOptionPane.showInputDialog(frame, text);
??????? frame.dispose();
??? 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;
??? }
??????? 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:");
??? }
??????? return readInt("Geben Sie eine ganze Zahl ein:");
??? }
??? public static int read(String text) {
??????? return readInt(text);
??? }
??????? return readInt(text);
??? }
??? public static int read() {
??????? return readInt();
??? }
??????? return readInt();
??? }
??? // Flie?komma Eingabe
??? public static double readDouble(String text) {
??????? JFrame frame = new JFrame();
??????? String s = JOptionPane.showInputDialog(frame, text);
??????? frame.dispose();
??? 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;
??? }
??????? 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:");
??? }
??????? 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();
??? }
??? // 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);
??? }
??????? write("" + output);
??? }
??? public static void write(double output) {
??????? write("" + output);
??? }
}
??????? write("" + output);
??? }
}