Main类
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class Main {
public static int matrixSize = 10000;
public static void main(String[] args) throws IOException, InterruptedException {
LifeChess lifeChess = new LifeChess();
lifeChess.setSIZE(matrixSize);//设置棋盘大小
lifeChess.setGENERATIONS(2000);
lifeChess.initializeBoardfromTxt("7.init.txt");//获取初始矩阵
lifeChess.getRes("7.end.txt");//获取目标矩阵
ExecutorService executor = Executors.newFixedThreadPool(8);
long startTime = System.currentTimeMillis();
for (int i = 0; i < lifeChess.getGENERATIONS(); i++) {
lifeChess.initializeMode();
if (lifeChess.checkRes()) {
break;
}
List<Future<Void>> futures = new ArrayList<>();
for (int j = 0; j < matrixSize; j++) {
final int rowIndex = j;
futures.add(executor.submit(() -> {
new Thread_l(lifeChess, matrixSize, rowIndex).run();
return null;
}));
}
for (Future<Void> future : futures) {
try {
future.get(); // 阻塞直到每个任务完成
} catch (ExecutionException e) {
e.printStackTrace(); // 处理异常
}
}
}
long endTime = System.currentTimeMillis();
executor.shutdown();
System.out.println(endTime - startTime+"ms");
}
}
LifeChess类
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
public class LifeChess {
protected int SIZE = 10001; // 棋盘大小
protected int GENERATIONS = 3; // 代数
public int[][] board = new int[SIZE][SIZE];
public int[][] mode = new int[SIZE][SIZE];
protected String str;
public void initializeBoardfromTxt(String fileName) throws IOException {
Path path = Paths.get("test/"+fileName);
String data = Files.readString(path);
for (int j = 0; j < data.length(); j++) {
int k = j /SIZE;
int l = j % SIZE;
this.board[k][l] = data.charAt(j)-'0';
}
}
public void initializeMode() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
this.mode[i][j] = this.board[i][j];
}
}
}
public void getRes(String fileName) throws IOException {
Path path = Paths.get("test/"+fileName);
this.str = Files.readString(path);
}
// 运行生命游戏
public void runGameOfLife() throws IOException {
while (!checkRes()) {
this.board = nextGeneration(this.board);
if (checkRes()) {
printBoard(this.board);
Path outputPath = Paths.get("test/res.txt");
Files.write(outputPath, Arrays.deepToString(this.board).getBytes());
}
}
}
public int[][] nextGeneration(int[][] currentBoard) {
int[][] newBoard = new int[this.SIZE][this.SIZE];
//迭代
for (int i = 0; i < this.SIZE; i++) {
for (int j = 0; j < this.SIZE; j++) {
int alivenbCells = countAliveNeighbors(currentBoard, i, j);
if (currentBoard[i][j] == 1) {
// 规则1和规则2
newBoard[i][j] = (alivenbCells == 2 || alivenbCells == 3) ? 1 : 0;
} else {
// 规则3
newBoard[i][j] = (alivenbCells == 3) ? 1 : 0;
}
}
}
return newBoard;
}
// 计算某细胞周围的状态为生的邻居细胞数量
public int countAliveNeighbors(int[][] board, int x, int y) {
int count = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0) continue; // 自身不算
int nx = x + i;
int ny = y + j;
if (nx >= 0 && nx < this.SIZE && ny >= 0 && ny < this.SIZE) {
count += board[nx][ny];
}
}
}
return count;
}
//打印棋盘
public void printBoard(int[][] board) {
for (int i = 0; i < this.SIZE; i++) {
for (int j = 0; j < this.SIZE; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
System.out.println();
}
public boolean checkRes() {
int k = 0;
for (int i = 0; i < this.SIZE; i++) {
for (int j = 0; j < this.SIZE; j++) {
if(this.board[i][j] != this.str.charAt(k)-'0') {return false;}
k++;
}
}
return true;
}
public LifeChess() {}
public void setSIZE(int size) {
this.SIZE = size;
}
public int getSIZE() {
return SIZE;
}
public void setGENERATIONS(int GENERATIONS) {
this.GENERATIONS = GENERATIONS;
}
public int getGENERATIONS() {
return GENERATIONS;
}
}
Thread_l类
public class Thread_l implements Runnable {
protected int SIZE; // 棋盘大小
public int LINE; // 代数
public LifeChess lifeChess;
@Override
public void run(){
nextGeneration();
}
public Thread_l(LifeChess lifeChess, int SIZE, int LINE){
this.lifeChess = lifeChess;
this.SIZE = SIZE;
this.LINE = LINE;
}
public void nextGeneration() {
//迭代
for (int j = 0; j < SIZE; j++) {
int alivenbCells = countAliveNeighbors(LINE, j);
if (lifeChess.mode[LINE][j] == 1) {
// 规则1和规则2
lifeChess.board[LINE][j] = (alivenbCells == 2 || alivenbCells == 3) ? 1 : 0;
} else {
// 规则3
lifeChess.board[LINE][j] = (alivenbCells == 3) ? 1 : 0;
}
}
}
public int countAliveNeighbors(int x, int y) {
int count = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0) continue; // 自身不算
int nx = x + i;
int ny = y + j;
if (nx >= 0 && nx < SIZE && ny >= 0 && ny < SIZE) {
count += lifeChess.mode[nx][ny];
}
}
}
return count;
}
}
1099



被折叠的 条评论
为什么被折叠?



