Переменные уходят в бесконечность при первом запуске цикла while c#
Если запустить код приведённый ниже, то первая игра приведет нас к счёту 0:0 потому что переменные xx и yy почему-то уходят в бесконечность. Как это, и почему? Также есть вопросик, как можно добавлять картинки на окно, с помощью C#. И если кому не лень подскажите, можно ли эту "Войну и мир" как-то сократить? Заранее спасибо:) P.S. Появилась ещё одна проблема, если выиграть сначала за X, а потом за Y очки засчитают X, а следующее очко засчитают Y. Также действует, если начать играть и за Y. Есть следующий код на C#:
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace XorO
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
WinOrDefeat();
}
int x = 0;
int y = 0;
int one = 0;
int two = 0;
int three = 0;
int four = 0;
int five = 0;
int six = 0;
int seven = 0;
int eight = 0;
int nine = 0;
int win = 0;
int xx = 0;
int yy = 0;
int WOD = 0;
private void Return()
{
while (WOD == 1)
{
GameGrid.Children.Clear();
X1.Visibility = Visibility.Visible;
X2.Visibility = Visibility.Visible;
X3.Visibility = Visibility.Visible;
X4.Visibility = Visibility.Visible;
X5.Visibility = Visibility.Visible;
X6.Visibility = Visibility.Visible;
X7.Visibility = Visibility.Visible;
X8.Visibility = Visibility.Visible;
X9.Visibility = Visibility.Visible;
GameGrid.Children.Add(X1);
GameGrid.Children.Add(X2);
GameGrid.Children.Add(X3);
GameGrid.Children.Add(X4);
GameGrid.Children.Add(X5);
GameGrid.Children.Add(X6);
GameGrid.Children.Add(X7);
GameGrid.Children.Add(X8);
GameGrid.Children.Add(X9);
GameGrid.Children.Add(X);
GameGrid.Children.Add(O);
X.Content = $"X: {xx}";
O.Content = $"O: {yy}";
WOD = 0;
MessageBox.Show($"{xx}{yy}");
x = 0; y = 0; one = 0; two = 0; three = 0; four = 0; five = 0; six = 0; seven = 0; eight = 0; nine = 0;
}
}
private void X1_Click(object sender, RoutedEventArgs e)
{
TextBlock ST = new TextBlock
{
Background = Brushes.Black,
};
if (x == 0)
{
X1.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(ST);
ST.Background = Brushes.Black;
Grid.SetColumn(ST, 1);
Grid.SetRow(ST, 1);
one = 1;
x++;
win = 1;
}
else if (y < x)
{
X1.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(ST);
ST.Background = Brushes.Black;
Grid.SetColumn(ST, 1);
Grid.SetRow(ST, 1);
one = 2;
y++;
win = 1;
}
else if (x == y)
{
X1.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(ST);
ST.Background = Brushes.Black;
Grid.SetColumn(ST, 1);
Grid.SetRow(ST, 1);
one = 1;
x++;
win = 1;
}
WinOrDefeat();
win = 0;
}
private void X2_Click(object sender, RoutedEventArgs e)
{
TextBlock STD = new TextBlock
{
Background = Brushes.Black,
};
if (x == 0)
{
X2.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STD);
STD.Background = Brushes.Black;
Grid.SetColumn(STD, 2);
Grid.SetRow(STD, 1);
two = 1;
x++;
win = 1;
}
else if (y < x)
{
X2.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STD);
STD.Background = Brushes.Black;
Grid.SetColumn(STD, 2);
Grid.SetRow(STD, 1);
two = 2;
y++;
win = 1;
}
else if (x == y)
{
X2.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STD);
STD.Background = Brushes.Black;
Grid.SetColumn(STD, 2);
Grid.SetRow(STD, 1);
two = 1;
x++;
win = 1;
}
WinOrDefeat();
win = 0;
}
private void X3_Click(object sender, RoutedEventArgs e)
{
TextBlock STO = new TextBlock
{
Background = Brushes.Black,
};
if (x == 0)
{
X3.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STO);
STO.Background = Brushes.Black;
Grid.SetColumn(STO, 3);
Grid.SetRow(STO, 1);
three = 1;
x++;
win = 1;
}
else if (y < x)
{
X3.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STO);
STO.Background = Brushes.Black;
Grid.SetColumn(STO, 3);
Grid.SetRow(STO, 1);
three = 2;
y++;
win = 1;
}
else if (x == y)
{
X3.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STO);
STO.Background = Brushes.Black;
Grid.SetColumn(STO, 3);
Grid.SetRow(STO, 1);
three = 1;
x++;
win = 1;
}
WinOrDefeat();
win = 0;
}
private void X4_Click(object sender, RoutedEventArgs e)
{
TextBlock STT = new TextBlock
{
Background = Brushes.Black,
};
if (x == 0)
{
X4.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STT);
STT.Background = Brushes.Black;
Grid.SetColumn(STT, 1);
Grid.SetRow(STT, 2);
four = 1;
x++;
win = 1;
}
else if (y < x)
{
X4.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STT);
STT.Background = Brushes.Black;
Grid.SetColumn(STT, 1);
Grid.SetRow(STT, 2);
four = 2;
y++;
win = 1;
}
else if (x == y)
{
X4.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STT);
STT.Background = Brushes.Black;
Grid.SetColumn(STT, 1);
Grid.SetRow(STT, 2);
four = 1;
x++;
win = 1;
}
WinOrDefeat();
win = 0;
}
private void X5_Click(object sender, RoutedEventArgs e)
{
TextBlock STDT = new TextBlock
{
Background = Brushes.Black,
};
if (x == 0)
{
X5.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STDT);
STDT.Background = Brushes.Black;
Grid.SetColumn(STDT, 2);
Grid.SetRow(STDT, 2);
five = 1;
x++;
win = 1;
}
else if (y < x)
{
X5.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STDT);
STDT.Background = Brushes.Black;
Grid.SetColumn(STDT, 2);
Grid.SetRow(STDT, 2);
five = 2;
y++;
win = 1;
}
else if (x == y)
{
X5.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STDT);
STDT.Background = Brushes.Black;
Grid.SetColumn(STDT, 2);
Grid.SetRow(STDT, 2);
five = 1;
x++;
win = 1;
}
WinOrDefeat();
win = 0;
}
private void X6_Click(object sender, RoutedEventArgs e)
{
TextBlock STF = new TextBlock
{
Background = Brushes.Black,
};
if (x == 0)
{
X6.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STF);
STF.Background = Brushes.Black;
Grid.SetColumn(STF, 3);
Grid.SetRow(STF, 2);
six = 1;
x++;
win = 1;
}
else if (y < x)
{
X6.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STF);
STF.Background = Brushes.Black;
Grid.SetColumn(STF, 3);
Grid.SetRow(STF, 2);
six = 2;
y++;
win = 1;
}
else if (x == y)
{
X6.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STF);
STF.Background = Brushes.Black;
Grid.SetColumn(STF, 3);
Grid.SetRow(STF, 2);
six = 1;
x++;
win = 1;
}
WinOrDefeat();
win = 0;
}
private void X7_Click(object sender, RoutedEventArgs e)
{
TextBlock STDF = new TextBlock
{
Background = Brushes.Black,
};
if (x == 0)
{
X7.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STDF);
STDF.Background = Brushes.Black;
Grid.SetColumn(STDF, 1);
Grid.SetRow(STDF, 3);
seven = 1;
x++;
win = 1;
}
else if (y < x)
{
X7.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STDF);
STDF.Background = Brushes.Black;
Grid.SetColumn(STDF, 1);
Grid.SetRow(STDF, 3);
seven = 2;
y++;
win = 1;
}
else if (x == y)
{
X7.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STDF);
STDF.Background = Brushes.Black;
Grid.SetColumn(STDF, 1);
Grid.SetRow(STDF, 3);
seven = 1;
x++;
win = 1;
}
WinOrDefeat();
win = 0;
}
private void X8_Click(object sender, RoutedEventArgs e)
{
TextBlock STS = new TextBlock
{
Background = Brushes.Black,
};
if (x == 0)
{
X8.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STS);
STS.Background = Brushes.Black;
Grid.SetColumn(STS, 2);
Grid.SetRow(STS, 3);
eight = 1;
x++;
win = 1;
}
else if (y < x)
{
X8.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STS);
STS.Background = Brushes.Black;
Grid.SetColumn(STS, 2);
Grid.SetRow(STS, 3);
eight = 2;
y++;
win = 1;
}
else if (x == y)
{
X8.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STS);
STS.Background = Brushes.Black;
Grid.SetColumn(STS, 2);
Grid.SetRow(STS, 3);
eight = 1;
x++;
win = 1;
}
WinOrDefeat();
win = 0;
}
private void X9_Click(object sender, RoutedEventArgs e)
{
TextBlock STDS = new TextBlock
{
Background = Brushes.Black,
};
if (x == 0)
{
X9.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STDS);
STDS.Background = Brushes.Black;
Grid.SetColumn(STDS, 3);
Grid.SetRow(STDS, 3);
nine = 1;
x++;
WinOrDefeat();
win = 0;
}
else if (y < x)
{
X9.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STDS);
STDS.Background = Brushes.Black;
Grid.SetColumn(STDS, 3);
Grid.SetRow(STDS, 3);
nine = 2;
y++;
WinOrDefeat();
win = 0;
}
else if (x == y)
{
X9.Visibility = Visibility.Collapsed;
GameGrid.Children.Add(STDS);
STDS.Background = Brushes.Black;
Grid.SetColumn(STDS, 3);
Grid.SetRow(STDS, 3);
nine = 1;
x++;
WinOrDefeat();
win = 0;
}
WinOrDefeat();
win = 0;
}
private void WinOrDefeat()
{
while (win == 1)
{
if (one == 1 && two == 1 && three == 1)
{
MessageBox.Show("Победа X");
WOD = 1; Return(); xx+=1; win = 0; break;
}
else if (one == 2 && two == 2 && three == 2)
{
MessageBox.Show("Победа Y");
WOD = 1; Return(); yy+=1; break;
}
else if (three == 1 && six == 1 && nine == 1)
{
MessageBox.Show("Победа X");
WOD = 1; Return(); xx+=1; win = 0; break;
}
else if (three == 2 && six == 2 && nine == 2)
{
MessageBox.Show("Победа Y");
WOD = 1; Return(); yy+=1; break;
}
else if (seven == 1 && eight == 1 && nine == 1)
{
MessageBox.Show("Победа X");
WOD = 1; Return(); xx+=1; win = 0; break;
}
else if (seven == 2 && eight == 2 && nine == 2)
{
MessageBox.Show("Победа Y");
WOD = 1; Return(); yy+=1; break;
}
else if (one == 1 && four == 1 && seven == 1)
{
MessageBox.Show("Победа X");
WOD = 1; Return(); xx+=1; win = 0; break;
}
else if (one == 2 && four == 2 && seven == 2)
{
MessageBox.Show("Победа Y");
WOD = 1; Return(); yy+=1; break;
}
else if (four == 1 && five == 1 && six == 1)
{
MessageBox.Show("Победа X");
WOD = 1; Return(); xx+=1; win = 0; break;
}
else if (four == 2 && five == 2 && six == 2)
{
MessageBox.Show("Победа Y");
WOD = 1; Return(); yy+=1; break;
}
else if (two == 1 && five == 1 && eight == 1)
{
MessageBox.Show("Победа X");
WOD = 1; Return(); xx+=1; win = 0; break;
}
else if (two == 2 && five == 2 && eight == 2)
{
MessageBox.Show("Победа Y");
WOD = 1; Return(); yy+=1; break;
}
else if (one == 1 && five == 1 && nine == 1)
{
MessageBox.Show("Победа X");
WOD = 1; Return(); xx+=1; win = 0; break;
}
else if (one == 2 && five == 2 && nine == 2)
{
MessageBox.Show("Победа Y");
WOD = 1; Return(); yy+=1; break;
}
else if (three == 1 && five == 1 && seven == 1)
{
MessageBox.Show("Победа X");
WOD = 1; Return(); xx+=1; win = 0; break;
}
else if (three == 2 && five == 2 && seven == 2)
{
MessageBox.Show("Победа Y");
WOD = 1; Return(); yy+=1; break;
}
else if (one != 0 && two != 0 && three != 0 && four != 0 && five != 0 && six != 0 && seven != 0 && eight != 0 && nine != 0)
{
MessageBox.Show("Ничья");
WOD = 1; Return(); xx+=1; yy+=1;; break;
}
else
{
break;
}
}
}
}
}
А вот ещё код на xaml:
<Window x:Class="XorO.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XorO"
mc:Ignorable="d"
Title="MainWindow" Height="500" Width="500">
<Grid x:Name="GameGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button x:Name="X1" Grid.Column="1" Grid.Row="1" Click="X1_Click"/>
<Button x:Name="X2" Grid.Column="2" Grid.Row="1" Click="X2_Click"/>
<Button x:Name="X3" Grid.Column="3" Grid.Row="1" Click="X3_Click"/>
<Button x:Name="X4" Grid.Column="1" Grid.Row="2" Click="X4_Click"/>
<Button x:Name="X5" Grid.Column="2" Grid.Row="2" Click="X5_Click"/>
<Button x:Name="X6" Grid.Column="3" Grid.Row="2" Click="X6_Click"/>
<Button x:Name="X7" Grid.Column="1" Grid.Row="3" Click="X7_Click"/>
<Button x:Name="X8" Grid.Column="2" Grid.Row="3" Click="X8_Click"/>
<Button x:Name="X9" Grid.Column="3" Grid.Row="3" Click="X9_Click"/>
<Label x:Name="X" Content="X: 0" Grid.Column="1" Grid.Row="0" FontSize="40"/>
<Label x:Name="O" Content="O: 0" Grid.Column="2" Grid.Row="0" FontSize="40"/>
</Grid>
</Window>