<commit_before>package com.github.forhacks.evolve2048;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.*;

public class Evolution {

    private static final int PLAYER_NUM = 5;
    private static final int NUM_TRIAL = 5;
    private static final int GEN_NUM = 10;
    private static final double KILL_RATE = 0.5;

    Player[] players = new Player[PLAYER_NUM];

    public Evolution() throws InterruptedException, ExecutionException {

        for (int i = 0; i < PLAYER_NUM; i++) {
            players[i] = Player.generate();
        }
        for (int i = 0; i < GEN_NUM; i++) {

            int[] scores = run();

            Integer[] indices = new Integer[PLAYER_NUM];

            for (int j = 0; j < indices.length; j++) {
                indices[j] = j;
            }

            Arrays.sort(indices, Comparator.comparingInt((Integer o) -> (-scores[o])));
            for (int z : indices)
                System.out.println(z);
            System.out.println();
            for (int z : scores) {
                System.out.println(z);
            }

            Player[] tempPlayers = players.clone();
            for (int j = 0; j < indices.length; j++) {
                players[j] = tempPlayers[indices[j]];
            }

            for (int j = 0; j < PLAYER_NUM * KILL_RATE; j++) {
                players[j + (int) (PLAYER_NUM * KILL_RATE)] = players[j].clone();
                players[j].mutate();
            }

            Main.g.initGame();

            for (;;) {
                if (!Main.g.game.game) {
                    break;
                }
                Main.g.move(players[0].run(Main.g.game));
                Thread.sleep(20);
            }
        }
    }

    public boolean isRipple() {
        int[][] grid = Main.g.game.grid;
        if ((grid[0][2] == grid[1][1]) && (grid[0][2] == grid[2][0])) {
            return true;
        } else if ((grid[1][3] == grid[2][2]) && (grid[1][3] == grid[3][1])) {
            return true;
        } else if (((grid[0][3] == grid[1][2]) && (grid[0][3] == grid[2][1])) ||
                ((grid[0][3] == grid[1][2]) && (grid[0][3] == grid[3][0])) ||
                ((grid[0][3] == grid[2][1]) && (grid[0][3] == grid[3][0])) ||
                ((grid[1][2] == grid[2][1]) && (grid[1][2] == grid[3][0]))) {
            return true;
        }
//        if ((grid[0][1] == grid[1][2]) && (grid[0][1] == grid[2][3])) {
//            return true;
//        } else if ((grid[1][0] == grid[2][1]) && (grid[1][0] == grid[3][2])) {
//            return true;
//        } else if (((grid[0][0] == grid[1][1]) && (grid[0][0] == grid[2][2])) ||
//                ((grid[0][0] == grid[1][1]) && (grid[0][0] == grid[3][3])) ||
//                ((grid[0][0] == grid[2][2]) && (grid[0][0] == grid[3][3])) ||
//                ((grid[1][1] == grid[2][2]) && (grid[1][1] == grid[3][3]))) {
//            return true;
//        }
        return false;
    }

