/// <summary> /// Checking for the conditions of win / draw. /// </summary> public void Check() { int size = Convert.ToInt16(Math.Sqrt(mainGrid.Children.Count)); Button[] buttons = mainGrid.Children.Cast <Button>().ToArray <Button>(); char[,] arr = new char[size, size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { switch (buttons[size * i + j].Tag) { case "button": arr[i, j] = ' '; break; case "x": arr[i, j] = 'x'; break; case "o": arr[i, j] = 'o'; break; } } } int[,] result = MatrixLogics.checkForWin(arr); if (movesCount == mainGrid.Children.Count) { MessageBox.Show("Koniec", "Game over", MessageBoxButton.OK); isPlayable = false; } if (result is null) { return; } isPlayable = false; int won = 0; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (result[i, j] != 0) { buttons[size * i + j].Background = Brushes.LightGreen; won = result[i, j]; } } } if (won == 1) { playerWon(firstPlayer); } if (won == 2) { playerWon(secondPlayer); } }
public void Check() { int size = Convert.ToInt16(Math.Sqrt(mainGrid.Children.Count)); Button[] buttons = mainGrid.Children.Cast <Button>().ToArray <Button>(); char[,] arr = new char[size, size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { switch (buttons[size * i + j].Tag) { case "button": arr[i, j] = ' '; break; case "x": arr[i, j] = 'x'; break; case "o": arr[i, j] = 'o'; break; } } } int[,] result = MatrixLogics.checkForWin(arr); foreach (int i in result) { if (i == 1) { break; } if (i == 2) { break; } } for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (result[i, j] != 0) { buttons[size * i + j].Content = result[i, j]; } } } }
public void CreateGrid(int size) { MatrixLogics.Reset(); mainGrid.Children.Clear(); mainGrid.RowDefinitions.Clear(); mainGrid.ColumnDefinitions.Clear(); RowDefinition rd; ColumnDefinition cd; for (int i = 0; i < size; i++) { rd = new RowDefinition(); rd.Height = new GridLength(1, GridUnitType.Star); mainGrid.RowDefinitions.Add(rd); cd = new ColumnDefinition(); cd.Width = new GridLength(1, GridUnitType.Star); mainGrid.ColumnDefinitions.Add(cd); } for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { Button b = new Button(); b.Tag = "button"; b.FontFamily = new FontFamily("Comic Sans MS"); b.FontWeight = FontWeights.Bold; b.FontSize = 200 / size; b.Foreground = Brushes.Orange; b.Background = Brushes.White; Style s = new Style(); //Trigger t = new Trigger(); //t.Property = TextBlock.IsMouseOverProperty; //t.Value = true; //Setter setter = new Setter(TextBlock.ForegroundProperty, Brushes.Red); //t.Setters.Add(setter); //s.Triggers.Add(t); b.Style = s; b.Click += move; Grid.SetRow(b, i); Grid.SetColumn(b, j); mainGrid.Children.Add(b); } } }
public void CreateGrid(int size) { movesCount = 0; isPlayerXturn = false; isPlayable = true; MatrixLogics.Reset(); mainGrid.Children.Clear(); mainGrid.RowDefinitions.Clear(); mainGrid.ColumnDefinitions.Clear(); RowDefinition rd; ColumnDefinition cd; for (int i = 0; i < size; i++) { rd = new RowDefinition(); rd.Height = new GridLength(1, GridUnitType.Star); mainGrid.RowDefinitions.Add(rd); cd = new ColumnDefinition(); cd.Width = new GridLength(1, GridUnitType.Star); mainGrid.ColumnDefinitions.Add(cd); } for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { Button b = new Button(); b.Tag = "button"; b.FontFamily = new FontFamily("Comic Sans MS"); b.FontWeight = FontWeights.Bold; b.FontSize = 200 / size; b.Foreground = Brushes.Orange; b.Background = Brushes.White; b.Click += move; Grid.SetRow(b, i); Grid.SetColumn(b, j); mainGrid.Children.Add(b); } } }