예제 #1
0
 public void PlayTest()
 {
     var game = new Game(
         new Board(4, 3),
         new GameState(new List <Koma>()
     {
         new Koma(PlayerType.SecondPlayer, GameFactory.KomaRaion, new OnBoard(new BoardPosition(1, 0))),
         new Koma(PlayerType.SecondPlayer, GameFactory.KomaKirin, new OnBoard(new BoardPosition(0, 1))),
         new Koma(PlayerType.SecondPlayer, GameFactory.KomaZou, new OnBoard(new BoardPosition(2, 0))),
         new Koma(PlayerType.SecondPlayer, GameFactory.KomaHiyoko, InHand.State),
         new Koma(PlayerType.FirstPlayer, GameFactory.KomaHiyoko, InHand.State),
         new Koma(PlayerType.FirstPlayer, GameFactory.KomaRaion, new OnBoard(new BoardPosition(1, 2))),
         new Koma(PlayerType.FirstPlayer, GameFactory.KomaKirin, new OnBoard(new BoardPosition(2, 3))),
         new Koma(PlayerType.FirstPlayer, GameFactory.KomaZou, new OnBoard(new BoardPosition(0, 3))),
     },
                       PlayerType.FirstPlayer
                       ),
         new CustomRule(
             1,
             new NullProhibitedMoveSpecification(),
             new MultiWinningChecker(new List <IWinningChecker>()
     {
         new TakeKingWinningChecker(),
         new EnterOpponentTerritoryWinningChecker(),
     })
             ));
     var ai = new NegaAlphaAI(7);
     //ai.Play(game, null);
 }
