C# 数独板生成器
首先,是的,我已经搜索过,但没有找到与我的问题相关的答案.我正在尝试在 C# 中创建一个数独板.我相信我的单元格是正确的,我的游戏板"类是正确的,但是我无法生成实际的板.我确信我遗漏了一些明显的东西,但我想要做的是让我的代码输出一个实际的电路板.没有这部分,我无法测试我的细胞课程以确保我真的可以玩.
First off, yes I have searched and no I did not find a relevant answer to my question. I am trying to create a Sudoku Board in C#. I believe my cells are correct and my "Game board" class is correct, however I cannot get an actual board generated. I am sure I am missing something obvious, but what I am looking to do is to have my code output an actual board. Without this part, I cannot test my cells class to make sure I can actually play.
特别是我试图获取我的代码并生成正在编码的内容的显示,以便我能够正确地对我的代码的其余部分进行故障排除.澄清一下,我所记录的是数据,但没有要显示的输出.在通过尝试创建网格、使用 Console.WriteLine() 并尝试使用辅助列表显示来推动显示后,我完成了几个教程.显示问题已由John回答,他帮我在评论字符串中指定了问题.我希望这个在搜索中保持可用,因为我认为很多人都遇到过这个问题,但我认为搜索结果很少
我的主程序有一个要填写的单元格列表(我正在通过手动创建板进行测试).源码如下:
What I have for my main program is a list of cells that are to be filled in (I am testing by creating the board manually). The source code is as follows:
namespace Sudoku
{
static class Program
{
static void Main(string[] args)
{
{
Board board = new Board();
board.SetCellValue(1, 3, 4);
board.SetCellValue(1, 5, 5);
board.SetCellValue(1, 9, 2);
board.SetCellValue(2, 1, 1);
board.SetCellValue(2, 4, 2);
board.SetCellValue(3, 1, 7);
board.SetCellValue(3, 3, 5);
board.SetCellValue(3, 4, 1);
board.SetCellValue(3, 6, 8);
board.SetCellValue(3, 7, 9);
board.SetCellValue(4, 1, 3);
board.SetCellValue(4, 2, 5);
board.SetCellValue(4, 3, 2);
board.SetCellValue(4, 6, 1);
board.SetCellValue(4, 7, 7);
board.SetCellValue(4, 8, 8);
board.SetCellValue(5, 2, 6);
board.SetCellValue(5, 5, 7);
board.SetCellValue(5, 8, 5);
board.SetCellValue(6, 2, 8);
board.SetCellValue(6, 3, 7);
board.SetCellValue(6, 4, 6);
board.SetCellValue(6, 7, 4);
board.SetCellValue(6, 8, 3);
board.SetCellValue(6, 9, 1);
board.SetCellValue(7, 3, 6);
board.SetCellValue(7, 4, 3);
board.SetCellValue(7, 6, 7);
board.SetCellValue(7, 7, 5);
board.SetCellValue(7, 9, 8);
board.SetCellValue(8, 6, 2);
board.SetCellValue(8, 9, 4);
board.SetCellValue(9, 1, 8);
board.SetCellValue(9, 5, 1);
board.SetCellValue(9, 7, 3);
}
}
}
}
由此,我需要添加什么才能使代码生成实际的电路板?我认为问题是生成了数据,但是没有特定的输出可以推送任何内容以将代码显示为板.
From this, what do I need to add to make the code generate an actual board? I believe the problem is that the data is generated, but there is no specific output to push anything to display the code as a board.
我有一个 Board
类和一个 Cell
类,如果有帮助,我也可以提供.我联系这里是因为我已经浏览了教程、其他网站和朋友,但我们似乎无法弄清楚这一点.
I have a Board
class and a Cell
class that I can provide as well if that helps. I'm reaching out on here because I have gone back through tutorials, other websites, and a friend but we can't seem to figure this out.
这是我的 Board 类,目的是处理电路板的基本逻辑,将电路板留空:
Here is my Board class, which the intention is to handle the basic logic of the board, leaving the board blank:
namespace Sudoku
{
public class Board
{
public List<Cell> Cells { get; set; }
public Board()
{
Cells = new List<Cell>();
for (int row = 1; row < 10; row++)
{
for (int column = 1; column < 10; column++)
{
Cells.Add(new Cell(row, column));
}
}
}
public void SetCellValue(int row, int column, int value)
{
Cell activeCell = Cells.Single(x => (x.Row == row) && (x.Column == column));
activeCell.Value = value;
foreach (Cell cell in Cells.Where(s => !s.IsSolved && (s.Row == row)))
{
cell.PotentialValues.Remove(value);
}
foreach (Cell square in Cells.Where(s => !s.IsSolved && (s.Column == column)))
{
square.PotentialValues.Remove(value);
}
foreach (Cell cell in Cells.Where(s => !s.IsSolved && (s.Block == activeCell.Block)))
{
cell.PotentialValues.Remove(value);
}
foreach (Cell cell in Cells.Where(s => !s.IsSolved && (s.PotentialValues.Count == 1)))
{
SetCellValue(cell.Row, cell.Column, cell.PotentialValues[0]);
}
}
}
}
这里是我的 Cell 类,我的 Cell 类的意图是管理每个单元的 IO:
and here is my Cell class, which my intention of the cell class is to manage the IO of each cell:
namespace Sudoku
{
public class Cell
{
private readonly List<int> values = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
internal enum Blocks
{
UpperLeft,
UpperMiddle,
UpperRight,
MiddleLeft,
Middle,
MiddleRight,
LowerLeft,
LowerMiddle,
LowerRight
}
public int Row { get; private set; }
public int Column { get; private set; }
internal Blocks Block
{
get
{
if (Row < 4)
{
if (Column < 4)
{
return Blocks.UpperLeft;
}
return Column < 7 ? Blocks.UpperMiddle : Blocks.UpperRight;
}
if (Row < 7)
{
if (Column < 4)
{
return Blocks.MiddleLeft;
}
return Column < 7 ? Blocks.Middle : Blocks.MiddleRight;
}
if (Column < 4)
{
return Blocks.LowerLeft;
}
return Column < 7 ? Blocks.LowerMiddle : Blocks.LowerRight;
}
}
public bool IsSolved { get { return Value != null; } }
public int? Value { get; set; }
internal List<int> PotentialValues { get; private set; }
internal Cell(int row, int column)
{
Row = row;
Column = column;
PotentialValues = values;
}
}
}
这样的事情将提供您的电路板的基本控制台输出.
Something like this will provide a basic console output of your board.
foreach (var cell in board.Cells)
{
int x = cell.Column;
int y = cell.Row;
Console.SetCursorPosition(x * 2, y * 2);
Console.Write(cell.Value.HasValue ? cell.Value.Value.ToString() : " ");
}
Console.ReadKey();
为了分隔数字,我将 X 和 Y 乘以 2,但这不是必需的.
I'm multiplying the X and Y by 2 for the purpose of separating the numbers, but it isn't necessary.