Back to index

gcompris  8.2.2
rules.py
Go to the documentation of this file.
00001 #  gcompris - connect4 
00002 # 
00003 # Time-stamp: 
00004 # 
00005 # Copyright (C) 2005 Laurent Lacheny 
00006 # 
00007 #   This program is free software; you can redistribute it and/or modify
00008 #   it under the terms of the GNU General Public License as published by
00009 #   the Free Software Foundation; either version 2 of the License, or
00010 #   (at your option) any later version.
00011 # 
00012 #   This program is distributed in the hope that it will be useful,
00013 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 #   GNU General Public License for more details.
00016 # 
00017 #   You should have received a copy of the GNU General Public License
00018 #   along with this program; if not, write to the Free Software
00019 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021 #
00022 # This code comes from the project 4stattack
00023 # http://forcedattack.sourceforge.net/
00024 #
00025 #########################################################################
00026 #                            4st Attack 2                               #
00027 #########################################################################
00028 # Created by:                                                           #
00029 # Developer            - "slm" - Jeroen Vloothuis                       #
00030 # Graphics             - "Korruptor" - Gareth Noyce                     #
00031 # Music                - "theGREENzebra"                                #
00032 #########################################################################
00033 # Specail thanks:                                                       #
00034 # chakie(Jan Elkholm)    - letting me "embrace and extend" his gui lib  #
00035 # Mighty(Xander Soldaat) - for the Makefile and the Debian packages     #
00036 # Han                    - for the rpms                                 #
00037 # jk                     - for the FreeBSD port                         #
00038 # Tjerk Nan              - for the Windows version                      #
00039 # Micon                  - for the webdesign                            #
00040 # Everyone in #pygame and the opensource community in general           #
00041 #########################################################################
00042 # This software is licensed under the GPL - General Public License      #
00043 #########################################################################
00044 
00045 from board import *
00046 
00047 # Checks if the move is legal
00048 def isMoveLegal(board, selector_pos):
00049        return len(board.state[selector_pos]) < 6 and selector_pos >= 0
00050 
00051 def isBoardFull(board):
00052        for pos in range(7):
00053               if len(board.state[pos]) < 6:
00054                      return 0 
00055        return 1
00056 
00057 def isWinner(board, player):
00058        result = _isVerticalWin(board, player)
00059         if not result:
00060            result =  _isHorizontalWin(board, player)
00061        if not result:
00062           result = _isDiagonalWin(board, player)
00063         return result
00064 
00065 def _isVerticalWin(board, player):
00066    x = board.last_move
00067    four_in_a_row = [player, player, player, player]
00068    if board.state[x][-4:] == four_in_a_row:
00069       return ((x,len(board.state[x])-4),(x,len(board.state[x])-1))
00070    else:
00071       return None
00072 
00073 def _isHorizontalWin(board, player):
00074         x = board.last_move
00075         y = len(board.state[x]) - 1
00076         four_in_a_row = [player, player, player, player]
00077         row = []
00078         for i in range(7):
00079                 try:
00080                         row.append(board.state[i][y])
00081                 except IndexError:
00082                         row.append('s')  # 's' stands for sentinel
00083        for i in range(4):
00084          try:
00085              if row[x - 3 + i : x + 1 +i] == four_in_a_row:
00086                return ((x -3 + i,y),( x +i,y))
00087          except IndexError:
00088             pass
00089             
00090         return None
00091 
00092 def _isDiagonalWin(board, player):
00093         x = board.last_move
00094         y = len(board.state[x]) - 1
00095         four_in_a_row = [player, player, player, player]
00096         row = []
00097         for i in range(-3, 4):
00098                 try:
00099                      if  (x+i < 0) or (y+i < 0):
00100                             row.append('s')  # 's' stands for sentinel
00101                      else: 
00102                             row.append(board.state[x+i][y+i])
00103                 except IndexError:
00104                         row.append('s')  # 's' stands for sentinel
00105        for i in range(4):
00106          try:
00107              if row[i : i + 4] == four_in_a_row:
00108                return ((x -3 + i,y-3+i),( x +i,y+i))
00109          except IndexError:
00110             pass
00111 
00112         row = []
00113         for i in range(-3, 4):
00114                 try:
00115                      if  (x+i < 0) or (y-i < 0):
00116                             row.append('s')  # 's' stands for sentinel
00117                      else: 
00118                             row.append(board.state[x+i][y-i])
00119                 except IndexError:
00120                         row.append('s')  # 's' stands for sentinel
00121        for i in range(4):
00122          try:
00123              if row[i : i + 4] == four_in_a_row:
00124                return ((x -3 + i , y + 3 - i),( x +i , y - i))
00125          except IndexError:
00126             pass
00127        
00128         return None