private void load_game_Click(object sender, RoutedEventArgs e) { if (timer.IsEnabled) { return; } LoadWindow lw = new LoadWindow(con); lw.ShowDialog(); if (Globals.isChosen) { uniqueIdentifier = Globals.guid; arr = CalcForProjectLife.StringToArray(Globals.sequence, columns, rows); newGame = false; isLogSaved = true; generations = Globals.generation; canvas_field.Children.Clear(); for (int x = 0; x < columns; x++) { for (int y = 0; y < rows; y++) { //Rectangle rect = GetRectangle(isAlive); //Rectangle rect = CalcForProjectLife.GetRectangle(isAlive, resolution); //Canvas.SetLeft(rect, x * resolution); //Canvas.SetTop(rect, y * resolution); //canvas_field.Children.Add(rect); CanvasRectangleShow(x, y, arr[x, y]); } } } }
private void save_game_Click(object sender, RoutedEventArgs e) { if (timer.IsEnabled || !Globals.isConSuccess) { return; } DataTable dt = new DataTable(); using (SqlDataReader reader = con.CheckSave(CalcForProjectLife.ArrayToString(arr, columns, rows))) { dt.Columns.Add("code"); dt.Columns.Add("message"); while (reader.Read()) { dt.Rows.Add((int)reader["code"], (string)reader["message"]); } } if ((string)dt.Rows[0]["code"] == "1") { if (con.SaveGame(CalcForProjectLife.ArrayToString(arr, columns, rows), uniqueIdentifier, generations)) { MessageBox.Show("Сохранение прошло успешно"); } else { MessageBox.Show("Что-то пошло не так"); } } else { MessageBox.Show("Такое сохранение уже существует"); } }
//private int CheckNeighbours(bool[,] arrIn, bool universeIsLocked, int x, int y) //{ // bool isSelf; // bool hasLife; // int col; // int row; // int count = 0; // if (!universeIsLocked) // { // //int xmin = Math.Max(x - 1, 0), // // xmax = Math.Min(x + 2, columns), // // ymin = Math.Max(y - 1, 0), // // ymax = Math.Min(y + 2, rows); // for (int i = -1; i < 2; i++) // { // for (int j = -1; j < 2; j++) // { // col = x + i; // row = y + j; // isSelf = col == x && row == y; // if (col < 0 || col >= columns || row < 0 || row >= rows) // hasLife = false; // else // hasLife = arrIn[col, row]; // if (hasLife && !isSelf) // count++; // } // } // } // else // { // for (int i = - 1; i < 2; i++) // { // for (int j = - 1; j < 2; j++) // { // col = (x + i + columns) % columns; // row = (y + j + rows) % rows; // isSelf = col == x && row == y; // hasLife = arrIn[col, row]; // if (hasLife && !isSelf) // count++; // } // } // } // return count; //} //private bool CheckIsAlive(int neighbours, bool isAlive) //{ // bool result; // if (!isAlive && neighbours == 3) // result = true; // else if (isAlive && (neighbours < 2 || neighbours > 3)) // result = false; // else // result = isAlive; // return result; //} private void CanvasRectangleShow(int x, int y, bool isAlive) { Rectangle rect = CalcForProjectLife.GetRectangle(isAlive, resolution); Canvas.SetLeft(rect, x * resolution); Canvas.SetTop(rect, y * resolution); canvas_field.Children.Add(rect); }
private bool[,] NextGeneration(bool[,] arrIn) { generations++; canvas_field.Children.Clear(); bool[,] arrOut = new bool[columns, rows]; if (newGame) { for (int x = 0; x < columns; x++) { for (int y = 0; y < rows; y++) { int r = rnd.Next(0, density + 1); bool isAlive = r == density; arrOut[x, y] = isAlive; //Rectangle rect = GetRectangle(isAlive); //Rectangle rect = CalcForProjectLife.GetRectangle(isAlive, resolution); //Canvas.SetLeft(rect, x * resolution); //Canvas.SetTop(rect, y * resolution); //canvas_field.Children.Add(rect); CanvasRectangleShow(x, y, isAlive); } } newGame = false; } else { for (int x = 0; x < columns; x++) { for (int y = 0; y < rows; y++) { //arrOut[x, y] = CalcForProjectLife.CheckIsAlive(CheckNeighbours(arrIn, isLocked, x, y), arrIn[x, y]); arrOut[x, y] = CalcForProjectLife.CheckIsAlive(CalcForProjectLife.CheckNeighbours(arrIn, isLocked, x, y, columns, rows), arrIn[x, y]); //Rectangle rect = GetRectangle(arrOut[x, y]); //Rectangle rect = CalcForProjectLife.GetRectangle(arrOut[x, y], resolution); //Canvas.SetLeft(rect, x * resolution); //Canvas.SetTop(rect, y * resolution); //canvas_field.Children.Add(rect); CanvasRectangleShow(x, y, arrOut[x, y]); } } } return(arrOut); }
private void timer_tick(object sender, EventArgs e) { if (Globals.isConSuccess && !isLogSaved && generations == 1) { con.SaveLog(CalcForProjectLife.ArrayToString(arr, columns, rows), "START"); isLogSaved = true; } last = (bool[, ])arr.Clone(); arr = NextGeneration(arr); main_window.Title = $"Generation №{generations}"; if (CalcForProjectLife.AnalizeGame(arr, last, columns, rows)) { MessageBox.Show("Игра окончена"); newGame = true; if (Globals.isConSuccess) { con.SaveLog(CalcForProjectLife.ArrayToString(arr, columns, rows), "END"); } isLogSaved = false; timer.Stop(); } }