.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_random_strategy.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_random_strategy.py: .. _l-example-with-dummy-strategy: Tries a random strategy and show the results ============================================ The following examples runs the game 2048 and keeps the highest number obtained for every try and two strategies. .. GENERATED FROM PYTHON SOURCE LINES 11-19 .. code-block:: Python import numpy import matplotlib.pyplot as plt from pystrat2048.random_strategy import ( random_strategy, random_strategy_all_but_one ) from pystrat2048 import evaluate_strategy, Game2048 .. GENERATED FROM PYTHON SOURCE LINES 20-23 The first strategy :func:`random_strategy ` draws a random direction. .. GENERATED FROM PYTHON SOURCE LINES 23-29 .. code-block:: Python gen1 = evaluate_strategy(random_strategy, 50) res1 = list(gen1) res1.sort() print(res1) .. rst-class:: sphx-glr-script-out .. code-block:: none [16, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128] .. GENERATED FROM PYTHON SOURCE LINES 30-33 The second strategy :func:`random_strategy_all_but_one ` draws a random direction among four except one. .. GENERATED FROM PYTHON SOURCE LINES 33-39 .. code-block:: Python gen2 = evaluate_strategy(random_strategy_all_but_one, 50) res2 = list(gen2) res2.sort() print(res2) .. rst-class:: sphx-glr-script-out .. code-block:: none [16, 16, 16, 16, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128] .. GENERATED FROM PYTHON SOURCE LINES 40-41 Finaly plots the gains obtained by the two strategies. .. GENERATED FROM PYTHON SOURCE LINES 41-50 .. code-block:: Python fig, ax = plt.subplots(1, 1, figsize=(8, 4)) ax.bar(numpy.arange(len(res1)), res1, color="b", label="random", width=0.4) ax.bar(numpy.arange(len(res2)) + 0.4, res2, color="orange", label="all_but_one", width=0.4) ax.set_title("Compares two strategies for 2048.") ax.legend() .. image-sg:: /auto_examples/images/sphx_glr_plot_random_strategy_001.png :alt: Compares two strategies for 2048. :srcset: /auto_examples/images/sphx_glr_plot_random_strategy_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 51-54 Now a custom strategy. This one tries every direction and chooses the direction which keeps the most empty cells. .. GENERATED FROM PYTHON SOURCE LINES 54-73 .. code-block:: Python def look_into_every_direction_choose_best(game, state, moves): """ The strategy tries every direction and chooses the direction which keeps the most empty cells. """ best = None bestd = None for d in range(0, 4): g = Game2048(game.copy()) g.play(d) empty = numpy.sum(g.game.ravel() == 0) if best is None or empty > best: best = empty bestd = d return bestd .. GENERATED FROM PYTHON SOURCE LINES 74-75 Let's play 50 games. .. GENERATED FROM PYTHON SOURCE LINES 75-82 .. code-block:: Python gen3 = evaluate_strategy(look_into_every_direction_choose_best, 50) res3 = list(gen3) res3.sort() print(res3) .. rst-class:: sphx-glr-script-out .. code-block:: none [32, 32, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 256, 256, 256, 256, 256, 256, 256, 256] .. GENERATED FROM PYTHON SOURCE LINES 83-84 Finaly plots the gains obtained by the three strategies. .. GENERATED FROM PYTHON SOURCE LINES 84-96 .. code-block:: Python fig, ax = plt.subplots(1, 1, figsize=(8, 4)) ax.bar(numpy.arange(len(res1)), res1, color="b", label="random", width=0.27) ax.bar(numpy.arange(len(res2)) + 0.27, res2, color="orange", label="all_but_one", width=0.27) ax.bar(numpy.arange(len(res3)) + 0.54, res3, color="limegreen", label="best_empty", width=0.37) ax.set_title("Compares three strategies for 2048.") ax.legend() .. image-sg:: /auto_examples/images/sphx_glr_plot_random_strategy_002.png :alt: Compares three strategies for 2048. :srcset: /auto_examples/images/sphx_glr_plot_random_strategy_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 97-99 One seems better but 50 tries does not seem to be enough to be fully sure. .. GENERATED FROM PYTHON SOURCE LINES 99-100 .. code-block:: Python plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 23.984 seconds) .. _sphx_glr_download_auto_examples_plot_random_strategy.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_random_strategy.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_random_strategy.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_