Реализация сдвига ячеек в игре 2048 на C#
Пишу код уже несколько дней и все не решается проблема: при клике либо вообще не появляется ячейка, думаю, проблема в сдвиге, так как если убрать функции сдвига, то ячейки нормально появляются
public partial class Form1 : Form
{
public int[,] map = new int[4, 4];
public Label[,] labels = new Label[4, 4];
public Button[,] but = new Button[4, 4];
//public static int indexpic = 0;
private int score = 0;
bool IsShift = false;
public Form1()
{
InitializeComponent();
createMap();
createNewPic();
}
private void createMap()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
//Button bu = new Button();
but[i, j] = new Button();
but[i, j].Location = new Point(30 + 58 * j, 73 + 58 * i);
but[i, j].Size = new Size(50, 50);
but[i, j].BackColor = Color.Gray;
but[i, j].Click += new EventHandler(Click1);
this.Controls.Add(but[i, j]);
map[i, j] = 0;
}
}
return;
}
private void createNewPic()
{
Random rnd = new Random();
int i = rnd.Next(0, 3);
int j = rnd.Next(0, 3);
map[i, j] = 1;
but[i, j] = new Button();
labels[i, j] = new Label();
labels[i, j].Text = "2";
labels[i, j].Size = new Size(50, 50);
labels[i, j].TextAlign = ContentAlignment.MiddleCenter;
but[i, j].Font = new Font(new FontFamily("Microsoft Sans Serif"), 15);
but[i, j].Controls.Add(labels[i, j]);
but[i, j].Location = new Point(30 + j * 58, 73 + 58 * i);
but[i, j].Size = new Size(50, 50);
but[i, j].BackColor = Color.LightSalmon;
this.Controls.Add(but[i, j]);
but[i, j].BringToFront();
}
private void ColorOfBut(int sum, int i, int j)
{
if (sum / 2048 == 1) but[i, j].BackColor = Color.Green;
else if (sum / 1024 == 1) but[i, j].BackColor = Color.LightBlue;
else if (sum / 512 == 1) but[i, j].BackColor = Color.Sienna;
else if (sum / 256 == 1) but[i, j].BackColor = Color.DarkViolet;
else if (sum / 128 == 1) but[i, j].BackColor = Color.Yellow;
else if (sum / 64 == 1) but[i, j].BackColor = Color.Red;
else if (sum / 32 == 1) but[i, j].BackColor = Color.Pink;
else if (sum / 16 == 1) but[i, j].BackColor = Color.Coral;
else if (sum / 8 == 1) but[i, j].BackColor = Color.LightCoral;
else if (sum / 4 == 1) but[i, j].BackColor = Color.LightPink;
}
#region Функции сдвигов
private void RightShift()
{
for (int k = 0; k < 4; k++)
{
for (int l = 2; l >= 0; l--)
{
if (map[k, l] == 1)
{
for (int j = l + 1; j < 4; j++)
{
if (map[k, j] == 0)//&& j<4)
{
map[k, j - 1] = 0;
map[k, j] = 1;
but[k, j] = but[k, j - 1];
but[k, j - 1] = null;
labels[k, j] = labels[k, j - 1];
labels[k, j - 1] = null;
but[k, j].Location = new Point(but[k, j].Location.X + 58, but[k, j].Location.Y);
}
else
{
int a = int.Parse(labels[k, j].Text);
int b = int.Parse(labels[k, j - 1].Text);
if (a == b)
{
labels[k, j].Text = (a + b).ToString();
score += (a + b);
//map[k, j] = 1;
label1.Text = "Очки: " + score;
ColorOfBut(a + b, k, j);
map[k, j - 1] = 0;
//this.Controls.Remove(but[k, j - 1]);
//this.Controls.Remove(labels[k, j - 1]);
but[k, j - 1] = null;
labels[k, j - 1] = null;
}
}
}
}
}
}
}
private void LeftShift()
{
for (int k = 0; k < 4; k++)
{
for (int l = 1; l < 4; l++)
{
if (map[k, l] == 1)
{
for (int j = l - 1; j >= 0; j--)
{
if (map[k, j] == 0)
{
map[k, j + 1] = 0;
map[k, j] = 1;
but[k, j] = but[k, j + 1];
but[k, j + 1] = null;
labels[k, j] = labels[k, j + 1];
labels[k, j + 1] = null;
but[k, j].Location = new Point(but[k, j].Location.X - 58, but[k, j].Location.Y);
}
else
{
int a = int.Parse(labels[k, j].Text);
int b = int.Parse(labels[k, j + 1].Text);
if (a == b)
{
labels[k, j].Text = (a + b).ToString();
map[k, j] = 1;
score += (a + b);
label1.Text = "Очки: " + score;
ColorOfBut(a + b, k, j);
map[k, j + 1] = 0;
this.Controls.Remove(but[k, j + 1]);
this.Controls.Remove(labels[k, j + 1]);
but[k, j + 1] = null;
labels[k, j + 1] = null;
}
}
}
}
}
}
}
private void DownShift()
{
for (int k = 2; k >= 0; k--)
{
for (int l = 0; l < 4; l++)
{
if (map[k, l] == 1)
{
for (int j = k + 1; j < 4; j++)
{
if (map[j, l] == 0)
{
map[j - 1, l] = 0;
map[j, l] = 1;
but[j, l] = but[j - 1, l];
but[j - 1, l] = null;
labels[j, l] = labels[j - 1, l];
labels[j - 1, l] = null;
but[j, l].Location = new Point(but[j, l].Location.X, but[j, l].Location.Y + 58);
}
else
{
int a = int.Parse(labels[j, l].Text);
int b = int.Parse(labels[j - 1, l].Text);
if (a == b)
{
labels[j, l].Text = (a + b).ToString();
score += (a + b);
label1.Text = "Очки: " + score;
ColorOfBut(a + b, j, l);
map[j - 1, l] = 0;
map[k, j] = 1;
this.Controls.Remove(but[j - 1, l]);
this.Controls.Remove(labels[j - 1, l]);
but[j - 1, l] = null;
labels[j - 1, l] = null;
}
}
}
}
}
}
}
private void UpShift()
{
for (int k = 1; k < 4; k++)
{
for (int l = 0; l < 4; l++)
{
if (map[k, l] == 1)
{
for (int j = k - 1; j >= 0; j--)
{
if (map[j, l] == 0)
{
map[j + 1, l] = 0;
map[j, l] = 1;
but[j, l] = but[j + 1, l];
but[j + 1, l] = null;
labels[j, l] = labels[j + 1, l];
labels[j + 1, l] = null;
but[j, l].Location = new Point(but[j, l].Location.X, but[j, l].Location.Y - 58);
}
else
{
int a = int.Parse(labels[j, l].Text);
int b = int.Parse(labels[j + 1, l].Text);
if (a == b)
{
labels[j, l].Text = (a + b).ToString();
map[j, l] = 1;
score += (a + b);
ColorOfBut(a + b, j, l);
label1.Text = "Очки: " + score;
map[j + 1, l] = 0;
this.Controls.Remove(but[j + 1, l]);
this.Controls.Remove(labels[j + 1, l]);
but[j + 1, l] = null;
labels[j + 1, l] = null;
}
}
//if (labels[j, l].Text == "2048") MessageBox.Show("Игра окончена! Вы выиграли!");
}
}
}
}
}
#endregion
private void Click1(object sender, EventArgs e)
{
for (int i = 0; i < 4; i++)
{
for (int m = 0; m < 4; m++)
{
if ((Button)sender == but[i, m])
{
but[i, m] = new Button();
labels[i, m] = new Label();
labels[i, m].Text = "2";
labels[i, m].Size = new Size(50, 50);
labels[i, m].TextAlign = ContentAlignment.MiddleCenter;
labels[i, m].Font = new Font(new FontFamily("Microsoft Sans Serif"), 15);
but[i, m].Controls.Add(labels[i, m]);
but[i, m].Location = new Point(30 + m * 58, 73 + 58 * i);
but[i, m].Size = new Size(50, 50);
but[i, m].BackColor = Color.LightSalmon;
map[i, m] = 1;
UpShift();
this.Controls.Add(but[i, m]);
UpShift();
but[i, m].BringToFront();
//ClickShift();
//break;
}
}
}
}
private void ClickShift()
{
bool Shift = false;
Refresh();
do
{
for (int k = 0; k < 4; k++)
{
for (int j = 2; j >= 0; j--)
{
if (map[k, j] == map[k, j + 1] || map[k, j] != 0 && map[k, j + 1] == 0)
{
RightShift();
Shift = true;
break;
}
else
{
for (int f = 1; f < 4; f++)
{
for (int l = 0; l < 4; l++)
{
if (map[f, k] == map[f - 1, k] || map[f, k] != 0 && map[f - 1, k] == 0)
{
UpShift();
Shift = true;
break;
}
else
{
for (int s = 1; s < 4; s++)
{
if (map[k, f] == map[k, f - 1] || map[k, f] == 1 && map[k, f - 1] == 0)
{
LeftShift();
Shift = true;
break;
}
else
{
if (map[j, k] == map[j + 1, k] || map[j, k] == 1 && map[j + 1, k] == 0)
{
DownShift();
Shift = true;
break;
}
}
}
}
}
}
}
}
}
} while (Shift = true);
}
}
Ответы (1 шт):
Автор решения: Alexander Pavlov
→ Ссылка
while (Shift = true) это бесконечный цикл, потому что здесь не сравнение, а присваивание+сравнение. Д.б. while (Shift)