// Method to check the players strategy and whether second turn they started in a corner or not
 private void CornerMoveCheck(Button[] buttons)
 {
     if (_previousMove == 0 || _previousMove == 2 || _previousMove == 6 || _previousMove == 8 && buttons[_previousMove].Enabled == false)
     {
         _playerSecondTurnCornerStart = true;
     }
     else
     {
         _playerSecondTurnCornerStart = false;
     }
 }
 // Gets called when it is the computers turn to move
 private void OpponentMove(Button[] buttons)
 {
     // check who started the game first and if computer started goes into the if
     if (!_playerStart)
     {
         // switches on the game turn count to determine where it should move
         switch (_turnCount)
         {
             case 0:
                 buttons[4].PerformClick();
                 return;
             case 2:
                 // algorithmn strategy for opponent corner move
                 CornerMoveCheck(buttons);
                 if (_playerSecondTurnCornerStart)
                 {
                     if (buttons[0].Enabled == false)
                     {
                         buttons[8].PerformClick();
                     }
                     else if (buttons[2].Enabled == false)
                     {
                         buttons[6].PerformClick();
                     }
                     else if (buttons[6].Enabled == false)
                     {
                         buttons[2].PerformClick();
                     }
                     else
                     {
                         buttons[0].PerformClick();
                     }
                 }
                 // Strategy for opponent side move
                 else
                 {
                     if (buttons[1].Enabled == false || buttons[7].Enabled == false)
                     {
                         buttons[5].PerformClick();
                     }
                     else if(buttons[3].Enabled == false || buttons[5].Enabled == false)
                     {
                         buttons[7].PerformClick();
                     }
                 }
                 return;
             case 4:
                 // checks to see if player started with corner move
                 if (_playerSecondTurnCornerStart)
                 {
                     if (CheckForWinMove(buttons))
                     {
                         buttons[_winMove].PerformClick();
                     }
                     else if (CheckForBlockMove(buttons))
                     {
                         buttons[_blockMove].PerformClick();
                     }
                     else
                     {
                         CheckForNextMove(buttons);
                         buttons[_normalMove].PerformClick();
                     }
                 }
                 // if player started with side move
                 else
                 {
                     if (CheckForWinMove(buttons))
                     {
                         buttons[_winMove].PerformClick();
                     }
                     else if (CheckForBlockMove(buttons))
                     {
                         buttons[_blockMove].PerformClick();
                     }
                     else
                     {
                         if (_previousMove == 3)
                         {
                             buttons[2].PerformClick();
                         }
                         else
                         {
                             buttons[6].PerformClick();
                         }
                     }
                 }
                 return;
             default:
                 // default move case
                 if (CheckForWinMove(buttons))
                 {
                     buttons[_winMove].PerformClick();
                 }
                 else if (CheckForBlockMove(buttons))
                 {
                     buttons[_blockMove].PerformClick();
                 }
                 else
                 {
                     CheckForNextMove(buttons);
                     buttons[_normalMove].PerformClick();
                 }
                 return;
         }
     }
     // if the player started first
     else
     {
         // switches on the players turn
         switch (_turnCount)
         {
             // checks if the player started in the center or not then chooses move
             case 1:
                 if (buttons[4].Enabled == true)
                 {
                     buttons[4].PerformClick();
                 }
                 else
                 {
                     CheckForNextMove(buttons);
                     buttons[_normalMove].PerformClick();
                 }
                 return;
             // Check to counter Player setup for win
             case 3:
                 if (CheckForBlockMove(buttons))
                 {
                     buttons[_blockMove].PerformClick();
                 }
                 else if (CheckForDoubleWin(buttons))
                 {
                     buttons[_blockDoubleWin].PerformClick();
                 }
                 else if (_previousMove == 8)
                 {
                     buttons[2].PerformClick();
                 }
                 else if (buttons[7].Enabled == true)
                 {
                     buttons[7].PerformClick();
                 }
                 else
                 {
                     buttons[5].PerformClick();
                 }
                 return;
             // default computer move to tie game or win if player chooses bad move
             default:
             {
                 if (CheckForWinMove(buttons))
                 {
                     buttons[_winMove].PerformClick();
                 }
                 else if (CheckForBlockMove(buttons))
                 {
                     buttons[_blockMove].PerformClick();
                 }
                 else
                 {
                     CheckForNextMove(buttons);
                     buttons[_normalMove].PerformClick();
                 }
                 return;
             }
         }
     }
 }
        // checks to see if someone has won the game
        private void CheckWin(Button[] buttons)
        {
            // Pop up if you lose the game
            var loseDialog = new AlertDialog.Builder (this).SetTitle("Sorry!,").SetMessage("You lost please try again.").SetPositiveButton("Restart",(sender, e) =>
            {
                Finish();
                StartActivity(typeof(MainActivity));
            }).Create();

            // Pop up if you win the game
            var winDialog = new AlertDialog.Builder (this).SetTitle("Congratulations!").SetMessage("You won.").SetPositiveButton("Restart",(sender, e) =>
            {
                Finish();
                StartActivity(typeof(MainActivity));
            }).Create();

            // Pop up if you tie the game
            var tieDialog = new AlertDialog.Builder (this).SetTitle("Tie Game!").SetPositiveButton("Restart",(sender, e) =>
            {
                Finish();
                StartActivity(typeof(MainActivity));
            }).Create();

            if(buttons[0].Text == buttons[1].Text & buttons[1].Text == buttons[2].Text & buttons[0].Text != String.Empty)
            {
                foreach (var button in buttons)
                {
                    if (button.Enabled)
                    {
                        button.Enabled = false;
                    }
                }
                if (_yourTurn)
                {
                    winDialog.Show ();
                }
                else
                {
                    loseDialog.Show ();
                }
            }
            else if(buttons[3].Text == buttons[4].Text & buttons[4].Text == buttons[5].Text & buttons[3].Text != String.Empty)
            {
                foreach (var button in buttons)
                {
                    if (button.Enabled)
                    {
                        button.Enabled = false;
                    }
                }
                if (_yourTurn)
                {
                    winDialog.Show ();
                }
                else
                {
                    loseDialog.Show ();
                }
            }
            else if(buttons[6].Text == buttons[7].Text & buttons[7].Text == buttons[8].Text & buttons[6].Text != String.Empty)
            {
                foreach (var button in buttons)
                {
                    if (button.Enabled)
                    {
                        button.Enabled = false;
                    }
                }
                if (_yourTurn)
                {
                    winDialog.Show ();
                }
                else
                {
                    loseDialog.Show ();
                }
            }
            else if(buttons[0].Text == buttons[3].Text & buttons[3].Text == buttons[6].Text & buttons[0].Text != String.Empty)
            {
                foreach (var button in buttons)
                {
                    if (button.Enabled)
                    {
                        button.Enabled = false;
                    }
                }
                if (_yourTurn)
                {
                    winDialog.Show ();
                }
                else
                {
                    loseDialog.Show ();
                }
            }
            else if(buttons[1].Text == buttons[4].Text & buttons[4].Text == buttons[7].Text & buttons[1].Text != String.Empty)
            {
                foreach (var button in buttons)
                {
                    if (button.Enabled)
                    {
                        button.Enabled = false;
                    }
                }

                if (_yourTurn)
                {
                    winDialog.Show ();
                }
                else
                {
                    loseDialog.Show ();
                }
            }
            else if(buttons[2].Text == buttons[5].Text & buttons[5].Text == buttons[8].Text & buttons[2].Text != String.Empty)
            {
                foreach (var button in buttons)
                {
                    if (button.Enabled)
                    {
                        button.Enabled = false;
                    }
                }

                if (_yourTurn)
                {
                    winDialog.Show ();
                }
                else
                {
                    loseDialog.Show ();
                }
            }
            else if(buttons[0].Text == buttons[4].Text & buttons[4].Text == buttons[8].Text & buttons[0].Text != String.Empty)
            {
                foreach (var button in buttons)
                {
                    if (button.Enabled)
                    {
                        button.Enabled = false;
                    }
                }
                if (_yourTurn)
                {
                    winDialog.Show ();
                }
                else
                {
                    loseDialog.Show ();
                }
            }
            else if(buttons[2].Text == buttons[4].Text & buttons[4].Text == buttons[6].Text & buttons[2].Text != String.Empty)
            {
                foreach (var button in buttons)
                {
                    if (button.Enabled)
                    {
                        button.Enabled = false;
                    }
                }
                if (_yourTurn)
                {
                    winDialog.Show ();
                }
                else
                {
                    loseDialog.Show ();
                }
            }
            else if(_turnCount == 9)
            {
                foreach (var button in buttons)
                {
                    if (button.Enabled)
                    {
                        button.Enabled = false;
                    }
                }
                tieDialog.Show ();
            }
            else
            {
                return;
            }
        }
 // check for if computer has a move that will win the game
 private bool CheckForWinMove(Button[] buttons)
 {
     // if computer started the game
     if (!_playerStart)
     {
         if (buttons[1].Text == "X" & buttons[2].Text == "X" & buttons[0].Enabled == true)
         {
             _winMove = 0;
             return true;
         }
         else if (buttons[0].Text == "X" & buttons[2].Text == "X" & buttons[1].Enabled == true)
         {
             _winMove = 1;
             return true;
         }
         else if (buttons[0].Text == "X" & buttons[1].Text == "X" & buttons[2].Enabled == true)
         {
             _winMove = 2;
             return true;
         }
         else if (buttons[4].Text == "X" & buttons[5].Text == "X" & buttons[3].Enabled == true)
         {
             _winMove = 3;
             return true;
         }
         else if (buttons[3].Text == "X" & buttons[5].Text == "X" & buttons[4].Enabled == true)
         {
             _winMove = 4;
             return true;
         }
         else if (buttons[3].Text == "X" & buttons[4].Text == "X" & buttons[5].Enabled == true)
         {
             _winMove = 5;
             return true;
         }
         else if (buttons[7].Text == "X" & buttons[8].Text == "X" & buttons[6].Enabled == true)
         {
             _winMove = 6;
             return true;
         }
         else if (buttons[6].Text == "X" & buttons[8].Text == "X" & buttons[7].Enabled == true)
         {
             _winMove = 7;
             return true;
         }
         else if (buttons[6].Text == "X" & buttons[7].Text == "X" & buttons[8].Enabled == true)
         {
             _winMove = 8;
             return true;
         }
         else if (buttons[0].Text == "X" & buttons[4].Text == "X" & buttons[8].Enabled == true)
         {
             _winMove = 8;
             return true;
         }
         else if (buttons[4].Text == "X" & buttons[8].Text == "X" & buttons[0].Enabled == true)
         {
             _winMove = 0;
             return true;
         }
         else if (buttons[6].Text == "X" & buttons[4].Text == "X" & buttons[2].Enabled == true)
         {
             _winMove = 2;
             return true;
         }
         else if (buttons[2].Text == "X" & buttons[4].Text == "X" & buttons[6].Enabled == true)
         {
             _winMove = 6;
             return true;
         }
         else if (buttons[3].Text == "X" & buttons[6].Text == "X" & buttons[0].Enabled == true)
         {
             _winMove = 0;
             return true;
         }
         else if (buttons[4].Text == "X" & buttons[7].Text == "X" & buttons[1].Enabled == true)
         {
             _winMove = 1;
             return true;
         }
         else if (buttons[5].Text == "X" & buttons[8].Text == "X" & buttons[2].Enabled == true)
         {
             _winMove = 2;
             return true;
         }
         else if (buttons[0].Text == "X" & buttons[6].Text == "X" & buttons[3].Enabled == true)
         {
             _winMove = 3;
             return true;
         }
         else if (buttons[1].Text == "X" & buttons[7].Text == "X" & buttons[4].Enabled == true)
         {
             _winMove = 4;
             return true;
         }
         else if (buttons[2].Text == "X" & buttons[8].Text == "X" & buttons[5].Enabled == true)
         {
             _winMove = 5;
             return true;
         }
         else if (buttons[0].Text == "X" & buttons[3].Text == "X" & buttons[6].Enabled == true)
         {
             _winMove = 6;
             return true;
         }
         else if (buttons[1].Text == "X" & buttons[4].Text == "X" & buttons[7].Enabled == true)
         {
             _winMove = 7;
             return true;
         }
         else if (buttons[2].Text == "X" & buttons[5].Text == "X" & buttons[8].Enabled == true)
         {
             _winMove = 8;
             return true;
         }
         else
         {
             return false;
         }
     }
     // if the player started the game
     else
     {
         if (buttons[1].Text == "O" & buttons[2].Text == "O" & buttons[0].Enabled == true)
         {
             _winMove = 0;
             return true;
         }
         else if (buttons[0].Text == "O" & buttons[2].Text == "O" & buttons[1].Enabled == true)
         {
             _winMove = 1;
             return true;
         }
         else if (buttons[0].Text == "O" & buttons[1].Text == "O" & buttons[2].Enabled == true)
         {
             _winMove = 2;
             return true;
         }
         else if (buttons[4].Text == "O" & buttons[5].Text == "O" & buttons[3].Enabled == true)
         {
             _winMove = 3;
             return true;
         }
         else if (buttons[3].Text == "O" & buttons[5].Text == "O" & buttons[4].Enabled == true)
         {
             _winMove = 4;
             return true;
         }
         else if (buttons[3].Text == "O" & buttons[4].Text == "O" & buttons[5].Enabled == true)
         {
             _winMove = 5;
             return true;
         }
         else if (buttons[7].Text == "O" & buttons[8].Text == "O" & buttons[6].Enabled == true)
         {
             _winMove = 6;
             return true;
         }
         else if (buttons[6].Text == "O" & buttons[8].Text == "O" & buttons[7].Enabled == true)
         {
             _winMove = 7;
             return true;
         }
         else if (buttons[6].Text == "O" & buttons[7].Text == "O" & buttons[8].Enabled == true)
         {
             _winMove = 8;
             return true;
         }
         else if (buttons[0].Text == "O" & buttons[4].Text == "O" & buttons[8].Enabled == true)
         {
             _winMove = 8;
             return true;
         }
         else if (buttons[4].Text == "O" & buttons[8].Text == "O" & buttons[0].Enabled == true)
         {
             _winMove = 0;
             return true;
         }
         else if (buttons[6].Text == "O" & buttons[4].Text == "O" & buttons[2].Enabled == true)
         {
             _winMove = 2;
             return true;
         }
         else if (buttons[2].Text == "O" & buttons[4].Text == "O" & buttons[6].Enabled == true)
         {
             _winMove = 6;
             return true;
         }
         else if (buttons[3].Text == "O" & buttons[6].Text == "O" & buttons[0].Enabled == true)
         {
             _winMove = 0;
             return true;
         }
         else if (buttons[4].Text == "O" & buttons[7].Text == "O" & buttons[1].Enabled == true)
         {
             _winMove = 1;
             return true;
         }
         else if (buttons[5].Text == "O" & buttons[8].Text == "O" & buttons[2].Enabled == true)
         {
             _winMove = 2;
             return true;
         }
         else if (buttons[0].Text == "O" & buttons[6].Text == "O" & buttons[3].Enabled == true)
         {
             _winMove = 3;
             return true;
         }
         else if (buttons[1].Text == "O" & buttons[7].Text == "O" & buttons[4].Enabled == true)
         {
             _winMove = 4;
             return true;
         }
         else if (buttons[2].Text == "O" & buttons[8].Text == "O" & buttons[5].Enabled == true)
         {
             _winMove = 5;
             return true;
         }
         else if (buttons[0].Text == "O" & buttons[3].Text == "O" & buttons[6].Enabled == true)
         {
             _winMove = 6;
             return true;
         }
         else if (buttons[1].Text == "O" & buttons[4].Text == "O" & buttons[7].Enabled == true)
         {
             _winMove = 7;
             return true;
         }
         else if (buttons[2].Text == "O" & buttons[5].Text == "O" & buttons[8].Enabled == true)
         {
             _winMove = 8;
             return true;
         }
         else
         {
             return false;
         }
     }
 }
 // Check for a normal computer move starting with empty corners then moving to sides as backup
 private void CheckForNextMove(Button[] buttons)
 {
     if (buttons[0].Enabled == true)
     {
         _normalMove = 0;
     }
     else if (buttons[2].Enabled == true)
     {
         _normalMove = 2;
     }
     else if (buttons[6].Enabled == true)
     {
         _normalMove = 6;
     }
     else if (buttons[8].Enabled == true)
     {
         _normalMove = 8;
     }
     else if (buttons[1].Enabled == true)
     {
         _normalMove = 1;
     }
     else if (buttons[3].Enabled == true)
     {
         _normalMove = 3;
     }
     else if (buttons[4].Enabled == true)
     {
         _normalMove = 4;
     }
     else if (buttons[5].Enabled == true)
     {
         _normalMove = 5;
     }
     else
     {
         _normalMove = 7;
     }
 }
 // Method to prevent 3rd turn double win if player doesn't start in center
 private bool CheckForDoubleWin(Button[] buttons)
 {
     if (buttons[5].Text == "X" & (buttons[7].Text == "X" || buttons[6].Text == "X"))
     {
         _blockDoubleWin = 8;
         return true;
     }
     else if (buttons[5].Text == "X" & (buttons[1].Text == "X" || buttons[0].Text == "X"))
     {
         _blockDoubleWin = 2;
         return true;
     }
     else if (buttons[3].Text == "X" & (buttons[7].Text == "X" || buttons[8].Text == "X"))
     {
         _blockDoubleWin = 6;
         return true;
     }
     else if (buttons[3].Text == "X" & (buttons[1].Text == "X" || buttons[2].Text == "X"))
     {
         _blockDoubleWin = 0;
         return true;
     }
     else if (buttons[7].Text == "X" & (buttons[5].Text == "X" || buttons[2].Text == "X"))
     {
         _blockDoubleWin = 8;
         return true;
     }
     else if (buttons[7].Text == "X" & (buttons[3].Text == "X" || buttons[0].Text == "X"))
     {
         _blockDoubleWin = 6;
         return true;
     }
     else if (buttons[1].Text == "X" & (buttons[3].Text == "X" || buttons[6].Text == "X"))
     {
         _blockDoubleWin = 0;
         return true;
     }
     else if (buttons[1].Text == "X" & (buttons[5].Text == "X" || buttons[8].Text == "X"))
     {
         _blockDoubleWin = 2;
         return true;
     }
     else if ((buttons[0].Text == "X" & buttons[8].Text == "X") || (buttons[2].Text == "X" & buttons[6].Text == "X"))
     {
         _blockDoubleWin = 3;
         return true;
     }
     else
     {
         return false;
     }
 }
        void changeButton( Button but, int column, int row )
        {
            // Update board
            if (Record [row, column] == 0) {
                Record [row, column] = turn;

                // Check for a victory
                checkVictory ();

                // Swap turns
                if (turn == 1) {
                    but.Text = "X";
                    turn = 2;
                } else if ( turn == 2 ) {
                    but.Text = "O";
                    turn = 1;
                }
            } else {
                dbg ("Please pick an empty slot.");
            }
        }