[QUIZ#7] Thomas Darimont (Java)

Thomas Darimont

Erfahrenes Mitglied
Hallo,

hier meine kleine Java Anwendung mit Unterstützung für die Formulierung von Regeln in gängiger Schreibweise (siehe Wikipedia: http://de.wikipedia.org/wiki/Conways_Spiel_des_Lebens#Abweichende_Regeln)Variante:

Java:
/**
 * 
 */
package de.tutorials.contest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

/**
 * @author Thomas.Darimont
 * 
 */
public class GameOfLife {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		Scanner scanner = new Scanner(System.in);
		int rows = scanner.nextInt();
		int columns = scanner.nextInt();
		int iterations = scanner.nextInt();

		StringBuilder sb = readWorld(scanner, rows);
		List<Rule> rules = readRules(scanner);

		scanner.close();

		World world = new World(sb.toString(), rows, columns, '+', 'o',rules);

		for (int iteration = 0; iteration < iterations; iteration++) {
			world.evolve();
//			TimeUnit.SECONDS.sleep(1);
//			System.out.println(world);
		}

		System.out.println();
		System.out.println(world);
	}

	static StringBuilder readWorld(Scanner scanner, int rows) {
		StringBuilder sb = new StringBuilder();
		for (int row = 0; row <= rows; row++) {
			sb.append(scanner.nextLine());
		}
		return sb;
	}

	static List<Rule> readRules(Scanner scanner) {
		List<Rule> rules = new ArrayList<Rule>();
		while(scanner.hasNextLine()){
			String line = scanner.nextLine();
			if(line.length() == 0){
				break;
			}
			rules.add(new Rule(line));
		}
		
		if(rules.size() == 0){
			rules = Arrays.asList(new Rule("23/3"));
		}
		return rules;
	}

	static class World {
		Grid grid;
		char deadChar;
		char lifeChar;
		int dead;
		int life = 1;
		Iterable<Rule> rules;
		
		public World(String worldMap, int rows, int columns, char deadChar,
				char lifeChar, List<Rule> rules) {
			this.deadChar = deadChar;
			this.lifeChar = lifeChar;
			this.rules = rules;
			this.grid = new Grid(rows, columns, encode(worldMap));
		}

		private int[] encode(String map) {
			int[] data = new int[map.length()];
			char[] chars = map.toCharArray();
			for (int i = 0; i < chars.length; i++) {
				if (chars[i] == lifeChar) {
					data[i] = 1;
				}
			}
			return data;
		}

		int getNeighbourCount(int row, int column) {
			int neighbourCount = 0;
			neighbourCount += grid.getCellValue(row - 1, column - 1);
			neighbourCount += grid.getCellValue(row - 1, column);
			neighbourCount += grid.getCellValue(row - 1, column + 1);
			neighbourCount += grid.getCellValue(row, column - 1);
			neighbourCount += grid.getCellValue(row, column + 1);
			neighbourCount += grid.getCellValue(row + 1, column - 1);
			neighbourCount += grid.getCellValue(row + 1, column);
			neighbourCount += grid.getCellValue(row + 1, column + 1);
			return neighbourCount;
		}

		public void evolve() {
			Grid newGrid = grid.copy();
			for (int row = 0; row < grid.rows; row++) {
				for (int column = 0; column < grid.columns; column++) {
					int alifeNeighbourCount = getNeighbourCount(row, column);
					int cellValue = grid.getCellValue(row, column);

					for (Rule rule : rules) {
						rule.apply(this, newGrid, row, column,
								alifeNeighbourCount, cellValue);
					}
				}
			}
			grid = newGrid;
		}

		public String toString() {
			StringBuilder sb = new StringBuilder();
			for (int row = 0; row < grid.rows; row++) {
				for (int column = 0; column < grid.columns; column++) {
					sb.append(grid.getCellValue(row, column) == 1 ? 'o' : '+');
				}
				sb.append("\n");
			}
			return sb.toString();
		}
	}

	static class Rule {
		String neighbourCountsForSurvival;
		String neighbourCountForNewLife;

		public Rule(String ruleExpression) {
			String[] survivalRuleVSNewLifeRule = ruleExpression.split("/");
			this.neighbourCountsForSurvival = survivalRuleVSNewLifeRule[0];
			this.neighbourCountForNewLife = survivalRuleVSNewLifeRule[1];
		}

		public void apply(World world, Grid grid, int row, int column,
				int aliveNeighbours, int cellValue) {
			String s = String.valueOf(aliveNeighbours);

			if (cellValue == 1 && neighbourCountsForSurvival.contains(s)) {
				grid.setCellValue(row, column, 1);
			}

			if (cellValue == 0 && neighbourCountForNewLife.contains(s)) {
				grid.setCellValue(row, column, 1);
			}
		}
	}

	static class Grid implements Cloneable {
		int rows;
		int columns;
		int[] data;

		public Grid(int rows, int columns, int[] data) {
			this.rows = rows;
			this.columns = columns;
			this.data = data;
		}

		public void setCellValue(int row, int column, int value) {
			data[row * columns + column] = value;
		}

		public int getCellValue(int row, int column) {
			if (row < 0 || row >= rows || column < 0 || column >= columns) {
				return 0;
			}
			return data[row * columns + column];
		}

		public Grid copy() {
			return new Grid(rows, columns, new int[data.length]);
		}
	}
}

Beispielsitzung:
Code:
6
7
4
+++++++
++o++++
+++o+++
+ooo+++
+++++++
+++++++
23/3


+++++++
+++++++
+++o+++
++++o++
++ooo++
+++++++

Gruß Tom
 

hasta_jesaja

Erfahrenes Mitglied
Hui... feine Sache mit der Grid Klasse. Das macht das ganze schön übersichtlich. Da kann man noch viel lernen. Ich denke immernoch zu prozedural...