Example #1
0
        public override async Task Decide(Board board, Decider decider)
        {
            if (_tile.TileType.Suit == Suit.Jihai && _tile.Index < 4 && board.IsFirstGoAround)
            {
                if (board.Seats.SelectMany(s => s.Discards).Count(t => t.TileType == _tile.TileType) == 4)
                {
                    _nextState = new Abort();
                    return;
                }
            }

            var fourKanAbortIfNoRon = board.Seats.SelectMany(s => s.Melds).Count(m => m.IsKan) == 4 && board.Seats.Count(s => s.Melds.Any(m => m.IsKan)) > 1;

            var reactionTasks = new Task <DiscardResponse> [4];
            var clients       = new Client[4];

            for (var i = 0; i < 4; i++)
            {
                if (i == board.ActiveSeatIndex)
                {
                    reactionTasks[i] = Task.FromResult(DiscardResponse.Pass());
                    clients[i]       = new Client(i, DiscardActions.Pass);
                    continue;
                }

                var actions = GetPossibleActions(board, i, fourKanAbortIfNoRon);
                if (actions != DiscardActions.Pass)
                {
                    reactionTasks[i] = decider.OnDiscard(actions, i);
                    clients[i]       = new Client(i, actions);
                    continue;
                }

                reactionTasks[i] = Task.FromResult(DiscardResponse.Pass());
                clients[i]       = new Client(i, DiscardActions.Pass);
            }

            await Task.WhenAll(reactionTasks);

            for (var i = 0; i < 4; i++)
            {
                reactionTasks[i].Result.Execute(clients[i]);
                if (clients[i].IgnoredRon)
                {
                    _ignoredRonSeats.Add(i);
                }
            }

            var ronCount = clients.Count(c => c.Ron);

            if (ronCount == 3)
            {
                _nextState = new Abort();
                return;
            }

            if (ronCount > 0)
            {
                _nextState = new Ron(clients.Where(r => r.Ron).Select(r => r.SeatIndex));
                return;
            }

            if (fourKanAbortIfNoRon)
            {
                _nextState = new Abort();
                return;
            }

            var kan = clients.FirstOrDefault(c => c.Daiminkan);

            if (kan != null)
            {
                _nextState = new Daiminkan(kan.SeatIndex);
                return;
            }

            var pon = clients.FirstOrDefault(c => c.Pon);

            if (pon != null)
            {
                _nextState = new Pon(pon.SeatIndex, pon.Tile0 !, pon.Tile1 !, pon.Discard !);
                return;
            }

            var chii = clients.FirstOrDefault(c => c.Chii);

            if (chii != null)
            {
                _nextState = new Chii(chii.SeatIndex, chii.Tile0 !, chii.Tile1 !, chii.Discard !);
                return;
            }

            if (board.Wall.RemainingDraws == 0)
            {
                _nextState = new ExhaustiveDraw();
            }
            else
            {
                _nextState = new Draw((board.ActiveSeatIndex + 1) % 4);
            }
        }
Example #2
0
 public override void KyuushuKyuuhai()
 {
     NextState = new Abort();
 }