1 Replies - 503 Views - Last Post: 12 January 2013 - 12:22 PM Rate Topic: -----

#1 lbecker34  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-November 11

Recursion in Tic Tac Toe Problem

Posted 12 January 2013 - 11:22 AM

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");
                    }
                }
            }

            
            
        }




    }


Is This A Good Question/Topic? 0
  • +

Replies To: Recursion in Tic Tac Toe Problem

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5469
  • View blog
  • Posts: 11,749
  • Joined: 02-June 10

Re: Recursion in Tic Tac Toe Problem

Posted 12 January 2013 - 12:22 PM

"I need help" isn't exactly a question we can do much with.

Quote

The problem is that after I input the first X it brings up the message box "You Lose".

Ok - then you need to debug your program to follow its execution to see *why* it does this. See FAQ 2

lbecker34: What this shows us is that you aren't familiar with breakpoints and how to debug your own code.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Jan 2013
Spoiler



This post has been edited by tlhIn`toq: 12 January 2013 - 12:23 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1