C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 160 を表示中)
■92655 / )  Re[1]: 三目並べ 関数化
□投稿者/ YuO (2回)-(2019/10/18(Fri) 11:48:00)
2019/10/18(Fri) 11:54:05 編集(投稿者)
No92654 (aiti さん) に返信
> 同じ処理が何度も続くので短くまとめたいのですが、やり方がわかりません。

手順を踏むとよいでしょう。


1. 最初に、「どこが同じ処理か」を認識します。
今回の場合、
> If table(0, 0) = " " Then
> table(0, 0) = Mark
> LoopCount1 += 1
> End If
が添字を除いて同じ処理になります。

2. 同じ処理の異なる部分を変数化します。
Dim x As Integer
Dim y As Integer
Select Case Cursor
    Case 1
        x = 0
        y = 0

        If table(y, x) = " " Then
            table(y, x) = Mark
            LoopCount1 += 1
        End If
    Case 2
        x = 1
        y = 0

        If table(y, x) = " " Then
            table(y, x) = Mark
            LoopCount1 += 1
        End If
    Case 3
        x = 2
        y = 0

        If table(y, x) = " " Then
            table(y, x) = Mark
            LoopCount1 += 1
        End If
    Case 4
        x = 0
        y = 1

        If table(y, x) = " " Then
            table(y, x) = Mark
            LoopCount1 += 1
        End If
    Case 5
        x = 1
        y = 1

        If table(y, x) = " " Then
            table(y, x) = Mark
            LoopCount1 += 1
        End If
    Case 6
        x = 2
        y = 1

        If table(y, x) = " " Then
            table(y, x) = Mark
            LoopCount1 += 1
        End If
    Case 7
        x = 0
        y = 2

        If table(y, x) = " " Then
            table(y, x) = Mark
            LoopCount1 += 1
        End If
    Case 8
        x = 1
        y = 2

        If table(y, x) = " " Then
            table(y, x) = Mark
            LoopCount1 += 1
        End If
    Case 9
        x = 2
        y = 2

        If table(y, x) = " " Then
            table(y, x) = Mark
            LoopCount1 += 1
        End If
End Select

3. 同じ処理を外に出します。
今回の場合、省略されているCase Elseに相当する場合に処理しない、という処理があるので、処理するかどうかを変数で指定しておきます。

Dim x As Integer
Dim y As Integer
Dim matched As Boolean = False
Select Case Cursor
    Case 1
        x = 0
        y = 0
        matched = True
    Case 2
        x = 1
        y = 0
        matched = True
    Case 3
        x = 2
        y = 0
        matched = True
    Case 4
        x = 0
        y = 1
        matched = True
    Case 5
        x = 1
        y = 1
        matched = True
    Case 6
        x = 2
        y = 1
        matched = True
    Case 7
        x = 0
        y = 2
        matched = True
    Case 8
        x = 1
        y = 2
        matched = True
    Case 9
        x = 2
        y = 2
        matched = True
End Select
If matched Then
    If table(y, x) = " " Then
        table(y, x) = Mark
        LoopCount1 += 1
    End If
End If

4. 今回の場合、xとyはCursorから計算で出るため、xとyへの代入を計算式に変更します。
Dim x As Integer
Dim y As Integer
Dim matched As Boolean = False
Select Case Cursor
    Case 1
        x = (Cursor - 1) Mod 3
        y = (Cursor - 1) \ 3
        matched = True
    Case 2
        x = (Cursor - 1) Mod 3
        y = (Cursor - 1) \ 3
        matched = True
    Case 3
        x = (Cursor - 1) Mod 3
        y = (Cursor - 1) \ 3
        matched = True
    Case 4
        x = (Cursor - 1) Mod 3
        y = (Cursor - 1) \ 3
        matched = True
    Case 5
        x = (Cursor - 1) Mod 3
        y = (Cursor - 1) \ 3
        matched = True
    Case 6
        x = (Cursor - 1) Mod 3
        y = (Cursor - 1) \ 3
        matched = True
    Case 7
        x = (Cursor - 1) Mod 3
        y = (Cursor - 1) \ 3
        matched = True
    Case 8
        x = (Cursor - 1) Mod 3
        y = (Cursor - 1) \ 3
        matched = True
    Case 9
        x = (Cursor - 1) Mod 3
        y = (Cursor - 1) \ 3
        matched = True
End Select
If matched Then
    If table(y, x) = " " Then
        table(y, x) = Mark
        LoopCount1 += 1
    End If
End If

5. xとyはCursorの値によらず同じ式で決まっているので、括りだした中に入れます。
Dim matched As Boolean = False
Select Case Cursor
    Case 1
        matched = True
    Case 2
        matched = True
    Case 3
        matched = True
    Case 4
        matched = True
    Case 5
        matched = True
    Case 6
        matched = True
    Case 7
        matched = True
    Case 8
        matched = True
    Case 9
        matched = True
End Select
If matched Then
    Dim x As Integer = (Cursor - 1) Mod 3
    Dim y As Integer = (Cursor - 1) \ 3

    If table(y, x) = " " Then
        table(y, x) = Mark
        LoopCount1 += 1
    End If
End If

6. Select Caseがmatchedの設定だけになったので、If文に直接埋め込みます。
If Cursor >= 1 AndAlso Cursor <= 9 Then
    Dim x As Integer = (Cursor - 1) Mod 3
    Dim y As Integer = (Cursor - 1) \ 3

    If table(y, x) = " " Then
        table(y, x) = Mark
        LoopCount1 += 1
    End If
End If

返信 編集キー/


管理者用

- Child Tree -