Back to index

gcompris  8.2.2
Public Member Functions | Public Attributes | Static Public Attributes
connect4p.minmax.MinMax Class Reference
Inheritance diagram for connect4p.minmax.MinMax:
Inheritance graph
[legend]
Collaboration diagram for connect4p.minmax.MinMax:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def setDifficulty
def evaluate
def score
def makeBoard
def listMoves
def statespace
def doMove
def gameOver
def doMove

Public Attributes

 search_depth
 f
 difficulty

Static Public Attributes

string type = 'AI'

Detailed Description

Definition at line 68 of file minmax.py.


Constructor & Destructor Documentation

def connect4p.minmax.MinMax.__init__ (   self,
  difficulty,
  f 
)

Definition at line 71 of file minmax.py.

00071 
00072   def __init__(self, difficulty, f):
00073     self.search_depth = difficulty
00074     self.f = f


Member Function Documentation

def connect4p.player.Player.doMove (   self) [inherited]

Definition at line 48 of file player.py.

00048 
00049   def doMove(self):
00050     return 0

def connect4p.minmax.MinMax.doMove (   self,
  current_board,
  player,
  event 
)

Definition at line 139 of file minmax.py.

00139 
00140   def doMove(self, current_board, player, event):
00141     board = copy.deepcopy(current_board)
00142 
00143     if player == 1: opponent = 2
00144     else: opponent = 1
00145 
00146     node = Node(board, 0, player)
00147 
00148     node =  self.statespace( node, 0, player, player, opponent);
00149 
00150     bestscore = -100000
00151     best_moves = []
00152     #print "New Round"
00153     for child in node.childs:
00154       #print "Child value=", child.value, "move=", child.board.last_move
00155       if child.value >= bestscore:
00156         #if rules.isMoveLegal(board, child.board.last_move):
00157         if bestscore == child.value:
00158           #print "Move added"
00159           best_moves.append(child.move)
00160         else:
00161           #print "New best move"
00162           bestscore = child.value
00163           best_moves = [child.move]
00164     return best_moves[int(random()*len(best_moves))]

Here is the call graph for this function:

def connect4p.minmax.MinMax.evaluate (   self,
  node,
  player,
  opponent,
  depth 
)

Definition at line 78 of file minmax.py.

00078 
00079   def evaluate(self, node, player, opponent, depth):
00080     if len(node.childs) > 0:
00081       list = []
00082       for child in node.childs:
00083         list.append(child.value)
00084       if node.player != player:
00085         node.value = min(list)
00086         #print 'min =', node.value,
00087       else:
00088         node.value = max(list)
00089         #print 'max =', node.value,
00090 
00091     else:
00092       node.value = self.score(node, player, opponent)  / (depth + 1)

Here is the call graph for this function:

Here is the caller graph for this function:

def connect4p.minmax.MinMax.gameOver (   self,
  move 
)

Definition at line 165 of file minmax.py.

00165 
00166   def gameOver(self, move):
00167     return None
00168 #try:
00169 #  psyco.bind(MinMax)
00170 #  psyco.bind(Node)
00171 #except:
00172 #  pass
def connect4p.minmax.MinMax.listMoves (   self,
  board,
  player 
)

Definition at line 102 of file minmax.py.

00102 
00103   def listMoves(self, board, player):
00104     checkmove = rules.isMoveLegal
00105 
00106     options = []
00107     for move in range(7):
00108       if checkmove(board, move):
00109         options.append(move)
00110     return options

Here is the caller graph for this function:

def connect4p.minmax.MinMax.makeBoard (   self,
  move,
  board,
  player 
)

Definition at line 97 of file minmax.py.

00097 
00098   def makeBoard(self, move, board, player):
00099     temp_board = copy.deepcopy(board)
00100     temp_board.move(move, player)
00101     return temp_board

def connect4p.minmax.MinMax.score (   self,
  node,
  player,
  opponent 
)

Definition at line 93 of file minmax.py.

00093 
00094   def score(self, node, player, opponent):
00095     return int(random() * 100) - 50
00096 

Here is the caller graph for this function:

def connect4p.minmax.MinMax.setDifficulty (   self,
  difficulty 
)

Reimplemented from connect4p.player.Player.

Definition at line 75 of file minmax.py.

00075 
00076   def setDifficulty(self, difficulty):
00077     self.search_depth = difficulty

def connect4p.minmax.MinMax.statespace (   self,
  node,
  depth,
  current_player,
  player,
  opponent 
)

Definition at line 111 of file minmax.py.

00111 
00112   def statespace(self, node, depth, current_player, player, opponent):
00113     self.f() 
00114     if rules.isWinner(node.board, opponent):
00115       #print "a lose up ahead"
00116       node.value = -10000 + depth
00117       return node
00118     elif rules.isWinner(node.board, player):
00119       node.value = 10000 - depth
00120       return node
00121 
00122     elif self.listMoves(node.board, 0) < 1:
00123       self.evaluate(node, player, opponent, depth)
00124       return node
00125     elif depth < self.search_depth:
00126       if current_player==2:
00127         next_player = 1
00128       else:
00129         next_player = 2
00130       for move in self.listMoves(node.board, current_player):
00131         node.board.move(move, current_player)
00132 
00133         node.childs.append(self.statespace(Node( node.board, move, next_player), depth+1, next_player, player, opponent))
00134         node.board.undomove(move)
00135     self.evaluate(node, player, opponent, depth)
00136     #print 'v=%d p=%d' %(node.value,node.player)
00137     return node
00138 

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 52 of file player.py.

Definition at line 73 of file minmax.py.

Definition at line 72 of file minmax.py.

string connect4p.minmax.MinMax.type = 'AI' [static]

Reimplemented from connect4p.player.Player.

Definition at line 69 of file minmax.py.


The documentation for this class was generated from the following file: