Esempio n. 1
0
 public Controller(BlockingMediator mediator)
 {
     _signal = new AutoResetEvent(false);
     mediator.AddSignal(_signal);
     dynamic config = ConfigurationManager.GetSection("delays");
     var delays = new Dictionary<MessageType, int>();
     foreach (string name in config)
     {
         delays[Hearts.Utility.Enum.Parse<MessageType>(name)] =
             int.Parse(config[name]);
     }
     foreach (var type in Hearts.Utility.Enum.GetValues<MessageType>())
     {
         var key = type;
         mediator.Subscribe(type, ignore =>
             {
                 if (delays.ContainsKey(key))
                 {
                     Thread.Sleep(delays[key]);
                 }
                 _signal.Reset();
                 if (!IsBlocking)
                 {
                     ThreadPool.QueueUserWorkItem(_ => _signal.Set());
                 }
             });
     }
 }
Esempio n. 2
0
 static void Main(string[] args)
 {
     LoadTestHands();
     var mediator = new BlockingMediator();
     var listener = new Listener(mediator);
     var controller = new Controller(mediator);
     RunGame(mediator);
     cmd.Title = "TC16 - Clarity Hearts (Running)";
     while (true)
     {
         var info = cmd.ReadKey(true);
         switch (info.Key)
         {
             case ConsoleKey.Q:
                 return;
             case ConsoleKey.R:
                 _gameThread.Abort();
                 Thread.Sleep(2000);
                 _gameThread.Join();
                 RunGame(mediator);
                 break;
             case ConsoleKey.T:
                 _gameThread.Abort();
                 Thread.Sleep(2000);
                 _gameThread.Join();
                 cmd.WriteLine("choose a test hand:");
                 int i = 0;
                 foreach (var pair in _testHands)
                 {
                     i += 1;
                     cmd.WriteLine("\t{0}. {1}", i, pair.Key);
                 }
                 int choice = cmd.ReadKey(true).KeyChar - '1';
                 var hand = _testHands
                     .Skip(choice)
                     .First()
                     .Value;
                 RigDeal(hand, mediator);
                 break;
             case ConsoleKey.Spacebar:
                 controller.Toggle();
                 cmd.Title = string.Format(
                     "TC16 - Clarity Hearts ({0})",
                     controller.IsBlocking
                         ? "Paused"
                         : "Running");
                 break;
             case ConsoleKey.Enter:
                 controller.Step();
                 break;
             default:
                 break;
         }
     }
 }
Esempio n. 3
0
 private static void RunGame(BlockingMediator mediator)
 {
     var players = GetPlayers();
     var game = new Game(players, mediator);
     _gameThread = new Thread(() => game.PlayToCompletion());
     _gameThread.Name = "Game Thread";
     _gameThread.IsBackground = true;
     _gameThread.Start();
 }
Esempio n. 4
0
 private static void RigDeal(IList<Card> hand, BlockingMediator mediator)
 {
     var players = GetPlayers();
     var map = new Dictionary<PlayerBase, IList<Card>>();
     map.Add(players[0], hand);
     var deal = new RiggedDeal(players, PassingMode.Left,
         mediator, map);
     _gameThread = new Thread(() => deal.PlayToCompletion());
     _gameThread.IsBackground = true;
     _gameThread.Start();
 }
Esempio n. 5
0
 public GameController(BlockingMediator mediator)
 {
     _mediator = mediator;
     _mediator.AddSignal(_signal);
 }