Skip to content

Instantly share code, notes, and snippets.

@gyosit
Created February 24, 2023 13:43
Show Gist options
  • Save gyosit/ee579d4fc92f394fee1ae58daacb4653 to your computer and use it in GitHub Desktop.
Save gyosit/ee579d4fc92f394fee1ae58daacb4653 to your computer and use it in GitHub Desktop.

Untitled

import numpy as np

class GoBoard:
    def __init__(self, size):
        self.size = size
        self.board = np.zeros((size, size))
        self.current_player = 1
        self.captured_stones = {1: 0, 2: 0}
    
    def play(self, move):
        x, y = move
        if self.board[x][y] != 0:
            return False
        
        self.board[x][y] = self.current_player
        
        # Check if any stones are captured
        for i, j in [(1,0), (-1,0), (0,1), (0,-1)]:
            captured = self._capture_stones(x+i, y+j, [])
            if captured:
                for stone in captured:
                    self.board[stone[0]][stone[1]] = 0
                    self.captured_stones[3 - self.current_player] += 1
        
        self.current_player = 3 - self.current_player
        
        return True
    
    def _capture_stones(self, x, y, captured):
        if x < 0 or x >= self.size or y < 0 or y >= self.size:
            return captured
        
        if self.board[x][y] == 0:
            return []
        
        if (x, y) in captured:
            return []
        
        color = self.board[x][y]
        group = [(x, y)]
        liberties = []
        for i, j in [(1,0), (-1,0), (0,1), (0,-1)]:
            neighbor_x, neighbor_y = x+i, y+j
            if neighbor_x < 0 or neighbor_x >= self.size or neighbor_y < 0 or neighbor_y >= self.size:
                continue
            if self.board[neighbor_x][neighbor_y] == 0:
                liberties.append((neighbor_x, neighbor_y))
            elif self.board[neighbor_x][neighbor_y] == color:
                group += self._capture_stones(neighbor_x, neighbor_y, captured)
        
        if not liberties:
            captured += group
        
        return captured
    
    def display(self):
        for row in self.board:
            print(row)
        print("Captured stones: Player 1: {}, Player 2: {}".format(self.captured_stones[1], self.captured_stones[2]))
            
    def get_captured_stones(self, player):
        return self.captured_stones[player]
            
def get_input(size):
    while True:
        try:
            move = input("Enter a move (e.g. '3,4'): ")
            x, y = map(int, move.split(","))
            if x < 0 or x >= size or y < 0 or y >= size:
                print("Invalid move.")
            else:
                return (x, y)
        except ValueError:
            print("Invalid input.")

if __name__ == "__main__":
    size = 9
    game = GoBoard(size)
    
    while True:
        game.display()
        move = get_input(size)
        if not game.play(move):
            print("Invalid move.")
        else:
            print("Valid move.")

このコードは、Pythonで書かれた囲碁のゲームです。GoBoardクラスは、ボードのサイズ、現在のプレイヤー、キャプチャされた石の数などを追跡します。playメソッドは、プレイヤーが石を置くことができるかどうかを確認し、石を置いた場合には、キャプチャされた石を処理します。_capture_stonesメソッドは、石が取られた場合に、その石を取り除き、キャプチャされた石の数を更新します。displayメソッドは、現在のボード状態を表示します。get_inputメソッドは、プレイヤーに移動を入力させます。

このコードは、Pythonの基本的なクラスやメソッドを使用しています。Pythonのリストや辞書、forループなどが使われています。また、NumPyパッケージを使用して、2次元のゼロ行列を作成するとともに、2次元配列を操作しています。

このコードを実行すると、9x9の囲碁盤が表示されます。プレイヤー1は黒で、プレイヤー2は白です。プレイヤーは、標準入力を使用して、有効な移動を入力することができます。プレイヤーが無効な移動を入力した場合、メッセージが表示されます。正しい移動を入力した場合、Valid move.というメッセージが表示され、次のプレイヤーに移ります。

このコードには、囲碁のルールに関する説明は含まれていません。したがって、囲碁のルールをある程度理解している人を対象としています。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment