The class in which we read the text file with the matrix and the number of generations:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class TextFileReader { private char[][] cells; private int width = 0; private int height = 0; private int generation; public void readFromFile(){ try(BufferedReader bufferedReader = new BufferedReader(new FileReader("D:\\life\\test.txt"))) { String value; while ((value = bufferedReader.readLine()) != null) { if (width == 0) { width = value.length(); } height++; } } catch (IOException e) { e.printStackTrace(); } cells = new char[height-1][width]; try(BufferedReader bufferedReader = new BufferedReader(new FileReader("D:\\life\\test.txt"))) { String currentLine; int row = 0; while (bufferedReader.ready()) { currentLine = bufferedReader.readLine(); if (row < (height-1)) { for (int i = 0; i < width; i++) { cells[row][i] = currentLine.charAt(i); } } else { generation = Integer.parseInt(currentLine); } row++; } } catch (IOException e) { e.printStackTrace(); } //========================= For Testing ===========================// System.out.println("Ширина: " + width + " Высота: " + (height-1)); for (int i = 0; i < height - 1; i++) { for (int j = 0; j < width; j++) System.out.print(cells[i][j] + " "); System.out.println(); } System.out.println("Количество поколений: " + generation); } public char[][] getCells() { return cells; } public char getCell(int row, int col) { return cells[row][col]; } public void setCells(char[][] cells) { this.cells = cells; } public int getGeneration() { return generation; } public int getHeight() { return height-1; } public int getWidth() { return width; } }
The class in which all the logic is executed:
public class Cell { TextFileReader textFileReader = new TextFileReader(); private char[][] temporalCell; public void action () { textFileReader.readFromFile(); temporalCell = new char[textFileReader.getHeight()][textFileReader.getWidth()]; for (int i = 1; i < textFileReader.getGeneration(); i++) { for (int row = 0; row < textFileReader.getHeight(); row++) { for (int col = 0; col < textFileReader.getWidth(); col++) { int currentNeighbours = neighbours(row, col); if (currentNeighbours > 3 || currentNeighbours < 2) { temporalCell[row][col] = '.'; } else if (currentNeighbours == 3) { temporalCell[row][col] = 'o'; } else { temporalCell[row][col] = textFileReader.getCell(row, col); } } } textFileReader.setCells(temporalCell); } } public int neighbours (int row, int col) { int neighboursCount = 0; for (int i = row - 1; i <= row + 1; i++) { for (int j = col - 1; j <= col + 1; j++) { if (textFileReader.getCell(i, j) == ('o') && (i != row || j != col)) { neighboursCount++; } } } return neighboursCount; } }
The program throws an Exception in the neighbors () method, which is logical, because we go beyond the bounds of the matrix, because we do not have a method that defines the boundaries of the field. But that's not the point. How do I realize the surface of the torus, so that the left border is connected to the right, and the top - from the bottom? I reviewed many examples, but I could not understand how to implement it ((