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