sapporocpp / othello-battler

MIT License
0 stars 0 forks source link

変数の基本方針としての const 化 #26

Open usagi opened 8 years ago

usagi commented 8 years ago

反対が無ければconstでよいものはconstにした方が読み易いのでそうしたい。

maraigue commented 8 years ago

これはどういう意味でしょう? https://github.com/sapporocpp/othello-battler/issues/25#issuecomment-151879266 のことでしょうか、それとも別の話でしょうか。

ignisan commented 8 years ago

ということだと思います。基本的にすべてconst変数にしようという提案。

maraigue commented 8 years ago

そういう意味では、constにすべきところはして、しないところはしない使い方をしていたと自分では思っているのですが、具体的にはどの部分を指してのことでしょう。

ignisan commented 8 years ago

こことか https://github.com/sapporocpp/othello-shell/blob/master/othello_ai.hpp#L139

diff --git a/othello_ai.hpp b/othello_ai.hpp
index 5598a01..5efa0c5 100644
--- a/othello_ai.hpp
+++ b/othello_ai.hpp
@@ -136,11 +136,10 @@ namespace Othello{
         // Colorクラスのインスタンスをキー、石の数を値とする連想配列になっている
         std::map<Color, std::size_t> pieces(){
             std::map<Color, std::size_t> result;
-            Color c;

             for(int i = 0; i < rows_; ++i){
                 for(int j = 0; j < cols_; ++j){
-                    c = get(i, j);
+                    const auto c = get(i, j);^M
                     if(result.count(c) == 0) result[c] = 0;
                     ++(result[c]);
                 }
ignisan commented 8 years ago

こんなとこも

diff --git a/othello_ai.hpp b/othello_ai.hpp
index 5598a01..f5885fb 100644
--- a/othello_ai.hpp
+++ b/othello_ai.hpp
@@ -185,7 +184,7 @@ namespace Othello{
             // もしその場所にすでに石がある場合は、置けない
             if(get(i, j) != Color::EMPTY) return 0;

-            Color opponent_color = get_opponent_color(my_color); // 相手の色
+            const auto opponent_color = get_opponent_color(my_color); // 相手の色^M
             int flipnum = 0; // 裏返した数

             // 8方向に石を伸ばしていく
diff --git a/othello_match.cpp b/othello_match.cpp
index dc78313..a0c9716 100644
--- a/othello_match.cpp
+++ b/othello_match.cpp
@@ -24,17 +24,16 @@ constexpr std::size_t BOARD_COLS_DEFAULT = 8;
 // パスした場合はfalse、それ以外の場合はtrueを返す。
 template <class OthelloAIClass>
 bool conduct_placement(OthelloAIClass & ai, const char * ai_name, Othello::Color turn, Othello::Board & board){
-    Othello::Coord place_from_ai;
     bool piece_placed = true;

     Othello::Board board_tmp(board);

-    place_from_ai = ai.place(board_tmp);
+    const auto place_from_ai = ai.place(board_tmp);^M
     if(place_from_ai.is_valid()){
         // 返ってきた値が有効な位置を示していた場合

         // その場所に石を置く
-        std::size_t flipped = board.put_and_flip(place_from_ai, turn);
+        const auto flipped = board.put_and_flip(place_from_ai, turn);^M

         // もし置いても石を何も裏返せなかった場合は、エラーとする
         if(flipped == 0){
@@ -125,11 +124,11 @@ int main(int argc, char ** argv){
     }

     // 結果を数える
-    std::map<Othello::Color, std::size_t> result = board.pieces();
+    const auto result = board.pieces();^M

     std::cout << "[Final Result]" << std::endl;
-    std::cout << "Player B [" << OTHELLO_AI_1_NAME << "]: " << result[Othello::Color::BLACK] << std::endl;
-    std::cout << "Player W [" << OTHELLO_AI_2_NAME << "]: " << result[Othello::Color::WHITE] << std::endl;
+    std::cout << "Player B [" << OTHELLO_AI_1_NAME << "]: " << result.at(Othello::Color::BLACK) << std::endl;^M
+    std::cout << "Player W [" << OTHELLO_AI_2_NAME << "]: " << result.at(Othello::Color::WHITE) << std::endl;^M

     std::cin.getline(tmp_input, 3); // コンソールが消える前に入力待ちする。必要ならば
maraigue commented 8 years ago

ああなるほど…そもそも私には「関数内でだけ使う変数にconstを付ける」という考えがありませんでした。

ちなみに

-            Color c;

             for(int i = 0; i < rows_; ++i){
                 for(int j = 0; j < cols_; ++j){
-                    c = get(i, j);
+                    const auto c = get(i, j);^M
                     if(result.count(c) == 0) result[c] = 0;
                     ++(result[c]);
                 }

これって変更しても最適化されますかねえ(もしかしたらパフォーマンス面ではよくないのかも?と思いまして)。意味論的にはこう書いたほうがよいのですけど。

ignisan commented 8 years ago

これは最適化を期待してる。されなかったとしてもColorは

enum class Color : char

なので大した影響ないと考えてる。重いクラスだったらまた違うかもしれない。

maraigue commented 8 years ago

新プログラムのほうですが、const付けてみました。 https://github.com/sapporocpp/othello-shell/commit/6561e8b9edf056c021cadecefe1f1b1feb36d407