Advertisement
Guest User

Untitled

a guest
Apr 19th, 2021
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. #v1.2
  2.  
  3. OBSTACLE = "+"
  4. EMPTY = "."
  5. MINE = "M"
  6. BEACON = "B"
  7.  
  8. raw_board="""\
  9. ++++++++++++++++++++
  10. +++++++..+++++++++++
  11. +++++++...++++.+++++
  12. ++++++.+..+++.....++
  13. ++++++.....+......++
  14. +++++++..++.......++
  15. ++....+..+++.+.+..++
  16. ++........+........+
  17. ++........+.++.+++++
  18. ++..........++.+++++
  19. ++..........++.+++++
  20. +++++..+..........++
  21. ++++++++++++...++.++
  22. ++++++++++++......++
  23. +.++++...+++......++
  24. +..+++...+++++++++++
  25. ++++++...+++++++++++
  26. ++++++++++++++++++++"""
  27.  
  28. #all functions assume that: a board's outermost rows/columns are entirely obstacles
  29.  
  30. def copy(board):
  31. #could just use deepcopy, but meh
  32. return [row[:] for row in board]
  33.  
  34. def iter_neighbors(board, x,y):
  35. """yield the tile type of every adjacent tile"""
  36.  
  37. #todo: make this work for border tiles
  38. assert 0 < y < len(board) - 1
  39. assert 0 < x < len(board[0]) - 1
  40.  
  41. for dx in (-1,0,1):
  42. for dy in (-1, 0, 1):
  43. if dx == dy == 0: continue
  44. yield board[dy+y][dx+x]
  45.  
  46. def calculate_score(board):
  47. """Returns the total score of the board."""
  48. def _score(x,y):
  49. """the score of this one tile."""
  50. if board[y][x] == MINE:
  51. return 100 + sum(40 for tile in iter_neighbors(board, x, y) if tile == BEACON)
  52. else:
  53. return 0
  54. return sum(_score(x,y) for y, row in enumerate(board) for x, _ in enumerate(row))
  55.  
  56. def display(board):
  57. """print the board."""
  58. print("\n".join("".join(row) for row in board))
  59.  
  60.  
  61. #example optimizers. Each one may mutate board in-place in an attempt to improve its score.
  62. def literally_nothing(board):
  63. pass
  64.  
  65. def fill_empty_with_mines(board):
  66. for y, row in enumerate(board):
  67. for x, tile in enumerate(row):
  68. if board[y][x] == EMPTY:
  69. board[y][x] = MINE
  70.  
  71. def optimize_with_hill_climb(board):
  72. """Make only simple strict improvements. Very likely to get stuck in local optima."""
  73.  
  74. #pass 1: fill up empty tiles
  75. fill_empty_with_mines(board)
  76.  
  77. #pass 2: replace a mine with a beacon iff doing so increases score
  78. cur_score = calculate_score(board)
  79. for y, row in enumerate(board):
  80. for x, tile in enumerate(row):
  81. if board[y][x] == MINE:
  82. board[y][x] = BEACON
  83. new_score = calculate_score(board)
  84. if new_score > cur_score:
  85. #keep change
  86. cur_score = new_score
  87. else:
  88. #revert
  89. board[y][x] = MINE
  90.  
  91. def sample_showcase():
  92. """showcase the performance of the sample optimizers."""
  93. starting_board = [list(row) for row in raw_board.strip().split("\n")]
  94. optimizers = [literally_nothing, fill_empty_with_mines, optimize_with_hill_climb]
  95. print("Starting board:")
  96. display(starting_board)
  97. print("\n")
  98.  
  99. for i, optimize_func in enumerate(optimizers, 1):
  100. board = copy(starting_board)
  101. print(f"Candidate #{i}: {optimize_func.__name__}")
  102. optimize_func(board)
  103. display(board)
  104. print("Score:", calculate_score(board))
  105. print(f"End of report for candidate #{i}.\n\n")
  106.  
  107. if __name__ == "__main__":
  108. sample_showcase()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement