- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在制作宾果游戏,在宾果游戏中,卡片是 5x5 的数字网格(减去“自由”中心空间)。我正在寻找一种以强类型方式表示 5x5 网格的方法。它可能是一个 5x5 的整数、 bool 值、某些类等网格。我最初的想法是使用二维数组,但这有一个问题,我不允许指定大小,所以在传递对象时,有其他类无法知道它应该有 5 行和 5 列。
例如,我可能会创建界面:
public interface ICard
{
int[,] cells { get; }
}
但是这里没有明确说明整数数组有 5 行和 5 列。此外,为了定义要匹配的模式,我可能会使用 5x5 bool 值网格,所以我希望它看起来更像这样:
public interface ICard<T>
{
T[,] cells { get; }
}
那么我该如何更改它以返回一个强类型 Card 对象,该对象强制执行应该只有 5 行和 5 列的规则,并使其显而易见。我认为我的问题的答案是创建一个新的 Card 类,但我不确定如何以优雅的方式执行此操作,以强制执行并使其明显表明它是 5x5 网格。
任何想法表示赞赏。谢谢。
加油,感谢所有如此迅速提供答案的人。基于所有的答案,我想出了一些混合方法。这是我最终做的:
创建了一个新的通用 Matrix 接口(interface)和类:
public interface IMatrix<T>
{
int NumberOfColumns { get; }
int NumberOfRows { get; }
T GetCell(int column, int row);
void SetCell(int column, int row, T value);
}
public class Matrix<T> : IMatrix<T>
{
protected readonly T[,] Cells;
public int NumberOfColumns { get; }
public int NumberOfRows { get; }
public Matrix(int numberOfColumns, int numberOfRows)
{
NumberOfColumns = numberOfColumns;
NumberOfRows = numberOfRows;
Cells = new T[numberOfColumns, numberOfRows];
}
public T GetCell(int column, int row)
{
ThrowExceptionIfIndexesAreOutOfRange(column, row);
return Cells[column, row];
}
public void SetCell(int column, int row, T value)
{
ThrowExceptionIfIndexesAreOutOfRange(column, row);
Cells[column, row] = value;
}
private void ThrowExceptionIfIndexesAreOutOfRange(int column, int row)
{
if (column < 0 || column >= NumberOfColumns || row < 0 || row >= NumberOfRows)
{
throw new ArgumentException($"The given column index '{column}' or row index '{row}' is outside of the expected range. Max column range is '{NumberOfColumns}' and max row range is '{NumberOfRows}'.");
}
}
}
然后我的实际 Card 对象在构造函数中采用 IMatrix 并验证它是否具有预期的行数和列数:
public interface ICard
{
int NumberOfColumns { get; }
int NumberOfRows { get; }
ICell GetCellValue(int column, int row);
bool Mark(int number);
bool Unmark(int number);
}
public class Card : ICard
{
// A standard Bingo card has 5 columns and 5 rows.
private const int _numberOfColumns = 5;
private const int _numberOfRows = 5;
private IMatrix<ICell> Cells { get; } = new Matrix<ICell>(_numberOfColumns, _numberOfRows);
public Card(IMatrix<ICell> numbers)
{
if (numbers.NumberOfColumns != NumberOfColumns || numbers.NumberOfRows != NumberOfRows)
throw new ArgumentException($"A {numbers.NumberOfColumns}x{numbers.NumberOfRows} matrix of numbers was provided for the Card with ID '{id}' instead of the expected {NumberOfColumns}x{NumberOfRows} matrix of numbers.", nameof(provider));
for (int column = 0; column < NumberOfColumns; column++)
{
for (int row = 0; row < NumberOfRows; row++)
{
var number = numbers.GetCell(column, row);
var value = (column == 2 && row == 2) ? new Cell(-1, true) : new Cell(number);
Cells.SetCell(column, row, value);
}
}
}
public int NumberOfColumns => _numberOfColumns;
public int NumberOfRows => _numberOfRows;
public ICell GetCellValue(int column, int row) => Cells.GetCell(column, row).Clone();
public bool Mark(int number)
{
var cell = GetCell(number);
if (cell != null)
{
cell.Called = true;
}
return cell != null;
}
public bool Unmark(int number)
{
var cell = GetCell(number);
if (cell != null)
{
cell.Called = false;
}
return cell != null;
}
...
}
我喜欢这种方法,因为它通过 IMatrix 属性使行数和列数显而易见,并允许我在以后轻松添加另一个 LargeCard 类,它可以采用 10x10 矩阵或任何我需要的矩阵。由于它们都在使用接口(interface),这应该意味着需要最少的代码更改。此外,如果我决定在内部使用 List 而不是多维数组(可能出于性能原因),我需要做的就是更新 Matrix 类实现。
最佳答案
如果你需要像cell[row, column]
这样的东西,这可能是一个建议:
static void Main()
{
var card = new Card();
card.cells[3, 2] = true;
Console.WriteLine(card.cells[2, 4]); // False
Console.WriteLine(card.cells[3, 2]); // True
Console.WriteLine(card.cells[8, 9]); // Exception
}
public interface ICard
{
Cells cells { get; set; }
}
public class Card : ICard
{
Cells _cells = new Cells();
public Cells cells { get { return _cells; } set { _cells = value; } }
}
public class Cells : List<bool>
{
public Cells()
{
for (int i = 0; i < 25; i++)
{
this.Add(false);
}
}
public virtual bool this[int row, int col]
{
get
{
if (row < 0 || row >= 5 || col < 0 || col >= 5) throw new IndexOutOfRangeException("Something");
return this[row * 5 + col];
}
set
{
if (row < 0 || row >= 5 || col < 0 || col >= 5) throw new IndexOutOfRangeException("Something");
this[row * 5 + col] = value;
}
}
}
关于c# - 如何在接口(interface)或基类中定义一个固定大小的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35667424/
经过几个小时的(重新)搜索,我无法想出普通抽象类和使用模板模式之间的可解释区别。 我唯一看到的是: 使用抽象类时,您需要实现所有方法。但是在使用模板方法时,您只需要实现这两个抽象方法。 有人可以向我解
我正在尝试实现一种算法,该算法可找到以下形状给出的外多边形的每个单独边的对应区域。也就是说,1,2 边的相应区域是 [1,6,7,8,2],2,3 边的区域是 [2,8,3] 等等,CCW 或 CW
我正在尝试在派生 self 的 BaseController 类的任何 Controller 上自动设置一个属性。这是我的 Application_Start 方法中的代码。 UnitOfWork 属
我正在使用 mgcv 包通过以下方式将一些多项式样条拟合到一些数据: x.gam smooth$knots [1] -0.081161 -0.054107 -0.027053 0.000001
考虑以下代码: void foo(){ ..... } int main() { int arr[3][3] ; char string[10]; foo();
本书The c++ programming language有这个代码: class BB_ival_slider : public Ival_slider, protected BBslider {
是否有一个 package.json 属性可用于指定模块解析应启动的根文件夹? 例如,假设我们在 node_modules/mypackage/src/file1 中有一个安装。我们要导入的所有文件都
我正在尝试使用聚合函数来实现与 SQL 查询相同的结果: 查询语句: sqldf(" SELECT PhotoID, UserID,
我正在比较使用 LOESS 回归的两条线。我想清楚地显示两条线的置信区间,我遇到了一些困难。 我尝试过使用各种线型和颜色,但在我看来,结果仍然是忙碌和凌乱。我认为置信区间之间的阴影可能会使事情变得更清
给定这段代码 public override void Serialize(BaseContentObject obj) { string file = ObjectDataStoreFold
我正在构建某种工厂方法,它按以下方式将 DerivedClass 作为 BaseClass 返回: BaseClass Factory() { return DerivedClass(); }
当重写 class delegation 实现的接口(interface)方法时,是否可以调用通常从重写函数中委托(delegate)给的类?类似于使用继承时调用 super 的方式。 来自docum
我有一个基类 fragment (如下所示)。我在其他 3 个 fragment 类中扩展了此类,每个类都共享需要在这 3 个 fragment 中访问的相同 EditText。因此,我在基类中设置了
如何在不加载额外库的情况下在 R 中计算两个排列之间的 Kendall tau 距离(又名冒泡排序距离)? 最佳答案 这是一个 O(n.log(n)) 的实现,在阅读后拼凑而成,但我怀疑可能有更好的
情况 我创建了一个具有国际化 (i18n) 的 Angular 应用程序。我想在子域中托管不同的版本,例如: zh.myexample.com es.myexample.com 问题 当我使用命令 n
std::is_base_of 之间的唯一区别和 std::is_convertible是前者在 Base 时也成立是 私有(private)或 protected Derived 的基类.但是,您何
我创建了一个名为 baseviewcontroller 的父类(super class) uiviewcontroller 类,用于包含大多数应用屏幕所需的基本 UI。它包括一个自定义导航栏和一个“自
我是一名优秀的程序员,十分优秀!