コード例 #1
0
        public TicTacToeViewModel()
        {
            _playerManager      = new PlayerManager();
            CheckIfCatCanAppear = 0;
            Cards = new List <CardTicTacToe>();
            for (int i = 0; i < 9; i++)
            {
                CardTicTacToe card = new CardTicTacToe("");
                Cards.Add(card);
            }
            Message = "It is the turn of player";
            win     = 0;
            numberOcupatedSpaces = 0;
            _gameRecordManager   = new GameRecordManager();
            TicTacToeCommand     = new RelayCommand(param => Logica((CardTicTacToe)param));

            SaveTicTacToeCommand = new RelayCommand(param => SaveGame());
            OpenTicTacToeCommand = new RelayCommand(param => OpenGame());
            NewGameCommand       = new RelayCommand(param => NewGame());
        }
コード例 #2
0
        private async void Logica(CardTicTacToe card)
        {
            if (win != 0)
            {
                return;
            }
            Random rnd = new Random();

            if (CheckIfCatCanAppear == 0)
            {
                if (!Cards[Cards.IndexOf(card)].Card.Equals(""))
                {
                    if (numberOcupatedSpaces <= 8)
                    {
                        MessageBox.Show("Press another one");
                    }
                }
                else
                {
                    int ok = 0;
                    Cards[Cards.IndexOf(card)].Card = $"../../Images/For TicTacToe/cat.jpg";
                    Message             = "It is the turn of the computer";
                    IsEnabledSave       = true;
                    CheckIfCatCanAppear = 1;
                    await Task.Delay(400);

                    numberOcupatedSpaces++;
                    IsEnabledSave = true;
                    if (Winner() != $"../../Images/For TicTacToe/cat.jpg")
                    {
                        if (numberOcupatedSpaces < 8)
                        {
                            if (win == 0)
                            {
                                do
                                {
                                    int r = rnd.Next(9);
                                    if (Cards[r].Card.Equals(""))
                                    {
                                        Cards[r].Card       = $"../../Images/For TicTacToe/mouse.jpg";
                                        Message             = "It is the turn of player";
                                        CheckIfCatCanAppear = 0;
                                        ok = 1;
                                    }
                                    if (Winner() == $"../../Images/For TicTacToe/mouse.jpg")
                                    {
                                        Message = "";
                                        MessageBox.Show("You Lost!", "Message", MessageBoxButton.OK,
                                                        MessageBoxImage.Exclamation);
                                        win           = 2;
                                        IsEnabledSave = false;
                                        break;
                                    }
                                } while (ok == 0);
                            }
                            Message = "It is the turn of player";
                            numberOcupatedSpaces++;
                        }
                        else
                        {
                            Message = "";
                            MessageBox.Show("Drow!", "Message", MessageBoxButton.OK,
                                            MessageBoxImage.Exclamation);
                            IsEnabledSave = false;
                            return;
                        }
                    }
                    else
                    {
                        Message = "";
                        MessageBox.Show("You Won!", "Message", MessageBoxButton.OK,
                                        MessageBoxImage.Exclamation);
                        win           = 1;
                        IsEnabledSave = false;
                        IsXWinner();
                    }
                }
            }
        }