예제 #1
0
 public void InitializeComponent()
 {
     if (_contentLoaded)
     {
         return;
     }
     _contentLoaded = true;
     System.Windows.Application.LoadComponent(this, new System.Uri("/Bubbles%20Projeto;component/Views/TicTacToe.xaml", System.UriKind.Relative));
     this.LayoutRoot     = ((System.Windows.Controls.Grid)(this.FindName("LayoutRoot")));
     this.b00            = ((System.Windows.Controls.Button)(this.FindName("b00")));
     this.b10            = ((System.Windows.Controls.Button)(this.FindName("b10")));
     this.b20            = ((System.Windows.Controls.Button)(this.FindName("b20")));
     this.b01            = ((System.Windows.Controls.Button)(this.FindName("b01")));
     this.b11            = ((System.Windows.Controls.Button)(this.FindName("b11")));
     this.b21            = ((System.Windows.Controls.Button)(this.FindName("b21")));
     this.b02            = ((System.Windows.Controls.Button)(this.FindName("b02")));
     this.b12            = ((System.Windows.Controls.Button)(this.FindName("b12")));
     this.b22            = ((System.Windows.Controls.Button)(this.FindName("b22")));
     this.GameOverSplash = ((TicTacToe.GameOver)(this.FindName("GameOverSplash")));
 }
예제 #2
0
파일: Game.cs 프로젝트: Waleed-KH/TicTacToe
        public void Play(int index)
        {
            // Check if he can play
            if (IsGameOver)
            {
                throw new GameException(GameErrorType.GameOver);
            }
            if (index <= 0 && index >= 8)
            {
                throw new GameException(GameErrorType.IndexOutOfRange);
            }
            if (plays[index] != GamePlay.None)
            {
                throw new GameException(GameErrorType.IndexPlayedOn);
            }

            // Play
            plays[index] = CurrentPlayer;


            // Chack if current player wins
            foreach (var winLine in Winners)
            {
                if (plays[winLine[0]] == GamePlay.None || plays[winLine[1]] == GamePlay.None || plays[winLine[2]] == GamePlay.None)
                {
                    continue;
                }

                if (plays[winLine[0]] == plays[winLine[1]] && plays[winLine[1]] == plays[winLine[2]])
                {
                    Winning = new GameWin(CurrentPlayer, winLine);

                    IsGameOver = true;
                    Played?.Invoke(this, EventArgs.Empty);
                    GameOver?.Invoke(this, new GameOverEventArgs(Winning));
                    return;
                }
            }

            // Chack if game is over with no winner
            if (!plays.Contains(GamePlay.None) ||
                (!plays.Select((value, i) => new { value, index = i })
                 .Where((play) => Array.IndexOf(new int[] { 0, 1, 2, 6, 7, 8 }, play.index) > -1)
                 .Select(p => p.value).Contains(GamePlay.None) &&
                 plays[0] == plays[2] && plays[2] == plays[7] &&
                 plays[1] == plays[6] && plays[6] == plays[8]) ||
                (!plays.Select((value, i) => new { value, index = i })
                 .Where((play) => Array.IndexOf(new int[] { 0, 2, 3, 5, 6, 8 }, play.index) > -1)
                 .Select(p => p.value).Contains(GamePlay.None) &&
                 plays[0] == plays[5] && plays[5] == plays[6] &&
                 plays[2] == plays[3] && plays[3] == plays[8]))
            {
                Winning = new GameWin(GamePlay.None, null);

                IsGameOver = true;
                Played?.Invoke(this, EventArgs.Empty);
                GameOver?.Invoke(this, new GameOverEventArgs(Winning));
                return;
            }

            Played?.Invoke(this, EventArgs.Empty);
            // If not over get ready for next round
            CurrentPlayer = (GamePlay)((int)CurrentPlayer % 2 + 1);

            // Auto play if only one left
            if (plays.Where((p) => p == GamePlay.None).Count() == 1)
            {
                Play(plays.IndexOf(GamePlay.None));
            }
        }
예제 #3
0
        void FindWinner(Occupier Occupier)
        {
            var isWon = false;

            for (var n = 0; n < 8; ++n)
            {
                if (_waysToWin[n, 0].Occupier != Occupier ||
                    _waysToWin[n, 1].Occupier != Occupier ||
                    _waysToWin[n, 2].Occupier != Occupier)
                {
                    continue;
                }

                isWon = true;
                break;
            }

            if (isWon)
            {
                if (Occupier == Occupier.Player1)
                {
                    if (GameType == GameType.SinglePlayer)
                    {
                        Stats.SinglePlayer.Player.Increment(Level);
                    }

                    else
                    {
                        Stats.TwoPlayer.Player1++;
                    }

                    _playerStarts = true;

                    GameOver?.Invoke(Occupier.Player1);
                }

                else
                {
                    if (GameType == GameType.SinglePlayer)
                    {
                        Stats.SinglePlayer.Computer.Increment(Level);
                    }

                    else
                    {
                        Stats.TwoPlayer.Player2++;
                    }

                    _playerStarts = false;

                    GameOver?.Invoke(Occupier.ComputerOrPlayer2);
                }
            }

            else
            {
                if (_done > 8)
                {
                    if (GameType == GameType.SinglePlayer)
                    {
                        Stats.SinglePlayer.Draws.Increment(Level);
                    }

                    else
                    {
                        Stats.TwoPlayer.Draws++;
                    }

                    _playerStarts = !_playerStarts;
                    GameOver?.Invoke(Occupier.None);
                }

                else if (Occupier == Occupier.Player1 &&
                         GameType == GameType.SinglePlayer)
                {
                    ComputerTurn();
                }
            }
        }