【问题标题】:C# puzzle queryC# 谜题查询
【发布时间】: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


    【解决方案1】:

    我喜欢你所做的。这是解决方法和完成它的技巧。

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

    int gaprow = 3, gapcol = 3;
    

    那么,我给你R右命令的基本代码:-

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

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

    设置当前间隙单元格的网格值等于左边的单元格。 为此,我使用 -- 将 gapcol 的值在计算之前减少 1

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

    【讨论】:

    • 请不要编写这样的代码,它有多个副作用,必须以正确的顺序运行。这是不必要的混乱。这段代码写成tg[r, c] = tg[r, c-1]; tg[r, c-1] = s.e; c = c - 1的形式会好很多。这段代码的重点是改变三个变量,所以应该有三个语句来改变变量,而不是两个.
    • @EricLippert 你是这个意思吗? case "U": if (gaprow &lt; 3) { thegrid[gaprow, gapcol] = thegrid[gaprow+1, gapcol]; thegrid[gaprow+1, gapcol] = String.Empty; gaprow = gaprow + 1; }
    • 就是这样,@user10480265。我同意 --gapcol 是个坏主意。
    【解决方案2】:

    实现它的一种方法是,想想当你向右移动一个图块时实际发生的事情......你正在用你的一个图块交换间隙/空白空间的位置。

    所以应该是这样的:

    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;
    

    通过前面的例子,你应该可以完成其他案例。

    【讨论】:

      猜你喜欢
      • 2012-08-31
      • 1970-01-01
      • 2011-10-11
      • 2011-02-19
      • 1970-01-01
      • 2021-11-27
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多