Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner; // for user input
- public class GameOfLife {
- /**
- * Program: GameOfLife.java
- * Purpose: Implement Conway's Game of Life, using examples
- * Creator: Chris Clarke
- * Created: 02.10.2012
- * Modified: 19.10.2012
- */
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int n=0;
- System.out.print("Example 1, 2, 3 or 4? ");
- try {
- n = Integer.parseInt(scan.nextLine());
- } catch (NumberFormatException e) {
- System.out.println("Must be 1, 2, 3 or 4!");
- System.exit(1);
- }
- clearBoard();
- setUpExample(n);
- printBoard();
- while (true) {
- System.out.print("Press Enter to continue (x to exit)...");
- String s = scan.nextLine();
- if (s.toLowerCase().startsWith("x")) break;
- board = applyRules();
- printBoard();
- } // while
- } // main
- public static void clearBoard() {
- for (int i = 0; i < NUMBER_OF_ROWS; i++) {
- for (int j = 0; j < NUMBER_OF_COLUMNS; j++) {
- board[i][j] = false;
- } // for j
- } // for i
- } // clearBoard
- public static void example1() {
- board[2][2] = true;
- board[2][3] = true;
- board[3][2] = true;
- board[3][3] = true;
- board[5][10] = true;
- board[5][11] = true;
- board[5][12] = true;
- board[4][9] = true;
- board[4][10] = true;
- board[4][11] = true;
- board[8][20] = true;
- board[8][21] = true;
- board[9][20] = true;
- board[9][21] = true;
- board[10][22] = true;
- board[10][23] = true;
- board[11][22] = true;
- board[11][23] = true;
- } // example1
- public static void example2() {
- board[3][12] = true;
- board[3][13] = true;
- board[3][14] = true;
- board[4][11] = true;
- board[4][12] = true;
- board[4][13] = true;
- board[7][2] = true;
- board[7][3] = true;
- board[8][1] = true;
- board[8][4] = true;
- board[9][2] = true;
- board[9][3] = true;
- board[10][10] = true;
- board[10][11] = true;
- board[10][12] = true;
- } // example2
- public static void example3() {
- board[5][5] = true;
- board[5][6] = true;
- board[5][7] = true;
- board[5][9] = true;
- board[6][5] = true;
- board[7][8] = true;
- board[7][9] = true;
- board[8][6] = true;
- board[8][7] = true;
- board[8][9] = true;
- board[9][5] = true;
- board[9][7] = true;
- board[9][9] = true;
- } // example3
- public static void example4() {
- for (int i=2; i<=4; i++) {
- board[8][i] = true;
- }
- board[9][4] = true;
- board[10][2] = true;
- } // example4
- public static void setUpExample(int x) {
- switch (x) {
- case 1 : example1(); break;
- case 2 : example2(); break;
- case 3 : example3(); break;
- case 4 : example4(); break;
- default : System.out.println("Error");
- System.exit(1);
- } // switch
- } // setUpExample
- public static void printBoard() {
- for (int i=0; i<20; i++) System.out.println(); // make space
- for (int i = 0; i < NUMBER_OF_ROWS; i++) {
- for (int j = 0; j < NUMBER_OF_COLUMNS; j++) {
- if (board[i][j]) {
- System.out.print("*");
- } else {
- System.out.print(".");
- } // if
- } // for j
- System.out.println();
- } // for i
- } // printBoard
- public static int countLiveNeighbours(int i, int j) {
- int n = 0;
- if (i>0) { // up
- if (board[i-1][j]) {
- n++;
- }
- if (j>0) { // up & left
- if (board[i-1][j-1]) {
- n++;
- }
- }
- if (j < NUMBER_OF_COLUMNS-1) { // up & right
- if (board[i-1][j+1]) {
- n++;
- }
- }
- }
- if (i < NUMBER_OF_ROWS-1) { // down
- if (board[i+1][j]) {
- n++;
- }
- if (j>0) { // down & left
- if (board[i+1][j-1]) {
- n++;
- }
- }
- if (j < NUMBER_OF_COLUMNS-1) { // down & right
- if (board[i+1][j+1]) {
- n++;
- }
- }
- }
- if (j>0) { // left
- if (board[i][j-1]) {
- n++;
- }
- }
- if (j < NUMBER_OF_COLUMNS-1) { // right
- if (board[i][j+1]) {
- n++;
- }
- }
- return n;
- } // countLiveNeighbours
- public static boolean isAlive(boolean alive, int n) {
- if (alive) {
- if (n<2) {
- return false;
- } else if (n<=3) {
- return true;
- } else {
- return false;
- }
- } else {
- if (n==3) {
- return true;
- } else {
- return false;
- }
- } // if (alive)
- } // isAlive
- public static boolean[][] applyRules() {
- boolean[][] newBoard = new boolean[ NUMBER_OF_ROWS][ NUMBER_OF_COLUMNS];
- for (int i = 0; i < NUMBER_OF_ROWS; i++) {
- for (int j = 0; j < NUMBER_OF_COLUMNS; j++) {
- int n = countLiveNeighbours(i, j);
- newBoard[i][j] = isAlive(board[i][j], n);
- } // for j
- } // for i
- return newBoard;
- } // applyRules
- static final int NUMBER_OF_ROWS = 22;
- static final int NUMBER_OF_COLUMNS = 27;
- static boolean[][] board = new boolean[ NUMBER_OF_ROWS][ NUMBER_OF_COLUMNS];
- } // class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement