int[,] field = new int[4, 4];
/// <summary>
/// Prüft ob das Feld voll ist
/// </summary>
/// <returns></returns>
private bool CheckFull()
{
bool isit = false;
int c = 0;
for (int a = 0; a <= 3; a++)
{
for (int b = 0; b <= 3; b++)
{
if (field[a, b] != 0) { c += 1; }
}
}
if (c == 16) { isit = true; }
return isit;
}
/// <summary>
/// Wird ausgeführt, wenn das Spiel zuende ist.
/// </summary>
/// <param name="win"></param>
private void GameOver(bool win)
{
if (win) { MessageBox.Show("Gewonnen"); } else { MessageBox.Show("Verloren"); } Application.Exit();
}
/// <summary>
/// Fügt eine neue Nummer ein
/// </summary>
/// <param name="Sonder"></param>
private void SpawnNewNumber(bool Sonder)
{
if (!CheckFull())
{
string[] DB = new string[16];
int count = 0;
for (int xx = 0; xx <= 3; xx++)
{
for (int yy = 0; yy <= 3; yy++)
{
if (field[xx, yy] == 0) { DB[count] = xx.ToString() + ";" + yy.ToString(); count++; }
}
}
bool cont = true;
while (cont)
{
Random r = new Random();
int r1 = r.Next(0, count);
string[] SPLIT = DB[r1].Split(';');
int P1 = Convert.ToInt32(SPLIT[0]);
int P2 = Convert.ToInt32(SPLIT[1]);
if (!Sonder)
{
if (PUNKTZAHL < 5000)
{
if (field[P1, P2] == 0) { field[P1, P2] = 2; break; }
}
else
{
if (field[P1, P2] == 0)
{
int z = r.Next(1, 10);
if (z == 2 || z == 7)
{
field[P1, P2] = 4;
break;
}
else { field[P1, P2] = 2; break; }
}
}
}
else
{
for (int i = 0; i <= 1; i++)
{
field[P1, P2] = numbers[new Random().Next(0, 10)];
PUNKTZAHL += field[P1, P2];
}
}
}
}
else
{
GameOver(false);
}
}
/// <summary>
/// Führt ein "Hochstreichen" durch
/// </summary>
private void Up()
{
for (int a = 1; a <= 4; a++)
{
for (int e = 1; e <= 2; e++)
{
//Addition der Zahlen in der Spalte nach oben
for (int b = 0; b <= 3; b++)
{
int VALUE = field[a - 1, b];
if (VALUE != 0)
{
if (b < 3)
{
if (field[a - 1, b] == field[a - 1, b + 1])
{
field[a - 1, b + 1] = 0;
field[a - 1, b] = 2 * field[a - 1, b];
PUNKTZAHL += field[a - 1, b];
}
}
}
}
//Verschiebung der Zahlen nach oben hin7
for (int c = 0; c <= 3; c++)
{
if (field[a - 1, c] == 0)
{
if (c < 3)
{
for (int d = c + 1; d <= 3; d++)
{
if (field[a - 1, d] != 0)
{
field[a - 1, c] = field[a - 1, d];
field[a - 1, d] = 0;
break;
}
}
}
}
}
}
}
}
/// <summary>
/// Führt ein "Runterschieben" durch
/// </summary>
private void Down()
{
for (int a = 1; a <= 4; a++)
{
for (int e = 1; e <= 2; e++)
{
//Addition der Zahlen in der Spalte nach oben
for (int b = 3; b >= 0; b--)
{
int VALUE = field[a - 1, b];
if (VALUE != 0)
{
if (b > 0)
{
if (field[a - 1, b] == field[a - 1, b -1])
{
field[a - 1, b - 1] = 0;
field[a - 1, b] = 2 * field[a - 1, b];
PUNKTZAHL += field[a - 1, b];
}
}
}
}
//Verschiebung der Zahlen nach oben hin7
for (int c = 3; c >= 0; c--)
{
if (field[a - 1, c] == 0)
{
if (c > 0)
{
for (int d = c - 1; d >= 0; d--)
{
if (field[a - 1, d] != 0)
{
field[a - 1, c] = field[a - 1, d];
field[a - 1, d] = 0;
break;
}
}
}
}
}
}
}
}
/// <summary>
/// Schiebt nach Rechts.
/// </summary>
private void Right()
{
for (int a = 1; a <= 4; a++)
{
for (int e = 1; e <= 2; e++)
{
//Addition der Zahlen in der Spalte nach oben
for (int b = 3; b >= 0; b--)
{
int VALUE = field[b, a - 1];
if (VALUE != 0)
{
if (b > 0)
{
if (field[b, a - 1] == field[b - 1, a - 1])
{
field[ b - 1, a - 1] = 0;
field[b, a - 1] = 2 * field[b, a - 1];
PUNKTZAHL += field[b, a - 1];
}
}
}
}
//Verschiebung der Zahlen nach oben hin7
for (int c = 3; c >= 0; c--)
{
if (field[c, a - 1] == 0)
{
if (c > 0)
{
for (int d = c - 1; d >= 0; d--)
{
if (field[d, a - 1] != 0)
{
field[c, a - 1] = field[d, a - 1];
field[d, a - 1] = 0;
break;
}
}
}
}
}
}
}
}
/// <summary>
/// Schiebt nach links
/// </summary>
private void Left()
{
for (int a = 1; a <= 4; a++)
{
for (int e = 1; e <= 2; e++)
{
//Addition der Zahlen in der Spalte nach oben
for (int b = 0; b <= 3; b++)
{
int VALUE = field[b, a - 1];
if (VALUE != 0)
{
if (b < 3)
{
if (field[b, a - 1] == field[b+1,a - 1])
{
field[b + 1, a - 1] = 0;
field[b, a - 1] = 2 * field[b, a - 1];
PUNKTZAHL += field[b, a - 1];
}
}
}
}
//Verschiebung der Zahlen nach oben hin7
for (int c = 0; c <= 3; c++)
{
if (field[c, a - 1] == 0)
{
if (c < 3)
{
for (int d = c + 1; d <= 3; d++)
{
if (field[d, a - 1] != 0)
{
field[c, a - 1] = field[d, a - 1];
field[d, a - 1] = 0;
break;
}
}
}
}
}
}
}
}
int PUNKTZAHL = 0;
private void Initialisieren()
{
button6.Focus();
PS.Text = "Punkte: " + PUNKTZAHL.ToString();
Random r = new Random();
bool doit = true;
int[] numbers = new int[4];
string N = "";
while (doit)
{
N = "";
for (int i = 0; i <= 3; i++)
{
numbers[i] = new Random().Next(0, 4);
N += numbers[i].ToString() + " ";
System.Threading.Thread.Sleep(6);
}
if (!(numbers[0] == numbers[2] && numbers[1] == numbers[3]))
{
doit = false;
}
}
field[numbers[0], numbers[1]] = 2;
field[numbers[2], numbers[3]] = 2;
}
public int[] numbers = new int[] { 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384 };
/// <summary>
/// Generiert abgestufte Item-Farben
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public Color GenerateColor(int index)
{
int base2 = ((index + 1) * 11);
return Color.FromArgb(255, 255 - base2 - 10, 255 - base2 - 50, 255 - base2 - 80);
}
/// <summary>
/// Visualisiert das Spielfeld in die Labels
/// </summary>
private void Render()
{
for (int y = 0; y <= 3; y++)
{
for (int x = 0; x <= 3; x++)
{
int X = x +1;
int lnr = X + (y * 4);
for (int i = 1; i <= this.Controls.Count -1; i++)
{
//MessageBox.Show(this.Controls[i].ToString());
if (this.Controls[i].Name == "label" + lnr.ToString())
{
if (field[x, y].ToString() != "0")
{
for (int p = 0; p <= numbers.Length - 1; p++)
{
if (numbers[p] == field[x, y]) { this.Controls[i].BackColor = GenerateColor(p); break; }
}
this.Controls[i].Text = field[x, y].ToString();
if (this.Controls[i].Text == "2048")
{
GameOver(true);
}
}
else
{
this.Controls[i].Text = "";
this.Controls[i].BackColor = Color.Transparent;
}
}
}
}
}
PS.Text = "Punkte: " + PUNKTZAHL.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
//if (Environment.UserName.ToLower() == "konrad") { Application.Exit(); }
Initialisieren();
Render();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
bool h = false;
if (e.KeyData == Keys.W) { Up(); h = true; }
if (e.KeyData == Keys.S) { Down(); h = true; }
if (e.KeyData == Keys.A) { Left(); h = true; }
if (e.KeyData == Keys.D) { Right(); h = true; }
if (h)
{
SpawnNewNumber(false);
Render();
}
}
private void button6_KeyDown(object sender, KeyEventArgs e)
{
bool h = false;
if (e.KeyData == Keys.W) { Up(); h = true; }
if (e.KeyData == Keys.S) { Down(); h = true; }
if (e.KeyData == Keys.A) { Left(); h = true; }
if (e.KeyData == Keys.D) { Right(); h = true; }
if (h)
{
SpawnNewNumber(false);
Render();
}
}