Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/boards/python/connect4p/rules.py
blob: 6c06f9a1d01d4c22fbffdf7d08747d582cf5e0c9 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#  gcompris - connect4 
# 
# Time-stamp: 
# 
# Copyright (C) 2005 Laurent Lacheny 
# 
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
# 
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
# 
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#
# This code comes from the project 4stattack
# http://forcedattack.sourceforge.net/
#
#########################################################################
#                            4st Attack 2                               #
#########################################################################
# Created by:                                                           #
# Developer            - "slm" - Jeroen Vloothuis                       #
# Graphics             - "Korruptor" - Gareth Noyce                     #
# Music                - "theGREENzebra"                                #
#########################################################################
# Specail thanks:                                                       #
# chakie(Jan Elkholm)    - letting me "embrace and extend" his gui lib  #
# Mighty(Xander Soldaat) - for the Makefile and the Debian packages     #
# Han                    - for the rpms                                 #
# jk                     - for the FreeBSD port                         #
# Tjerk Nan              - for the Windows version                      #
# Micon                  - for the webdesign                            #
# Everyone in #pygame and the opensource community in general           #
#########################################################################
# This software is licensed under the GPL - General Public License      #
#########################################################################

from board import *

# Checks if the move is legal
def isMoveLegal(board, selector_pos):
	return len(board.state[selector_pos]) < 6 and selector_pos >= 0

def isBoardFull(board):
	for pos in range(7):
		if len(board.state[pos]) < 6:
			return 0 
	return 1

def isWinner(board, player):
	result = _isVerticalWin(board, player)
        if not result:
           result =  _isHorizontalWin(board, player)
	if not result:
	   result = _isDiagonalWin(board, player)
        return result

def _isVerticalWin(board, player):
   x = board.last_move
   four_in_a_row = [player, player, player, player]
   if board.state[x][-4:] == four_in_a_row:
      return ((x,len(board.state[x])-4),(x,len(board.state[x])-1))
   else:
      return None

def _isHorizontalWin(board, player):
        x = board.last_move
        y = len(board.state[x]) - 1
        four_in_a_row = [player, player, player, player]
        row = []
        for i in range(7):
                try:
                        row.append(board.state[i][y])
                except IndexError:
                        row.append('s')  # 's' stands for sentinel
	for i in range(4):
	  try:
             if row[x - 3 + i : x + 1 +i] == four_in_a_row:
	        return ((x -3 + i,y),( x +i,y))
	  except IndexError:
	     pass
	     
        return None

def _isDiagonalWin(board, player):
        x = board.last_move
        y = len(board.state[x]) - 1
        four_in_a_row = [player, player, player, player]
        row = []
        for i in range(-3, 4):
                try:
			if  (x+i < 0) or (y+i < 0):
				row.append('s')  # 's' stands for sentinel
			else: 
				row.append(board.state[x+i][y+i])
                except IndexError:
                        row.append('s')  # 's' stands for sentinel
	for i in range(4):
	  try:
             if row[i : i + 4] == four_in_a_row:
	        return ((x -3 + i,y-3+i),( x +i,y+i))
	  except IndexError:
	     pass

        row = []
        for i in range(-3, 4):
                try:
			if  (x+i < 0) or (y-i < 0):
				row.append('s')  # 's' stands for sentinel
			else: 
				row.append(board.state[x+i][y-i])
                except IndexError:
                        row.append('s')  # 's' stands for sentinel
	for i in range(4):
	  try:
             if row[i : i + 4] == four_in_a_row:
	        return ((x -3 + i , y + 3 - i),( x +i , y - i))
	  except IndexError:
	     pass
	
        return None