Back to index

gcompris  8.2.2
Functions
connect4p.rules Namespace Reference

Functions

def isMoveLegal
def isBoardFull
def isWinner
def _isVerticalWin
def _isHorizontalWin
def _isDiagonalWin

Function Documentation

def connect4p.rules._isDiagonalWin (   board,
  player 
) [private]

Definition at line 92 of file rules.py.

00092 
00093 def _isDiagonalWin(board, player):
00094         x = board.last_move
00095         y = len(board.state[x]) - 1
00096         four_in_a_row = [player, player, player, player]
00097         row = []
00098         for i in range(-3, 4):
00099                 try:
00100                      if  (x+i < 0) or (y+i < 0):
00101                             row.append('s')  # 's' stands for sentinel
00102                      else: 
00103                             row.append(board.state[x+i][y+i])
00104                 except IndexError:
00105                         row.append('s')  # 's' stands for sentinel
00106        for i in range(4):
00107          try:
00108              if row[i : i + 4] == four_in_a_row:
00109                return ((x -3 + i,y-3+i),( x +i,y+i))
00110          except IndexError:
00111             pass
00112 
00113         row = []
00114         for i in range(-3, 4):
00115                 try:
00116                      if  (x+i < 0) or (y-i < 0):
00117                             row.append('s')  # 's' stands for sentinel
00118                      else: 
00119                             row.append(board.state[x+i][y-i])
00120                 except IndexError:
00121                         row.append('s')  # 's' stands for sentinel
00122        for i in range(4):
00123          try:
00124              if row[i : i + 4] == four_in_a_row:
00125                return ((x -3 + i , y + 3 - i),( x +i , y - i))
00126          except IndexError:
00127             pass
00128        
00129         return None

Here is the caller graph for this function:

def connect4p.rules._isHorizontalWin (   board,
  player 
) [private]

Definition at line 73 of file rules.py.

00073 
00074 def _isHorizontalWin(board, player):
00075         x = board.last_move
00076         y = len(board.state[x]) - 1
00077         four_in_a_row = [player, player, player, player]
00078         row = []
00079         for i in range(7):
00080                 try:
00081                         row.append(board.state[i][y])
00082                 except IndexError:
00083                         row.append('s')  # 's' stands for sentinel
00084        for i in range(4):
00085          try:
00086              if row[x - 3 + i : x + 1 +i] == four_in_a_row:
00087                return ((x -3 + i,y),( x +i,y))
00088          except IndexError:
00089             pass
00090             
00091         return None

Here is the caller graph for this function:

def connect4p.rules._isVerticalWin (   board,
  player 
) [private]

Definition at line 65 of file rules.py.

00065 
00066 def _isVerticalWin(board, player):
00067    x = board.last_move
00068    four_in_a_row = [player, player, player, player]
00069    if board.state[x][-4:] == four_in_a_row:
00070       return ((x,len(board.state[x])-4),(x,len(board.state[x])-1))
00071    else:
00072       return None

Here is the caller graph for this function:

def connect4p.rules.isBoardFull (   board)

Definition at line 51 of file rules.py.

00051 
00052 def isBoardFull(board):
00053        for pos in range(7):
00054               if len(board.state[pos]) < 6:
00055                      return 0 
00056        return 1

def connect4p.rules.isMoveLegal (   board,
  selector_pos 
)

Definition at line 48 of file rules.py.

00048 
00049 def isMoveLegal(board, selector_pos):
00050        return len(board.state[selector_pos]) < 6 and selector_pos >= 0

def connect4p.rules.isWinner (   board,
  player 
)

Definition at line 57 of file rules.py.

00057 
00058 def isWinner(board, player):
00059        result = _isVerticalWin(board, player)
00060         if not result:
00061            result =  _isHorizontalWin(board, player)
00062        if not result:
00063           result = _isDiagonalWin(board, player)
00064         return result

Here is the call graph for this function: