首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >产生精英星系

产生精英星系
EN

Code Golf用户
提问于 2015-03-22 21:28:22
回答 1查看 542关注 0票数 3

Introduction

精锐是第一款太空交易电子游戏,由David Braben和Ian Bell编写和开发,并于1984年9月由Acornsoft为BBC显微和橡子电子计算机出版。

精英宇宙包含八个星系,每个星系都有256颗行星可供探索。由于当时8位计算机的能力有限,这些世界是在程序上产生的。一个种子数通过一个固定的算法-适当的次数-运行,并创建一个确定每个行星的完整组成(星系位置、商品价格、名称和本地细节)的数字序列;从查找表中数字选择文本字符串并进行组装,以产生独特的描述,例如有“食肉艺术毕业生”的行星)。这意味着不需要额外的内存来存储每个行星的特性,但是每个行星都是唯一的,并且有固定的属性。在这里阅读更多关于这一点的信息:精英随机数发生器

挑战

就像精英游戏一样,编写一个脚本,在程序上生成8个星系的列表,每个星系包含256个行星。(您需要生成总共2048个行星名称)。

这8个星系的名称可以在手工或程序生成之前定义。这是你的选择。

输出

  • 请在这里张贴您的脚本,也许使用糊状桶或等效主机您的输出。

规则

  • 至少,您的脚本必须生成2048个星球名称和8个星系名称。
  • 每256个行星应该嵌套在一个星系的阵列中。
  • 这是一场人气竞赛!尝试使您的代码聪明,有趣,流畅或复古。
  • 这不是暗号-高尔夫。因此,值得赞赏的是可读的代码。尽管如此,尽量保持较低的大小记住,原来的程序员只有32 or或内存内工作的整个游戏。
  • 不允许连续重复3次以上的行星名称,在总共2048个行星中,单个行星名称的出现次数不得超过5次。
  • 输入是可选的,而不是必需的。
  • 适用标准漏洞

如何获得荣誉

  • 如果你的剧本也产生经济,政府,科技水平,人口,总生产力,平均半径和评论每个星球。
  • 如果你的星球名称看起来是有机的(不一定要用英语),但例如'Lave‘看起来更好,而且比Esdqzfjp更容易阅读。
  • 如果你的星球的名字不包含这样的脏话,“屁股”。我之所以提到这一点,是因为最初的程序员必须这样做。
  • 如果你用BBC Basic做这个挑战的话。
EN

回答 1

Code Golf用户

回答已采纳

发布于 2015-03-23 00:44:23

Java

我只是想从这场比赛开始:)。名字是有机的,是动态生成的。使用任何参数运行以使用交互模式。