    public int[] run() throws InterruptedException, ExecutionException {

        List<Callable<Integer>> threads = new ArrayList<>();

        ExecutorService executor = Executors.newFixedThreadPool(PLAYER_NUM);

        for (int i = 0; i < PLAYER_NUM; i++) {

            final int z = i;

            Callable<Integer> thread = () -> {

                int score = 0;

                for (int j = 0; j < NUM_TRIAL; j++) {
                    Game game = new Game();
                    for (;;) {
                        if (!game.game) {
                            break;
                        }
                        int r = players[z].run(game);
                        System.out.println(r);
                        game.move(r);
                    }
                    score += game.score;
                }

                return score / NUM_TRIAL;

            };

            threads.add(thread);

        }

        List<Future<Integer>> scores = executor.invokeAll(threads);

        int[] result = new int[PLAYER_NUM];
<commit_msg>Changes to Evolution<commit_after>package com.github.forhacks.evolve2048;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.*;

public class Evolution {

    private static final int PLAYER_NUM = 5;
    private static final int NUM_TRIAL = 5;
    private static final int GEN_NUM = 10;
    private static final double KILL_RATE = 0.5;

    Player[] players = new Player[PLAYER_NUM];

    public Evolution() throws InterruptedException, ExecutionException {

        for (int i = 0; i < PLAYER_NUM; i++) {
            players[i] = Player.generate();
        }
        for (int i = 0; i < GEN_NUM; i++) {

            int[] scores = run();

            Integer[] indices = new Integer[PLAYER_NUM];

            for (int j = 0; j < indices.length; j++) {
                indices[j] = j;
            }

            Arrays.sort(indices, Comparator.comparingInt((Integer o) -> (-scores[o])));
//            for (int z : indices)
//                System.out.println(z);
//            System.out.println();
//            for (int z : scores) {
//                System.out.println(z);
//            }

            Player[] tempPlayers = players.clone();
            for (int j = 0; j < indices.length; j++) {
                players[j] = tempPlayers[indices[j]];
            }

            for (int j = 0; j < PLAYER_NUM * KILL_RATE; j++) {
                players[j + (int) (PLAYER_NUM * KILL_RATE)] = players[j].clone();
                players[j].mutate();
            }

            Main.g.initGame();

            for (;;) {
                if (!Main.g.game.game) {
                    break;
                }
                Main.g.move(players[0].run(Main.g.game));
                Thread.sleep(20);
            }
        }
    }

    public boolean isRipple() {
        int[][] grid = Main.g.game.grid;
        if ((grid[0][2] == grid[1][1]) && (grid[0][2] == grid[2][0])) {
            return true;
        } else if ((grid[1][3] == grid[2][2]) && (grid[1][3] == grid[3][1])) {
            return true;
        } else if (((grid[0][3] == grid[1][2]) && (grid[0][3] == grid[2][1])) ||
                ((grid[0][3] == grid[1][2]) && (grid[0][3] == grid[3][0])) ||
                ((grid[0][3] == grid[2][1]) && (grid[0][3] == grid[3][0])) ||
                ((grid[1][2] == grid[2][1]) && (grid[1][2] == grid[3][0]))) {
            return true;
        }
//        if ((grid[0][1] == grid[1][2]) && (grid[0][1] == grid[2][3])) {
//            return true;
//        } else if ((grid[1][0] == grid[2][1]) && (grid[1][0] == grid[3][2])) {
//            return true;
//        } else if (((grid[0][0] == grid[1][1]) && (grid[0][0] == grid[2][2])) ||
//                ((grid[0][0] == grid[1][1]) && (grid[0][0] == grid[3][3])) ||
//                ((grid[0][0] == grid[2][2]) && (grid[0][0] == grid[3][3])) ||
//                ((grid[1][1] == grid[2][2]) && (grid[1][1] == grid[3][3]))) {
//            return true;
//        }
        return false;
    }

    public int[] run() throws InterruptedException, ExecutionException {

        List<Callable<Integer>> threads = new ArrayList<>();

        ExecutorService executor = Executors.newFixedThreadPool(PLAYER_NUM);

        for (int i = 0; i < PLAYER_NUM; i++) {

            final int z = i;

            Callable<Integer> thread = () -> {

                int score = 0;

                for (int j = 0; j < NUM_TRIAL; j++) {
                    Game game = new Game();
                    for (;;) {
                        if (!game.game) {
                            break;
                        }
                        int r = players[z].run(game);
                        game.move(r);
                    }
                    score += game.score;
                }

                return score / NUM_TRIAL;

            };

            threads.add(thread);

        }

        List<Future<Integer>> scores = executor.invokeAll(threads);

        int[] result = new int[PLAYER_NUM];

{'fraction_non_alphanumeric': 0.1860953321807854, 'fraction_numerical': 0.028896023709557916, 'mean_word_length': 1.5230529595015576, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6031211', 'n_tokens_mistral': 2653, 'n_tokens_neox': 2488, 'n_words': 537}