【发布时间】:2022-01-23 03:07:30
【问题描述】:
我这样做是为了知道有多少个星号出现在相邻的方格中。
private int CheckAdjacents(Coordinate cord)
{
List<Coordinate> coordinates = new List<Coordinate>()
{
new Coordinate(cord.X - 1, cord.Y - 1),
new Coordinate(cord.X, cord.Y-1),
new Coordinate(cord.X + 1, cord.Y -1),
new Coordinate(cord.X + 1, cord.Y),
new Coordinate(cord.X + 1, cord.Y + 1),
new Coordinate(cord.X, cord.Y + 1),
new Coordinate(cord.X - 1, cord.Y + 1),
new Coordinate(cord.X - 1, cord.Y)
};
return coordinates.Count(x => _matrix.At(x).Value == '*');
}
这里的事情显然是它返回了一个异常,因为它正在检查不会被检查的索引。跳过这些索引的最佳方法是什么?使用 try/catch 可能有点棘手?谢谢!
编辑:
矩阵类
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace MineSweeper
{
public record Coordinate (int X, int Y);
public record Size(int M, int N);
public class Matrix
{
private readonly Size _size;
private readonly Cell[,] _matrix;
private const char InitValue = '.';
public Matrix(Size size)
{
_size = size;
_matrix = new Cell[size.M, size.N];
Initialize();
}
private void Initialize()
{
for (int m = 0; m < _size.M; m++)
for (int n = 0; n < _size.N; n++)
_matrix[m, n] = new Cell(InitValue);
}
public Size GetSize()
=> _size;
public Cell At(Coordinate coordinate)
=> _matrix[coordinate.X, coordinate.Y];
public void SetMine(Coordinate coordinate)
=> _matrix[coordinate.X, coordinate.Y] = new Cell('*');
public void ChangeValue(Coordinate coordinate, char value)
=> _matrix[coordinate.X, coordinate.Y] = new Cell(value);
public Cell Open(Coordinate coordinate)
=> _matrix[coordinate.X, coordinate.Y];
public IEnumerable ToList()
=> _matrix.Cast<Cell>().ToList();
private string CellsAsString()
=> string.Concat(_matrix.OfType<Cell>().Select(c => c.Value));
public override bool Equals(object other)
=> this.CellsAsString().Equals((other as Matrix)?.CellsAsString());
public override int GetHashCode()
=> this.CellsAsString().GetHashCode();
}
}
编辑(2):
主类中的 PrintMatrix 和 Open 方法。
public void Open(Coordinate coordinate)
{
if (_matrix.At(coordinate).Value == '*')
HasLose = true;
int numOfMines = _matrix.NeighborsOf(coordinate).Count(cell => cell.Value == '*');
_showedMatrix.ChangeValue(coordinate, char.Parse(numOfMines.ToString()));
HasWin = PlayerHasWin();
}
public String PrintMatrix()
{
string temp = "";
for (int x = 0; x < _size.M; x++)
{
for (int y = 0; y < _size.N; y++)
{
temp += _showedMatrix.At(new Coordinate(x, y)).Value;
}
temp += '\n';
}
return temp;
}
请注意,我使用的是showedMatrix,它是另一个包含单元格的矩阵,每个单元格的值都是一个简单的.。我正在使用这个新矩阵,所以我可以更改它的值并打印它。
这是失败的两个测试。
[Fact]
public void CellIsOpenWithoutAMineButWithOneMineAdjacent()
{
string printExpected = "1...\n....\n....\n....\n";
Matrix matrix = new Matrix(new(4, 4));
matrix.SetMine(new(0,1));
MineSweeper mineSweeper = new(matrix, 2);
mineSweeper.Open(new(0,0));
mineSweeper.PrintMatrix().Should().Be(printExpected);
}
[Fact]
public void CellIsOpenWithoutAMineButWithTwoMineAdjacent()
{
string printExpected = "2...\n....\n....\n....\n";
Matrix matrix = new Matrix(new(4, 4));
matrix.SetMine(new(0,1));
matrix.SetMine(new(1,0));
MineSweeper mineSweeper = new(matrix, 2);
mineSweeper.Open(new(0,0));
mineSweeper.PrintMatrix().Should().Be(printExpected);
}
由于我知道这些测试的主要课程是放置 2 个随机地雷以及我自己使用 SetMine() 方法放置的地雷,因此我多次执行这些测试以确保它失败了。结论是 "2...\n....\n....\n....\n"; 出于某种原因始终是 0 而不是 2 或 1。
【问题讨论】:
-
“检查”是一个非常笼统的术语。您到底想对相邻元素做什么?另外,
_matrix是什么?它来自哪里? -
matrix 是一个具有多维单元格数组的类,基本上使用方法 At 我可以在某个坐标及其属性值处访问单元格,我需要知道相邻元素的值所以我可以计算其中有多少有星号作为值,无论如何我都会用 Matrix 类编辑问题