Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Recursion in Tic Tac Toe Problem

$
0
0
Ok, I need help with my recursion method in TIC TAC TOE. I have just used 9 textboxes that check for either a valid input of 'o' or 'x'. I used a 2D array to represent the board. The problem is that after I input the first X it brings up the message box "You Lose". It's almost like it didn't go through the recursion method at all. I feel like I have a lot of problems with this code, but a push in the right direction might help.

Also for your information, CheckForVictory( int[,]) returns an int based on who has won. So, if the computer wins it returns a 2 and returns a 1 for a user win. It returns a 0 if nobody has won yet. The computer is represented as the int 2 in ComputerMove and the user is represented as 1. Here is the recursion method.

private Tuple<int, int> ComputerMove(int[,] board ,  int player)
        {
            int[,] temp = board;
            Tuple<int, int> t = null;
            if (CheckForVictory(temp) == 1 || CheckForVictory(temp) == 2)
            {
                return t;
            }
            if (player == 1)
            {
               for(int i = 0; i < 3; i++)
               {
                   for(int j = 0; j < 3; j++)
                   {
                       if (temp[i, j] == 0)
                        {
                            temp[i, j] = 1;
                            if (CheckForVictory(temp) == 1)
                            {
                                temp = _board;
                            }
                            t = ComputerMove(temp, 2);
                          
                        }
                   }
               }
               return t;
             }
            else if (player == 2)
            {
                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if (temp[i, j] == 0) //board position is empty
                        {
                            temp[i, j] = 2;
                            t = ComputerMove(temp, 1);
                            if (t == null)
                            {
                                t = new Tuple<int, int>(i, j);
                                return t;
                            }

                        }
                    }
                }
                return t;
            }
            else
            {
                Tuple<int, int> nulled = null;
                return nulled;
            }
        }


Also, here is my button click to make a move

   private void uxSubmit_Click(object sender, EventArgs e)
        {
            Tuple<int, int> x = null;
            if (CheckForVictory(_board) == 1)
            {
                MessageBox.Show("You Win");
            }
            else if(CheckForVictory(_board) == 0)
            {
                x = ComputerMove(_board, 2);
                if (x == null)
                {
                    int[,] temp = _board;
                    for (int i = 0; i < 3; i++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            if (temp[i, j] == 0)
                            {
                                _board[i, j] = 2;
                            }
                        }
                    }
                    if (CheckForVictory(_board) == 2)
                    {
                        MessageBox.Show("You Lose");
                    }
                }
                else
                {
                    _board[x.Item1, x.Item2] = 2;

                    if (CheckForVictory(_board) == 2)
                    {
                        MessageBox.Show("You Lose");
                    }
                }
            }

            
            
        }




    }

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>