~ubuntu-branches/ubuntu/maverick/gcompris/maverick

« back to all changes in this revision

Viewing changes to src/connect4-activity/connect4p/rules.py

  • Committer: Bazaar Package Importer
  • Author(s): Marc Gariepy, Marc Gariepy, Stephane Graber
  • Date: 2010-01-04 17:42:49 UTC
  • mfrom: (1.1.14 upstream)
  • Revision ID: james.westby@ubuntu.com-20100104174249-7bupatd9dtxyhvs4
Tags: 9.0-0ubuntu1
[Marc Gariepy]
* New upstream release (9.0).
* Remove cache.c from POTFILES to avoid FTBFS
* Remove unneeded rm in debian/rules (file no longer exists upstream)

[Stephane Graber]
* Bump Debian standards to 3.8.3
* Add patch system (dpatch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#  gcompris - connect4
 
2
#
 
3
# Copyright (C) 2005, 2008 Laurent Lacheny
 
4
#
 
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.
 
9
#
 
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.
 
14
#
 
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/>.
 
17
 
 
18
#
 
19
# This code comes from the project 4stattack
 
20
# http://forcedattack.sourceforge.net/
 
21
#
 
22
#########################################################################
 
23
#                            4st Attack 2                               #
 
24
#########################################################################
 
25
# Created by:                                                           #
 
26
# Developer            - "slm" - Jeroen Vloothuis                       #
 
27
# Graphics             - "Korruptor" - Gareth Noyce                     #
 
28
# Music                - "theGREENzebra"                                #
 
29
#########################################################################
 
30
# Specail thanks:                                                       #
 
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
#########################################################################
 
41
 
 
42
from board import *
 
43
 
 
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
 
47
 
 
48
def isBoardFull(board):
 
49
        for pos in range(7):
 
50
                if len(board.state[pos]) < 6:
 
51
                        return 0
 
52
        return 1
 
53
 
 
54
def isWinner(board, player):
 
55
        result = _isVerticalWin(board, player)
 
56
        if not result:
 
57
           result =  _isHorizontalWin(board, player)
 
58
        if not result:
 
59
           result = _isDiagonalWin(board, player)
 
60
        return result
 
61
 
 
62
def _isVerticalWin(board, player):
 
63
   x = board.last_move
 
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))
 
67
   else:
 
68
      return None
 
69
 
 
70
def _isHorizontalWin(board, player):
 
71
        x = board.last_move
 
72
        y = len(board.state[x]) - 1
 
73
        four_in_a_row = [player, player, player, player]
 
74
        row = []
 
75
        for i in range(7):
 
76
                try:
 
77
                        row.append(board.state[i][y])
 
78
                except IndexError:
 
79
                        row.append('s')  # 's' stands for sentinel
 
80
        for i in range(4):
 
81
          try:
 
82
             if row[x - 3 + i : x + 1 +i] == four_in_a_row:
 
83
                return ((x -3 + i,y),( x +i,y))
 
84
          except IndexError:
 
85
             pass
 
86
 
 
87
        return None
 
88
 
 
89
def _isDiagonalWin(board, player):
 
90
        x = board.last_move
 
91
        y = len(board.state[x]) - 1
 
92
        four_in_a_row = [player, player, player, player]
 
93
        row = []
 
94
        for i in range(-3, 4):
 
95
                try:
 
96
                        if  (x+i < 0) or (y+i < 0):
 
97
                                row.append('s')  # 's' stands for sentinel
 
98
                        else:
 
99
                                row.append(board.state[x+i][y+i])
 
100
                except IndexError:
 
101
                        row.append('s')  # 's' stands for sentinel
 
102
        for i in range(4):
 
103
          try:
 
104
             if row[i : i + 4] == four_in_a_row:
 
105
                return ((x -3 + i,y-3+i),( x +i,y+i))
 
106
          except IndexError:
 
107
             pass
 
108
 
 
109
        row = []
 
110
        for i in range(-3, 4):
 
111
                try:
 
112
                        if  (x+i < 0) or (y-i < 0):
 
113
                                row.append('s')  # 's' stands for sentinel
 
114
                        else:
 
115
                                row.append(board.state[x+i][y-i])
 
116
                except IndexError:
 
117
                        row.append('s')  # 's' stands for sentinel
 
118
        for i in range(4):
 
119
          try:
 
120
             if row[i : i + 4] == four_in_a_row:
 
121
                return ((x -3 + i , y + 3 - i),( x +i , y - i))
 
122
          except IndexError:
 
123
             pass
 
124
 
 
125
        return None