@jutge.org/toolkit 4.2.22 → 4.2.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/assets/problems/games/the-walking-dead.pbm/README.md +12 -0
  2. package/assets/problems/games/the-walking-dead.pbm/ca/Doc/Makefile +6 -0
  3. package/assets/problems/games/the-walking-dead.pbm/ca/Doc/api.tex +246 -0
  4. package/assets/problems/games/the-walking-dead.pbm/ca/Doc/defs.tex +2 -0
  5. package/assets/problems/games/the-walking-dead.pbm/ca/Doc/main.tex +63 -0
  6. package/assets/problems/games/the-walking-dead.pbm/ca/Doc/programming.tex +452 -0
  7. package/assets/problems/games/the-walking-dead.pbm/ca/Doc/rules.tex +222 -0
  8. package/assets/problems/games/the-walking-dead.pbm/ca/Doc/screenshot.png +0 -0
  9. package/assets/problems/games/the-walking-dead.pbm/ca/Doc/tips.tex +81 -0
  10. package/assets/problems/games/the-walking-dead.pbm/ca/Doc/twd.png +0 -0
  11. package/assets/problems/games/the-walking-dead.pbm/ca/Doc/viewer.tex +41 -0
  12. package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/Makefile +6 -0
  13. package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/api.tex +246 -0
  14. package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/defs.tex +2 -0
  15. package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/main.tex +63 -0
  16. package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/programming.tex +176 -0
  17. package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/rules.tex +194 -0
  18. package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/screenshot.png +0 -0
  19. package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/tips.tex +85 -0
  20. package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/twd.png +0 -0
  21. package/assets/problems/games/the-walking-dead.pbm/ca/Doc-eng/viewer.tex +36 -0
  22. package/assets/problems/games/the-walking-dead.pbm/ca/Obj/AIDummy.o.Linux64 +0 -0
  23. package/assets/problems/games/the-walking-dead.pbm/ca/Obj/AIDummy.o.Linux64.Debug +0 -0
  24. package/assets/problems/games/the-walking-dead.pbm/ca/Obj/AIDummy.o.MacOS +0 -0
  25. package/assets/problems/games/the-walking-dead.pbm/ca/Obj/AIDummy.o.MacOS.ARM +0 -0
  26. package/assets/problems/games/the-walking-dead.pbm/ca/Obj/AIDummy.o.MacOS.ARM.Debug +0 -0
  27. package/assets/problems/games/the-walking-dead.pbm/ca/Obj/AIDummy.o.MacOS.Debug +0 -0
  28. package/assets/problems/games/the-walking-dead.pbm/ca/Obj/Board.o.Linux64 +0 -0
  29. package/assets/problems/games/the-walking-dead.pbm/ca/Obj/Board.o.Linux64.Debug +0 -0
  30. package/assets/problems/games/the-walking-dead.pbm/ca/Obj/Board.o.MacOS +0 -0
  31. package/assets/problems/games/the-walking-dead.pbm/ca/Obj/Board.o.MacOS.ARM +0 -0
  32. package/assets/problems/games/the-walking-dead.pbm/ca/Obj/Board.o.MacOS.ARM.Debug +0 -0
  33. package/assets/problems/games/the-walking-dead.pbm/ca/Obj/Board.o.MacOS.Debug +0 -0
  34. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/AIDemo.cc +89 -0
  35. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/AIDummy.cc +202 -0
  36. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/AINull.cc +37 -0
  37. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Action.cc +34 -0
  38. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Action.hh +107 -0
  39. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Board.cc +975 -0
  40. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Board.hh +287 -0
  41. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Defs.hh +2 -0
  42. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Game.cc +55 -0
  43. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Game.hh +23 -0
  44. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Info.cc +174 -0
  45. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Info.hh +129 -0
  46. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Main.cc +83 -0
  47. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Makefile +56 -0
  48. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Player.cc +66 -0
  49. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Player.hh +63 -0
  50. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/README.txt +9 -0
  51. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Random.cc +3 -0
  52. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Random.hh +88 -0
  53. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Registry.cc +28 -0
  54. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Registry.hh +42 -0
  55. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/SecGame.cc +368 -0
  56. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/SecGame.hh +90 -0
  57. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/SecMain.cc +99 -0
  58. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Settings.cc +67 -0
  59. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Settings.hh +175 -0
  60. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/State.cc +3 -0
  61. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/State.hh +188 -0
  62. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Structs.cc +3 -0
  63. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Structs.hh +377 -0
  64. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Utils.cc +3 -0
  65. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/Utils.hh +77 -0
  66. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/default-fixed.cnf +120 -0
  67. package/assets/problems/games/the-walking-dead.pbm/ca/Runner/default.cnf +17 -0
  68. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_diagonals-thick_18_b81900_40x40.png +0 -0
  69. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_diagonals-thick_20_666666_40x40.png +0 -0
  70. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_flat_10_000000_40x100.png +0 -0
  71. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_glass_100_f6f6f6_1x400.png +0 -0
  72. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_glass_100_fdf5ce_1x400.png +0 -0
  73. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  74. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_gloss-wave_35_f6a828_500x100.png +0 -0
  75. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png +0 -0
  76. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-bg_highlight-soft_75_ffe45c_1x100.png +0 -0
  77. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-icons_222222_256x240.png +0 -0
  78. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-icons_228ef1_256x240.png +0 -0
  79. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-icons_ef8c08_256x240.png +0 -0
  80. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-icons_ffd27a_256x240.png +0 -0
  81. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/images/ui-icons_ffffff_256x240.png +0 -0
  82. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/css/ui-lightness/jquery-ui-1.8.18.custom.css +310 -0
  83. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/help.html +18 -0
  84. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/img/but_close.png +0 -0
  85. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/img/but_end.png +0 -0
  86. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/img/but_help.png +0 -0
  87. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/img/but_pause.png +0 -0
  88. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/img/but_play.png +0 -0
  89. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/img/but_refresh.png +0 -0
  90. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/img/but_start.png +0 -0
  91. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/img/logo.png +0 -0
  92. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/js/jquery-1.7.1.min.js +4 -0
  93. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/js/jquery-ui-1.8.18.custom.min.js +49 -0
  94. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/sample.out +52807 -0
  95. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/viewer.html +118 -0
  96. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/viewer.js +846 -0
  97. package/assets/problems/games/the-walking-dead.pbm/ca/Viewer/viewer.sh +28 -0
  98. package/assets/problems/games/the-walking-dead.pbm/ca/handler.yml +6 -0
  99. package/assets/problems/games/the-walking-dead.pbm/ca/problem.ca.yml +3 -0
  100. package/assets/prompts/creators/create-solution.tpl.txt +15 -8
  101. package/assets/prompts/proglangs/cc.md +6 -2
  102. package/assets/prompts/proglangs/py.md +10 -6
  103. package/dist/index.js +370 -366
  104. package/docs/getting-started-guide.md +1 -1
  105. package/docs/install-linux.md +1 -1
  106. package/docs/install-macos.md +1 -1
  107. package/docs/install-windows.md +1 -1
  108. package/package.json +11 -11
  109. package/toolkit/ask.ts +0 -2
  110. package/toolkit/doctor.ts +1 -1
  111. package/toolkit/make.ts +2 -2
