C#拼图查询

时间:2018-10-25 22:34:19

标签: c# arrays multidimensional-array

我一直在摆弄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");
    }
}
}

2 个答案:

答案 0 :(得分:0)

一种实现它的方法是,思考一下向右移动图块时实际发生的情况……您正在用一个图块交换间隙/空白空间的位置。

所以它就像:

case "R":
    if (gapcol > 0)//if the gap is not at the end...
    {//then swap empty space with array 
        string buff = thegrid[gaprow, gapcol - 1];
        thegrid[gaprow, gapcol - 1] = "";
        thegrid[gaprow, gapcol] = buff;
        //update gap position
        gapcol--;
    }
    break;

请记住,数组索引从零开始,因此间隙的初始位置应为:

int gaprow = 3, gapcol = 3;

在前面的示例中,您应该能够完成其他情况。

答案 1 :(得分:0)

我喜欢你所做的。这是解决问题的诀窍和技巧。

首先,网格为4x4,因此从零开始的数组索引从0到3,而不是从1到4。

int gaprow = 3, gapcol = 3;

因此,我将为您提供 R Right命令的基本代码:-

case "R":
    if (gapcol > 0)
    {
        thegrid[gaprow, gapcol] = thegrid[gaprow, --gapcol];
        thegrid[gaprow, gapcol] = String.Empty;
    }

该代码执行以下操作。假设我们不在网格的左边缘:-

设置当前间隙单元格的栅格值等于左侧的单元格。 为此,我使用-将gapcol的值在评估之前减少1

重新使用减少的gapcol值,将该位置的单元格设置为空字符串