This commit is contained in:
2023-05-23 15:52:09 +08:00
parent e8f9c8a287
commit 663edbb5e2
88 changed files with 6250 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
# tag::randombotimports[]
import random
from tugo.agent.base import Agent
from tugo.agent.helpers import is_point_an_eye
from tugo.goboard_slow import Move
from tugo.gotypes import Point
# end::randombotimports[]
__all__ = ['RandomBot']
# tag::random_bot[]
class RandomBot(Agent):
def select_move(self, game_state):
"""Choose a random valid move that preserves our own eyes."""
candidates = []
for r in range(1, game_state.board.num_rows + 1):
for c in range(1, game_state.board.num_cols + 1):
candidate = Point(row=r, col=c)
if game_state.is_valid_move(Move.play(candidate)) and \
not is_point_an_eye(game_state.board,
candidate,
game_state.next_player):
candidates.append(candidate)
if not candidates:
return Move.pass_turn()
return Move.play(random.choice(candidates))
# end::random_bot[]