如何在15益智控制台应用程序中检查用户输入的数字是否有效



我试图在控制台中用C#制作一个15个谜题的解决方案,我几乎完成了所有任务除了检查数组中的数字是否在该数组中为0的空位的上方、下方或旁边,请注意,我是初学者,所以请放心类播放器只接受用户的输入,但只接受数组中的有效输入index类返回空点为"0"的索引,并返回用户输入的索引板类使用用户输入交换空位

这是代码

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;
}

相关内容

最新更新