Пример #1
0
 // Use this for initialization
 void OnMouseDown()
 {
     if (Input.GetMouseButtonDown(0) && active)
     {
         if (TicTacToe.turn == 1)
         {
             //player = new Color (255, 0, 0, 1);
             this.gameObject.renderer.material.color = Color.red;
         }
         else
         {
             this.gameObject.renderer.material.color = Color.blue;
             //player = new Color (0, 0, 255, 1);
         }
         TicTacToe.turn *= -1;
         active          = false;
         TicTacToe.CheckWin();
     }
 }
Пример #2
0
        /// <summary>
        /// Executes a game move
        /// </summary>
        private void GameExecute(object sender)
        {
            int    number     = 0;
            Button btn        = null;
            string nextPlayer = "";

            // Checks who is currently playing
            if (IsPlayerTurn == true)
            {
                // Get the selected cell number
                number = int.Parse(sender.ToString().Substring((sender.ToString().Length - 1)));
                // Get the button
                btn          = GetButton(sender.ToString());
                IsPlayerTurn = false;
            }
            else
            {
                // Get the computer selected number
                Task <int> computerTurn = Task.Run(() => ticTacToe.ComputerSelectField(currentResults));
                number = computerTurn.Result;
                // Get the button with that name
                btn          = GetButton("Button" + number);
                IsPlayerTurn = true;
            }

            // Check result
            int result = 0;

            // Current game order
            orderNumber++;
            // Even numbers are saved as 'O', while odds are saved as 'X', the button content gets updated with that value and disabled.
            if (orderNumber % 2 == 0)
            {
                currentResults[number] = 'O';
                btn.Content            = "O";
                btn.IsEnabled          = false;
            }
            else
            {
                currentResults[number] = 'X';
                btn.Content            = "X";
                btn.IsEnabled          = false;
            }

            // Check if there was a winner
            result = ticTacToe.CheckWin(currentResults);
            string currentMark = orderNumber % 2 == 0 ? "O" : "X";

            // There is a winner
            if (result == 1)
            {
                // Select who starts next the match
                if (FirstPlayer == true)
                {
                    nextPlayer   = "Computer";
                    IsPlayerTurn = false;
                    FirstPlayer  = false;
                }
                else
                {
                    nextPlayer   = "Player";
                    IsPlayerTurn = true;
                    FirstPlayer  = true;
                }

                Xceed.Wpf.Toolkit.MessageBox.Show($"The winner is: {currentMark}!\n{nextPlayer} starts first.", "Close", MessageBoxButton.OK, MessageBoxImage.Warning);
                RestartBoard();
            }
            // No winnder
            else if (result == -1)
            {
                // Select who starts next the match
                if (FirstPlayer == true)
                {
                    nextPlayer   = "Computer";
                    IsPlayerTurn = false;
                    FirstPlayer  = false;
                }
                else
                {
                    nextPlayer   = "Player";
                    IsPlayerTurn = true;
                    FirstPlayer  = true;
                }
                Xceed.Wpf.Toolkit.MessageBox.Show($"There is no winner!\n{nextPlayer} starts first.", "Close", MessageBoxButton.OK, MessageBoxImage.Warning);
                RestartBoard();
            }

            // If its not players turn, let the computer play
            if (IsPlayerTurn == false)
            {
                GameExecute(sender);
            }
        }
Пример #3
0
 public void ThenPlayerHasWonTheMatch(string p0)
 {
     _game.CurrentPlayer.symbol = p0;
     _currentPlayerHasWon       = _game.CheckWin();
     Assert.IsTrue(_currentPlayerHasWon);
 }