コード例 #1
0
        /// <summary>
        /// Tic-Tac-Toe játékmodell példányosítása.
        /// </summary>
        public TicTacToeViewModel(ITicTacToeModel model)
        {
            TableSize         = 10;
            _model            = model;
            _model._tableSize = TableSize;
            _model.NewGame();
            _model.GameStarted  += new EventHandler <GameStartedEventArgs>(Model_GameStarted);
            _model.GameLoaded   += Model_GameLoaded;
            _model.FieldChanged += new EventHandler <FieldChangedEventArgs>(Model_FieldChanged);
            _model.GameOver     += new EventHandler <GameOverEventArgs>(Model_GameOver);
            _model.TimePassed   += new EventHandler <TimePassedEventArgs>(Model_TimePassed);
            CurrentPlayer        = PlayerToField(_model._currentPlayer);

            // parancsok kezelése
            //NewGameCommand = new DelegateCommand(param => _model.NewGame());
            NewGameCommand   = new DelegateCommand(param => onNewGame());
            LoadGameCommand  = new DelegateCommand(param => OnLoadGame());
            SaveGameCommand  = new DelegateCommand(param => OnSaveGame());
            ExitGameCommand  = new DelegateCommand(param => OnGameExit());
            PauseGameCommand = new DelegateCommand(param => OnGamePause());

            // játéktábla létrehozása
            Fields = new ObservableCollection <TicTacToeField>();
            Refresh();
        }
コード例 #2
0
        /// <summary>
        /// Initializes the current instance.
        /// </summary>
        /// <param name="m">Game logic.</param>
        public void Initialize(ITicTacToeModel m)
        {
            _model               = m;
            _model.GameStarted  += new EventHandler(Model_GameStarted);
            _model.FieldChanged += new EventHandler <FieldChangedEventArgs>(Model_FieldChanged);
            _model.GameOver     += new EventHandler <GameOverEventArgs>(Model_GameOver);

            Fields = new ObservableCollection <ITicTacToeField>();
        }
コード例 #3
0
        // Construct a new model specific child of the View Model
        public TicTacToeViewModelChild(TicTacToeViewModel parent, ITicTacToeModel <Game, Move, Player> TicTacToeModel)
        {
            this.parent         = parent;
            this.TicTacToeModel = TicTacToeModel;

            // if the game state has changed and it's not null, update the UI to reflect the new game state
            this.WhenAnyValue(vm => vm.gameState)
            .Where(gameState => gameState != null)
            .Subscribe(gameState => RefreshView());
        }
コード例 #4
0
ファイル: CPUPlayer.cs プロジェクト: fujimura-n/TicTacToe
        /// <summary>
        /// ランダムに<see cref="playerForm"/>の駒を配置します
        /// </summary>
        /// <param name="player"></param>
        /// <param name="model"></param>
        public async void ChangedToMyTurn(PlayerForm player, ITicTacToeModel model)
        {
            await Task.Delay(TimeSpan.FromSeconds(1));

            int row;
            int column;

            (row, column) = VacantPosition(model, model.BoardSize);
            model.PutPiece(row, column, player);
        }
コード例 #5
0
ファイル: CPUPlayer.cs プロジェクト: fujimura-n/TicTacToe
        /// <summary>
        /// 駒が置かれていない座標をランダムに取得します。
        /// </summary>
        /// <returns></returns>
        private (int row, int column) VacantPosition(ITicTacToeModel model, int boardSize)
        {
            int    row;
            int    column;
            Random random = new Random();

            row    = random.Next(0, boardSize);
            column = random.Next(0, boardSize);

            while (!model.BoardStatuses[row, column].Equals(PlayerForm.None))
            {
                row    = random.Next(0, boardSize);
                column = random.Next(0, boardSize);
            }

            return(row, column);
        }
コード例 #6
0
        /// <summary>
        /// Tic-Tac-Toe játékmodell példányosítása.
        /// </summary>
        public TicTacToeViewModel(ITicTacToeModel model)
        {
            _model               = model;
            _model.GameStarted  += new EventHandler(Model_GameStarted);
            _model.FieldChanged += new EventHandler <FieldChangedEventArgs>(Model_FieldChanged);

            // parancsok kezelése
            NewGameCommand  = new DelegateCommand(param => _model.NewGame());
            LoadGameCommand = new DelegateCommand(param => OnLoadGame());
            SaveGameCommand = new DelegateCommand(param => OnSaveGame());
            ExitGameCommand = new DelegateCommand(param => OnGameExit());

            // játéktábla létrehozása
            Fields    = new ObservableCollection <TicTacToeField>();
            TableSize = 30;
            Refresh();
        }
コード例 #7
0
        /// <summary>
        /// Alkalmazás indulásának eseménykezelője.
        /// </summary>
        private void App_Startup(object sender, StartupEventArgs e)
        {
            _dataAccess = new TicTacToeFileDataAccess();

            _model           = new BasicTicTacToeModel(_dataAccess);
            _model.GameWon  += new EventHandler <GameWonEventArgs>(Model_GameWon);
            _model.GameOver += new EventHandler(Model_GameOver);
            _model.NewGame();

            _viewModel           = new TicTacToeViewModel(_model);
            _viewModel.LoadGame += new EventHandler(ViewModel_LoadGame); // kezeljük a nézetmodell eseményeit
            _viewModel.SaveGame += new EventHandler(ViewModel_SaveGame);
            _viewModel.GameExit += new EventHandler(ViewModel_GameExit);

            _window             = new TicTacToeWindow();
            _window.DataContext = _viewModel;
            _window.Show();
        }
