-1

I am trying to make tic tac toe game for windows store application.. i tried many things but nothing works fair ..i dont know the condition that i am using to compare the content of two buttons is correct or not or if anything else is not right please help me i need to know my mistake .. i used try catch method to remove the null refrence exception..any suggestion for removing try catch method..??? my code (xaml)

  using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at         http://go.microsoft.com/fwlink/?LinkId=234238

namespace App1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        bool turn = true;
        int turn_count = 0;

        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            Button b = (Button)sender;
            if(turn)
            {
                b.Content = "X";
            }
            else
            {
                b.Content = "O";
            }
            turn = !turn;
            b.IsEnabled=false;
            turn_count++;
            checkForWinner();
        }
        private  async void checkForWinner()
        {

            bool win = false;

           try
           {
               //hori

                if (((A1.Content).Equals(A2.Content)) && ((A2.Content).Equals(A3.Content)) && (!A1.IsEnabled))
                {
                    win = true;
                  //  var MessageDialog9 = new Windows.UI.Popups.MessageDialog("inside a condtion");
                  //  await MessageDialog9.ShowAsync();

                }

                   else if (((B1.Content).Equals(B2.Content)) && ((B2.Content).Equals(B3.Content)) && (!B1.IsEnabled))
                    {
                        win = true;
                     //   var MessageDialog10 = new Windows.UI.Popups.MessageDialog("inside b condtioin");
                      //  await MessageDialog10.ShowAsync();
                    }




                    else if (((C1.Content).Equals(C2.Content)) && ((C2.Content).Equals(C3.Content)) && (!C1.IsEnabled))
                    {
                        win = true;
                       // var MessageDialog11 = new Windows.UI.Popups.MessageDialog("inside c condition");
                      //  await MessageDialog11.ShowAsync();

                    }
                    //vertical
                    else if (((A1.Content).Equals(B1.Content)) && ((B1.Content).Equals(C1.Content)) && (!A1.IsEnabled))
                    {
                        win = true;
                       // var MessageDialog12 = new Windows.UI.Popups.MessageDialog("inside v1");
                       // await MessageDialog12.ShowAsync();

                    }
                    else if (((A2.Content).Equals(B2.Content)) && ((B2.Content).Equals(C2.Content)) && (!A2.IsEnabled))
                    {
                        win = true;
                        //var MessageDialog13 = new Windows.UI.Popups.MessageDialog("inside v2");
                       // await MessageDialog13.ShowAsync();

                    }
                    else if (((A3.Content).Equals(B3.Content)) && ((B3.Content).Equals(C3.Content)) && (!A3.IsEnabled))
                    {
                        win = true;
                       // var MessageDialog14 = new Windows.UI.Popups.MessageDialog("inside v3");
                       // await MessageDialog14.ShowAsync();

                    }
                    //diag
                    else if (((A1.Content).Equals(B2.Content)) && ((B2.Content).Equals(C3.Content)) && (!A1.IsEnabled))
                    {
                        win = true;
                    }
                    else if (((A3.Content).Equals(B2.Content)) && ((B2.Content).Equals(C1.Content)) && (!C1.IsEnabled))
                    {
                        win = true;
                    }

                }
                catch { }

            //win codn


            if (win)
                {
                    disableButtons();

                    String winner = "";
                    if (turn)
                    {
                        winner = "O";
                    }
                    else
                    {
                        winner = "X";
                    }

                    var MessageDialog2 = new Windows.UI.Popups.MessageDialog(winner + "wins...!!!");
                    await MessageDialog2.ShowAsync();

                }

                //draw
                else
                {
                    if (turn_count == 9)
                    {

                        var MessageDialog1 = new Windows.UI.Popups.MessageDialog("Draw...");
                        await MessageDialog1.ShowAsync();
                    }
                }
        }




        private void disableButtons()
        {

            A1.IsEnabled = false;
            A2.IsEnabled = false;
            A3.IsEnabled = false;
            B1.IsEnabled = false;
            B2.IsEnabled = false;
            B3.IsEnabled = false;
            C1.IsEnabled = false;
            C2.IsEnabled = false;
            C3.IsEnabled = false;


        }

        private void Reset_Click(object sender, RoutedEventArgs e)
        {

            A1.Content = "";
            A2.Content = "";
            A3.Content = "";
            B1.Content = "";
            B2.Content = "";
            B3.Content = "";
            C1.Content = "";
            C2.Content = "";
            C3.Content = "";
            A1.IsEnabled = true;
            A2.IsEnabled = true;
            A3.IsEnabled = true;
            B1.IsEnabled = true;
            B2.IsEnabled = true;
            B3.IsEnabled = true;
            C1.IsEnabled = true;
            C2.IsEnabled = true;
            C3.IsEnabled = true;
            turn_count = 0;

        }
    }
}
  • Never, *ever*, **ever** (am I clear enough?) use try/catch to catch a NRE unless you know `null` is OK. The fact that you got a NRE here is your problem, *fix it, don't hide it*. – BradleyDotNET Feb 03 '15 at 18:05

1 Answers1

1

If you're getting a nullrefexception, your way of comparing the values is not the problem, instead check if all your buttons have initial values.

It would help to have your XAML code as well.

EDIT: Have you tried this with a synchronous method? AFAIK async methods are no longer in the UI context and therefore unable to access controls

fantaztig
  • 96
  • 1
  • 9
  • To your edit, this is not true. `async/await` is executed on the same thread. Even if you were right, he would get `InvalidOperationException` not NRE. – BradleyDotNET Feb 03 '15 at 18:07
  • Thanks for the correction! I'm still not that deep with `async/await` :( – fantaztig Feb 03 '15 at 18:15
  • No problem. This is a decent answer to an unfortunately poor question. i'll upvote it, but you may want to steer clear of questions that are pretty unanswerable, like this one. – BradleyDotNET Feb 03 '15 at 18:23