public async Task <SolvePegBoardQueryResponse> Handle(SolvePegBoardQuery request, CancellationToken cancellationToken) { var moves = await Task.Run(() => { List <HistoricalMove> historicalMoves = new List <HistoricalMove>(); LegacyPegGame game = new LegacyPegGame(); game.InitGame(); foreach (var hole in request.PegBoard.Holes.Where(x => !x.Filled)) { var(row, col) = Conversion.ConvertToGridLocation(hole.Number); game.board.EmptyPeg(row, col); } game.EvalBoard(historicalMoves); return(historicalMoves); }, cancellationToken); return(new SolvePegBoardQueryResponse { SuccessfullySolved = moves.Any(), Moves = GetPegMoveWithBoards(moves, request.PegBoard.Clone()) }); }
public Task <int> Handle(GetAvailableMovesQuery request, CancellationToken cancellationToken) { LegacyPegGame game = new LegacyPegGame(); game.InitGame(); foreach (var hole in request.Board.Holes.Where(x => !x.Filled)) { var(row, col) = Conversion.ConvertToGridLocation(hole.Number); game.board.EmptyPeg(row, col); } var availableMoves = game.GetMovesOnBoard(); return(Task.FromResult(availableMoves.Count)); }
public Task <MakeMoveCommandResponse> Handle(MakeMoveCommand request, CancellationToken cancellationToken) { LegacyPegGame game = new LegacyPegGame(); game.InitGame(); foreach (var hole in request.PegBoard.Holes.Where(x => !x.Filled)) { var(row, col) = Conversion.ConvertToGridLocation(hole.Number); game.board.EmptyPeg(row, col); } var availableMoves = game.GetMovesOnBoard(); var move = availableMoves.FirstOrDefault(x => { var from = Conversion.ConvertToHoleNumber(x.fromLocation); var to = Conversion.ConvertToHoleNumber(x.toLocation); return(from == request.From.Number && to == request.To.Number); }); if (move == null) { return(Task.FromResult(new MakeMoveCommandResponse { IsValidMove = false, NewBoard = request.PegBoard })); } var newBoard = request.PegBoard.Clone(); MakeMove(newBoard, move); return(Task.FromResult(new MakeMoveCommandResponse { IsValidMove = true, NewBoard = newBoard })); }