如何让我的生命游戏继续下一代?

时间:2015-10-23 14:12:02

标签: java java.util.scanner conways-game-of-life

主类

package edu.bsu.cs121.mamurphy;

public class GameOfLifeMain {

    public static void main(String[] args) {
        {
            new GameOfLifeGUI();
            System.out.println();

        }

    }

}

Tester Class

package edu.bsu.cs121.mamurphy;

import java.awt.Color;
import java.awt.Graphics;
import java.io.File;
import java.util.Scanner;

import javax.swing.JPanel;

public class tester extends JPanel

{
    final static int ROW = 25, COL = 75;
    final static char DOT = '.';
    static char[][] grid = new char[ROW + 2][COL + 2];
    static char[][] nextgrid = new char[ROW + 2][COL + 2];
    boolean sameFlag;
    boolean blankFlag;

    public static void init(char[][] grid, char[][] nextgrid) {
        for (int row = 0; row <= ROW + 1; row++) {
            for (int col = 0; col <= COL + 1; col++) {
                grid[row][col] = DOT;
                nextgrid[row][col] = DOT;
            }
        }
    }

    public static void pause() {
        try {
            Thread.currentThread();
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
        }
    }

    public void begin() {
        init(grid, nextgrid);
        read(grid);
        repaint(); // calls paintComponent
        pause();
        while (sameFlag == true && blankFlag == false) {
            nextGen(grid, nextgrid);
        }
    }

    public static void read(char[][] grid) {
        Scanner world = new Scanner(System.in);
        System.out.println("Type the file name of the world you'd like to create.");
        String fileName = world.nextLine();
        {
            try {
                world = new Scanner(new File(fileName));
            } catch (Exception ex) {
                System.out.println("Please insert a valid file name.");
            }
            for (int row = 1; row <= ROW; row++) {
                String s = world.next();
                for (int col = 1; col <= COL; col++) {
                    grid[row][col] = s.charAt(col - 1);
                }

            }
        }
    }

    public void print(Graphics g) {
        int x, y;
        y = 20;
        for (int row = 1; row <= ROW; row++) {
            x = 20;
            for (int col = 1; col <= COL; col++) {
                g.drawString("" + grid[row][col], x, y);
                x = x + 7;
            }
            y = y + 15;
        }
    }

    public static int neighbors(char[][] grid, int r, int c) {
        // counts the # of closest neighbors that are X's
        int count = 0;
        for (int row = r - 1; row <= r + 1; row++) {
            for (int col = c - 1; col <= c + 1; col++) {
                if (grid[row][col] == 'X') {
                    count++;
                }
            }
        }
        if (grid[r][c] == 'X') {
            count = count - 1;
        }
        return count;
    }

    public static void nextGen(char[][] grid, char[][] nextgrid) {
        for (int row = 1; row <= ROW; row++) {
            for (int col = 1; col <= COL; col++) {
                if (grid[row][col] == 'X') {
                    int count = 0;
                    {
                        if (grid[row][col - 1] == 'X')
                            count = count + 1;
                        if (grid[row][col + 1] == 'X')
                            count = count + 1;
                        if (grid[row - 1][col] == 'X')
                            count = count + 1;
                        if (grid[row - 1][col - 1] == 'X')
                            count = count + 1;
                        if (grid[row - 1][col + 1] == 'X')
                            count = count + 1;
                        if (grid[row + 1][col - 1] == 'X')
                            count = count + 1;
                        if (grid[row + 1][col] == 'X')
                            count = count + 1;
                        if (grid[row + 1][col + 1] == 'X')
                            count = count + 1;
                    }

                    if (count == 2 || count == 3) {
                        nextgrid[row][col] = 'X';
                    } else
                        nextgrid[row][col] = DOT;
                }
                if (grid[row][col] == DOT) {
                    int count1 = 0;
                    {
                        if (grid[row][col - 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[row][col + 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[row - 1][col] == 'X')
                            count1 = count1 + 1;
                        if (grid[row - 1][col - 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[row - 1][col + 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[row + 1][col - 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[row + 1][col] == 'X')
                            count1 = count1 + 1;
                        if (grid[row + 1][col + 1] == 'X')
                            count1 = count1 + 1;
                    }
                    if (count1 == 3)
                        nextgrid[row][col] = 'X';
                }
            }
        }
    }

    public static void copy(char[][] grid, char[][] nextgrid) {
        for (int i = 0; i < ROW + 1; i++) {
            for (int j = 0; j < COL + 1; j++) {
                grid[i][j] = nextgrid[i][j];
            }
        }
    }

    public static boolean isEmpty(char[][] grid, char[][] nextgrid) {
        boolean blankFlag = true;
        for (int i = 0; i < ROW + 1; i++) {
            for (int j = 0; j < COL + 1; j++) {
                if (grid[i][j] != DOT) {
                    blankFlag = false;
                }
            }
        }
        return blankFlag;
    }

    public static boolean isSame(char[][] grid, char[][] nextgrid) {
        boolean sameFlag = false;
        for (int i = 0; i < ROW + 1; i++) {
            for (int j = 0; j < COL + 1; j++) {
                if (grid[i][j] == nextgrid[i][j]) {
                    sameFlag = true;
                    break;
                }
            }
        }
        return sameFlag;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);// erases panel Contents
        g.setColor(Color.black);
        if (sameFlag == false && blankFlag == false) {
            print(g);
        } else {
            if (sameFlag == true) {
                g.drawString("This worldis the same as the last one.", 10, 250);
            }
            if (blankFlag == true) {
                g.drawString("Everyone died. Good job.", 10, 250);
            }
        }
    }

}

GUI类

package edu.bsu.cs121.mamurphy;

import javax.swing.*;
import java.awt.*;

//

public class GameOfLifeGUI extends JFrame {

    public GameOfLifeGUI() {
        super("Game of Life");
        setSize(600, 445);
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 2));
        tester test1 = new tester();
        test1.setBackground(Color.LIGHT_GRAY);
        panel.add(test1);
        setContentPane(panel);
        setVisible(true);
        Temp one = new Temp(test1);
        one.start();
    }

    class Temp extends Thread {
        tester anim;

        public Temp(tester anim) {
            this.anim = anim;

        }

        public void run()// for each instance of test begin will be executed
        {
            anim.begin();
        }
    }
}

你好stackoverflow。在过去的几天里,我一直致力于这个生命游戏项目,并且我试图完成它的最后一道墙。如何让它继续下一代?

我知道我需要做什么我需要问用户他们是否希望继续下一代。我知道我需要使用一个扫描仪,然后有一个循环检查用户的输入是什么,以及一个try catch,以确保程序不会崩溃如果他们输入任何不是程序的东西问他们。

我唯一的问题是我不知道在我的测试人员课程中将这些代码放在哪里(如果这实际上是它需要去的地方)。

非常感谢任何帮助。

0 个答案:

没有答案