Module check_stone
[hide private]
[frames] | no frames]

Source Code for Module check_stone

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  #    check_stone.py 
  5  #        
  6  #    Copyright 2010 Victor Ramirez <virako.9@gmail.com> 
  7  #        
  8  #    This program is free software: you can redistribute it and/or modify 
  9  #    it under the terms of the GNU General Public License as published by 
 10  #    the Free Software Foundation, either version 3 of the License, or 
 11  #    (at your option) any later version. 
 12  # 
 13  #    This program is distributed in the hope that it will be useful, 
 14  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  #    GNU General Public License for more details. 
 17  # 
 18  #    You should have received a copy of the GNU General Public License 
 19  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 20   
 21   
 22  from cte import * 
 23   
 24   
25 -def check_stone(tablero, p, color): # la piedra puesta tiene que contenerla, pero no hace falta comprobarla
26 """Funcion recursiva que comprueba si la piedra pasada por parámetro está 27 muerta o esta viva. Ver reglas del go para más detalles. """ 28 x = p[0] 29 y = p[1] 30 31 # Recogemos los colores que tenemos alrededor, si no hay punto estamos en el borde y devolvemos True 32 33 # Comprobamos que color tenemos y hacemos su funcion 34 # Si es del mismo color, recursividad 35 # si es de distinto color, True 36 # Si está libre, False y nos salimos, la piedra no nos sirve 37 38 all_pos = ((0, -1), (0, 1), (-1, 0), (1, 0)) # u, d, l, r 39 bool_stone = True 40 41 for pos in all_pos: 42 try: 43 c = tablero.matriz[x - pos[0]][y - pos[1]][3] 44 if c == SPACE: 45 bool_stone = False 46 break 47 elif c != color: 48 new_p = (p[0] - pos[0], p[1] - pos[0]) 49 check_stone(tablero, new_p, color) 50 51 except IndexError: 52 bool_stone = False 53 break 54 55 return bool_stone 56 57 """ 58 try: 59 u = self.tablero.matriz[x][y][3] 60 if u == color: 61 bool_u = True 62 elif (u == SPACE): 63 bool_u = False 64 return False 65 else: 66 new_p = (p[0], p[1]-1) 67 check_stone(new_p, color) 68 except: # La excepción será si sale de rango la matriz 69 bool_u = True 70 71 try: 72 d = self.tablero.matriz[x][y][3] 73 if d == color: 74 bool_d = True 75 elif d == SPACE: 76 bool_d = False 77 return False 78 else: 79 new_p = (p[0], p[1]-1) 80 check_stone(new_p, color) 81 except: 82 bool_d = True 83 84 try: 85 l = self.tablero.matriz[x][y][3] 86 if l == color: 87 bool_l = True 88 elif l == SPACE: 89 bool_l = False 90 return False 91 else: 92 new_p = (p[0], p[1]-1) 93 check_stone(new_p, color) 94 except: 95 bool_l = True 96 97 try: 98 r = self.tablero.matriz[x][y][3] 99 if r == color: 100 bool_r = True 101 elif r == SPACE: 102 bool_r = False 103 return False 104 else: 105 new_p = (p[0], p[1]-1) 106 check_stone(new_p, color) 107 except: 108 bool_r = True 109 110 111 # TODO creo que cuando encuentra uno falso no se sale directamente ... 112 return bool_u * bool_d * bool_l * bool_r 113 """ 114