@chengzhao-dev/dsh-cpp-workflow 0.7.1 → 0.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +16 -10
- package/README.md +16 -10
- package/cordis.patch.yml +6 -6
- package/lib/client.js +2 -2
- package/lib/index.js +45 -37
- package/lib/workspace.js +10 -17
- package/package.json +1 -1
- package/skills/building-cpp/SKILL.md +3 -3
- package/skills/building-cpp/assets/templates/projects/common/running-build.sh +0 -0
- package/skills/building-cpp/references/environment.md +1 -1
- package/skills/building-cpp/scripts/checking-environment.mjs +11 -13
- package/skills/building-cpp/steps/01-build.md +3 -2
- package/skills/building-cpp/steps/02-tooling.md +2 -2
- package/skills/developing-cpp/SKILL.md +10 -7
- package/skills/developing-cpp/references/comments/comment-rules.md +56 -42
- package/skills/developing-cpp/references/comments/comment-style.md +8 -12
- package/skills/developing-cpp/references/comments/examples/declaration-class.md +15 -16
- package/skills/developing-cpp/references/comments/examples/declaration-constructor.md +1 -1
- package/skills/developing-cpp/references/comments/examples/declaration-enum.md +17 -17
- package/skills/developing-cpp/references/comments/examples/declaration-file.md +3 -3
- package/skills/developing-cpp/references/comments/examples/declaration-function.md +30 -22
- package/skills/developing-cpp/references/comments/examples/declaration-member.md +3 -3
- package/skills/developing-cpp/references/comments/examples/declaration-predicate.md +1 -1
- package/skills/developing-cpp/references/comments/examples/declaration-struct.md +3 -3
- package/skills/developing-cpp/references/comments/examples/implementation-body.md +34 -17
- package/skills/developing-cpp/references/comments/glossary/comment-terms.md +30 -31
- package/skills/developing-cpp/references/design/examples/coordinate-table.md +3 -3
- package/skills/developing-cpp/references/design/examples/header-place.md +1 -1
- package/skills/developing-cpp/references/design/examples/move-result.md +31 -22
- package/skills/developing-cpp/references/design/examples/preview-read.md +6 -10
- package/skills/developing-cpp/references/design/project-design.md +17 -14
- package/skills/developing-cpp/references/editing/01-edit-operations.md +1 -1
- package/skills/developing-cpp/references/exceptions/examples/throw-build-mode.md +4 -4
- package/skills/developing-cpp/references/exceptions/examples/throw-message.md +8 -0
- package/skills/developing-cpp/references/exceptions/throw-rules.md +62 -59
- package/skills/developing-cpp/references/language/language-basics/01-objects.md +1 -1
- package/skills/developing-cpp/references/language/language-basics/02-initialization.md +1 -1
- package/skills/developing-cpp/references/language/language-basics/04-const.md +4 -4
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +8 -3
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +2 -0
- package/skills/developing-cpp/references/language/standard-library/02-access.md +1 -1
- package/skills/developing-cpp/references/language/standard-library/06-format.md +13 -5
- package/skills/developing-cpp/references/language/value-types/04-enum.md +1 -1
- package/skills/developing-cpp/references/language/value-types/07-nodiscard.md +1 -1
- package/skills/developing-cpp/references/language/value-types/08-comparison.md +1 -1
- package/skills/developing-cpp/references/layout/01-source-layout.md +2 -3
- package/skills/developing-cpp/references/layout/02-file-template.md +7 -7
- package/skills/developing-cpp/references/layout/03-declaration-order.md +12 -9
- package/skills/developing-cpp/references/naming/examples/naming-accessor.md +11 -0
- package/skills/developing-cpp/references/naming/examples/naming-local.md +45 -0
- package/skills/developing-cpp/references/naming/examples/naming-lookup.md +1 -1
- package/skills/developing-cpp/references/naming/examples/naming-lower-layer.md +20 -13
- package/skills/developing-cpp/references/naming/examples/naming-player-value.md +4 -5
- package/skills/developing-cpp/references/naming/examples/naming-predicate.md +1 -1
- package/skills/developing-cpp/references/naming/examples/naming-role.md +1 -1
- package/skills/developing-cpp/references/naming/examples/naming-state-change.md +38 -25
- package/skills/developing-cpp/references/naming/glossary/naming-terms.md +23 -0
- package/skills/developing-cpp/references/naming/glossary/position.md +23 -0
- package/skills/developing-cpp/references/naming/glossary/square.md +47 -0
- package/skills/developing-cpp/references/naming/identifier-names.md +24 -19
- package/skills/developing-cpp/references/style/google-cpp-style.md +4 -4
- package/skills/developing-cpp/scripts/checking-comments.mjs +9 -1
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +27 -1
- package/skills/developing-cpp/steps/03-declaration.md +4 -4
- package/skills/developing-cpp/steps/04-implementation.md +8 -8
- package/skills/engineering/SKILL.md +7 -7
- package/skills/engineering/references/cmake/linking/03-undefined-reference.md +1 -1
- package/skills/engineering/references/cmake/modules/functions.md +1 -1
- package/skills/engineering/references/cmake/{standard/standards → standards}/README.md +10 -10
- package/skills/engineering/references/cmake/{standard/standards → standards}/c++23/README.md +3 -3
- package/skills/engineering/references/cmake/{standard → standards}/cxx-standard.md +3 -3
- package/skills/engineering/references/cmake/targets/examples/target.md +1 -1
- package/skills/engineering/references/cmake/targets/rules.md +1 -1
- package/skills/engineering/references/cmake/testing/ctest.md +1 -1
- package/skills/engineering/references/formatting/02-clang-format.md +1 -1
- package/skills/engineering/references/formatting/scripts/checking-encoding.mjs +6 -7
- package/skills/engineering/steps/01-targets.md +7 -6
- package/skills/engineering/steps/03-check.md +3 -2
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++11/README.md +0 -0
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++11/cmake-snippet.cmake +0 -0
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++14/README.md +0 -0
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++14/cmake-snippet.cmake +0 -0
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++17/README.md +0 -0
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++17/cmake-snippet.cmake +0 -0
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++20/README.md +0 -0
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++20/cmake-snippet.cmake +0 -0
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++23/cmake-snippet.cmake +0 -0
- /package/skills/engineering/references/cmake/{standard → standards}/examples/standard.md +0 -0
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
```cpp
|
|
8
8
|
/// 检查坐标。
|
|
9
|
-
///
|
|
9
|
+
/// 返回方格;越界会抛异常。
|
|
10
10
|
Square GetSquare(Position pos) const;
|
|
11
11
|
```
|
|
12
12
|
|
|
@@ -35,7 +35,9 @@ friend bool operator==(const Position&, const Position&) = default;
|
|
|
35
35
|
**好**:
|
|
36
36
|
|
|
37
37
|
```cpp
|
|
38
|
-
///
|
|
38
|
+
/// 相等运算符。
|
|
39
|
+
///
|
|
40
|
+
/// 当两个坐标的行与列都相同时返回 true。
|
|
39
41
|
friend bool operator==(const Position&, const Position&) = default;
|
|
40
42
|
```
|
|
41
43
|
|
|
@@ -50,24 +52,25 @@ friend bool operator==(const Position&, const Position&) = default;
|
|
|
50
52
|
///
|
|
51
53
|
/// @param move 落子。
|
|
52
54
|
///
|
|
53
|
-
/// @returns 落子执行成功返回
|
|
54
|
-
[[nodiscard]]
|
|
55
|
+
/// @returns 落子执行成功返回 kPlaced,失败返回 kOccupiedSquare 或 kEmptySquare。
|
|
56
|
+
[[nodiscard]] PlaceResult TryPlace(Move move);
|
|
55
57
|
```
|
|
56
58
|
|
|
57
|
-
`@param move 落子`
|
|
59
|
+
`@param move 落子` 是把参数名翻译成中文,没有信息;`@returns` 与 `@return` 同义,本契约统一写 `@return`;没有说被拒绝的条件,调用方只能读实现。
|
|
58
60
|
|
|
59
61
|
**好**:
|
|
60
62
|
|
|
61
63
|
```cpp
|
|
62
64
|
/// 执行一次落子。
|
|
63
65
|
///
|
|
64
|
-
///
|
|
66
|
+
/// 依次检查玩家、对局状态、要写入的值与目标方格;全部通过才写入棋盘、记录历史并轮换玩家。
|
|
67
|
+
///
|
|
68
|
+
/// @param move 要执行的落子。
|
|
65
69
|
///
|
|
66
|
-
/// @
|
|
70
|
+
/// @return 落子成功返回 `kPlaced`;其余值表示没有写入,原因见 `PlaceResult`。
|
|
67
71
|
///
|
|
68
|
-
/// @
|
|
69
|
-
|
|
70
|
-
[[nodiscard]] Game::PlaceResult Place(const Move& move);
|
|
72
|
+
/// @throws std::out_of_range 对局进行中且行或列超出棋盘时抛出。
|
|
73
|
+
[[nodiscard]] PlaceResult Place(Move move);
|
|
71
74
|
```
|
|
72
75
|
|
|
73
76
|
## 从函数体注释到函数头注释
|
|
@@ -78,7 +81,7 @@ friend bool operator==(const Position&, const Position&) = default;
|
|
|
78
81
|
// 检查传入的值是否为空。
|
|
79
82
|
```
|
|
80
83
|
|
|
81
|
-
|
|
84
|
+
这一句作函数头的简要描述;后置条件(对局不变)由分支里的 `return` 表达,需要写进函数头时用 `@return` 补上返回值:
|
|
82
85
|
|
|
83
86
|
```cpp
|
|
84
87
|
/// 检查传入的值是否为空。
|
|
@@ -92,21 +95,26 @@ friend bool operator==(const Position&, const Position&) = default;
|
|
|
92
95
|
返回值不止一个时,不要只写一个 `@return`;按 `@retval` 每值一行,成功在前:
|
|
93
96
|
|
|
94
97
|
```cpp
|
|
95
|
-
///
|
|
98
|
+
/// 执行一次落子。
|
|
99
|
+
///
|
|
100
|
+
/// 依次检查玩家、对局状态、要写入的值与目标方格;全部通过才写入棋盘、
|
|
101
|
+
/// 记录历史并轮换玩家。任一检查不通过时对局不变。
|
|
96
102
|
///
|
|
97
|
-
///
|
|
103
|
+
/// @param move 要执行的落子。
|
|
98
104
|
///
|
|
99
|
-
/// @
|
|
105
|
+
/// @retval kPlaced 成功:落子玩家是当前玩家,值已写入方格
|
|
106
|
+
/// @retval kInvalidPlayer 失败:落子玩家不是当前玩家,没有写入
|
|
107
|
+
/// @retval kFinishedGame 失败:对局已经结束,没有写入
|
|
108
|
+
/// @retval kEmptySquare 失败:要写入的值是 `kEmpty`,没有写入
|
|
109
|
+
/// @retval kOccupiedSquare 失败:目标方格已有值,没有写入
|
|
100
110
|
///
|
|
101
|
-
/// @
|
|
102
|
-
|
|
103
|
-
/// @retval kOccupied 传入的是 kX 或 kO,格子已有值,没有写入
|
|
104
|
-
[[nodiscard]] Square::PlaceResult Place(SquareValue value);
|
|
111
|
+
/// @throws std::out_of_range 对局进行中且行或列超出棋盘时抛出。
|
|
112
|
+
[[nodiscard]] PlaceResult Place(Move move);
|
|
105
113
|
```
|
|
106
114
|
|
|
107
|
-
第一句必须说清这个函数做什么;函数体第一句只是其中一次检查时,补上动作再作简要描述。`@return`
|
|
115
|
+
第一句必须说清这个函数做什么;函数体第一句只是其中一次检查时,补上动作再作简要描述。`@return` 写返回值,不写「对局不变」这种后置条件本身;多种结果时按 `@retval` 写清每种结果何时返回,说法与枚举项的行尾注释一致即可。布尔查询把条件映射成 true/false;失败结果写清没有写入与原因,不重复类型注释已经交代的共同后果。
|
|
108
116
|
|
|
109
|
-
第一段不写参数名,像「把 value 写到 pos
|
|
117
|
+
第一段不写参数名,像「把 value 写到 pos 对应的方格上」这种句子把签名里的名字搬进摘要,离开签名就读不懂;改成「把指定值写到指定坐标的方格上」,名字留给 `@param`。`@param` 里的参数名是绑定声明,`kX`、`kO` 这类枚举值仍用反引号。
|
|
110
118
|
|
|
111
119
|
## 用词:先找名词
|
|
112
120
|
|
|
@@ -114,14 +122,14 @@ friend bool operator==(const Position&, const Position&) = default;
|
|
|
114
122
|
|
|
115
123
|
```cpp
|
|
116
124
|
/// 获取已经下过的每一次落子。
|
|
117
|
-
[[nodiscard]]
|
|
125
|
+
[[nodiscard]] std::span<const Move> GetHistory() const;
|
|
118
126
|
```
|
|
119
127
|
|
|
120
128
|
**好**:
|
|
121
129
|
|
|
122
130
|
```cpp
|
|
123
131
|
/// 获取落子历史。
|
|
124
|
-
[[nodiscard]]
|
|
132
|
+
[[nodiscard]] std::span<const Move> GetHistory() const;
|
|
125
133
|
```
|
|
126
134
|
|
|
127
135
|
领域里已经有名词(历史、棋盘、落子)时直接用它,不把集合摊成「已经下过的每一次…」这类短语:后者更长,读者还要自己把它归纳成「历史」。用词规则见 `references/comments/glossary/comment-terms.md`。
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
```cpp
|
|
6
6
|
Board board_; // 对局棋盘。
|
|
7
|
-
std::array<Square, kSquareCount> squares_{}; //
|
|
7
|
+
std::array<Square, kSquareCount> squares_{}; // 存储棋盘方格。
|
|
8
8
|
```
|
|
9
9
|
|
|
10
10
|
前一句把名字翻译了一遍;后一句只复述声明,没有说清下标怎样换算成行列。
|
|
@@ -12,9 +12,9 @@ std::array<Square, kSquareCount> squares_{}; // 存储棋盘格子。
|
|
|
12
12
|
**好**:
|
|
13
13
|
|
|
14
14
|
```cpp
|
|
15
|
-
///
|
|
15
|
+
/// 方格数组:按行优先排列。
|
|
16
16
|
std::array<Square, kSquareCount> squares_{};
|
|
17
|
-
/// 当前玩家:开局由 X
|
|
17
|
+
/// 当前玩家:开局由 X 玩家落子。
|
|
18
18
|
Player current_player_{Player::kX};
|
|
19
19
|
/// 落子历史:按落子先后保存。
|
|
20
20
|
std::vector<Move> history_;
|
|
@@ -3,16 +3,16 @@
|
|
|
3
3
|
把上面的规则放在一起,`Board` 的声明是这样:
|
|
4
4
|
|
|
5
5
|
```cpp
|
|
6
|
-
///
|
|
6
|
+
/// 棋盘。
|
|
7
7
|
class Board {
|
|
8
8
|
public:
|
|
9
9
|
/// 默认构造函数:初始化为空棋盘。
|
|
10
10
|
Board() = default;
|
|
11
11
|
|
|
12
12
|
private:
|
|
13
|
-
///
|
|
13
|
+
/// 方格数组:按行优先排列。
|
|
14
14
|
std::array<Square, kSquareCount> squares_{};
|
|
15
15
|
};
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
类注释只说这个类型是什么;构造注释补的是 `= default` 配合 `squares_{}` 才建立的后置条件;`squares_`
|
|
18
|
+
类注释只说这个类型是什么;构造注释补的是 `= default` 配合 `squares_{}` 才建立的后置条件;`squares_` 的上一行先说是方格数组,再用全角冒号补排列方向。行尾不写补充;`{}` 把每个 `Square` 初始化成空方格,这件事由构造注释表达。三处各说一件事,没有一句复述签名。
|
|
@@ -22,43 +22,60 @@ for (const auto& pos : empty_squares) {
|
|
|
22
22
|
**好**:
|
|
23
23
|
|
|
24
24
|
```cpp
|
|
25
|
-
|
|
26
|
-
//
|
|
27
|
-
if (
|
|
28
|
-
return PlaceResult::
|
|
25
|
+
PlaceResult Game::Place(Move move) {
|
|
26
|
+
// 检查对局是否仍在进行。
|
|
27
|
+
if (GetResult() != GameResult::kInProgress) {
|
|
28
|
+
return PlaceResult::kFinishedGame;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
//
|
|
32
|
-
if (
|
|
33
|
-
return PlaceResult::
|
|
31
|
+
// 检查落子玩家是否为当前玩家。
|
|
32
|
+
if (current_player_ != move.player) {
|
|
33
|
+
return PlaceResult::kInvalidPlayer;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
// 检查要写入的值是否为空。
|
|
37
|
+
const SquareValue target_square_value = PlayerToSquareValue(move.player);
|
|
38
|
+
if (target_square_value == SquareValue::kEmpty) {
|
|
39
|
+
return PlaceResult::kEmptySquare;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 检查目标方格是否为空。
|
|
43
|
+
const Square target_square = board_.GetSquare(move.position);
|
|
44
|
+
if (!target_square.IsEmpty()) {
|
|
45
|
+
return PlaceResult::kOccupiedSquare;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
current_player_ = PlayerToOpponent(current_player_);
|
|
49
|
+
board_.SetSquare(move.position, target_square_value);
|
|
50
|
+
history_.push_back(move);
|
|
51
|
+
return PlaceResult::kPlaced;
|
|
39
52
|
}
|
|
40
53
|
```
|
|
41
54
|
|
|
42
|
-
|
|
55
|
+
每一条拒绝各写一条独立 `if`,在判定处用一句「检查…是否…」写这一支在检查什么;结果由分支里的 `return` 表达,不再复述「没有写入」「按失败返回」。局部值由上一行赋值取出、赋值与上一个阶段之间又有空行时,注释写在赋值那一行上方,说明赋值与紧随其后的分支这整块,`if` 上不再重复。`//` 里的枚举值不包反引号。分支的完整规则见 `references/comments/comment-rules.md` 的「实现」节。
|
|
43
56
|
|
|
44
57
|
**好(多条件)**:
|
|
45
58
|
|
|
46
59
|
```cpp
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
60
|
+
std::array<SquareValue, 3> values{};
|
|
61
|
+
for (std::size_t i = 0; i < line.size(); ++i) {
|
|
62
|
+
values[i] = board_.GetSquare(line[i]).GetValue();
|
|
63
|
+
}
|
|
64
|
+
// 检查第一个方格是否有值,再核对三个方格的取值是否相同。
|
|
65
|
+
if (values[0] != SquareValue::kEmpty && values[0] == values[1] && values[1] == values[2]) {
|
|
66
|
+
return values[0] == SquareValue::kX ? GameResult::kXWin : GameResult::kOWin;
|
|
50
67
|
}
|
|
51
68
|
```
|
|
52
69
|
|
|
53
|
-
多条件判断按代码的判定顺序写:先确认的条件在前,后核对的在后;结果由分支里的 `return`
|
|
70
|
+
多条件判断按代码的判定顺序写:先确认的条件在前,后核对的在后;结果由分支里的 `return` 表达,不在注释里重复。三个同类取值存进 `values` 数组并按下标读取,不写 `value0`、`value1`、`value2`。
|
|
54
71
|
|
|
55
72
|
**好(成功路径)**:
|
|
56
73
|
|
|
57
74
|
```cpp
|
|
58
75
|
// 落子成功,记录这一步并轮到对方。
|
|
59
76
|
history_.push_back(move);
|
|
60
|
-
current_player_ =
|
|
61
|
-
return
|
|
77
|
+
current_player_ = PlayerToOpponent(current_player_);
|
|
78
|
+
return GetResult();
|
|
62
79
|
```
|
|
63
80
|
|
|
64
81
|
成功路径有多步状态更新时,用一句阶段注释概括整段;不在每一步上方逐行解释 `push_back` 与赋值。
|
|
@@ -7,29 +7,28 @@
|
|
|
7
7
|
| 概念 | 首选 | 避免 | 说明 |
|
|
8
8
|
| --- | --- | --- | --- |
|
|
9
9
|
| `Position` 类型 | 行列坐标 | 位置类型、坐标点、有序数对 | 类型名保持 `Position`,参数名用 `pos` |
|
|
10
|
-
| 棋盘上的点 | 位置 |
|
|
11
|
-
| `Square` |
|
|
10
|
+
| 棋盘上的点 | 位置 | 坐标点、方格坐标 | 说语义位置时用「位置」 |
|
|
11
|
+
| `Square` | 方格 | 单元、格子、棋子 | 「棋子」只指对局中的实体棋子 |
|
|
12
12
|
| `Board` | 棋盘 | 盘面、棋局 | 「棋局」只指一局对局 |
|
|
13
|
-
| `SquareValue` 类型 |
|
|
14
|
-
| `SquareValue` 的取值 |
|
|
15
|
-
| `Place` 的动作 |
|
|
16
|
-
| `
|
|
17
|
-
| `
|
|
18
|
-
| `
|
|
19
|
-
| `
|
|
13
|
+
| `SquareValue` 类型 | 方格的值 | 值类型、方格状态、状态枚举 | 类型注释写「方格的值。」 |
|
|
14
|
+
| `SquareValue` 的取值 | 空、X、O | 状态为空、尚未设置、状态为 X | 行尾写「空」「X」「O」;「空」也用于指没有值的方格本身 |
|
|
15
|
+
| `Place` 的动作 | 落子 | 放置、更新状态、写入 | `Game::Place` 执行一手;`Board::SetSquare` 只写入,不叫落子 |
|
|
16
|
+
| `PlaceResult` 类型 | 一次落子的结果 | 放置结果、落子结果、方格状态变更结果 | 类型注释写「一次落子的结果。」;由 `Game` 产生 |
|
|
17
|
+
| `PlaceResult` 的取值 | `kPlaced`、`kInvalidPlayer`、`kFinishedGame`、`kEmptySquare`、`kOccupiedSquare` | 成功、失败、写入成功、落子失败 | 行尾写「成功:…」「失败:…」;成功排第一,其余按固定顺序;玩家不对是 `kInvalidPlayer`,终局是 `kFinishedGame`,空值是 `kEmptySquare`,占用是 `kOccupiedSquare` |
|
|
18
|
+
| `GameResult` 类型 | 对局结果 | 对局状态、棋局结果、GameState | 类型注释写「对局结果。」 |
|
|
19
|
+
| `GameResult` 的取值 | 对局进行中、X 已获胜、O 已获胜、平局 | 未结束、X 赢、O 赢、和棋 | 每行以「对局」或胜负主体开头,写完整状态 |
|
|
20
20
|
| `Game::Place` 的动作 | 执行一次落子 | 交给棋盘、落下这一手 | 第一句写「执行一次落子。」,交接与记录由 `@retval` 承担 |
|
|
21
|
-
| `
|
|
22
|
-
| `BoardStatus` 的取值 | 棋盘尚未结束、棋盘上 X 已连成一线、棋盘上 O 已连成一线、棋盘已满且无人连成一线 | 尚未结束、X 成线、O 成线、棋盘已满 | 每行都以「棋盘」开头,写完整状态 |
|
|
21
|
+
| `GetResult` | 当前对局结果 | 棋盘状态、对局状态 | 第一句写「获取当前对局结果。」 |
|
|
23
22
|
| `Player` 类型 | 玩家 | 对局的一方、落子的一方 | 类型注释写「玩家。」,取值由枚举项说明 |
|
|
24
|
-
| `Player` 的取值 | 玩家为 X
|
|
23
|
+
| `Player` 的取值 | 玩家为 X、玩家为 O | 先手、后手、X 方、O 方 | 每行只写「玩家为」加取值,先后手由对局规则说明 |
|
|
25
24
|
| `Move` 类型 | 落子记录 | 一次落子、落子 | 类型注释写「落子记录。」,第二段写「记录一次落子,包括落子位置与落子玩家。」 |
|
|
26
25
|
| `Move` 的短字段 | 落子位置:棋盘上的行列坐标、落子玩家:游戏中的玩家 | 坐标、玩家 | 行尾写「字段:说明」,冒号后补字段名看不出的语境 |
|
|
27
26
|
| `history_` | 落子历史 | 已经下过的落子、每一次落子 | 主注释写「落子历史:按落子先后保存。」,`GetHistory` 写「获取落子历史。」 |
|
|
28
|
-
| `Clear` 的动作 | 清空 | 重置为空、清空值、收回 `kEmpty` | `
|
|
29
|
-
| `GetSquare` 的返回 |
|
|
27
|
+
| `Clear` 的动作 | 清空 | 重置为空、清空值、收回 `kEmpty` | `Board::ClearSquare` 清一格;`Board::Clear` 清整盘;单格只 `SetValue` |
|
|
28
|
+
| `GetSquare` 的返回 | 方格副本 | 方格、引用 | 按值返回必须说「副本」 |
|
|
30
29
|
| 坐标越界 | 坐标越界时抛出 `std::out_of_range` | 检查越界 | 只用于会抛的校验函数 |
|
|
31
|
-
| `GetValue` | 当前值 |
|
|
32
|
-
| `
|
|
30
|
+
| `GetValue` | 当前值 | 状态值、方格的状态 | 返回当前值 |
|
|
31
|
+
| `GameResult` | 对局结果 | 棋盘状态、对局状态 | 结果由 `Game` 计算,棋盘不保存状态 |
|
|
33
32
|
| `row`、`col` | 行号、列号 | 行、列、第一行、第一列 | 成员注释写「行号,默认值为 0」,不写棋盘语义,类注释不重复 |
|
|
34
33
|
|
|
35
34
|
## 位置与坐标
|
|
@@ -39,17 +38,17 @@
|
|
|
39
38
|
- 成员注释写「行号,默认值为 0」「列号,默认值为 0」;类型是 `int`,可为负,不写「从 0 起算」;类注释不再重复默认值,否则和成员注释冲突。
|
|
40
39
|
- `Position` 是通用坐标,不写「0 表示第一行」「0 表示第一列」这类棋盘语义;哪一行是首行由使用坐标的模块(例如 `Board`)说明。
|
|
41
40
|
|
|
42
|
-
##
|
|
41
|
+
## 方格与棋盘
|
|
43
42
|
|
|
44
|
-
- `Square`
|
|
45
|
-
- `Board` 只说「棋盘」;`squares_`
|
|
46
|
-
-
|
|
47
|
-
- `
|
|
43
|
+
- `Square` 只说「方格」;「棋盘上的方格」只在需要强调归属时用,类型注释本身不写棋盘。
|
|
44
|
+
- `Board` 只说「棋盘」;`squares_` 说「方格数组」,不写「存储方格的数组」这种复述。
|
|
45
|
+
- 「棋子」只指对局中的实体棋子;取值里的 X 或 O 叫「值」,不叫「棋子」。
|
|
46
|
+
- `PlaceResult` 的五项先写「成功」或「失败」;冒号后按固定顺序写「落子玩家 → 对局状态 → 写入的值 → 目标方格」,某一项不适用时省略。成功说「落子玩家是当前玩家,值已写入方格」,玩家不对说「落子玩家不是当前玩家,没有写入」,终局说「对局已经结束,没有写入」,空值说「要写入的值是 kEmpty,没有写入」,占用说「目标方格已有值,没有写入」;「没有写入」由类型注释统一交代,不在每一行重复;不说「已有状态」或「占据」。
|
|
48
47
|
- 结果枚举与分支注释的完整写法见 `references/naming/identifier-names.md` 与 `references/comments/comment-rules.md`;本表只定词。
|
|
49
48
|
|
|
50
49
|
## 玩家
|
|
51
50
|
|
|
52
|
-
- `Player` 的类型注释与枚举项统一用「玩家」:类型写「玩家。」,取值写「玩家为 X
|
|
51
|
+
- `Player` 的类型注释与枚举项统一用「玩家」:类型写「玩家。」,取值写「玩家为 X」「玩家为 O」,不把先后手这类对局规则写进取值说明。
|
|
53
52
|
- 不用「对局的一方」「落子的一方」指 `Player`;「一方」只在对局叙述里说明轮换关系。
|
|
54
53
|
|
|
55
54
|
## 落子记录
|
|
@@ -62,26 +61,26 @@
|
|
|
62
61
|
## 对局
|
|
63
62
|
|
|
64
63
|
- `Game::Place` 写「执行一次落子。」;不写「把一次落子交给棋盘;成功时记录这一步」,第一句只写动作,交给棋盘与记录这一步由 `@retval` 承担。
|
|
65
|
-
-
|
|
64
|
+
- 固定顺序由 `@retval` 的排列表达,不用 `@note` 复述;终局作为正常结果返回,不靠 `@note` 解释。
|
|
66
65
|
|
|
67
66
|
## 更新与写入
|
|
68
67
|
|
|
69
|
-
- `Square::
|
|
70
|
-
-
|
|
71
|
-
- `
|
|
72
|
-
- `Clear` 说「清空」或「清空为 `kEmpty`」,不写「重置为空」「清空值」「删除状态」;`Board::
|
|
68
|
+
- `Square::SetValue` 的动作统一写「设值」:`Square::SetValue` 把传入的值写入方格,覆盖原值;`Board::SetSquare` 只检查坐标再写入,不判空值与占用。
|
|
69
|
+
- 「放置」不用在任何接口语境:`Board::SetSquare` 说「写入」,`Game::Place` 说「落子」。
|
|
70
|
+
- `kPlaced` 说「成功:落子玩家是当前玩家,值已写入方格」;`kInvalidPlayer` 说「失败:落子玩家不是当前玩家,没有写入」;`kFinishedGame` 说「失败:对局已经结束,没有写入」;`kEmptySquare` 说「失败:要写入的值是 kEmpty,没有写入」;`kOccupiedSquare` 说「失败:目标方格已有值,没有写入」。枚举项行尾、`@retval` 与设置预览三处用同一句,三者不互换。
|
|
71
|
+
- `Clear` 说「清空」或「清空为 `kEmpty`」,不写「重置为空」「清空值」「删除状态」;`Board::ClearSquare` 说「清空一个方格」,`Board::Clear` 说「清空棋盘」。
|
|
73
72
|
|
|
74
73
|
## 返回值与副本
|
|
75
74
|
|
|
76
|
-
- `GetSquare` 按值返回:第一句和 `@return`
|
|
75
|
+
- `GetSquare` 按值返回:第一句和 `@return` 都写「方格副本」,不写「该位置的方格」,否则读者会以为拿到引用。
|
|
77
76
|
- `GetValue` 返回枚举值,写「当前值」,不写「状态值」。
|
|
78
|
-
- `Board
|
|
77
|
+
- `GameResult` 写「对局结果」,不写「状态」;`Board` 没有 `GetStatus`,终局由 `Game::GetResult` 计算。
|
|
79
78
|
- 会抛的查询先写抛出条件,再写 `@throws`;`@param` 写「待检查/读取/更新的坐标」,不把「棋盘内」当前置条件。
|
|
80
79
|
|
|
81
80
|
## 初始状态与特殊成员
|
|
82
81
|
|
|
83
|
-
-
|
|
84
|
-
- 数据成员只写成员自身:`row`、`col` 写行号/列号与默认值;`value_`
|
|
82
|
+
- 类有不变量时,类注释拥有初始状态与生命周期;纯值容器的初始状态归默认构造函数,类注释只写这个类型是什么。默认构造函数写具体初始状态,如「初始化为空方格」「初始化为空棋盘」「初始化为空对局」,不写「建立初始状态」。对象由多个成员组成时写整个对象的初始状态,不逐项罗列成员。
|
|
83
|
+
- 数据成员只写成员自身:`row`、`col` 写行号/列号与默认值;`value_` 写「方格的值」,不重复类注释的初始值。
|
|
85
84
|
- 文件头只写 `@file`、`@brief` 与文件组成;不写作者、版权年份、变更史。
|
|
86
85
|
|
|
87
86
|
## 依据
|
|
@@ -12,10 +12,10 @@ std::vector<std::vector<Position>> BuildLines() {
|
|
|
12
12
|
}
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
**好**:固定坐标写成具名命名空间里的 `constexpr` 常量表,`HasLine` 与 `PreviewWin`
|
|
15
|
+
**好**:固定坐标写成具名命名空间里的 `constexpr` 常量表,`HasLine` 与 `PreviewWin` 共用,所以不放进任何一个函数体;方格用 `at()`,整条线 `all_of`、八条线 `any_of`,函数不标 `noexcept`。
|
|
16
16
|
|
|
17
17
|
```cpp
|
|
18
|
-
// board.cpp
|
|
18
|
+
// board.cpp 的命名空间:行、列、对角线三组,坐标固定。
|
|
19
19
|
constexpr std::array<std::array<Position, 3>, 8> kLines = {{
|
|
20
20
|
{{{0, 0}, {0, 1}, {0, 2}}}, // 行 0
|
|
21
21
|
{{{1, 0}, {1, 1}, {1, 2}}}, // 行 1
|
|
@@ -28,4 +28,4 @@ constexpr std::array<std::array<Position, 3>, 8> kLines = {{
|
|
|
28
28
|
}};
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
每个方格要分别处理时仍写 `row` / `col` 循环,例如收集空方格、终端打印。
|
|
@@ -23,4 +23,4 @@ task_list/src/detail.h // 私有头,只被本组件包含
|
|
|
23
23
|
#include "task_list.h" // 公共头按裸文件名包含
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
构建时把组件的 `include/` 目录加入 include 搜索路径,模板写法是 `target_include_directories(greeting PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)`;不要用 `#include "../task_list/include/task_list.h"` 这种随文件位置漂移的写法。
|
|
@@ -1,39 +1,48 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 正反例:落子结果用返回值回答
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**差**:`Game::Place` 返回整局结果,一次落子的失败只能挤进异常。调用方要写 `try/catch` 才能提示「方格已被占用」,终局与非法玩家只能从消息文本里猜;想区分失败就得解析字符串。这一手成没成与整局如何是两件事,混在一个返回值里,调用方每步还要再问一次 `GetResult()`。
|
|
4
4
|
|
|
5
5
|
```cpp
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
GameResult Game::Place(Move move) {
|
|
7
|
+
if (!board_.GetSquare(move.position).IsEmpty()) {
|
|
8
|
+
throw std::logic_error{"Game::Place: board rejected an occupied square"};
|
|
9
|
+
}
|
|
10
|
+
board_.SetSquare(move.position, PlayerToSquareValue(move.player));
|
|
11
|
+
history_.push_back(move);
|
|
12
|
+
current_player_ = PlayerToOpponent(current_player_);
|
|
13
|
+
return GetResult();
|
|
8
14
|
}
|
|
9
|
-
const Move rebuilt{move.position, GetCurrentPlayer()};
|
|
10
|
-
static_cast<void>(board_.Place(rebuilt.position, PlayerToSquareValue(rebuilt.player)));
|
|
11
|
-
history_.push_back(rebuilt);
|
|
12
|
-
return PlaceResult::kSuccess;
|
|
13
15
|
```
|
|
14
16
|
|
|
15
|
-
**好**:`Game::Place`
|
|
17
|
+
**好**:`Game::Place` 返回 `PlaceResult`,五个取值各答一条规则:`kPlaced` 说这一手写成,`kInvalidPlayer`、`kFinishedGame`、`kEmptySquare` 与 `kOccupiedSquare` 各说一条拒绝的规则。调用方按值分支,不解析消息;整局结果仍由 `GetResult()` 单独回答。棋盘写入由 `Board::SetSquare` 完成,它只检查坐标,不报告占用,因此占用必须在写入前判断。多个拒绝分支各自写成一条 `if`,检查不通过就返回;成功路径落在函数末尾,不串 `else if`。
|
|
16
18
|
|
|
17
19
|
```cpp
|
|
18
|
-
|
|
19
|
-
//
|
|
20
|
+
PlaceResult Game::Place(Move move) {
|
|
21
|
+
// 检查对局是否仍在进行。
|
|
22
|
+
if (GetResult() != GameResult::kInProgress) {
|
|
23
|
+
return PlaceResult::kFinishedGame;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 检查落子玩家是否为当前玩家。
|
|
20
27
|
if (current_player_ != move.player) {
|
|
21
|
-
|
|
22
|
-
"Game::Place: current player {} cannot accept move {}", current_player_, move)};
|
|
28
|
+
return PlaceResult::kInvalidPlayer;
|
|
23
29
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
30
|
+
|
|
31
|
+
// 检查要写入的值是否为空。
|
|
32
|
+
const SquareValue target_square_value = PlayerToSquareValue(move.player);
|
|
33
|
+
if (target_square_value == SquareValue::kEmpty) {
|
|
34
|
+
return PlaceResult::kEmptySquare;
|
|
27
35
|
}
|
|
28
36
|
|
|
29
|
-
//
|
|
30
|
-
|
|
31
|
-
|
|
37
|
+
// 检查目标方格是否为空。
|
|
38
|
+
const Square target_square = board_.GetSquare(move.position);
|
|
39
|
+
if (!target_square.IsEmpty()) {
|
|
40
|
+
return PlaceResult::kOccupiedSquare;
|
|
32
41
|
}
|
|
33
42
|
|
|
34
|
-
|
|
43
|
+
current_player_ = PlayerToOpponent(current_player_);
|
|
44
|
+
board_.SetSquare(move.position, target_square_value);
|
|
35
45
|
history_.push_back(move);
|
|
36
|
-
|
|
37
|
-
return PlaceResult::kSuccess;
|
|
46
|
+
return PlaceResult::kPlaced;
|
|
38
47
|
}
|
|
39
48
|
```
|
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
# 正反例:推演一步读原数据
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**差**:先复制整个棋盘,再用 `SetSquare` 改副本。`SetSquare` 后写覆盖前值,也不报告目标方格原本有没有值,副本上的「落子」看不出这一格本来已有棋子;越界时它抛异常,预览的失败又多出一种形态。
|
|
4
4
|
|
|
5
5
|
```cpp
|
|
6
6
|
Board hypothetical = *this;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return WinPreviewResult::kInvalidPlace;
|
|
10
|
-
}
|
|
11
|
-
return hypothetical.HasLine(value) ? WinPreviewResult::kWouldWin
|
|
12
|
-
: WinPreviewResult::kWouldNotWin;
|
|
7
|
+
hypothetical.SetSquare(pos, value);
|
|
8
|
+
return hypothetical.HasLine(value) ? WinPreviewResult::kWouldWin : WinPreviewResult::kWouldNotWin;
|
|
13
9
|
```
|
|
14
10
|
|
|
15
|
-
|
|
11
|
+
**好**:判断「这一格换成某种棋子后会不会成线」不需要第二份棋盘。越界与占用是预览自己要先拒绝的条件,放在最前并返回原因;通过之后只在包含该坐标的线上把这一格算作 `value`,其余方格读原棋盘。
|
|
16
12
|
|
|
17
13
|
```cpp
|
|
18
14
|
WinPreviewResult Board::PreviewWin(Position pos, SquareValue value) const {
|
|
19
|
-
if (
|
|
15
|
+
if (!IsInRange(pos) || !GetSquare(pos).IsEmpty()) {
|
|
20
16
|
return WinPreviewResult::kInvalidPlace;
|
|
21
17
|
}
|
|
22
18
|
|
|
@@ -37,4 +33,4 @@ WinPreviewResult Board::PreviewWin(Position pos, SquareValue value) const {
|
|
|
37
33
|
}
|
|
38
34
|
```
|
|
39
35
|
|
|
40
|
-
先用 `IsInRange`
|
|
36
|
+
先用 `IsInRange` 拒绝越界,再读取方格:越界在触碰数组之前被拒绝,`GetSquare` 实际不会抛,函数内没有抛出点,可以标 `noexcept`。`SetSquare` 不能拿来试落,因为它只管写入,不回答这一格能不能落。
|
|
@@ -20,11 +20,12 @@
|
|
|
20
20
|
|
|
21
21
|
### 取值的边界
|
|
22
22
|
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
-
|
|
26
|
-
-
|
|
27
|
-
-
|
|
23
|
+
- 封闭取值一次列全。方格的取值是空、X、O;不要把 X 和 O 做成不含空的棋子,再用空表示空方格。
|
|
24
|
+
- 位置是行和列,不是方格的另一种取值。胜负、轮到谁、已下过的序列属于对局,不要塞进位置类型。坐标类型单独一个头文件;持有尺寸的类型包含它,不把坐标塞回尺寸类型里。
|
|
25
|
+
- 方格只是值容器:默认是空,写入任意取值,后写覆盖前值;是否允许覆盖、何时清空由容器决定。不要给方格加落子、清空或终局判断。
|
|
26
|
+
- 一次落子能区分的失败放进结果枚举:玩家不对、对局已结束、值非法、方格已有值。棋盘只做坐标检查与写入,不回答这一手成没成;判断留在对局层。
|
|
27
|
+
- 结果枚举跟随产生它的那一层:棋盘没有结果枚举,写入要么完成,要么因越界抛出;落子结果 `PlaceResult` 与整局结果 `GameResult` 都留在对局层。
|
|
28
|
+
- 对局层的落子入口返回这一手的 `PlaceResult`,整局结果由 `GetResult()` 另外回答;不要用 `GameResult` 兼答两件事,也不要再包一层只带成败与终局的类型。结果类型默认用 `enum class`;C++23 项目要在结果里携带更多信息时,可用 `std::expected<T, E>` 或具名 struct,但不用 out 参数。
|
|
28
29
|
- 调用方已经把完整值对象交进来时,下层直接用它,不再用散参数或查询重新拼出同一对象。可以校验,并在不符时返回原因;不要静默替换成自己重算的值。
|
|
29
30
|
- 构造值对象所需的只读查询留在拥有状态的一层;调用方用它组值对象,下层函数不重复推导。不要为了去掉查询,让每个调用方各自维护轮次副本。
|
|
30
31
|
- 范围检查在接收位置的那一层直接做,越界直接抛出;不要为了统一诊断把 `std::source_location` 穿过每个公共接口。
|
|
@@ -43,10 +44,10 @@
|
|
|
43
44
|
- 失败是调用方的正常结果(编号不存在、输入格式不对)时用返回值表达;函数无法完成自己的任务、或状态机进入不可能分支时抛异常,规则见 `references/exceptions/throw-rules.md`。
|
|
44
45
|
- 结果对象携带足够信息让调用方决定下一步;失败原因不止一种时不要只返回 `bool` 再让调用方猜原因。
|
|
45
46
|
- 动作只有一个失败原因、调用方也不需要区分时返回 `bool`,`@return` 写清 true 与 false 各自的含义,例如「撤回成功返回 true;没有可撤回的落子时返回 false」;不要为单一失败建只有一个失败取值的枚举。
|
|
46
|
-
- 结果枚举先列成功(编号 0
|
|
47
|
+
- 结果枚举先列成功(编号 0),再按固定顺序列失败原因:`kPlaced = 0`、`kInvalidPlayer = 1`、`kFinishedGame = 2`、`kEmptySquare = 3`、`kOccupiedSquare = 4`。编号只用来区分结果,不表示检查优先级。
|
|
47
48
|
- 每类能区分的失败单独一个取值,不合并成 `kInvalid`;调用方与测试才知道究竟哪条规则拒绝。
|
|
48
|
-
-
|
|
49
|
-
- 调用方穷举枚举时不写 `default`,让 `-Wswitch`
|
|
49
|
+
- 终局是正常结果项,不抛异常。空值与占用这类「这一手写不进」也返回结果项,由对局层的落子入口一次答清;只有坐标越界仍由棋盘抛出 `std::out_of_range`,因为那是调用方的程序错误,不是对局里的正常分支。
|
|
50
|
+
- 调用方穷举枚举时不写 `default`,让 `-Wswitch` 在新增取值时报警告,工程开了 `-Werror` 就是错误。一个值对多个离散取值分派时用 `switch`;范围比较、布尔组合或只有一两个分支时用 `if`。一个函数里有多条拒绝分支时,每条各写一条独立 `if`,检查不通过就返回;不同拒绝原因互不依赖,不串成 `else if`。成功路径写在函数末尾,不再用 `else` 包住;前面已经提前返回,能走到末尾就说明所有检查都通过。只有一条拒绝分支时同样提前返回,末尾直接写成功路径,不补 `throw` 兜底。
|
|
50
51
|
|
|
51
52
|
## 规则核心与外围
|
|
52
53
|
|
|
@@ -73,7 +74,7 @@
|
|
|
73
74
|
- 需要的数据在哪个类型里,就由那个类型回答(Information Expert)。
|
|
74
75
|
- 只有直接碰到内部表示的函数才做成成员;只组合公开查询的函数留在同一命名空间(C.4、C.5)。调用方不另写一份。
|
|
75
76
|
- 例外是虚函数、`=`、`()`、`[]`、`->` 这类必须为成员的运算符,以及同一组重载或链式调用里为了接口一致而留下的函数。使用例外时在声明旁写一句原因。
|
|
76
|
-
-
|
|
77
|
+
- 示例不指向任何仓库的类:一次落子由对局层接收,因为要同时有坐标、值与轮次;棋盘只检查坐标并写入,不回答占用与终局。对局层掌握参与者、历史与终局,把这一手的结果写成 `PlaceResult`。
|
|
77
78
|
|
|
78
79
|
## 边界的划分理由
|
|
79
80
|
|
|
@@ -102,10 +103,12 @@
|
|
|
102
103
|
|
|
103
104
|
## 函数接口设计
|
|
104
105
|
|
|
105
|
-
-
|
|
106
|
+
- 一个函数只做一件事;语义不同的动作不要靠默认参数合并。清空整格与清空整盘是两个动作,分别写成 `void ClearSquare(Position pos);` 与 `void Clear();`;不要用 `Clear(std::optional<Position>)` 让调用点猜省略参数的含义。
|
|
106
107
|
- 语义不同的动作不要共用名字,也不要靠重载区分(Google:调用点必须能直接看出在调用什么);重载只用于语义相同的变体。
|
|
107
108
|
- 不用 `bool` 参数切换行为;两个分支做不同的事时拆成两个函数,或把开关提成有名字的枚举。
|
|
108
109
|
- 参数顺序:被操作的对象在前,控制量与选项在后;输出参数放最后并标 `[out]`。
|
|
110
|
+
- 只赋值、不做校验的 setter 返回 `void`,可以标 `noexcept`;不要加 `[[nodiscard]]`,也不要用返回值暗示写入成功。写入是否允许由拥有容器的类型决定,setter 只改值。
|
|
111
|
+
- 落子类函数的检查顺序固定为:整局状态 → 本层前置条件(如玩家) → 值合法性 → 坐标(由下层的读取完成) → 占用 → 写入。整局状态不依赖参数,先判断能在对局结束后立刻短路,也不会把「终局后落子」误报成玩家错误;玩家与终局不依赖容器,先判断可省掉无谓的读取;值同样来自参数,先判断可省一次读取。多条拒绝分支按这个顺序各写一条独立 `if`,不串 `else if`;成功路径落在函数末尾。
|
|
109
112
|
- 公共接口只保留调用方需要的形态;测试专用的入口不放公共头文件。
|
|
110
113
|
- 默认参数只写在声明处;调用点省略参数时,注释要写清省略后的行为,避免「不传 = 全部」成为隐藏约定。
|
|
111
114
|
|
|
@@ -118,17 +121,17 @@
|
|
|
118
121
|
```cpp
|
|
119
122
|
board_ = Board{};
|
|
120
123
|
for (const Move& move : history_) {
|
|
121
|
-
board_.
|
|
124
|
+
board_.SetSquare(move.position, PlayerToSquareValue(move.player));
|
|
122
125
|
}
|
|
123
126
|
```
|
|
124
127
|
|
|
125
128
|
正例(清空那一格,再把轮次还给落子的一方):
|
|
126
129
|
|
|
127
130
|
```cpp
|
|
128
|
-
Move
|
|
131
|
+
Move last_move = history_.back();
|
|
132
|
+
current_player_ = last_move.player;
|
|
133
|
+
board_.ClearSquare(last_move.position);
|
|
129
134
|
history_.pop_back();
|
|
130
|
-
board_.Clear(last.position);
|
|
131
|
-
current_player_ = last.player;
|
|
132
135
|
```
|
|
133
136
|
|
|
134
137
|
棋盘有自己的单格清空接口时直接用它,不要用「清空整个棋盘」代替。
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
## 依据
|
|
58
58
|
|
|
59
59
|
- IPEd《Types of editing》把编辑分为 substantive editing、copyediting 与 proofreading;本模型把「写作、改写、润色」对应到「新增、重组、措辞」三档,版式独立成形式层。
|
|
60
|
-
- Google C++ Style Guide、LLVM Coding Standards 与
|
|
60
|
+
- Google C++ Style Guide、LLVM Coding Standards 与 C++ Core Guidelines 的注释条目都要求写意图、用法与陷阱,不把代码翻译成自然语言。
|
|
61
61
|
- 这些资料只解释模型,不替代项目技能、`environment.json` 或用户明确要求。
|
|
62
62
|
|
|
63
63
|
## 何时打开
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
#ifdef NDEBUG
|
|
7
7
|
return false;
|
|
8
8
|
#else
|
|
9
|
-
throw std::out_of_range{row, col};
|
|
9
|
+
throw std::out_of_range{std::format("position ({}, {}) is out of range", row, col)};
|
|
10
10
|
#endif
|
|
11
11
|
```
|
|
12
12
|
|
|
@@ -25,13 +25,13 @@ void Place(Position pos, Player player);
|
|
|
25
25
|
**差**:用 `assert` 承担 Release 也必须执行的 invariant 检查;`NDEBUG` 后检查消失。
|
|
26
26
|
|
|
27
27
|
```cpp
|
|
28
|
-
assert(game.
|
|
28
|
+
assert(game.GetResult() == GameResult::kInProgress);
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
**好**:两种模式都必须检查的 invariant 用 `Check`(项目保留时),失败时打印诊断并终止;只在开发期值得做、Release 可以省掉的自检用 `DebugCheck
|
|
31
|
+
**好**:两种模式都必须检查的 invariant 用 `Check`(项目保留时),失败时打印诊断并终止;只在开发期值得做、Release 可以省掉的自检用 `DebugCheck`。没有这两个宏时,两种模式都必须执行的检查用 `std::terminate`(失败前先打印诊断),只在开发期做的自检用 `assert`。
|
|
32
32
|
|
|
33
33
|
```cpp
|
|
34
|
-
Check(game.
|
|
34
|
+
Check(game.GetResult() == GameResult::kInProgress, "Ai::Choose: game is already over");
|
|
35
35
|
DebugCheck(index < history_.size(), "history index out of range");
|
|
36
36
|
```
|
|
37
37
|
|