Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: utf-8
- # A003:盤面ゲーム
- # 8*8=64の少ない数なので、盤面すべての石の状態を覚えておけばよさそう
- set_num = int(input()) # 石が置かれた回数
- all_set_log = [[str(j) for j in input().split()] for i in range(set_num)]
- #print("set_num",set_num)
- #print("all_set_log",all_set_log)
- board_status_white = set([(4,4),(5,5)])
- board_status_black = set([(5,4),(4,5)])
- #board_status_white = board_status_white | set([(3,2)])
- #board_status_black = board_status_black | set([(2,1)])
- #print("board_status_white0",board_status_white)
- #print("board_status_black0",board_status_black)
- # 現在の盤面の状態と、次に配置する石の情報を入力し、配置後の盤面を出力する関数
- def board_result(board_status_white, board_status_black, set_log):
- # 配置した石の色をチェック
- s_color = set_log[0]
- s_x = int(set_log[1])
- s_y = int(set_log[2])
- up_left = (s_x - 1, s_y - 1)
- up = (s_x, s_y - 1)
- up_right = (s_x + 1, s_y - 1)
- center_left = (s_x - 1, s_y)
- center = (s_x, s_y)
- center_right = (s_x + 1, s_y)
- down_left = (s_x - 1, s_y + 1)
- down = (s_x, s_y + 1)
- down_right = (s_x + 1, s_y + 1)
- tmp = set()
- if set_log[0] == "B":#設置した石が黒の場合
- other_board_status = board_status_white
- self_board_status = board_status_black
- board_status_black.add(center)
- else:
- other_board_status = board_status_black
- self_board_status = board_status_white
- board_status_white.add(center)
- # 上下左右、ななめの8方向に隣接する場所に石が配置されているかをチェック
- # 石が配置されている場合はその色をチェック
- # 黒の場合はスルー。白の場合は、同方向の一つ先の色を黒に当たるまでチェックする
- # 黒に当たったらそこまでの白石を裏返す
- # もし黒に当たらず盤面の端まで行きついたならばスルー。
- for direction in [up_left, up, up_right, center_left, center_right, down_left, down, down_right]:
- if direction in other_board_status:
- i = 1
- while(True):
- if direction == up_left:
- i_x = -i
- i_y = -i
- elif direction == up:
- i_x = 0
- i_y = -i
- elif direction == up_right:
- i_x = i
- i_y = -i
- elif direction == center_left:
- i_x = -i
- i_y = 0
- elif direction == center_right:
- i_x = i
- i_y = 0
- elif direction == down_left:
- i_x = -i
- i_y = i
- elif direction == down:
- i_x = 0
- i_y = i
- else:
- i_x = i
- i_y = i
- if (s_x + i_x, s_y + i_y) in other_board_status: # 白(黒)が続く限り探索
- tmp.add((s_x + i_x, s_y + i_y))
- #print("tmp",tmp)
- elif (s_x + i_x, s_y + i_y) in self_board_status: # 黒になった場合はその場所までの白石を裏返す
- for tmp_stone in tmp:
- other_board_status.remove(tmp_stone) # 盤面の該当する白石を削除し
- self_board_status.add(tmp_stone) # 黒石を追加する
- tmp = set()
- break
- else:#石が置いていない
- tmp = set()
- break
- i += 1
- else:
- pass
- for set_log in all_set_log:
- board_result(board_status_white, board_status_black, set_log)
- """
- for i in range(1,9):
- for j in range(1,9):
- if (j,i) in board_status_white:
- print("○",end="")
- elif (j,i) in board_status_black:
- print("●",end="")
- else:
- print("□",end="")
- if j == 8:
- print("")
- else:
- print("----------")
- """
- #print("board_status_white",board_status_white)
- #print("board_status_black",board_status_black)
- w_num = len(board_status_white)
- b_num = len(board_status_black)
- print(str(b_num).zfill(2) + "-" + str(w_num).zfill(2) + " ",end = "")
- if w_num > b_num:
- print("The white won!")
- elif w_num < b_num:
- print("The black won!")
- else:
- print("Draw!")
- #print("board_status_white",board_status_white)
- #print("board_status_black",board_status_black)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement