Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. # coding: utf-8
  2. # A003:盤面ゲーム
  3.  
  4. # 8*8=64の少ない数なので、盤面すべての石の状態を覚えておけばよさそう
  5.  
  6.  
  7.  
  8. set_num = int(input()) # 石が置かれた回数
  9. all_set_log = [[str(j) for j in input().split()] for i in range(set_num)]
  10. #print("set_num",set_num)
  11. #print("all_set_log",all_set_log)
  12.  
  13.  
  14.  
  15. board_status_white = set([(4,4),(5,5)])
  16. board_status_black = set([(5,4),(4,5)])
  17. #board_status_white = board_status_white | set([(3,2)])
  18. #board_status_black = board_status_black | set([(2,1)])
  19. #print("board_status_white0",board_status_white)
  20. #print("board_status_black0",board_status_black)
  21. # 現在の盤面の状態と、次に配置する石の情報を入力し、配置後の盤面を出力する関数
  22. def board_result(board_status_white, board_status_black, set_log):
  23. # 配置した石の色をチェック
  24. s_color = set_log[0]
  25. s_x = int(set_log[1])
  26. s_y = int(set_log[2])
  27.  
  28. up_left = (s_x - 1, s_y - 1)
  29. up = (s_x, s_y - 1)
  30. up_right = (s_x + 1, s_y - 1)
  31. center_left = (s_x - 1, s_y)
  32. center = (s_x, s_y)
  33. center_right = (s_x + 1, s_y)
  34. down_left = (s_x - 1, s_y + 1)
  35. down = (s_x, s_y + 1)
  36. down_right = (s_x + 1, s_y + 1)
  37.  
  38.  
  39. tmp = set()
  40. if set_log[0] == "B":#設置した石が黒の場合
  41. other_board_status = board_status_white
  42. self_board_status = board_status_black
  43. board_status_black.add(center)
  44. else:
  45. other_board_status = board_status_black
  46. self_board_status = board_status_white
  47. board_status_white.add(center)
  48.  
  49. # 上下左右、ななめの8方向に隣接する場所に石が配置されているかをチェック
  50. # 石が配置されている場合はその色をチェック
  51. # 黒の場合はスルー。白の場合は、同方向の一つ先の色を黒に当たるまでチェックする
  52. # 黒に当たったらそこまでの白石を裏返す
  53. # もし黒に当たらず盤面の端まで行きついたならばスルー。
  54. for direction in [up_left, up, up_right, center_left, center_right, down_left, down, down_right]:
  55. if direction in other_board_status:
  56. i = 1
  57. while(True):
  58. if direction == up_left:
  59. i_x = -i
  60. i_y = -i
  61. elif direction == up:
  62. i_x = 0
  63. i_y = -i
  64. elif direction == up_right:
  65. i_x = i
  66. i_y = -i
  67. elif direction == center_left:
  68. i_x = -i
  69. i_y = 0
  70. elif direction == center_right:
  71. i_x = i
  72. i_y = 0
  73. elif direction == down_left:
  74. i_x = -i
  75. i_y = i
  76. elif direction == down:
  77. i_x = 0
  78. i_y = i
  79. else:
  80. i_x = i
  81. i_y = i
  82.  
  83. if (s_x + i_x, s_y + i_y) in other_board_status: # 白(黒)が続く限り探索
  84. tmp.add((s_x + i_x, s_y + i_y))
  85. #print("tmp",tmp)
  86. elif (s_x + i_x, s_y + i_y) in self_board_status: # 黒になった場合はその場所までの白石を裏返す
  87. for tmp_stone in tmp:
  88. other_board_status.remove(tmp_stone) # 盤面の該当する白石を削除し
  89. self_board_status.add(tmp_stone) # 黒石を追加する
  90. tmp = set()
  91. break
  92. else:#石が置いていない
  93. tmp = set()
  94. break
  95. i += 1
  96. else:
  97. pass
  98.  
  99.  
  100.  
  101.  
  102.  
  103. for set_log in all_set_log:
  104. board_result(board_status_white, board_status_black, set_log)
  105. """
  106. for i in range(1,9):
  107. for j in range(1,9):
  108. if (j,i) in board_status_white:
  109. print("○",end="")
  110. elif (j,i) in board_status_black:
  111. print("●",end="")
  112. else:
  113. print("□",end="")
  114.  
  115. if j == 8:
  116. print("")
  117. else:
  118. print("----------")
  119. """
  120. #print("board_status_white",board_status_white)
  121. #print("board_status_black",board_status_black)
  122. w_num = len(board_status_white)
  123. b_num = len(board_status_black)
  124.  
  125. print(str(b_num).zfill(2) + "-" + str(w_num).zfill(2) + " ",end = "")
  126. if w_num > b_num:
  127. print("The white won!")
  128. elif w_num < b_num:
  129. print("The black won!")
  130. else:
  131. print("Draw!")
  132.  
  133.  
  134. #print("board_status_white",board_status_white)
  135. #print("board_status_black",board_status_black)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement