from enum import IntEnum from gameconstants import Cells from gameconstants import Turn import os class InputResult(IntEnum): MOVE = 1 PASS = 2 QUIT = 3 class GameUI: def __init__(self, board): self.empty_char = "." self.first_char = "@" self.second_char = "O" self.x_labels = "abcdefgh" self.y_labels = "12345678" self.first_turn_label = "FIRST" self.second_turn_label = "SECOND" self.user_turn_label = "UNKNOWN" self._board = board self.first_count = 0 self.second_count = 0 # ----------------------------- # コンソールクリア # ----------------------------- def clear_screen(self): os.system("cls" if os.name == "nt" else "clear") # ----------------------------- # 盤面表示 # ----------------------------- def draw_board(self): first_count = 0 second_count = 0 self.clear_screen() print(f" {self.x_labels}") for y in range(0,8): line = self.y_labels[y] for x in range(1,9): cell = self._board.get_gameboard_cell(x,y+1) if cell == Cells.BLANK : line += self.empty_char elif cell == Cells.FIRST_CHIP : first_count += 1 line += self.first_char elif cell == Cells.SECOND_CHIP : second_count += 1 line += self.second_char print(line) self.first_count = first_count self.second_count = second_count print() print(f"{self.first_turn_label} : {first_count} , {self.second_turn_label} : {second_count}") return # ----------------------------- # 終了画面 # ----------------------------- def draw_finish_screen(self) : print("*** GAME OVER ***") print(f"{self.first_turn_label} : {self.first_count} , {self.second_turn_label} : {self.second_count}") if self.first_count == self.second_count : print(f"draw game.") elif self.first_count > self.second_count : print("WINNER: {self.first_turn_label}!") else : print("WINNER: {self.second_turn_label}!") return # ----------------------------- # 先手後手選択 # ----------------------------- def select_side(self): while True: text = input("先手(f) / 後手(s) ? > ").strip().lower() if text == "f": self.user_turn_label = self.first_turn_label return Turn.FIRST if text == "s": self.user_turn_label = self.second_turn_label return Turn.SECOND if text == "q": return Turn.QUIT # ----------------------------- # 座標入力 # ----------------------------- def input_position(self, allow_pass=False): while True: text = input(f"{self.user_turn_label} 座標 > ").strip().lower() # 中断 if "q" in text: return (InputResult.QUIT, None) # パス if "z" in text: if allow_pass: return (InputResult.PASS, None) print("現在パスはできません") continue pos = self.parse_position(text) if pos is None: print("入力形式エラー") continue return (InputResult.MOVE, pos) # ----------------------------- # 座標文字列解析 # ----------------------------- def parse_position(self, text): parts = text.split(",") if len(parts) != 2: return None a = parts[0].strip() b = parts[1].strip() # a,1 result = self._parse_xy(a, b) if result is not None: return result # 1,a result = self._parse_xy(b, a) return result # ----------------------------- # x,y解析 # ----------------------------- def _parse_xy(self, x_text, y_text): if x_text not in self.x_labels: return None if y_text not in self.y_labels: return None x = self.x_labels.index(x_text) + 1 y = int(y_text) return (x, y)