如下:
# -*- coding: utf-8 -*-
import random
import sys
from Tkinter import *
'''
想要学
习
Python
?
'''
class Model:
"""
核心数据
类
,
维护
一个矩
阵
"""
def __init__(self,row,col):
self.width=col
self.height=row
self.items=[[0 for c in range(col)] for r in range(row)]
def setItemValue(self,r,c,value):
"""
设
置某个位置的
值为
value
"""
self.items[r][c]=value;
def checkValue(self,r,c,value):
"""
检测
某个位置的
值
是否
为
value
"""
if self.items[r][c]!=-1 and self.items[r][c]==value:
self.items[r][c]=-1 #
已
经检测过
return True
else:
return False
def countValue(self,r,c,value):
"""
统计
某个位置周
围
8
个位置中,
值为
value
的个数
"""
count=0
if r-1>=0 and c-1>=0:
if self.items[r-1][c-1]==1:count+=1
if r-1>=0 and c>=0:
if self.items[r-1][c]==1:count+=1
if r-1>=0 and c+1<=self.width-1:
if self.items[r-1][c+1]==1:count+=1
if c-1>=0:
if self.items[r][c-1]==1:count+=1
if c+1<=self.width-1 :
if self.items[r][c+1]==1:count+=1
if r+1<=self.height-1 and c-1>=0:
if self.items[r+1][c-1]==1:count+=1
if r+1<=self.height-1 :
if self.items[r+1][c]==1:count+=1
if r+1<=self.height-1 and c+1<=self.width-1:
if self.items[r+1][c+1]==1:count+=1
return count