@@ -0,0 +1,83 @@
1
+ //////// STUDENTS DO NOT NEED TO READ BELOW THIS LINE ////////
2
+
3
+ #include "Game.hh"
4
+
5
+
6
+ void help (int argc, char** argv) {
7
+ cout << "Usage: " << argv[0] << " [options] player1 player2 ... [< default.cnf] [> default.out] " << endl;
8
+ cout << "Available options:" << endl;
9
+ cout << "--seed=seed -s seed set random seed" << endl;
10
+ cout << "--input=file -i input set input file (default: stdin)" << endl;
11
+ cout << "--output=file -o output set output file (default: stdout)" << endl;
12
+ cout << "--list -l list registered players" << endl;
13
+ cout << "--version -v print version" << endl;
14
+ cout << "--help -h print help" << endl;
15
+ }
16
+
17
+
18
+ int main (int argc, char** argv) {
19
+ if (argc == 1) {
20
+ help(argc, argv);
21
+ return EXIT_SUCCESS;
22
+ }
23
+
24
+ struct option long_options[] = {
25
+ { "seed", required_argument, 0, 's' },
26
+ { "input", required_argument, 0, 'i' },
27
+ { "output", required_argument, 0, 'o' },
28
+ { "list", no_argument, 0, 'l' },
29
+ { "version", no_argument, 0, 'v' },
30
+ { "help", no_argument, 0, 'h' },
31
+ { 0, 0, 0, 0 }
32
+ };
33
+
34
+ char* ifile = 0;
35
+ char* ofile = 0;
36
+ int seed = -1;
37
+ vector<string> names;
38
+
39
+ while (true) {
40
+ int index = 0;
41
+ int c = getopt_long(argc, argv, "s:i:o:lvh", long_options, &index);
42
+ if (c == -1) break;
43
+
44
+ switch (c) {
45
+ case 's':
46
+ seed = string_to_int(optarg);
47
+ break;
48
+ case 'i':
49
+ ifile = optarg;
50
+ break;
51
+ case 'o':
52
+ ofile = optarg;
53
+ break;
54
+ case 'l':
55
+ Registry::print_players(cout);
56
+ return EXIT_SUCCESS;
57
+ case 'v':
58
+ cout << Board::version() << endl;
59
+ cout << "compiled " << __TIME__ << " " << __DATE__ << endl;
60
+ return EXIT_SUCCESS;
61
+ case 'h':
62
+ help(argc, argv);
63
+ return EXIT_SUCCESS;
64
+ default:
65
+ return EXIT_FAILURE;
66
+ }
67
+ }
68
+
69
+ while (optind < argc) {
70
+ names.push_back(argv[optind++]);
71
+ _my_assert(names.back().size() <= 12, "Player name too long.");
72
+ }
73
+
74
+ _my_assert(seed >= 0, "Missing seed?");
75
+
76
+ istream* is = ifile ? new ifstream(ifile) : &cin;
77
+ ostream* os = ofile ? new ofstream(ofile) : &cout;
78
+
79
+ Game::run(names, *is, *os, seed);
80
+
81
+ if (ifile) delete is;
82
+ if (ofile) delete os;
83
+ }
@@ -0,0 +1,56 @@
1
+ DUMMY_OBJ = AIDummy.o
2
+
3
+ # Add here any extra .o player files you want to link to the executable
4
+ EXTRA_OBJ =
5
+
6
+ # Configuration
7
+ OPTIMIZE = 3 # Optimization level (0 to 3)
8
+ DEBUG = 0 # Compile for debugging (0 or 1)
9
+ PROFILE = 0 # Compile for profile (0 or 1)
10
+
11
+ # For debugging matches against Dummy
12
+ # OPTIMIZE = 0, DEBUG = 1
13
+
14
+ # For debugging matches among your players
15
+ # OPTIMIZE = 0, DEBUG = 1 and add -D_GLIBCXX_DEBUG at the end of DEBUG_FLAGS
16
+
17
+ # Flags
18
+ ifeq ($(strip $(PROFILE)),1)
19
+ PROFILEFLAGS=-pg
20
+ endif
21
+
22
+ ifeq ($(strip $(DEBUG)),1)
23
+ DEBUGFLAGS=-g -O0 -fno-inline -D_GLIBCXX_DEBUG
24
+ endif
25
+
26
+ CXXFLAGS = -std=c++17 -Wall -Wno-unused-variable -fPIC $(PROFILEFLAGS) $(DEBUGFLAGS) -O$(strip $(OPTIMIZE))
27
+ LDFLAGS = -std=c++11 $(PROFILEFLAGS) $(DEBUGFLAGS) -O$(strip $(OPTIMIZE))
28
+
29
+
30
+ # The following two lines will detect all your players (files matching "AI*.cc")
31
+
32
+ PLAYERS_SRC = $(wildcard AI*.cc)
33
+ PLAYERS_OBJ = $(patsubst %.cc, %.o, $(PLAYERS_SRC)) $(EXTRA_OBJ) $(DUMMY_OBJ)
34
+
35
+ # Rules
36
+
37
+ OBJ = Structs.o Settings.o State.o Info.o Random.o Board.o Action.o Player.o Registry.o Utils.o
38
+
39
+ all: Game
40
+
41
+ clean:
42
+ rm -rf Game SecGame *.o *.exe Makefile.deps
43
+
44
+ Game: $(OBJ) Game.o Main.o $(PLAYERS_OBJ)
45
+ $(CXX) $^ -o $@ $(LDFLAGS)
46
+
47
+ SecGame: $(OBJ) SecGame.o SecMain.o
48
+ $(CXX) $^ -o $@ $(LDFLAGS) -lrt
49
+
50
+ %.exe: %.o $(OBJ) SecGame.o SecMain.o
51
+ $(CXX) $^ -o $@ $(LDFLAGS) -lrt
52
+
53
+ Makefile.deps: *.cc
54
+ $(CXX) $(CXXFLAGS) -MM *.cc > Makefile.deps
55
+
56
+ include Makefile.deps
@@ -0,0 +1,66 @@
1
+ //////// STUDENTS DO NOT NEED TO READ BELOW THIS LINE ////////
2
+
3
+ #include "Player.hh"
4
+
5
+ void Player::reset (ifstream& is) {
6
+
7
+ // Should read what Board::print_state() prints.
8
+ // Should fill the same data structures as
9
+ // Board::Board (istream& is, int seed), except for settings and names.
10
+ // THESE DATA STRUCTURES MUST BE RESET: maps WITH clear(), etc.
11
+
12
+
13
+ *(Action*)this = Action();
14
+
15
+ units .clear();
16
+ player2alive_units.clear();
17
+ player2dead_units .clear();
18
+ zombies_ .clear();
19
+ nb_cells .clear();
20
+
21
+ player2alive_units = vector<set<int>>(num_players());
22
+ player2dead_units = vector<set<int>>(num_players());
23
+ nb_cells = vector<int>(num_players(),0);
24
+
25
+ read_grid(is);
26
+
27
+ string s;
28
+ is >> s >> rnd;
29
+ _my_assert(s == "round", "Expected 'round' while parsing.");
30
+ _my_assert(rnd >= 0 and rnd < num_rounds(), "Round is not ok.");
31
+
32
+ is >> s;
33
+ _my_assert(s == "score", "Expected 'score' while parsing.");
34
+ scr = vector<int>(num_players());
35
+ for (auto& s : scr) {
36
+ is >> s;
37
+ _my_assert(s >= 0, "Score cannot be negative.");
38
+ }
39
+
40
+ is >> s;
41
+ _my_assert(s == "scr_acc", "Expected 'scr_acc' while parsing.");
42
+ scr_accumulated = vector<int>(num_players());
43
+ for (auto& s : scr_accumulated) {
44
+ is >> s;
45
+ _my_assert(s >= 0, "Accumulates scores cannot be negative.");
46
+ }
47
+
48
+ is >> s;
49
+ _my_assert(s == "strength", "Expected 'strength' while parsing.");
50
+ overall_strength = vector<int>(num_players());
51
+ for (auto& s : overall_strength) {
52
+ is >> s;
53
+ _my_assert(s >= 0, "Strength cannot be negative.");
54
+ }
55
+
56
+ is >> s;
57
+ _my_assert(s == "status", "Expected 'status' while parsing.");
58
+ stats = vector<double>(num_players());
59
+ for (auto& st : stats) {
60
+ is >> st;
61
+ _my_assert(st == -1 or (st >= 0 and st <= 1), "Status is not ok.");
62
+ }
63
+
64
+ _my_assert(ok(), "Invariants are not satisfied.");
65
+ // forget();
66
+ }
@@ -0,0 +1,63 @@
1
+ #ifndef Player_hh
2
+ #define Player_hh
3
+
4
+
5
+ #include "Info.hh"
6
+ #include "Action.hh"
7
+ #include "Random.hh"
8
+ #include "Registry.hh"
9
+
10
+
11
+ /***
12
+ * Abstract base class for players.
13
+ * *
14
+ * This class uses multiple inheritance from Info and Action,
15
+ * so that their public operations can been used from within Player.
16
+ *
17
+ * In order to create new players, inherit from this class and register them.
18
+ * See for example AINull.cc and AIDemo.cc.
19
+ */
20
+ class Player : public Info, public Random_generator, public Action {
21
+
22
+ public:
23
+
24
+ /**
25
+ * Identifier of my player.
26
+ */
27
+ int me () const;
28
+
29
+ //////// STUDENTS DO NOT NEED TO READ BELOW THIS LINE ////////
30
+
31
+ /**
32
+ * Play intelligence. Will be overwritten, thus declared virtual.
33
+ */
34
+ virtual void play () {
35
+ };
36
+
37
+ /**
38
+ * Empty constructor.
39
+ */
40
+ Player () { }
41
+
42
+ private:
43
+
44
+ friend class Game;
45
+ friend class SecGame;
46
+
47
+ int me_;
48
+
49
+ inline void reset (const Info& info) {
50
+ *static_cast<Action*>(this) = Action();
51
+ *static_cast<State*>(this) = (State) info;
52
+ // forget();
53
+ }
54
+
55
+ void reset (ifstream& is);
56
+ };
57
+
58
+
59
+ inline int Player::me () const {
60
+ return me_;
61
+ };
62
+
63
+ #endif
@@ -0,0 +1,9 @@
1
+
2
+ title: ThePurge
3
+
4
+ author(s): Albert Oliveras
5
+
6
+
7
+ contact: oliveras@cs.upc.edu
8
+
9
+ (c) Universitat Politècnica de Catalunya, 2022
@@ -0,0 +1,3 @@
1
+ //////// STUDENTS DO NOT NEED TO READ BELOW THIS LINE ////////
2
+
3
+ #include "Random.hh"
@@ -0,0 +1,88 @@
1
+ #ifndef Random_hh
2
+ #define Random_hh
3
+
4
+
5
+ #include "Utils.hh"
6
+
7
+
8
+ /**
9
+ * Defines a random generator.
10
+ */
11
+ class Random_generator {
12
+
13
+ public:
14
+
15
+ /**
16
+ * Returns a random integer in [l..u]. u - l + 1 must be between 1 and 10^6.
17
+ */
18
+ int random (int l, int u);
19
+
20
+ /**
21
+ * Returns a random permutation of [0..n-1]. n must be between 0 and 10^6.
22
+ */
23
+ vector<int> random_permutation (int n);
24
+
25
+
26
+ //////// STUDENTS DO NOT NEED TO READ BELOW THIS LINE ////////
27
+
28
+
29
+ private:
30
+
31
+ friend class Board;
32
+ friend class Game;
33
+ friend class SecGame;
34
+
35
+ static const long long RANDOM_MOD = ((long long)1)<<31;
36
+ static const long long RANDOM_MASK = RANDOM_MOD - 1;
37
+
38
+ long long rnd_seed;
39
+
40
+ /**
41
+ * Sets random seed.
42
+ */
43
+ void set_random_seed (int s) {
44
+ if (s < 0) s = -s;
45
+ rnd_seed = s & RANDOM_MASK;
46
+ }
47
+
48
+ /**
49
+ * Computes next seed from current seed.
50
+ */
51
+ void next_rnd () {
52
+ rnd_seed = (843314861*rnd_seed + 453816693) & RANDOM_MASK;
53
+ }
54
+
55
+ /**
56
+ * Returns a real random number in [0, 1).
57
+ */
58
+ double uniform() {
59
+ next_rnd();
60
+ return double(rnd_seed)/RANDOM_MOD;
61
+ }
62
+
63
+ };
64
+
65
+
66
+ inline int Random_generator::random (int l, int u) {
67
+ if (l > u) return l; // wrong interval
68
+
69
+ long long m = (long long)u - (long long)l + 1;
70
+ if (m > 1e6) return l; // interval too long
71
+
72
+ next_rnd();
73
+ return l + int((u - l + 1)*uniform()); //...
74
+ }
75
+
76
+ /**
77
+ * Returns a random permutation of [0..n-1]. n must be between 0 and 10^6.
78
+ */
79
+ inline vector<int> Random_generator::random_permutation (int n) {
80
+ if (n < 0 or n > 1e6) return vector<int>(0); // wrong n
81
+
82
+ vector<int> v(n);
83
+ for (int i = 0; i < n; ++i) v[i] = i;
84
+ for (int i = 0; i < n; ++i) swap(v[i], v[random(i, n - 1)]);
85
+ return v;
86
+ }
87
+
88
+ #endif
@@ -0,0 +1,28 @@
1
+ //////// STUDENTS DO NOT NEED TO READ BELOW THIS LINE ////////
2
+
3
+ #include "Registry.hh"
4
+
5
+
6
+ typedef map<string, Registry::Factory> dict_;
7
+
8
+
9
+ dict_* reg_ = 0;
10
+
11
+
12
+ int Registry::Register (const char* name, Factory factory) {
13
+ if (reg_ == 0) reg_ = new dict_();
14
+ (*reg_)[name] = factory;
15
+ return 999;
16
+ }
17
+
18
+
19
+ Player* Registry::new_player (string name) {
20
+ auto it = reg_->find(name);
21
+ _my_assert(it != reg_->end(), "Player " + name + " not registered.");
22
+ return (it->second)();
23
+ }
24
+
25
+
26
+ void Registry::print_players (ostream& os) {
27
+ for (const auto& it : *reg_) cout << it.first << endl;
28
+ }
@@ -0,0 +1,42 @@
1
+ //////// STUDENTS DO NOT NEED TO READ BELOW THIS LINE ////////
2
+
3
+ #ifndef Registry_hh
4
+ #define Registry_hh
5
+
6
+
7
+ #include "Utils.hh"
8
+
9
+
10
+ /**
11
+ * Magic to register players.
12
+ */
13
+
14
+
15
+ class Player;
16
+
17
+
18
+ /**
19
+ * Since the main program does not know how many players will be inherited
20
+ * from the Player class, we use a registration and factory pattern.
21
+ */
22
+ class Registry {
23
+
24
+ public:
25
+
26
+ typedef Player* (*Factory)();
27
+
28
+ static int Register (const char* name, Factory fact);
29
+
30
+ static Player* new_player (string name);
31
+
32
+ static void print_players (ostream& os);
33
+
34
+ };
35
+
36
+
37
+ #define _stringification(s) #s
38
+ #define RegisterPlayer(x) static int registration = \
39
+ Registry::Register(_stringification(x), x::factory)
40
+
41
+
42
+ #endif