C# 康威的生活游戏试图让代码背后的工作
我有四节课C# 康威的生活游戏试图让代码背后的工作,c#,wpf,conways-game-of-life,C#,Wpf,Conways Game Of Life,我有四节课 public class BoolToCol : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { bool v = (bool)value; if (v == true)
public class BoolToCol : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool v = (bool)value;
if (v == true)
{
return new SolidColorBrush(Colors.Red);
}
else
{
return new SolidColorBrush(Colors.Blue);
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class Cell : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public bool _ItsAlive;
public int _CellIndex;
public bool ItsAlive {
get {return _ItsAlive;}
set{
_ItsAlive = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("ItsAlive"));
}
}
}
public int CellIndex
{
get { return _CellIndex; }
set
{
_CellIndex = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("CellIndex"));
}
}
}
}
<Window x:Class="Pucketts_ConWaysGameOfLife.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Game of Life" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40*"/>
<ColumnDefinition Width="58*"/>
</Grid.ColumnDefinitions>
<Grid Background="Black"
Grid.Column="0">
<Grid.Resources>
<Style TargetType="Button">
<Setter Property="Width"
Value="80"/>
<Setter Property="Height"
Value="20"/>
<Setter Property="HorizontalAlignment"
Value="Left"/>
<Setter Property="VerticalAlignment"
Value="Top"/>
<Setter Property="BorderThickness"
Value="0"/>
<Setter Property="Foreground"
Value="Red"/>
<Setter Property="Background"
Value="Black"/>
<Setter Property="Grid.Row"
Value="3"/>
</Style>
<Style TargetType="Slider">
<Setter Property="TickFrequency"
Value="1"/>
<Setter Property="SmallChange"
Value="1"/>
<Setter Property="IsSnapToTickEnabled"
Value="True"/>
<Setter Property="LargeChange"
Value="10"/>
<Setter Property="Minimum"
Value="0"/>
<Setter Property="Width"
Value="80"/>
</Style>
<SolidColorBrush x:Key="foregroundBrush"
Color="Red"/>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="68"/>
<ColumnDefinition Width="76"/>
<ColumnDefinition Width="68"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0"
Grid.Row="2"
Content="GenSlide"
Foreground="{StaticResource foregroundBrush}"/>
<Label Grid.Column="0"
Grid.Row="1"
Content="Width"
Foreground="{StaticResource foregroundBrush}"/>
<Label Grid.Column="0"
Grid.Row="0"
Content="Height"
Foreground="{StaticResource foregroundBrush}"/>
<Slider Grid.Column="1"
Grid.Row="2"
LargeChange="2"
x:Name="GenSlide"
Maximum="15"/>
<Slider Grid.Column="1"
Grid.Row="1"
x:Name="WidthSlide"
Maximum="100"/>
<Slider Grid.Column="1"
Grid.Row="0"
x:Name="HeightSlide"
Maximum="100"/>
<Button Grid.Column="2"
Click="SetGrid"
Content="Set Grid"/>
<Button Grid.Column="0"
Click="Random"
Content="Random"/>
<Label HorizontalAlignment="Left"
Grid.Column="2"
Grid.Row="2"
x:Name="GenNum"
Content="{Binding ElementName=GenSlide, Path= Value}"
Foreground="{StaticResource foregroundBrush}"/>
<Label HorizontalAlignment="Left"
Grid.Column="2"
Grid.Row="1"
x:Name="WidthNum"
Content="{Binding ElementName=WidthSlide, Path= Value}"
Foreground="{StaticResource foregroundBrush}"/>
<Label HorizontalAlignment="Left"
Grid.Column="2"
Grid.Row="0"
x:Name="HeightNum"
Content="{Binding ElementName=HeightSlide, Path= Value}"
Foreground="{StaticResource foregroundBrush}"/>
<Button Grid.Column="1"
Click="Play"
Content="Play"/>
<Button Grid.Column="1"
VerticalAlignment="Bottom"
Click="Next"
Margin="0,0,0,-10"
Content="Next"/>
</Grid>
<UniformGrid x:Name="Board"
Grid.Column="1">
</UniformGrid>
</Grid>
</Window>
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
Cell[] CellA;
int _NumOfColumns;
int CellIndex;
public event PropertyChangedEventHandler PropertyChanged;
public MainWindow()
{
this.InitializeComponent();
}
public int NumOfColumns
{
get { return _NumOfColumns; }
set
{
_NumOfColumns = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("NumOfColumns"));
}
}
}
private void Initializeboard()
{
NumOfColumns = (int) (this.HeightSlide.Value);
CellA = new Cell[NumOfColumns * (int) (this.WidthSlide.Value)];
for (int i = 0; i < this.HeightSlide.Value * this.WidthSlide.Value; i++)
{
Rectangle rec = new Rectangle();
rec.Stroke = Brushes.Green;
rec.MouseLeftButtonDown += new MouseButtonEventHandler(rec_MouseLeftButtonDown);
this.Board.Children.Add(rec);
Cell c = new Cell();
c.CellIndex = i;
Binding newB = new Binding("ItsAlive");
newB.Source = c;
newB.Converter = new BoolToCol();
rec.SetBinding(Rectangle.FillProperty, newB);
CellA[i] = c;
}
}
public void rec_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
foreach (UIElement item in this.Board.Children)
{
if (item.IsMouseOver)
{
Cell c = (item as Rectangle).GetBindingExpression(Rectangle.FillProperty).ResolvedSource as Cell;
c.ItsAlive = !c.ItsAlive;
//c.CellIndex = this.Board.Children.IndexOf((Rectangle) sender);
CellIndex = c.CellIndex;
Console.WriteLine(CellIndex + " " + CellA[CellIndex].ItsAlive);
}
}
}
public List<Cell> CheckNeighbours(Cell c)
{
//NumOfColumns
//c.CellIndex
// 3 secations the ones above below and next to
List<Cell> NebourCell = new List<Cell> { };
NebourCell.Clear();
for (int i = -1; i < 2; i++)
{
try
{
NebourCell.Add(CellA[c.CellIndex - NumOfColumns - i]);
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("Ignore");
}
}
for (int i = -1; i < 2; i += 2)
{
try
{
NebourCell.Add(CellA[c.CellIndex -i]);
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("Ignore for middle index out of bounds");
}
}
for (int i = -1; i < 2; i++)
{
try
{
NebourCell.Add(CellA[c.CellIndex + NumOfColumns - i]);
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("Ignore");
}
}
return NebourCell;
}
private void SetGrid(object sender, RoutedEventArgs e)
{
this.Board.Children.Clear();
this.Initializeboard();
}
private void Random(object sender, RoutedEventArgs e)
{
Random rand = new Random();
foreach (UIElement item in this.Board.Children)
{
Cell c = (item as Rectangle).GetBindingExpression(Rectangle.FillProperty).ResolvedSource as Cell;
if (rand.Next(0, 2) == 1)
{
c.ItsAlive = !c.ItsAlive;
}
}
}
private void Play(object sender, RoutedEventArgs e)
{
foreach (UIElement item in this.Board.Children)
{
}
}
private void Next(object sender, RoutedEventArgs e)
{
int Alive = 0;
foreach (Cell AllCell in CellA)
{
List<Cell> NebourCells = CheckNeighbours(CellA[AllCell.CellIndex]);
foreach ( Cell AllBebourCells in NebourCells)
{
Console.WriteLine(CellA[AllBebourCells.CellIndex].ItsAlive);
if (CellA[AllBebourCells.CellIndex].ItsAlive)
Alive++;
}
Console.WriteLine(Alive);
if (Alive < 2 || Alive > 3)
{
Console.WriteLine("It dies");
CellA[CellIndex].ItsAlive = false;
}
if (Alive == 2 || Alive == 3)
{
Console.WriteLine("Its lives on");
}
if (Alive == 3)
{
Console.WriteLine("Its alive");
CellA[CellIndex].ItsAlive = true;
}
//foreach (Cell item in NebourCell)
//{
// Rectangle ind = this.Board.Children[item.CellIndex] as Rectangle;
// ind.Fill = new SolidColorBrush(Colors.LightCyan);
//}
}
}
}
公共类BoolToCol:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
bool v=(bool)值;
如果(v==true)
{
返回新的SolidColorBrush(颜色为红色);
}
其他的
{
返回新的SolidColorBrush(Colors.Blue);
}
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}
公共类单元格:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
公共图书馆还活着;
公共综合指数;
公共图书馆{
获取{return\u ItsAlive;}
设置{
_ItsAlive=价值;
if(PropertyChanged!=null)
{
PropertyChanged(新PropertyChangedEventArgs(“ITSLIVE”);
}
}
}
公共int单元索引
{
获取{return}CellIndex;}
设置
{
_细胞指数=值;
if(PropertyChanged!=null)
{
PropertyChanged(新PropertyChangedEventArgs(“CellIndex”);
}
}
}
}
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口,INotifyPropertyChanged
{
细胞[]细胞;
整数列;
细胞指数;
公共事件属性更改事件处理程序属性更改;
公共主窗口()
{
this.InitializeComponent();
}
公共整数列
{
获取{return}
设置
{
_numof列=值;
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(“NumOfColumns”);
}
}
}
私有无效初始化板()
{
NumOfColumns=(int)(this.HeightSlide.Value);
CellA=新单元格[NumOfColumns*(int)(this.WidthSlide.Value)];
对于(int i=0;iprivate void Next(object sender, RoutedEventArgs e)
{
Cell[] copy = copy of CellA
Loop over cells in copy
{
Alive = number of living neighbors of cell in CellA array
// apply Life logic here
copy[cell index].IsAlive = new status based on logic
}
CellA = copy
}