コード例 #8
0
        public void Initialize(Player p1, Player p2)
        {
            _model = Activator.CreateInstanceFrom("TicTacToeModel.dll", "TicTacToeModel.TicTacToeModel")
                     .Unwrap() as ITicTacToeModel;
            _model.Initialize(p1, p2);
            _model.GameOver += new EventHandler <GameOverEventArgs>(Model_GameOver);

            _viewModel = Activator.CreateInstanceFrom("TicTacToeViewModel.dll", "TicTacToeViewModel.TicTacToeViewModel")
                         .Unwrap() as ITicTacToeViewModel;
            _viewModel.Initialize(_model);
            _viewModel.BackCommand = new DelegateCommand(p => ViewModel_GameExit());

            _model.NewGame();

            _window             = Activator.CreateInstanceFrom("TicTacToeView.dll", "TicTacToeView.TicTacToeWindow").Unwrap() as Window;
            _window.DataContext = _viewModel;
            _window.Closed     += new EventHandler(OnWindowClosed);
        }
コード例 #9
0
ファイル: CPUPlayer.cs プロジェクト: fujimura-n/TicTacToe
 public void PutPiece(int row, int column, PlayerForm player, ITicTacToeModel model)
 {
     return;
 }
コード例 #10
0
        /// <summary>
        /// 駒が置かれていない座標を取得します。
        /// </summary>
        /// <returns></returns>
        private (int row, int column) VacantPosition(ITicTacToeModel model, int boardSize, PlayerForm player)
        {
            //駒が置かれていない座標をランダムに取得
            int    row;
            int    column;
            Random random = new Random();

            row    = random.Next(0, boardSize);
            column = random.Next(0, boardSize);

            while (!model.BoardStatuses[row, column].Equals(PlayerForm.None))
            {
                row    = random.Next(0, boardSize);
                column = random.Next(0, boardSize);
            }

            var list  = new List <PlayerForm>();
            var count = 0;

            //縦横斜めの列のうち、自分の駒が一つもないかつ、相手の駒が一つ以上あるかつ、相手の駒の数が多い列に駒を置く

            //行の判定
            for (int i = 0; i < boardSize; i++)
            {
                for (int j = 0; j < boardSize; j++)
                {
                    list.Add(model.BoardStatuses[i, j]);
                }

                if (!list.Contains(player) && list.Contains(player.GetOpponentPlayerForm()) && count < list.Count(element => element == player.GetOpponentPlayerForm()))
                {
                    count = list.Count(element => element == player.GetOpponentPlayerForm());
                    row   = i;
                    var index = random.Next(0, list.Count - 1);
                    while (list[index] != PlayerForm.None)
                    {
                        index = random.Next(0, list.Count - 1);
                    }
                    column = index;
                }
                list.Clear();
            }

            //列の判定
            for (int j = 0; j < boardSize; j++)
            {
                for (int i = 0; i < boardSize; i++)
                {
                    list.Add(model.BoardStatuses[i, j]);
                }

                if (!list.Contains(player) && list.Contains(player.GetOpponentPlayerForm()) && count < list.Count(element => element == player.GetOpponentPlayerForm()))
                {
                    count = list.Count(element => element == player.GetOpponentPlayerForm());
                    var index = random.Next(0, list.Count - 1);
                    while (list[index] != PlayerForm.None)
                    {
                        index = random.Next(0, list.Count - 1);
                    }
                    row    = index;
                    column = j;
                }
                list.Clear();
            }

            //右斜めの判定
            for (int i = 0; i < boardSize; i++)
            {
                for (int j = 0; j < boardSize; j++)
                {
                    var x = i;
                    var y = j;
                    while (0 <= x && y < boardSize)
                    {
                        list.Add(model.BoardStatuses[x, y]);
                        x--;
                        y++;
                    }
                    if (list.Count < model.AlignNumber)
                    {
                        list.Clear();
                        break;
                    }
                    if (!list.Contains(player) && list.Contains(player.GetOpponentPlayerForm()) && count < list.Count(element => element == player.GetOpponentPlayerForm()))
                    {
                        count = list.Count(element => element == player.GetOpponentPlayerForm());
                        var index = random.Next(0, list.Count - 1);
                        while (list[index] != PlayerForm.None)
                        {
                            index = random.Next(0, list.Count - 1);
                        }
                        row    = i - index;
                        column = j + index;
                    }
                    list.Clear();
                }
            }

            //左斜めの判定
            for (int i = 0; i < boardSize; i++)
            {
                for (int j = 0; j < boardSize; j++)
                {
                    var x = i;
                    var y = j;
                    while (x < boardSize && y < boardSize)
                    {
                        list.Add(model.BoardStatuses[x, y]);
                        x++;
                        y++;
                    }
                    if (list.Count < model.AlignNumber)
                    {
                        list.Clear();
                        break;
                    }
                    if (!list.Contains(player) && list.Contains(player.GetOpponentPlayerForm()) && count < list.Count(element => element == player.GetOpponentPlayerForm()))
                    {
                        count = list.Count(element => element == player.GetOpponentPlayerForm());
                        var index = random.Next(0, list.Count - 1);
                        while (list[index] != PlayerForm.None)
                        {
                            index = random.Next(0, list.Count - 1);
                        }
                        row    = i + index;
                        column = j + index;
                    }
                    list.Clear();
                }
            }

            return(row, column);
        }
コード例 #11
0
ファイル: PersonPlayer.cs プロジェクト: fujimura-n/TicTacToe
 public void ChangedToMyTurn(PlayerForm player, ITicTacToeModel model)
 {
     return;
 }
コード例 #12
0
ファイル: PersonPlayer.cs プロジェクト: fujimura-n/TicTacToe
 public void PutPiece(int row, int column, PlayerForm player, ITicTacToeModel model)
 {
     model.PutPiece(row, column, player);
 }