fork download
  1. #!/usr/bin/env python3
  2.  
  3. from random import randint
  4.  
  5. # 大域変数定義
  6. empty = '□'
  7. wall = '■'
  8. right, down, left, up, = 0, 1, 2, 3
  9. direction = [right, down, left, up]
  10.  
  11. # フィールド作成
  12. def make_field(x, y):
  13. return [[wall if i == 0 or i == x - 1 or j == 0 or j == y - 1 or (i % 2 == 0 and j % 2 == 0) else empty for j in range(y)] for i in range(x)]
  14.  
  15. # サイコロ定義
  16. def dice(n):
  17. return direction[randint(0, n - 1)]
  18.  
  19. # 条件により n 面体サイコロが代わり、
  20. # 変換されたリストのどこを変更するか決める
  21. def select(n, i, j, field):
  22. direction = dice(n)
  23. match direction:
  24. case 0:
  25. i += 1
  26. case 1:
  27. i -= 1
  28. case 2:
  29. j += 1
  30. case 3:
  31. j -= 1
  32. field[i][j] = wall
  33.  
  34. # maze(迷路)作成
  35. def make_maze(field):
  36. for i in range(2, len(field) - 1, 2):
  37. for j in range(2, len(field[0]) - 1, 2):
  38. if i == 2:
  39. select(4, i, j, field)
  40. elif field[i][j - 1] == wall:
  41. select(2, i, j, field)
  42. else:
  43. select(3, i, j, field)
  44. return field
  45.  
  46. # フィールド(迷路)を文字列に変換
  47. def maze2string(maze):
  48. return ''.join([''.join(i) + '\n' for i in maze])
  49.  
  50. if __name__ == '__main__':
  51. print(maze2string(make_maze(make_field(11, 11))))
  52.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Traceback (most recent call last):
  File "/usr/lib/python3.9/py_compile.py", line 144, in compile
    code = loader.source_to_code(source_bytes, dfile or file,
  File "<frozen importlib._bootstrap_external>", line 918, in source_to_code
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "./prog.py", line 23
    match direction:
          ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.9/py_compile.py", line 150, in compile
    raise py_exc
py_compile.PyCompileError:   File "./prog.py", line 23
    match direction:
          ^
SyntaxError: invalid syntax

stdout
Standard output is empty