Click here to Skip to main content
15,892,537 members
Articles / Mobile Apps / Windows Phone 7

Windows Phone 7 Calcoolation

Rate me:
Please Sign up or sign in to vote.
4.96/5 (46 votes)
16 May 2011CPOL7 min read 94.4K   1.2K   51  
Math-based, sudoku-like game for Windows Phone 7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WP7Calcoolation.UI
{
    /// <summary>
    /// Represents a user control that holds all controls needed to display a cell in the game board.
    /// </summary>
    public partial class CellBox : UserControl
    {
        #region attributes
        public event EventHandler TxtClick;
        public event KeyEventHandler TxtKeyPress;
        public event EventHandler TxtEnter;
        public event EventHandler TxtTextChanged;
        public event KeyEventHandler TxtKeyDown;
        public event EventHandler Selected;

        private int dimension = 0;
        private int column = 0;
        private int row = 0;
        private bool showCandidates = false;
        private bool showCandidate1 = true;
        private bool showCandidate2 = true;
        private bool showCandidate3 = true;
        private bool showCandidate4 = true;
        private bool showCandidate5 = true;
        private bool showCandidate6 = true;
        #endregion attributes

        #region constructor
        public CellBox()
        {
            InitializeComponent();
        }
        #endregion constructor

        #region properties
        public bool Left1
        {
            set { left1.Visibility = value ? Visibility.Visible : Visibility.Collapsed; }
        }
        public bool Right1
        {
            set { right1.Visibility = value ? Visibility.Visible : Visibility.Collapsed; }
        }
        public bool Upper1
        {
            set { upper1.Visibility = value ? Visibility.Visible : Visibility.Collapsed; }
        }
        public bool Lower1
        {
            set { lower1.Visibility = value ? Visibility.Visible : Visibility.Collapsed; }
        }

        public bool LeftBold
        {
            set { leftBold.Visibility = value ? Visibility.Visible : Visibility.Collapsed; }
        }
        public bool RightBold
        {
            set { rightBold.Visibility = value ? Visibility.Visible : Visibility.Collapsed; }
        }
        public bool UpperBold
        {
            set { upperBold.Visibility = value ? Visibility.Visible : Visibility.Collapsed; }
        }
        public bool LowerBold
        {
            set { lowerBold.Visibility = value ? Visibility.Visible : Visibility.Collapsed; }
        }

        public int Column
        {
            get { return column; }
            set { column = value; }
        }

        public string strOperation
        {
            get { return lblOp.Text; }
            set { lblOp.Text = value; }
        }

        public int Row
        {
            get { return row; }
            set { row = value; }
        }

        public string CellText
        {
            get { return lblSelected.Text; }
            set
            {
                lblSelected.Text = value;

                if (value.Trim().Length == 0)
                {
                    this.brdSelected.Visibility = Visibility.Collapsed;
                    this.Background = new SolidColorBrush(Colors.White);
                }
            }
        }

        //public bool TextEnabled
        //{
        //    get { return textBox1.IsEnabled; }
        //    set { textBox1.IsEnabled = value; }
        //}

        public bool ShowCandidates
        {
            get { return showCandidates; }
            set {
                showCandidates = value;
                DisplayCandidates();
            }
        }
        public bool ShowCandidate1
        {
            get { return showCandidate1; }
            set { 
                showCandidate1 = value;
                DisplayCandidates();
            }
        }
        public bool ShowCandidate2
        {
            get { return showCandidate2; }
            set { 
                showCandidate2 = value;
                DisplayCandidates();
            }
        }
        public bool ShowCandidate3
        {
            get { return showCandidate3; }
            set { 
                showCandidate3 = value;
                DisplayCandidates();
            }
        }
        public bool ShowCandidate4
        {
            get { return showCandidate4; }
            set { 
                showCandidate4 = value;
                DisplayCandidates();
            }
        }
        public bool ShowCandidate5
        {
            get { return showCandidate5; }
            set { 
                showCandidate5 = value;
                DisplayCandidates();
            }
        }
        public bool ShowCandidate6
        {
            get { return showCandidate6; }
            set { 
                showCandidate6 = value;
                DisplayCandidates();
            }
        }

        public bool IsSelected { get; set; }

        public int Dimension
        {
            get { return dimension; }
            set
            { 
                dimension = value;

                if (dimension < 6)
                    showCandidate6 = false;
                if (dimension < 5)
                    showCandidate5 = false;
                if (dimension < 4)
                    showCandidate4 = false;
            }
        }

        #endregion properties

        #region methods
        public void ShowResult()
        {
            if (this.lblSelected.Text != this.Tag.ToString())
            {
                this.lblCorrect.Text = this.Tag.ToString();
                this.brdCorrect.Visibility = Visibility.Visible;
                this.Background = new SolidColorBrush(Colors.Yellow);
            }
            else
            {
                this.brdCorrect.Visibility = Visibility.Collapsed;
                this.Background = new SolidColorBrush(Colors.White);
            }
        }

        public void HideResult()
        {
            this.brdCorrect.Visibility = Visibility.Collapsed;
            //if (this.lblSelected.Text != this.CellText)
            //{
            //    this.lblCorrect.Text = this.Tag.ToString();
            //    this.brdCorrect.Visibility = Visibility.Collapsed;
            //    //this.Background = new SolidColorBrush(Colors.Yellow);
            //}
            //else
            //{
            //    this.brdCorrect.Visibility = Visibility.Visible;
            //    //this.Background = new SolidColorBrush(Colors.White);
            //}
        }

        public void SetNumber(int number)
        {
            if (number == 0)
            {
                lblSelected.Text = "0";
                brdSelected.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                lblSelected.Text = number.ToString();
                brdSelected.Visibility = System.Windows.Visibility.Visible;
            }
        }

        public void Select()
        {
            recSelected.Opacity = 0.5;
            IsSelected = true;
        }

        public void Unselect()
        {
            recSelected.Opacity = 0.0;
            IsSelected = false;
        }

        private void DisplayCandidates()
        {
            //lbl1.Visibility = BoolToVis(showCandidates);
            //lbl2.Visibility = BoolToVis(showCandidates);
            //lbl3.Visibility = BoolToVis(showCandidates);
            //lbl4.Visibility = BoolToVis(showCandidates && (dimension > 3));
            //lbl5.Visibility = BoolToVis(showCandidates && (dimension > 4));
            //lbl6.Visibility = BoolToVis(showCandidates && (dimension > 5));

            //lbl1.Foreground = new SolidColorBrush(showCandidate1 ? Colors.Blue : Colors.LightGray);
            //lbl2.Foreground = new SolidColorBrush(showCandidate2 ? Colors.Blue : Colors.LightGray);
            //lbl3.Foreground = new SolidColorBrush(showCandidate3 ? Colors.Blue : Colors.LightGray);
            //lbl4.Foreground = new SolidColorBrush(showCandidate4 ? Colors.Blue : Colors.LightGray);
            //lbl5.Foreground = new SolidColorBrush(showCandidate5 ? Colors.Blue : Colors.LightGray);
            //lbl6.Foreground = new SolidColorBrush(showCandidate6 ? Colors.Blue : Colors.LightGray);
        }

        private Visibility BoolToVis(bool value)
        {
            return value ? Visibility.Visible : Visibility.Collapsed;
        }

        #endregion methods

        #region events
        private void textBox1_Click(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (!showCandidates)
                txt.SelectAll();

            TxtClick(this, e);
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            TxtKeyDown(this, e);
        }

        private void textBox1_KeyPress(object sender, KeyEventArgs e)
        {
            TxtKeyPress(this, e);
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (!showCandidates)
                txt.SelectAll();
            TxtEnter(this, e);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (!showCandidates)
                txt.SelectAll();
            TxtTextChanged(this, e);
            //showCandidates = (textBox1.Text.Trim().Length == 0);

            //if ((textBox1.Text.Trim().Length == 0) && showCandidates)
            //{
            //    showCandidate1 = 
            //    showCandidate2 =
            //    showCandidate3 = true;
            //    showCandidate4 = dimension > 3;
            //    showCandidate5 = dimension > 4;
            //    showCandidate6 = dimension > 5;
            //}
            DisplayCandidates();
        }

        private void lblCandidate_Click(object sender, EventArgs e)
        {
            switch (((Control)sender).Name)
            {
                case "lbl1":
                    ShowCandidate1 = !ShowCandidate1;
                    break;
                case "lbl2":
                    ShowCandidate2 = !ShowCandidate2;
                    break;
                case "lbl3":
                    ShowCandidate3 = !ShowCandidate3;
                    break;
                case "lbl4":
                    ShowCandidate4 = !ShowCandidate4;
                    break;
                case "lbl5":
                    ShowCandidate5 = !ShowCandidate5;
                    break;
                case "lbl6":
                    ShowCandidate6 = !ShowCandidate6;
                    break;
            }

            int candidateCount = 0;
            string currentCandidate = "";

            candidateCount = 
                (showCandidate1 ? 1 : 0) + 
                (showCandidate2 ? 1 : 0) + 
                (showCandidate3 ? 1 : 0) + 
                (showCandidate4 ? 1 : 0) + 
                (showCandidate5 ? 1 : 0) + 
                (showCandidate6 ? 1 : 0);

            showCandidates = (candidateCount != 1);

            if (candidateCount == 1)
            {
                currentCandidate =
                    (showCandidate1 ? "1" : "") +
                    (showCandidate2 ? "2" : "") +
                    (showCandidate3 ? "3" : "") +
                    (showCandidate4 ? "4" : "") +
                    (showCandidate5 ? "5" : "") +
                    (showCandidate6 ? "6" : "");
            }

            //textBox1.Text = currentCandidate;
            DisplayCandidates();
        }

        private void textBox1_DoubleClick(object sender, EventArgs e)
        {
            //textBox1.Text = "";
        }
        #endregion events    
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer Alura Cursos Online
Brazil Brazil

Comments and Discussions