ChatGPTにpythonで書かれたオセロのゲームのコードを作ってもらいました。
生成されたコード
class Othello:
def __init__(self):
self.board = [[' ' for _ in range(8)] for _ in range(8)]
self.board[3][3] = self.board[4][4] = 'W'
self.board[3][4] = self.board[4][3] = 'B'
self.current_turn = 'B'
def print_board(self):
print(' A B C D E F G H')
for i in range(8):
print(str(i+1), end=' ')
for j in range(8):
print(self.board[i][j], end=' ')
print()
def is_valid_move(self, x, y, color):
if self.board[x][y] != ' ':
return False
opponent = 'W' if color == 'B' else 'B'
directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
valid = False
for dx, dy in directions:
nx, ny = x + dx, y + dy
has_opponent = False
while 0 <= nx < 8 and 0 <= ny < 8:
if self.board[nx][ny] == opponent:
has_opponent = True
elif self.board[nx][ny] == color:
if has_opponent:
valid = True
break
else:
break
nx += dx
ny += dy
return valid
def make_move(self, x, y, color):
if not self.is_valid_move(x, y, color):
return False
opponent = 'W' if color == 'B' else 'B'
self.board[x][y] = color
directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
for dx, dy in directions:
nx, ny = x + dx, y + dy
to_flip = []
while 0 <= nx < 8 and 0 <= ny < 8:
if self.board[nx][ny] == opponent:
to_flip.append((nx, ny))
elif self.board[nx][ny] == color:
for fx, fy in to_flip:
self.board[fx][fy] = color
break
else:
break
nx += dx
ny += dy
return True
def has_valid_move(self, color):
for i in range(8):
for j in range(8):
if self.is_valid_move(i, j, color):
return True
return False
def switch_turn(self):
self.current_turn = 'W' if self.current_turn == 'B' else 'B'
def count_pieces(self):
b_count = sum(row.count('B') for row in self.board)
w_count = sum(row.count('W') for row in self.board)
return b_count, w_count
def play(self):
while True:
self.print_board()
b_count, w_count = self.count_pieces()
print(f'B: {b_count} W: {w_count}')
if not self.has_valid_move(self.current_turn):
self.switch_turn()
if not self.has_valid_move(self.current_turn):
print('Game Over')
if b_count > w_count:
print('Black wins!')
elif w_count > b_count:
print('White wins!')
else:
print('Draw!')
break
print(f'{self.current_turn} has no valid moves, switching turn.')
move = input(f'{self.current_turn} move (e.g., D3): ')
if len(move) != 2 or move[0] not in 'ABCDEFGH' or move[1] not in '12345678':
print('Invalid move format')
continue
x, y = int(move[1]) - 1, ord(move[0]) - ord('A')
if not self.make_move(x, y, self.current_turn):
print('Invalid move')
continue
self.switch_turn()
if __name__ == '__main__':
game = Othello()
game.play()
実行
$ python Othello.py
プレイ画面
W : 白
B : 黒
です。
A〜Hと1〜8のマスで、打ちたい場所を入力します。
例えばD3と打つと、C列 3行に打たれます。
A B C D E F G H
1
2
3
4 W B
5 B W
6
7
8
B: 2 W: 2
B move (e.g., D3): D3
A B C D E F G H
1
2
3 B
4 B B
5 B W
6
7
8
B: 4 W: 1
W move (e.g., D3): C5
A B C D E F G H
1
2
3 B
4 B B
5 W W W
6
7
8
B: 3 W: 3
終了画面
W move (e.g., D3): H8
A B C D E F G H
1 B B B B B B B B
2 B B W W B W B W
3 B B W B W B B W
4 B B B W B B B W
5 B B W B B W B W
6 B B B W B W B W
7 B B W B W B W W
8 B B B B B B B W
B: 44 W: 20
Game Over
Black wins!
以上。