見出し画像

#42 ライツアウト2

● 問題

点灯ゲームと言われているゲームです。
正しい位置が選択できません。
すべてが○になっても終了しません。

●は消灯,○は点灯です。
選択したところと上下左右の点灯・消灯が入れ替わります。
うまく選択して,すべてを点灯させれば完成です。
ライトは全部で9つ
左上から順に1,2,3,4・・・で指定します。

$$
\begin{array}{ccc}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{array}
$$

import random

def lights_change():
    print_str = ''
    lights_count = 0
    for i in range(3):
        for j in range(3):
          print_str += question[i][j]
          if question[i][j] == '○':
              lights_count += 1
        print(print_str)
        print_str = ''

question = [['','',''],['','',''],['','','']]
lights_count = 0
times = 0

for i in range(3):
    for j in range(3):
        if random.randint(0,1) == 1:
            question[i][j] = '○'
            lights_count += 1
        else:
            question[i][j] = '●'

lights_change()
while lights_count != 9:
    times += 1
    pos = input('どこを変えますか?')
    num = int(pos)-1
    if 0 <= num < 9:
        row = num % 3
        col = num // 3
        if question[row][col] == '●':
            question[row][col] = '○'
        else:
            question[row][col] = '●'

        if col-1 >= 0:
            if question[row][col-1] == '●':
                question[row][col-1] = '○'
            else:
                question[row][col-1] = '●'
        if col+1 <= 2:
            if question[row][col+1] == '●':
                question[row][col+1] = '○'
            else:
                question[row][col+1] = '●'

        if row-1 >= 0:
            if question[row-1][col] == '●':
                question[row-1][col] = '○'
            else:
                question[row-1][col] = '●'

        if row+1 <= 2:
            if question[row+1][col] == '●':
                question[row+1][col] = '○'
            else:
                question[row+1][col] = '●'

        lights_change()
print(str(times)+'回で完成!')

● 解答

データは以下のような2次元配列です。

$$
\begin{array}{c|cc}
& 列[0] & 列[1] & 列[2] \\ \hline
行[0] & [0][0] & [0][1] & [0][2] \\
行[1] & [1][0] & [1][1] & [1][2] \\
行[2] & [2][0] & [2][1] & [2][2]
\end{array}
$$

配列を指定するために,入力した数(正確には入力した数-1)を行番号と列番号に変換しています。

$$
\begin{array}{ccc}
0 & 1 & 2 \\
3 & 4 & 5 \\
6 & 7 & 8
\end{array}
$$

入力した数-1を3で割った商が行番号,3で割った余りが列番号になります。
例えば入力した数が5だと 入力した数ー1は4です。
4を3で割った商は1,4を3で割った余りは1となり,2次元配列[1][1]が求められます。
問題では行番号と列番号の求め方が逆だったので,指定された番号通りになりませんでした。
すべて○に変わったかどうかは,変数lights_countで判定しています。
def lights_change():の中で求められていますが,グローバル変数として指定していないので,終了することができませんでした。

import random

def lights_change():
    global lights_count # 追加
    print_str = ''
    lights_count = 0
    for i in range(3):
        for j in range(3):
          print_str += question[i][j]
          if question[i][j] == '○':
              lights_count += 1
        print(print_str)
        print_str = ''

question = [['','',''],['','',''],['','','']]
lights_count = 0
times = 0

for i in range(3):
    for j in range(3):
        if random.randint(0,1) == 1:
            question[i][j] = '○'
            lights_count += 1
        else:
            question[i][j] = '●'

lights_change()
while lights_count != 9:
    times += 1
    pos = input('どこを変えますか?')
    num = int(pos)-1
    if 0 <= num < 9:
        row = num // 3 # 修正
        col = num % 3  # 修正
        if question[row][col] == '●':
            question[row][col] = '○'
        else:
            question[row][col] = '●'

        if col-1 >= 0:
            if question[row][col-1] == '●':
                question[row][col-1] = '○'
            else:
                question[row][col-1] = '●'
        if col+1 <= 2:
            if question[row][col+1] == '●':
                question[row][col+1] = '○'
            else:
                question[row][col+1] = '●'

        if row-1 >= 0:
            if question[row-1][col] == '●':
                question[row-1][col] = '○'
            else:
                question[row-1][col] = '●'

        if row+1 <= 2:
            if question[row+1][col] == '●':
                question[row+1][col] = '○'
            else:
                question[row+1][col] = '●'

        lights_change()
print(str(times)+'回で完成!')

#Python  #プログラミング

この記事が気に入ったらサポートをしてみませんか?