-
Notifications
You must be signed in to change notification settings - Fork 0
/
HPCRecord.cpp
145 lines (125 loc) · 6.14 KB
/
HPCRecord.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//------------------------------------------------------------------------------
/// @file
/// @brief HPCRecord.hpp の実装
/// @author ハル研究所プログラミングコンテスト実行委員会
///
/// @copyright Copyright (c) 2015 HAL Laboratory, Inc.
/// @attention このファイルの利用は、同梱のREADMEにある
/// 利用条件に従ってください
//------------------------------------------------------------------------------
#include "HPCRecord.hpp"
#include "HPCCommon.hpp"
namespace hpc {
//------------------------------------------------------------------------------
/// クラスのインスタンスを生成します。
Record::Record()
: mStage()
, mCurrentStageIndex(0)
{
}
//------------------------------------------------------------------------------
/// ステージ開始時に一度呼ぶことで、ステージ開始を記録します。
///
/// @param[in] aStageIndex ステージ番号。
/// @param[in] aStage ステージ情報への参照。
///
/// @pre ステージ番号は有効な範囲を示している必要があります。
void Record::writeStartStage(int aStageIndex, const Stage& aStage)
{
HPC_RANGE_ASSERT_MIN_UB_I(aStageIndex, 0, Parameter::GameStageCount);
mCurrentStageIndex = aStageIndex;
mStage[mCurrentStageIndex].writeStart(aStage);
}
//------------------------------------------------------------------------------
/// ステージの各ターンの状態を記録します。
///
/// @pre 記録前に writeStartStage を呼び、ステージ開始状態にする必要があります。
///
/// @param[in] aResult ターンの実行結果。
void Record::writeTurn(const TurnResult& aResult)
{
mStage[mCurrentStageIndex].writeTurn(aResult);
}
//------------------------------------------------------------------------------
/// ステージ終了時に一度呼ぶことで、終了時の結果を記録します。
///
/// @param[in] aStage ステージ情報への参照。
///
/// @pre ステージ番号は有効な範囲を示している必要があります。
void Record::writeEndStage(const Stage& aStage)
{
mStage[mCurrentStageIndex].writeEnd(aStage);
}
//------------------------------------------------------------------------------
/// 各ステージの合計得点を返します。
/// すべてのステージが終了してから呼びます。
/// 記録中に呼び出した場合、正常な結果を得ることが出来ません。
///
/// @return 各ステージの合計得点。
int Record::score()const
{
// スコアの誤差が出ないように 浮動小数で計算し、
// 最後に整数に丸める。
//
// dobule を使うのは、float の加算を行って値が大きくなる可能性があるため。
double total = 0;
for (int index = 0; index < Parameter::GameStageCount; ++index) {
total += mStage[index].score();
}
return static_cast<int>(total);
}
//------------------------------------------------------------------------------
/// 引数に指定されたステージの記録を一覧形式で画面に表示します。
/// 記録がされていないステージの番号を指定した場合は何も表示されません。
///
/// @param[in] aStageIndex ステージ番号。有効な範囲の番号が指定される必要があります。
void Record::dumpStage(int aStageIndex)const
{
HPC_RANGE_ASSERT_MIN_UB_I(aStageIndex, 0, Parameter::GameStageCount);
HPC_PRINT_LOG("Stage", "%d\n", aStageIndex);
mStage[aStageIndex].dump();
}
//------------------------------------------------------------------------------
/// 引数に指定されたステージの記録を JSON 形式で画面に出力します。
/// 画面には指定されたステージの記録に関する部分のみの JSON が、非圧縮形式
/// (空白・改行・インデントなどを含み整形されている状態) で出力されます。
///
/// @param[in] aStageIndex ステージ番号。有効な範囲の番号が指定される必要があります。
void Record::dumpJsonStage(int aStageIndex)const
{
HPC_RANGE_ASSERT_MIN_UB_I(aStageIndex, 0, Parameter::GameStageCount);
mStage[aStageIndex].dumpJson(false);
}
//------------------------------------------------------------------------------
/// ゲームの全情報を含む JSON データを出力します。
/// この関数を利用して出力したデータはビューアに渡すことが出来ます。
///
/// @param[in] isCompressed 圧縮した形で出力するかどうか。
/// @c true を指定した場合、空白やインデントが取り除かれ、サイズが削減された
/// 形で出力されます。
void Record::dumpJson(bool isCompressed)const
{
HPC_PRINT("[");
// 基本情報
HPC_PRINT("%d,%d,%d,%d,%d,", Parameter::FieldWidthMax, Parameter::FieldHeightMax, Parameter::PeriodCount, Parameter::ItemCountMax, Parameter::TruckWeightCapacity);
HPC_PRINT_JSON_DEBUG(!isCompressed, "\n");
// ステージ情報表示
HPC_PRINT_JSON_DEBUG(!isCompressed, " "); // インデント (2)
HPC_PRINT("[");
HPC_PRINT_JSON_DEBUG(!isCompressed, "\n");
for (int index = 0; index < Parameter::GameStageCount; ++index) {
mStage[index].dumpJson(isCompressed);
if (index + 1 < Parameter::GameStageCount) {
HPC_PRINT(",");
}
HPC_PRINT_JSON_DEBUG(!isCompressed, "\n");
}
HPC_PRINT_JSON_DEBUG(!isCompressed, " "); // インデント (2)
HPC_PRINT("]");
HPC_PRINT_JSON_DEBUG(!isCompressed, "\n");
HPC_PRINT_JSON_DEBUG(!isCompressed, "\n");
HPC_PRINT("]\n");
}
}
//------------------------------------------------------------------------------
// EOF