namespace 刘老师推箱子
{
class Program
{
static int kkk1, kkk2;
static void Main(string[] args)
{
int x = 1, y = 1, i = 0;
int[, ,] dt = new int[2, 10, 10] //定义一个二维数组用于接收地图
{
{
{1,1,1,1,1,1,1,1,1,1},
{1,4,0,0,0,0,1,0,0,1},
{1,0,0,0,0,0,1,0,0,1},
{1,0,0,0,0,2,1,0,0,1},
{1,0,0,0,0,0,0,1,0,1},
{1,0,0,0,0,0,0,1,0,1},
{1,0,0,1,0,0,0,0,0,1},
{1,1,1,1,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,3,1},
{1,1,1,1,1,1,1,1,1,1}
},
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,1,0,0,1},
{1,0,2,0,0,0,1,0,0,1},
{1,0,0,0,0,0,1,0,0,1},
{1,0,4,0,0,0,0,1,0,1},
{1,0,0,0,0,0,0,1,0,1},
{1,0,0,1,0,0,0,0,0,1},
{1,1,1,1,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,3,1},
{1,1,1,1,1,1,1,1,1,1}
}
};
while (true)
{
int temp = 0;
ConsoleKeyInfo s = Console.ReadKey(); //s用于接收用户输入的键
if (s.Key.ToString() == "DownArrow")
{
if (dt[i, x + 1, y] == 0) //如果小人的下一步是0(空格),则两个元素交换 (下同)
{
temp = dt[i, x, y];
dt[i, x, y] = dt[i, x + 1, y];
dt[i, x + 1, y] = temp;
x++;
}
if (dt[i, x + 1, y] == 2 && (dt[i, x + 2, y] == 0 || dt[i, x + 2, y] == 3)) //如果小人的下一步是箱子,箱子的下一步是空格,则三个元素进行交换 (下同)
{
if (dt[i, x + 2, y] == 3) //如果小人的下面第二个元素是“★”
{
dt[i, x + 2, y] = 2; //让五角星变成箱子
dt[i, x + 1, y] = 4; // 让箱子变成小人
dt[i, x, y] = 0; //让小人变成空格
temp = 6; //给临时变量赋一个值作为跳出循环的条件
}
else
{
temp = dt[i, x + 2, y];
dt[i, x + 2, y] = dt[i, x + 1, y];
dt[i, x + 1, y] = temp;
temp = dt[i, x + 1, y];
dt[i, x + 1, y] = dt[i, x, y];
dt[i, x, y] = temp;
x++;
}
}
}
else if (s.Key.ToString() == "UpArrow")
{
if (dt[i, x - 1, y] == 0)
{
temp = dt[i, x - 1, y];
dt[i, x - 1, y] = dt[i, x, y];
dt[i, x, y] = temp;
x--;
}
if (dt[i, x - 1, y] == 2 && (dt[i, x - 2, y] == 0 || dt[i, x - 2, y] == 3))
{
if (dt[i, x - 2, y] == 3) //如果小人的下面第二个元素是“★”
{
dt[i, x - 2, y] = 2; //让五角星变成箱子
dt[i, x - 1, y] = 4; // 让箱子变成小人
dt[i, x, y] = 0; //让小人变成空格
temp = 6; //给临时变量赋一个值作为跳出循环的条件
}
else
{
temp = dt[i, x - 2, y];
dt[i, x - 2, y] = dt[i, x - 1, y];
dt[i, x - 1, y] = temp;
temp = dt[i, x, y];
dt[i, x, y] = dt[i, x - 1, y];
dt[i, x - 1, y] = temp;
x--;
}
}
}
else if (s.Key.ToString() == "LeftArrow")
{
if (dt[i, x, y - 1] == 0)
{
temp = dt[i, x, y - 1];
dt[i, x, y - 1] = dt[i, x, y];
dt[i, x, y] = temp;
y--;
}
if (dt[i, x, y - 1] == 2 && (dt[i, x, y - 2] == 0 || dt[i, x, y - 2] == 3))
{
if (dt[i, x, y - 2] == 3) //如果小人的下面第二个元素是“★”
{
dt[i, x, y - 2] = 2; //让五角星变成箱子
dt[i, x, y - 1] = 4; // 让箱子变成小人
dt[i, x, y] = 0; //让小人变成空格
temp = 6; //给临时变量赋一个值作为跳出循环的条件
}
else
{
temp = dt[i, x, y - 2];
dt[i, x, y - 2] = dt[i, x, y - 1];
dt[i, x, y - 1] = temp;
temp = dt[i, x, y];
dt[i, x, y] = dt[i, x, y - 1];
dt[i, x, y - 1] = temp;
y--;
}
}
}
else if (s.Key.ToString() == "RightArrow")
{
if (dt[i, x, y + 1] == 0)
{
temp = dt[i, x, y + 1];
dt[i, x, y + 1] = dt[i, x, y];
dt[i, x, y] = temp;
y++;
}
if (dt[i, x, y + 1] == 2 && (dt[i, x, y + 2] == 0 || dt[i, x, y + 2] == 3))
{
if (dt[i, x, y + 2] == 3) //如果小人的下面第二个元素是“★”
{
dt[i, x, y + 2] = 2; //让五角星变成箱子
dt[i, x, y + 1] = 4; // 让箱子变成小人
dt[i, x, y] = 0; //让小人变成空格
temp = 6; //给临时变量赋一个值作为跳出循环的条件
}
else
{
temp = dt[i, x, y + 2];
dt[i, x, y + 2] = dt[i, x, y + 1];
dt[i, x, y + 1] = temp;
temp = dt[i, x, y + 1];
dt[i, x, y + 1] = dt[i, x, y];
dt[i, x, y] = temp;
y++;
}
}
}
Console.Clear(); //清屏
ditu(dt, i); // 调用ditu函数打印地图
if (temp == 6) // 跳出循环
{
Console.WriteLine("恭喜您过关了!");
Console.WriteLine("按任意键进入下一关!");
Console.ReadKey();
x =4; y =1;//初始化小人的位置
i++;
}
}//while 的花括号
} //static函数的花括号
//用for循环做一个地图
private static void ditu(int[, ,] dt, int i)
{
for (int j = 0; j < 10; j++)
{
for (int k = 0; k < 10; k++)
{
if (dt[i, j, k] == 0)
{
Console.Write(" ");
}
else if (dt[i, j, k] == 1)
{
Console.Write("■");
}
else if (dt[i, j, k] == 2)
{
Console.Write("□");
}
else if (dt[i, j, k] == 3)
{
Console.Write("★");
}
else if (dt[i, j, k] == 4)
{
Console.Write("♀");
kkk1 =j; kkk2 = k;
}
}
Console.Write("
");
}
}
}
}