Advertisement
Guest User

Program.s

a guest
May 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.IO;
  5.  
  6. namespace Genetics
  7. {
  8.  
  9.     internal class Program
  10.     {
  11.  
  12.         private const string PathForTest = "maybebest.save";
  13.         private const string PathBotToSubmit = "../../save/bot.save";
  14.  
  15.         public static void Main ( string[] args )
  16.         {
  17.             RessourceLoad.InitMap ();
  18.  
  19.             string[] levels = GetLevels ();
  20.  
  21.             RessourceLoad.SetCurrentMap ( "flat" );
  22.             NewTraining ( 5 );
  23.  
  24.             foreach ( string level in levels )
  25.             {
  26.                 RessourceLoad.SetCurrentMap ( level );
  27.                 Train ( 5 );
  28.             }
  29.  
  30.             /*
  31.             Thread t1 = new Thread(() => Train(10000));
  32.             RessourceLoad.SetCurrentMap("long");
  33.             t1.Start();
  34.             */
  35.  
  36.             /*
  37.             Thread t3 = new Thread(() => Train(10);
  38.             RessourceLoad.SetCurrentMap(levels[2]);
  39.             t3.Start();
  40.            
  41.             if(!t1.IsAlive && !t2.IsAlive && !t3.IsAlive)
  42.             */
  43.             SaveBest ();
  44.         }
  45.  
  46.         private static string[] GetLevels ()
  47.         {
  48.             List <string> levels = new List <string> ();
  49.  
  50.             foreach ( string s in Directory.GetFiles ( "../../map" ) )
  51.             {
  52.                 int start = 0;
  53.  
  54.                 while ( s [start] != 'p' )
  55.                     start++;
  56.  
  57.                 start += 2;
  58.  
  59.                 string fulllevelname = s.Substring ( start );
  60.  
  61.                 int len = 0;
  62.  
  63.                 while ( fulllevelname [len] != '.' )
  64.                     len++;
  65.  
  66.                 string level = fulllevelname.Substring ( 0, len );
  67.  
  68.                 levels.Add ( level );
  69.             }
  70.  
  71.             foreach ( string s in levels )
  72.             {
  73.                 Console.WriteLine ( s );
  74.             }
  75.  
  76.             return levels.ToArray ();
  77.         }
  78.  
  79.         private static void NewTraining ( Object n )
  80.         {
  81.             NewTraining ( ( int ) n );
  82.         }
  83.  
  84.         private static void Train ( Object n )
  85.         {
  86.             Train ( ( int ) n );
  87.         }
  88.  
  89.         /// <summary>
  90.         /// This function create a whole new population
  91.         /// and trains a population of 200 players by using new players at each generation
  92.         /// </summary>
  93.         /// <param name="n">number of generations you want to proceed</param>
  94.         private static void NewTraining ( int n )
  95.         {
  96.             Factory.SetPathSave ( PathBotToSubmit );
  97.             Factory.Init ();
  98.             Factory.TrainWithNew ( n );
  99.             Factory.PrintScore ();
  100.             Factory.SaveState ();
  101.         }
  102.  
  103.  
  104.         /// <summary>
  105.         ///  This function trains a population of 200 players by using new players at each generation
  106.         /// </summary>
  107.         /// <param name="n">number of generations you want to proceed</param>
  108.         private static void TrainWithNew ( int n )
  109.         {
  110.             Factory.SetPathLoadAndSave ( PathBotToSubmit );
  111.             Factory.Init ();
  112.             Factory.TrainWithNew ( n );
  113.             Factory.PrintScore ();
  114.             Factory.SaveState ();
  115.         }
  116.  
  117.         /// <summary>
  118.         /// This function trains a population of 200 players by duplicating and applying modification to the copy of
  119.         /// the best players
  120.         /// </summary>
  121.         /// <param name="n">number of generations you want to proceed</param>
  122.         private static void Train ( int n )
  123.         {
  124.             Factory.SetPathLoadAndSave ( PathBotToSubmit );
  125.             Factory.Init ();
  126.             Factory.Train ( n );
  127.             Factory.PrintScore ();
  128.             Factory.SaveState ();
  129.         }
  130.  
  131.         /// <summary>
  132.         /// Show the current best player
  133.         /// </summary>
  134.         private static void Showbest ()
  135.         {
  136.             Game1 game = new Game1 ();
  137.             Factory.SetPathLoadAndSave ( PathBotToSubmit );
  138.             Factory.Init ();
  139.             Factory.GetBestPlayer ().SetStart ( RessourceLoad.GetCurrentMap () );
  140.             game.SetPlayer ( Factory.GetBestPlayer () );
  141.             game.Run ();
  142.         }
  143.  
  144.         /// <summary>
  145.         /// Show the nth player sorted by score in increasing order
  146.         /// </summary>
  147.         /// <param name="nth">player you want to access to, should be between 0 and 199</param>
  148.         private static void ShowNth ( int nth )
  149.         {
  150.             Game1 game = new Game1 ();
  151.             Factory.SetPathLoadAndSave ( PathBotToSubmit );
  152.             Factory.Init ();
  153.             Factory.PrintScore ();
  154.             game.SetPlayer ( Factory.GetNthPlayer ( nth ) );
  155.             game.Run ();
  156.         }
  157.  
  158.         /// <summary>
  159.         /// This function allows you to try the game
  160.         /// </summary>
  161.         private static void PlayAsHuman ()
  162.         {
  163.             Game1 game = new Game1 ();
  164.             Player player = new Player ( false );
  165.             player.SetStart ( RessourceLoad.GetCurrentMap () );
  166.             game.SetPlayer ( player, true );
  167.             game.Run ();
  168.         }
  169.  
  170.         /// <summary>
  171.         /// save best player in folder save, you will be marked on this, so DON'T forget it!
  172.         /// </summary>
  173.         private static void SaveBest ()
  174.         {
  175.             Factory.SetPathLoad ( PathBotToSubmit );
  176.             Factory.Init ();
  177.             var soloList = new List <Player> {Factory.GetBestPlayer ()};
  178.             SaveAndLoad.Save ( PathBotToSubmit, soloList );
  179.             Console.WriteLine ( "Saved Best Player" );
  180.         }
  181.  
  182.     }
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement