ERLEDIGT
NEIN
NEIN
ANTWORTEN
1
1
ZUGRIFFE
476
476
EMPFEHLEN
-
06.12.08 16:04 #1
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.885
- Blog-Einträge
- 29
Hallo,
hier meine kleine Java mit Unterstützung für die Formulierung von Regeln in gängiger Schreibweise (siehe Wikipedia: http://de.wikipedia.org/wiki/Conways...geln)Variante:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
/** * */ 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 :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
6 7 4 +++++++ ++o++++ +++o+++ +ooo+++ +++++++ +++++++ 23/3 +++++++ +++++++ +++o+++ ++++o++ ++ooo++ +++++++
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
08.12.08 09:45 #2
- Registriert seit
- Feb 2005
- Ort
- Berlin
- Beiträge
- 130
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...
Der Mensch ist ein Tier das zuviel denkt!
Ähnliche Themen
-
[Quiz#15] Thomas Darimont (Java Genetischer Algorithmus)
Von Thomas Darimont im Forum ArchivAntworten: 1Letzter Beitrag: 11.04.10, 18:58 -
[Quiz #13] Thomas Darimont (Java)
Von Thomas Darimont im Forum ArchivAntworten: 0Letzter Beitrag: 10.01.10, 23:28 -
[Quiz #9] Thomas Darimont (Java)
Von Thomas Darimont im Forum ArchivAntworten: 0Letzter Beitrag: 21.07.09, 23:35 -
[QUIZ #2] Thomas Darimont (Java)
Von Thomas Darimont im Forum ArchivAntworten: 0Letzter Beitrag: 28.09.08, 14:08 -
[QUIZ#1] Thomas Darimont (Java)
Von Thomas Darimont im Forum ArchivAntworten: 2Letzter Beitrag: 22.09.08, 14:13






Login





