3
# Copyright (C) 2005, 2008 Laurent Lacheny
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, see <http://www.gnu.org/licenses/>.
19
# This code comes from the project 4stattack
20
# http://forcedattack.sourceforge.net/
22
#########################################################################
24
#########################################################################
26
# Developer - "slm" - Jeroen Vloothuis #
27
# Graphics - "Korruptor" - Gareth Noyce #
28
# Music - "theGREENzebra" #
29
#########################################################################
31
# chakie(Jan Elkholm) - letting me "embrace and extend" his gui lib #
32
# Mighty(Xander Soldaat) - for the Makefile and the Debian packages #
33
# Han - for the rpms #
34
# jk - for the FreeBSD port #
35
# Tjerk Nan - for the Windows version #
36
# Micon - for the webdesign #
37
# Everyone in #pygame and the opensource community in general #
38
#########################################################################
39
# This software is licensed under the GPL - General Public License #
40
#########################################################################
44
# Checks if the move is legal
45
def isMoveLegal(board, selector_pos):
46
return len(board.state[selector_pos]) < 6 and selector_pos >= 0
48
def isBoardFull(board):
50
if len(board.state[pos]) < 6:
54
def isWinner(board, player):
55
result = _isVerticalWin(board, player)
57
result = _isHorizontalWin(board, player)
59
result = _isDiagonalWin(board, player)
62
def _isVerticalWin(board, player):
64
four_in_a_row = [player, player, player, player]
65
if board.state[x][-4:] == four_in_a_row:
66
return ((x,len(board.state[x])-4),(x,len(board.state[x])-1))
70
def _isHorizontalWin(board, player):
72
y = len(board.state[x]) - 1
73
four_in_a_row = [player, player, player, player]
77
row.append(board.state[i][y])
79
row.append('s') # 's' stands for sentinel
82
if row[x - 3 + i : x + 1 +i] == four_in_a_row:
83
return ((x -3 + i,y),( x +i,y))
89
def _isDiagonalWin(board, player):
91
y = len(board.state[x]) - 1
92
four_in_a_row = [player, player, player, player]
94
for i in range(-3, 4):
96
if (x+i < 0) or (y+i < 0):
97
row.append('s') # 's' stands for sentinel
99
row.append(board.state[x+i][y+i])
101
row.append('s') # 's' stands for sentinel
104
if row[i : i + 4] == four_in_a_row:
105
return ((x -3 + i,y-3+i),( x +i,y+i))
110
for i in range(-3, 4):
112
if (x+i < 0) or (y-i < 0):
113
row.append('s') # 's' stands for sentinel
115
row.append(board.state[x+i][y-i])
117
row.append('s') # 's' stands for sentinel
120
if row[i : i + 4] == four_in_a_row:
121
return ((x -3 + i , y + 3 - i),( x +i , y - i))