예제 #2
0
        public ShogiBoardViewModel(GameService gameService, IMessenger messenger, Func <GameSet> gameSetGetter)
        {
            Messenger        = messenger;
            this.gameService = gameService;

            OperationMode = OperationMode.SelectMoveSource;
            this.gameService.Subscribe(this);

            StartCommand = new DelegateCommand(
                async() =>
            {
                var gameSet            = gameSetGetter();
                FirstPlayerHands.Name  = gameSet.Players[PlayerType.FirstPlayer].Name;
                SecondPlayerHands.Name = gameSet.Players[PlayerType.SecondPlayer].Name;
                OperationMode          = OperationMode.AIThinking;
                cancelTokenSource      = new CancellationTokenSource();
                await Task.Run(() =>
                {
                    this.gameService.Start(gameSet, cancelTokenSource.Token);
                    //this.gameService.Start(new NegaAlphaAI(9), new NegaAlphaAI(9), GameType.AnimalShogi, this);
                });
                cancelTokenSource = null;
                // [MEMO:タスクが完了されるまでここは実行されない(AIThinkingのまま)]
                UpdateOperationModeOnTaskFinished();
            },
                () =>
            {
                return(OperationMode != OperationMode.AIThinking);
            });
            RestartCommand = new DelegateCommand(
                async() =>
            {
                OperationMode     = OperationMode.AIThinking;
                cancelTokenSource = new CancellationTokenSource();
                await Task.Run(() =>
                {
                    this.gameService.Restart(cancelTokenSource.Token);
                });
                cancelTokenSource = null;
                // [MEMO:タスクが完了されるまでここは実行されない(AIThinkingのまま)]
                UpdateOperationModeOnTaskFinished();
            },
                () =>
            {
                return(OperationMode != OperationMode.AIThinking);
            });

            MoveCommand = new DelegateCommand <object>(
                async(param) =>
            {
                if (param == null)
                {
                    return;
                }

                if (OperationMode == OperationMode.SelectMoveSource)
                {
                    var selectedMoveSource        = param as ISelectable;
                    selectedMoveSource.IsSelected = true;
                    OperationMode = OperationMode.SelectMoveDestination;

                    UpdateCanMove(selectedMoveSource);
                }
                else if (OperationMode == OperationMode.SelectMoveDestination)
                {
                    var cell = param as CellViewModel;

                    if (cell != null && cell.CanMove)
                    {
                        MoveCommand move = null;
                        if (cell.MoveCommands.Count() == 1)
                        {
                            move = cell.MoveCommands[0];
                        }
                        else
                        {
                            var doTransform = Messenger.MessageYesNo("成りますか?");
                            move            = cell.MoveCommands.FirstOrDefault(x => x.DoTransform == doTransform);
                        }
                        //game.Play(move);
                        OperationMode     = OperationMode.AIThinking;
                        cancelTokenSource = new CancellationTokenSource();
                        await Task.Run(() => this.gameService.Play(move, cancelTokenSource.Token));
                        cancelTokenSource = null;
                        // [MEMO:タスクが完了されるまでここは実行されない(AIThinkingのまま)]
                        UpdateOperationModeOnTaskFinished();
                    }
                    else
                    {
                        // [動けない位置の場合はキャンセルしすべて更新]
                        OperationMode = OperationMode.SelectMoveSource;
                        Update();
                    }
                }
            },
                (param) =>
            {
                if (param == null)
                {
                    return(false);
                }

                if (OperationMode == OperationMode.SelectMoveSource)
                {
                    var cell = param as CellViewModel;
                    if (cell != null)
                    {
                        if (cell.Koma == null)
                        {
                            return(false);
                        }

                        return(this.gameService.GetGame().State.TurnPlayer == cell.Koma.Player.ToDomain());
                    }

                    var hand = param as HandKomaViewModel;
                    if (hand != null)
                    {
                        return(this.gameService.GetGame().State.TurnPlayer == hand.Player.ToDomain());
                    }
                }
                else if (OperationMode == OperationMode.SelectMoveDestination)
                {
                    return(true);
                }

                // [OperationMode.GameEnd]
                // [OperationMode.AIThinking]
                // [OperationMode.Stopping]
                return(false);
            }
                );

            StopCommand = new DelegateCommand(
                () =>
            {
                cancelTokenSource?.Cancel();
                // [MEMO:OperationModeを変更すると、この時点でタスクが裏で動いているのに、他の操作ができるようになってしまうが]
                // [     アプリケーションサービス層でロックしているので一応問題ない(タスクがキャンセルされるまで少し固まる可能性はあるが)]
                OperationMode = OperationMode.Stopping;
            },
                () =>
            {
                return(OperationMode == OperationMode.AIThinking || OperationMode == OperationMode.SelectMoveSource || OperationMode == OperationMode.GameEnd);
            });
            ResumeCommand = new DelegateCommand(
                async() =>
            {
                OperationMode     = OperationMode.AIThinking;
                cancelTokenSource = new CancellationTokenSource();
                await Task.Run(() =>
                {
                    this.gameService.Resume(cancelTokenSource.Token);
                });
                cancelTokenSource = null;
                // [MEMO:タスクが完了されるまでここは実行されない(AIThinkingのまま)]
                UpdateOperationModeOnTaskFinished();
            },
                () =>
            {
                return(OperationMode == OperationMode.Stopping);
            });
            UndoCommand = new DelegateCommand(
                () =>
            {
                this.gameService.Undo(Game.UndoType.Undo);
                Update();
                UndoCommand?.RaiseCanExecuteChanged();
                RedoCommand?.RaiseCanExecuteChanged();
            },
                () =>
            {
                return(OperationMode == OperationMode.Stopping &&
                       this.gameService.GetGame().CanUndo(Game.UndoType.Undo));
            });
            RedoCommand = new DelegateCommand(
                () =>
            {
                this.gameService.Undo(Game.UndoType.Redo);
                Update();
                UndoCommand?.RaiseCanExecuteChanged();
                RedoCommand?.RaiseCanExecuteChanged();
            },
                () =>
            {
                return(OperationMode == OperationMode.Stopping &&
                       this.gameService.GetGame().CanUndo(Game.UndoType.Redo));
            });



            var ai    = new NegaAlphaAI(6);
            var human = new Human();

            FirstPlayerHands = new PlayerViewModel()
            {
                Player = Player.FirstPlayer, Name = human.Name
            };
            SecondPlayerHands = new PlayerViewModel()
            {
                Player = Player.SecondPlayer, Name = ai.Name
            };
            ForegroundPlayer = Player.FirstPlayer;

            // [MEMO:タスクで開始していない(コンストラクタなのできない)ので、必ず初手はHumanになるようにする]
            cancelTokenSource = new CancellationTokenSource();
            this.gameService.Start(new GameSet(human, ai, GameType.AnimalShogi), cancelTokenSource.Token);
            cancelTokenSource = null;
        }