【发布时间】:2018-10-25 22:34:19
【问题描述】:
我一直在摆弄 C#,我的任务是创建一个谜题,目的是通过用计算机键(R、L 等)随机播放幻灯片来排列数字。
我无法弄清楚如何在 switch 语句中实现这一点,例如,当移动例如“R”时。这将移动间隙左侧的任何瓷砖将向右移动到间隙中。
但我不确定如何执行此操作。我知道我应该在 switch 语句中执行此操作,并且我已经为空格声明了变量“gaprow”和“gapcol”,但无法将它们链接到数组以在 switch 语句中使用。
到目前为止,这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Slider
{
class Program
{
static string[,] thegrid = { { "1", "2", "3", "4" }, { "5", "6", "7", "8" }, { "9", "10", "11", "12" }, { "14", "15", "13", "" } }; // 2 dimensional array
static void Main(string[] args)
{
int gaprow = 4, gapcol = 4;
// defines location of the gap
string userinput;
bool completed = false;
do
{
draw(thegrid);
Console.Write("Your move : ");
userinput = Console.ReadLine().ToUpper();
switch (userinput)
{
case "U":
break;
case "D":
break;
case "R":
break;
case "L":
break;
default:
Console.WriteLine("Invalid Entry");
Console.ReadKey();
Environment.Exit(0);
break;
}
} while (!completed && userinput != "Q");
}
static void draw(string[,] grid)
{
Console.Clear();
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("S L I D E P U Z Z L E");
for (int i = 0; i < 4; i++)
{
Console.WriteLine("---------------------");
Console.WriteLine("| {0,2} | {1,2} | {2,2} | {3,2} |", grid[i, 0], grid[i, 1], grid[i, 2], grid[i, 3]);
}
Console.WriteLine("---------------------");
Console.WriteLine();
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(" U: to shift up");
Console.WriteLine(" D: to shift down");
Console.WriteLine(" R: to shift right");
Console.WriteLine(" L: to shift left");
Console.WriteLine(" Q: to quit");
}
}
}
【问题讨论】:
标签: c# arrays multidimensional-array