using System;
namespace _2048
{
public class App
{
int[,] arr = new int[4, 4];
Random random = new Random();
int count = 0;
public App()
{
arr[row, col] = GetTwoOrFour();
while (true)
{
if (row != row1)
{
arr[row1, col1] = GetTwoOrFour();
break;
}
}
Scan();
Console.WriteLine();
while (true)
{
ConsoleKeyInfo key;
key = Console.ReadKey(true);
{
case ConsoleKey.UpArrow:
moveUp();
while (true)
{
if (arr[row, col] == 0)
{
arr[row, col] = GetTwoOrFour();
break;
}
}
Console.WriteLine();
Scan();
break;
case ConsoleKey.DownArrow:
MoveDown();
while (true)
{
if (arr[row, col] == 0)
{
arr[row, col] = GetTwoOrFour();
break;
}
}
Console.WriteLine();
Scan();
break;
case ConsoleKey.LeftArrow:
MoveLeft();
while (true)
{
if (arr[row, col] == 0)
{
arr[row, col] = GetTwoOrFour();
break;
}
}
Console.WriteLine();
Scan();
break;
case ConsoleKey.RightArrow:
MoveRight();
while (true)
{
if (arr[row, col] == 0)
{
arr[row, col] = GetTwoOrFour();
break;
}
}
Console.WriteLine();
Scan();
break;
}
}
}
public void moveUp()
{
count = 1;
for (int i = 0; i < 4; i++)
{
for (int row = 0; row < 4; row++)
{
for (int col = 0; col < 4; col++)
{
bool b = Check(row, col);
if (b == true)
{
if (arr[row, col] == 0)
{
arr[row, col] = arr[row + 1, col];
arr[row + 1, col] = 0;
}
}
}
}
}
for (int row = 0; row < 4; row++)
{
for (int col = 0; col < 4; col++)
{
if (row < 3)
{
if (arr[row, col] == arr[row + 1, col])
{
arr[row, col] = arr[row, col] + arr[row + 1, col];
arr[row + 1, col] = 0;
}
}
}
}
for (int i = 0; i < 4; i++)
{
for (int row = 0; row < 4; row++)
{
for (int col = 0; col < 4; col++)
{
bool b = Check(row, col);
if (b == true)
{
if (arr[row, col] == 0)
{
arr[row, col] = arr[row + 1, col];
arr[row + 1, col] = 0;
}
}
}
}
}
}
public void MoveDown()
{
count = 2;
for (int i = 0; i < 4; i++)
{
for (int row = 3; row >= 0; row--)
{
for (int col = 0; col < 4; col++)
{
bool b = Check(row, col);
if (b == true)
{
if (arr[row, col] == 0)
{
arr[row, col] = arr[row - 1, col];
arr[row - 1, col] = 0;
}
}
}
}
}
for (int row = 3; row >= 0; row--)
{
for (int col = 0; col < 4; col++)
{
if (row > 0)
{
if (arr[row, col] == arr[row - 1, col])
{
arr[row, col] = arr[row, col] + arr[row - 1, col];
arr[row - 1, col] = 0;
}
}
}
}
for (int i = 0; i < 4; i++)
{
for (int row = 3; row >= 0; row--)
{
for (int col = 0; col < 4; col++)
{
bool b = Check(row, col);
if (b == true)
{
if (arr[row, col] == 0)
{
arr[row, col] = arr[row - 1, col];
arr[row - 1, col] = 0;
}
}
}
}
}
}
public void MoveLeft()
{
count = 3;
for (int i = 0; i < 4; i++)
{
for (int col = 0; col <4; col++)
{
for (int row = 0; row < 4; row++)
{
bool b = Check(row, col);
if (b == true)
{
if (arr[row, col] == 0)
{
arr[row, col] = arr[row , col+1];
arr[row, col+1] = 0;
}
}
}
}
}
for (int col = 0; col < 4; col++)
{
for (int row = 0; row < 4; row++)
{
if (col < 3)
{
if (arr[row, col] == arr[row, col+1])
{
arr[row, col] = arr[row, col] + arr[row, col+1];
arr[row, col+1] = 0;
}
}
}
}
for (int i = 0; i < 4; i++)
{
for (int col = 0; col < 4; col++)
{
for (int row = 0; row < 4; row++)
{
bool b = Check(row, col);
if (b == true)
{
if (arr[row, col] == 0)
{
arr[row, col] = arr[row, col + 1];
arr[row, col + 1] = 0;
}
}
}
}
}
}
public void MoveRight()
{
count = 4;
for (int i = 0; i < 4; i++)
{
for (int col = 3; col > 0; col--)
{
for (int row = 0; row < 4; row++)
{
bool b = Check(row, col);
if (b == true)
{
if (arr[row, col] == 0)
{
arr[row, col] = arr[row, col - 1];
arr[row, col - 1] = 0;
}
}
}
}
}
for (int col = 3; col > 0; col--)
{
for (int row = 0; row < 4; row++)
{
if (col > 0)
{
if (arr[row, col] == arr[row, col - 1])
{
arr[row, col] = arr[row, col] + arr[row, col - 1];
arr[row, col - 1] = 0;
}
}
}
}
for (int i = 0; i < 4; i++)
{
for (int col = 3; col > 0; col--)
{
for (int row = 0; row < 4; row++)
{
bool b = Check(row, col);
if (b == true)
{
if (arr[row, col] == 0)
{
arr[row, col] = arr[row, col - 1];
arr[row, col - 1] = 0;
}
}
}
}
}
}
public bool Check(int row, int col)
{
if (count == 1)
{
row = row + 1;
if (row < 4)
{
switch (arr[row, col])
{
case 0:
case 2:
case 4:
case 8:
case 16:
case 32:
case 64:
case 128:
case 256:
case 512:
case 1024:
case 2048:
return true;
default:
return true;
}
}
}
if (count == 2)
{
row = row - 1;
if (row >= 0)
{
switch (arr[row, col])
{
case 0:
case 2:
case 4:
case 8:
case 16:
case 32:
case 64:
case 128:
case 256:
case 512:
case 1024:
case 2048:
return true;
default:
return false;
}
}
}
if (count == 3)
{
col = col + 1;
if (col < 4)
{
switch (arr[row, col])
{
case 0:
case 2:
case 4:
case 8:
case 16:
case 32:
case 64:
case 128:
case 256:
case 512:
case 1024:
case 2048:
return true;
default:
return false;
}
}
}
if (count == 4)
{
col = col - 1;
if (col >=0)
{
switch (arr[row, col])
{
case 0:
case 2:
case 4:
case 8:
case 16:
case 32:
case 64:
case 128:
case 256:
case 512:
case 1024:
case 2048:
return true;
default:
return false;
}
}
}
return false;
}
public int GetTwoOrFour()
{
if (num > 50)
{
return 4;
}
return 2;
}
public void Scan()
{
Console.WriteLine(" -----------------------------------------");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write(" || ");
Console.Write(arr[i, j]);
}
Console.Write("||");
Console.WriteLine();
Console.WriteLine(" -----------------------------------------");
}
}
}
}