今天用到数组在控制台做了一个五子棋游戏,实现如下:
static void Main(string[] args)
{
string[,] QP = new string[17, 17]; //定义二维数组QP同时设置棋盘大小
while (true)
{
{
string[,] QP = new string[17, 17]; //定义二维数组QP同时设置棋盘大小
while (true)
{
Console.Clear();
int left = 16;
int top = 8;
for (int i = 0; i < QP.GetLength(0); i++)
{
for (int j = 0; j < QP.GetLength(1); j++) //为数组QP每个元素赋初值 用“□”代替棋盘网格
{
QP[i, j] = "□";
}
}
for (int i = 0; i < QP.GetLength(0); i++)
{
for (int j = 0; j < QP.GetLength(1); j++)
{
Console.Write(QP[i, j]); //遍历数组QP 并将其打印在窗体 使界面看起来像是网格状
}
Console.WriteLine();
}
int left = 16;
int top = 8;
for (int i = 0; i < QP.GetLength(0); i++)
{
for (int j = 0; j < QP.GetLength(1); j++) //为数组QP每个元素赋初值 用“□”代替棋盘网格
{
QP[i, j] = "□";
}
}
for (int i = 0; i < QP.GetLength(0); i++)
{
for (int j = 0; j < QP.GetLength(1); j++)
{
Console.Write(QP[i, j]); //遍历数组QP 并将其打印在窗体 使界面看起来像是网格状
}
Console.WriteLine();
}
bool mark = true;
bool Jmark = true;
while (Jmark)
{
Console.SetCursorPosition(left, top); //设置光标初始位置
ConsoleKey key = Console.ReadKey(true).Key; //读取输入值
bool Jmark = true;
while (Jmark)
{
Console.SetCursorPosition(left, top); //设置光标初始位置
ConsoleKey key = Console.ReadKey(true).Key; //读取输入值
switch (key) //switch语句控制光标的移动,上下左右共4个方向 可同时由方向键和“W”“S”“A”“D”2组组合键控制
{
case ConsoleKey.UpArrow:
case ConsoleKey.W:
if (top > 0)
{
top--;
}
break;
case ConsoleKey.DownArrow:
case ConsoleKey.S:
if (top < 16)
{
top++;
}
{
case ConsoleKey.UpArrow:
case ConsoleKey.W:
if (top > 0)
{
top--;
}
break;
case ConsoleKey.DownArrow:
case ConsoleKey.S:
if (top < 16)
{
top++;
}