1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Threading;
namespace WpfModelAnimation2
{
public class ConWayGame:CelluleBase
{
#region private fields
private bool bSetupVoisins ;
#endregion
#region protected fields
protected Random rnd;
#endregion
#region public properties
private ObservableCollection<Cellule > cells;
public ObservableCollection<Cellule> Cells
{
get { return cells; }
set {
cells = value;
base.RaisePropertyChanged("Cells"); }
}
private int cols;
public int Cols
{
get { return cols=16; }
set { cols = value; base.RaisePropertyChanged("Cols"); }
}
private int rows ;
public int Rows
{
get { return rows; }
set { rows = value; base.RaisePropertyChanged("Rows"); }
}
private int generationCount;
public int GenerationCount
{
get { return generationCount; }
}
private double tempsEcoule;
public double TempsEcoule
{
get { return tempsEcoule; }
}
#endregion
#region methods
public ConWayGame()
{
Rows = 18;
Cols = 18;
rnd = new Random();
Cells = new ObservableCollection<Cellule>();
CreateData();
}
private void CreateData()
{
Cellule oc;
int colOc = 0;
int rowOc = 0;
for (rowOc = 0; rowOc < Rows; rowOc++)
{
for (colOc = 0; colOc < Cols; colOc++)
{
oc = this.CreateCellule(colOc, rowOc);
Cells.Add(oc);
oc.ID = Cells.IndexOf(oc).ToString();
}
}
Rows = rowOc;
Cols = colOc;
generationCount = 0;
}
protected Cellule CreateCellule(int col, int row)
{
bSetupVoisins = false; //Nouvelle cellule , voisins non iNITIALISE
Cellule oCell = new Cellule( col,row);
oCell.Alive = PileFace();
return oCell;
}
//methode pile ou face (50% chances en vie ou mort)
protected bool PileFace()
{
return rnd.Next(0, Int16.MaxValue) % 2 == 0;
}
public void Tick(double t)
{
try
{
RunGeneration();
generationCount += 1;
}
finally
{
tempsEcoule = t;
}
}
protected void RunGeneration()
{
if (! bSetupVoisins) SetupVoisins();
// 3 passes ordonees car une passe peut affecter une autre
// 1- clear nbre voisins
foreach (Cellule oC in Cells)
oC.RazNbreVoisins();
// 2- Maj Nbre voisins si cellule Alive
foreach (Cellule oC in Cells)
oC.MajNbreVoisinsSiAlive();
// 2- MAj cellule suivant nbre voisins(resurrection)
foreach (Cellule oC in Cells)
oC.MajAliveSuivantNbreVoisins();
}
private void SetupVoisins()
{
int rowOc = 0;
int colOc = 0;
int rowVoisin = 0;
int colVoisin = 0;
Cellule oCellVoisin = null;
Cellule oc = null;
for (int i = 0; i < Cells.Count; i++)
{
oc = Cells[i];
oc.ClearVoisins();
rowOc = oc.Row;
colOc = oc.Col;
for (rowVoisin = -1; rowVoisin <= 1; rowVoisin++)
{
for (colVoisin = -1; colVoisin <= 1; colVoisin++)
{
if (rowVoisin == 0 && colVoisin == 0)
{ //cellule en cours ;
}
else
{
oCellVoisin = Cells.Where(
p => p.Row == (rowOc + rowVoisin) && p.Col == (colOc + colVoisin)).FirstOrDefault();
if (oCellVoisin != null) oc.AddVoisin(oCellVoisin);
}
}
}
}
bSetupVoisins = true;
}
#endregion
}
} |
Partager