Click here to Skip to main content
15,896,912 members
Articles / Desktop Programming / WPF

KenKen Solver in WPF

Rate me:
Please Sign up or sign in to vote.
4.43/5 (3 votes)
7 Dec 2008CPOL2 min read 48.1K   459   14  
My first WPF application to demonstrate solving KenKen puzzles.
//Terry Spitz 2008

using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace KenKen
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        KenKenSolver solver = new KenKenSolver();
        TextBox[,] textboxes = new TextBox[6,6];
        ObservableCollection<Rule> rules;

        public Window1()
        {
            InitializeComponent();
            solver.board = new Board();
            //solver.board.FillTestData();
            rules = new ObservableCollection<Rule>() { new Rule(' ', 0, Rule.Op.plus) };
            RulesGrid.ItemsSource = rules;
            for (int row = 0; row < 6; row++)
                for (int col = 0; col < 6; col++)
                {
                    TextBox t = new TextBox();
                    t.FontSize = 24;
                    t.TextAlignment = TextAlignment.Center;
                    t.MaxLength = 1;
                    Grid.SetColumn(t, col);
                    Grid.SetRow(t, row);
                    BoardGrid.Children.Add(t);
                    textboxes[row, col] = t;
                }
        }

        private void calculate_Click(object sender, RoutedEventArgs e)
        {
            for (int row= 0; row < 6; row++)
                for (int col = 0; col < 6; col++)
                {
                    string val = textboxes[row, col].Text.ToUpper();
                    if (val.CompareTo("A")<0 || val.CompareTo("Z")>0)
                    {
                        MessageBox.Show("Grid must contain letters between A and Z");
                        return;
                    }
                    solver.board.grid[row, col] = val[0];
                }
            solver.board.rules = rules.ToArray();

            try
            {
                solver.Solve();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            if (solver.solution == null)
                output.Content = "No solution";
            else
                output.Content = solver.solution.Aggregate("", (s, r) => s+r.Aggregate("", (ss,c)=> ss+","+c.ToString())+"\n");
        }

        private void Test_Click(object sender, RoutedEventArgs e)
        {
            solver.board.FillTestData();
            rules = new ObservableCollection<Rule>(solver.board.rules);
            RulesGrid.ItemsSource = rules;
            for (int row = 0; row < 6; row++)
                for (int col = 0; col < 6; col++)
                {
                    textboxes[row, col].Text = solver.board.grid[row, col].ToString();
                }
       }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            for (int row = 0; row < 6; row++)
                for (int col = 0; col < 6; col++)
                {
                    textboxes[row, col].Text = "";
                }
            rules.Clear();
        }

    }
}

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
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions