尝试写个五子棋。
折腾好久了,光画棋盘和落子棋子的部分就反复写了好几遍。
因为完全没有编程经验,发现如果开头没开好的话,后边根本没法写。
但愿这次不用重新写开头了。
这是定义的类:
class Chess():
#定义棋盘的属性
def __init__(self):
self.location=() #当前落子点的坐标
self.state=[]#当前的落子信息(状态),包含有无棋子、棋子的颜色
self.index=0 #当前落子点为棋局的第n步
self.horizontal=[] #棋子横向的共9个棋子
self.vertical=[] #棋子竖向的共9个棋子
self.slash=[] #棋子斜向的共9个棋子
self.backslash=[] #棋子反斜向的共9个棋子
def get_locations():
#初始计算出棋盘上所有的落子点位置,返回落子点坐标集合。
x=range(1,20)
y=range(1,20)
n=0
locations=[]
location=()
for i in x:
for j in y:
location=(i,j) #生成每个落子点的坐标
locations.append(location) #集合
return locations
locations=get_locations()
def get_location(event):
#获取鼠标指向的棋盘位置
get_x=int(event.x/35)
if event.x not in range(get_x*35-13,get_x*35+13):
get_x+=1
get_y=int(event.y/35)
if event.y not in range(get_y*35-13,get_y*35+13):
get_y+=1
return(get_x,get_y)
class Player():
#定义棋手的属性
def __init__(self,color):
self.color=color #落子颜色
self.token=0 #落子权利,获得令牌
self.locations=[] #已经落子的点集合
self.pionts=0 #已经落子的数量
self.last_piece=[] #最后一个棋子
class Pieces():
#定义棋子的属性
def __init__(self,color,location,cv):
self.color=color #棋子的颜色
self.location=location #棋子所在的位置
self.cv=cv #棋子所在的棋盘
def choose_piont(self):
#落子的动作
a=self.location[0]*35-15
b=self.location[1]*35-15
c=self.location[0]*35+15
d=self.location[1]*35+15
self.cv.create_oval(a,b,c,d,width=0,fill=self.color)
self.cv.update()