代码语言:javascript
运行
复制
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Main {

    private final static int[][] frequencies = new int[][]{   //Generated with names from http://www.minorplanetcenter.net/iau/lists/MPNames.html
            {96, 323, 512, 445, 222, 113, 407, 249, 514, 134, 740, 1287, 975, 3188, 212, 235, 13, 2131, 997, 909, 501, 398, 226, 50, 355, 165, 3223},
            {626, 61, 8, 8, 877, 3, 3, 28, 322, 5, 7, 114, 9, 8, 425, 4, 0, 396, 44, 6, 260, 0, 7, 0, 51, 3, 40},
            {607, 4, 129, 16, 287, 9, 14, 1728, 248, 4, 364, 139, 9, 8, 518, 7, 18, 99, 27, 41, 94, 2, 0, 0, 49, 42, 102},
            {842, 49, 13, 46, 784, 17, 38, 45, 425, 18, 21, 65, 56, 31, 503, 10, 2, 224, 90, 70, 169, 13, 50, 0, 86, 37, 421},
            {326, 203, 264, 375, 284, 128, 231, 143, 513, 67, 261, 1436, 399, 1874, 159, 179, 9, 2873, 940, 680, 206, 402, 180, 64, 317, 125, 1527},
            {161, 7, 3, 1, 246, 170, 6, 6, 128, 5, 2, 61, 16, 5, 122, 3, 0, 253, 16, 28, 105, 3, 3, 0, 4, 0, 122},
            {596, 16, 27, 27, 595, 8, 62, 150, 250, 17, 16, 134, 32, 100, 319, 11, 6, 272, 53, 28, 258, 3, 18, 10, 46, 18, 507},
            {1360, 24, 33, 16, 1001, 2, 9, 14, 1219, 9, 55, 123, 93, 204, 683, 8, 0, 178, 55, 88, 383, 21, 50, 0, 96, 0, 453},
            {1104, 148, 802, 362, 646, 85, 333, 97, 47, 160, 477, 789, 486, 2022, 309, 149, 15, 501, 1007, 614, 137, 142, 65, 43, 146, 147, 1598},
            {309, 5, 6, 13, 183, 1, 2, 2, 224, 1, 15, 2, 4, 11, 354, 1, 0, 3, 15, 6, 132, 2, 0, 0, 8, 2, 103},
            {1094, 15, 10, 9, 614, 6, 7, 166, 718, 9, 39, 133, 32, 36, 737, 9, 0, 137, 92, 20, 343, 17, 25, 1, 153, 2, 494},
            {1219, 105, 66, 280, 1319, 87, 73, 55, 1259, 17, 77, 1034, 142, 35, 622, 80, 4, 38, 209, 146, 271, 67, 21, 0, 224, 33, 612},
            {1790, 139, 135, 14, 591, 6, 6, 21, 859, 9, 16, 16, 120, 23, 599, 121, 5, 24, 87, 11, 252, 3, 12, 0, 78, 7, 249},
            {1139, 159, 291, 744, 1103, 55, 881, 124, 1014, 72, 298, 80, 108, 519, 735, 52, 12, 110, 543, 495, 140, 27, 59, 10, 203, 120, 2308},
            {72, 299, 215, 258, 167, 138, 150, 288, 124, 62, 282, 824, 456, 1643, 200, 199, 5, 1054, 815, 457, 408, 698, 201, 26, 145, 96, 1181},
            {517, 9, 5, 2, 474, 17, 3, 204, 263, 0, 16, 95, 10, 6, 311, 131, 0, 147, 45, 34, 87, 0, 1, 0, 24, 2, 66},
            {1, 0, 0, 0, 1, 0, 0, 0, 30, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 116, 2, 1, 0, 0, 0, 5},
            {1518, 162, 175, 366, 1113, 44, 325, 86, 1469, 18, 204, 176, 211, 294, 1196, 58, 11, 294, 331, 505, 439, 57, 37, 4, 311, 44, 1349},
            {904, 66, 452, 26, 770, 22, 17, 935, 522, 13, 439, 132, 109, 56, 570, 131, 10, 28, 442, 932, 437, 46, 44, 0, 68, 50, 1616},
            {1009, 24, 60, 13, 1026, 20, 21, 607, 618, 29, 47, 74, 52, 35, 862, 12, 0, 399, 365, 481, 180, 18, 30, 1, 90, 125, 657},
            {164, 191, 273, 207, 233, 51, 186, 75, 170, 55, 238, 344, 292, 555, 55, 72, 5, 694, 672, 274, 13, 35, 23, 32, 76, 82, 421},
            {535, 3, 4, 4, 441, 0, 10, 3, 577, 1, 16, 28, 1, 12, 153, 4, 0, 26, 91, 4, 12, 1, 0, 0, 30, 1, 475},
            {516, 5, 2, 5, 289, 4, 2, 43, 290, 0, 17, 32, 11, 26, 109, 2, 0, 25, 57, 1, 33, 0, 1, 0, 13, 0, 49},
            {35, 2, 7, 2, 25, 2, 2, 3, 63, 1, 3, 5, 0, 0, 9, 1, 0, 4, 3, 5, 26, 1, 3, 0, 0, 0, 82},
            {557, 38, 55, 46, 138, 22, 26, 31, 72, 18, 44, 126, 75, 143, 238, 31, 1, 81, 108, 57, 214, 9, 27, 9, 6, 8, 706},
            {241, 12, 4, 22, 196, 0, 8, 146, 142, 0, 17, 11, 16, 15, 87, 2, 1, 11, 23, 11, 122, 10, 13, 0, 19, 49, 206},
            {1282, 1246, 1014, 818, 540, 466, 757, 928, 351, 680, 1207, 835, 1479, 552, 375, 957, 40, 795, 1710, 952, 150, 455, 435, 34, 288, 226, 0}
    };

    private final static int MIN_NAME_LENGTH = 4;

    private final static String INFO = "Type \"galaxy x\" to change galaxy. Type a number to get the name of that planet. Type \"exit\" or any invalid input to exit.";

    private final static Scanner in = new Scanner(System.in);
    private final static PrintStream out = System.out;


    public static void main(String[] args) throws Exception {

        if (args.length == 0){

            long seed = hash(System.nanoTime());

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

                long galaxySeed = (hash(i) ^ seed) + 1;
                out.println("Galaxy " + generateName(new Random(galaxySeed)));

                for (int j = 0; j < 256; j++){

                    long planetSeed = (hash(j) ^ galaxySeed) + 1;
                    out.println(generateName(new Random(planetSeed)));

                }
            }
            return;
        }

        out.println(INFO);

        out.println("Seed?");
        long seed = hash(in.nextLong());

        out.println("Galaxy?");
        long galaxySeed = hash(in.nextInt()) ^ seed;
        in.nextLine();//Get rid of rest of line.

        String galaxyName = generateName(new Random(galaxySeed));
        out.println("The name of your galaxy is " + galaxyName + ".");

        while (true){

            String input = in.nextLine().trim().toLowerCase();

            try {
                if (input.startsWith("galaxy")) {

                    int galaxy = Integer.parseInt(input.substring(6).trim());
                    galaxySeed = (hash(galaxy) ^ seed) + 1;

                    galaxyName = generateName(new Random(galaxySeed));
                    out.println("The name of your galaxy is " + galaxyName + ".");

                } else if (input.startsWith("exit")){
                    return;
                } else {

                    int planet = Integer.parseInt(input);
                    long planetSeed = (hash(planet) ^ galaxySeed) + 1;

                    String planetName = generateName(new Random(planetSeed));
                    out.println("The name of your planet is " + planetName + ".");

                }
            } catch (Exception e){
                return;
            }
        }

    }

    private static String generateName(Random rand) {

        int previousChar = randomIndex(frequencies[26], rand);
        String name = "" + (char)(previousChar + 'A');

        for (;;){

            int[] frequency = frequencies[previousChar].clone();

            frequency[26] *= Math.max(name.length() - (MIN_NAME_LENGTH - 1), 0) ; //Hack, reduces name lengths.

            int nextChar = randomIndex(frequency, rand);

            if (nextChar == 26){
                return name;
            }

            previousChar = nextChar;

            name += (char) (nextChar + 'a');
        }
    }

    private static int randomIndex(int[] frequencies, Random rand) {

        int total = Arrays.stream(frequencies).sum();

        int n = rand.nextInt(total);
        int i = 0;

        for (int frequency : frequencies){
            n -= frequency;
            if (n < 0){
                return i;
            }
            i++;
        }

        throw new IllegalStateException();
    }

    private static long hash(long i) {
        return (int) ((131111L*i)^i^(1973*i)%7919); //http://stackoverflow.com/a/9640543/4230423
    }
}

它使用与行星名称相同的代码生成星系名称。以下是一些示例名称:

代码语言:javascript
运行
复制
Kanipeso
Danlerg
Deigaji
Kanialu
Fulla
Tirr
Smanger
Anikuraba
Dotia

8个星系的全部输出,看这儿

票数 5
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/48077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档