15拼图控制台应用中如何检查用户输入的数字是否有效

How to check if the number entered by the user is valid in 15 puzzle console application

我试图在控制台中用 C# 解决 15 个难题,我几乎完成了所有事情 除了检查数组中的数字是否在该数组中为 0 的空点之上、之下或旁边,请注意我是初学者所以对我放轻松 class 播放器只是从用户那里获取输入,但仅在数组中有效输入 索引 class return 是空位为 '0' 的索引, return 是用户输入的索引 棋盘 class 将空白点与用户输入

交换

这是代码

class Player
{
    public int playerInput;

    public int GetPlayer(Board board,Player player)
    {
        while (true)
        {
            Console.Write("Enter a number from the board to switch with the empty spot:  ");
            playerInput = int.Parse(Console.ReadLine());
            if (playerInput <= 0 || playerInput > 15)
            {
                Console.WriteLine("Invalid input");
            }
            if(Board.IsLocation(board,player)==false)
            {
                Console.WriteLine("Illegal move try again");
            }
            else
            {
                break;
            }
        }
        return playerInput;

    }

class Board
{
    public int[,] puzzle;
    public Board(int[,] Puzzle)
    {
        puzzle = Puzzle;
    }
    public void Swap(Player player, Board board)
    {
        Index index = new Index(0, 0);
        player.GetPlayer(board,player);
        index = index.GetIndex(board, player);
        for (var x = 0; x < 4; x++)
        {
            for (var y = 0; y < 4; y++)
            {
                if (board.puzzle[x, y] == 0)
                {
                    int temp = board.puzzle[x, y];
                    board.puzzle[x, y] = board.puzzle[index.x, index.y];
                    board.puzzle[index.x, index.y] = temp;
                }
            }
        }


    }

    static public bool IsLocation(Board board,Player player)
    {
        Index index = new(0, 0);
        Index embtyLocation = new(0, 0);
        embtyLocation = embtyLocation.GetEmptyLocation(board);
        index = index.GetIndex(board, player);

        if (board.puzzle[embtyLocation.x, embtyLocation.y] == 0)
        {
            if (board.puzzle[index.x, index.y] == board.puzzle[embtyLocation.x, embtyLocation.y + 1]) return true;
            if (board.puzzle[index.x, index.y] == board.puzzle[embtyLocation.x, embtyLocation.y - 1]) return true;
            if (board.puzzle[index.x, index.y] == board.puzzle[embtyLocation.x + 1, embtyLocation.y]) return true;
            if (board.puzzle[index.x, index.y] == board.puzzle[embtyLocation.x-1, embtyLocation.y]) return true;


        }
            return false;
    }

class Index
{
    public int x;
    public int y;
    public Index(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    public Index GetIndex(Board board, Player player)
    {
        for (x = 0; x < 4; x++)
        {
            for (y = 0; y < 4; y++)
            {
                if (board.puzzle[x, y] == player.playerInput) return new Index(x, y);
            }
        }
        return null;
    }
    public Index GetEmptyLocation(Board board)
    {
        for (x = 0; x < 4; x++)
        {
            for (y = 0; y < 4; y++)
            {
                if (board.puzzle[x, y] == 0) return new Index(x, y);
            }
        }
        return null;
    }

你应该试试这个

if (y > 0 && board.puzzle[index.x, index.y - 1] == 0) return true;
if (y < 4 && board.puzzle[index.x, index.y-1] == 0) return true;
if (x > 0 && board.puzzle[index.x-1, index.y] == 0) return true;
if (x < 4 && board.puzzle[index.x+1, index.y] == 0) return true;

多亏了你,我做到了, 这是代码

static public bool IsLocation(Board board,Player player)
    {
        Index index = new(0, 0);
        Index embtyLocation = new(0, 0);
        embtyLocation = embtyLocation.GetEmptyLocation(board);
        index = index.GetIndex(board, player);

        if (embtyLocation.y >= 0 && embtyLocation.y!=3)
        {
            if (board.puzzle[index.x, index.y] == board.puzzle[embtyLocation.x, embtyLocation.y + 1]) return true;
        }
        if (embtyLocation.y < 4 && embtyLocation.y!=0)
        {
            if (board.puzzle[index.x, index.y] == board.puzzle[embtyLocation.x, embtyLocation.y - 1]) return true;
        }
        if (embtyLocation.x < 4 && embtyLocation.x!=0)
        {
            if (board.puzzle[index.x, index.y] == board.puzzle[embtyLocation.x - 1, embtyLocation.y]) return true;
        }
        if (embtyLocation.x >= 0 && embtyLocation.x!=3)
        {
            if (board.puzzle[index.x, index.y] == board.puzzle[embtyLocation.x + 1, embtyLocation.y]) return true;
        }


        return false;
    }