-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
70 lines (53 loc) · 1.7 KB
/
agent.py
File metadata and controls
70 lines (53 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import board
import random
#####################
# Agent definitions #
#####################
##################
# Abstract agent #
##################
class Agent(object):
"""Abstract agent class"""
# Class constructor.
#
# PARAM [string] name: the name of this player
def __init__(self, name):
"""Class constructor"""
# Agent name
self.name = name
# Uninitialized player - will be set upon starting a Game
self.player = 0
# Pick a column.
#
# PARAM [board.Board] brd: the current board state
# RETURN [int]: the column where the token must be added
def go(self, brd):
"""Returns a column between 0 and (brd.w-1). The column must be free in the board."""
raise NotImplementedError("Please implement this method")
##########################
# Randomly playing agent #
##########################
class RandomAgent(Agent):
"""Randomly playing agent"""
# Pick a column at random.
#
# PARAM [board.Board] brd: the current board state
# RETURN [int]: the column where the token must be added
def go(self, brd):
return random.choice(brd.free_cols())
#####################
# Interactive Agent #
#####################
class InteractiveAgent(Agent):
"""Interactive player"""
# Ask a human to pick a column.
#
# PARAM [board.Board] brd: the current board state (ignored)
# RETURN [int]: the column where the token must be added
def go(self, brd):
freecols = brd.free_cols()
col = int(input("Which column? "))
while not col in freecols:
print("Can't place a token in column", col)
col = int(input("Which column? "))
return col