【Python开发实践】AI人机对战五子棋——基础参数及棋盘的绘制

设置基础参数

程序中,会有一些多次用到的基础设置参数,例如棋盘单元格的大小,棋盘大小等:

square_size=40                  #单格的宽度,为了方便绘制棋盘用的变量
chess_size=square_size//2-2     #棋子大小
web_broad=15                    #棋盘格数+1
map_w=web_broad*square_size     #棋盘长度
map_h=web_broad*square_size     #棋盘高度
info_w=60                       #按钮界面宽度
button_w=120                    #按钮长度
botton_h=45                     #按钮高度
screen_w=map_w                  #总窗口长度
screen_h=map_h+info_w           #总窗口宽度

棋盘的绘制

在程序中,我们使用类map_enum和Map绘制棋盘界面。具体实现流程:

1、使用数字表示当前格的情况。

在map_enum类中:

class map_enum(IntEnum):
    be_empty=0          #没有人下在这里
    player1=1           #玩家一,执白
    player2=2           #玩家二,执黑
    out_of_range=3      #出界

2、创建地图类。

使用self.map初始化二维棋盘表示棋盘大小,数据中的值和类map_enum中的值对应:0表示空,1表示玩家1下的棋,2表示玩家2下的棋,3表示超过允许的限制。并且用self.steps按顺序保存已下的棋子:

class Map:                                  #地图类
    def __init__(self,width,height):        #构造函数
        self.width=width
        self.height=height
        #存储棋盘的二维数组
        self.map=[[0 for x in range(self.width)] for y in range(self.height)]
        self.steps=[]                       #记录步骤先后
    def get_init(self):                     #重置棋盘
        for y in range(self.height):
            for x in range(self.width):
                self.map[y][x]=0
        self.steps=[]

3、进入下一回合的比赛,交换下棋人

    def intoNextTurn(self,turn):
        if turn==map_enum.player1:
            return map_enum.player2
        else:
            return map_enum.player1

4、根据输入的下标返回棋子的具体位置

    def getLocate(self,x,y):
        map_x=x*square_size
        map_y=y*square_size
        return (map_x,map_y,square_size,square_size)    #返回位置信息

5、根据输入的具体位置返回下标

    def getIndex(self,map_x,map_y):
        x=map_x//square_size
        y=map_y//square_size
        return (x,y)

6、判断当前的位置是否在棋盘的有效范围内

    def isInside(self,map_x,map_y):
        if (map_x<=0 or map_x>=map_w or map_y<=0 or map_y>=map_h):
            return False
        return True

7、判断当前格子中是否已经有棋子

    def isEmpty(self,x,y):
        return (self.map[y][x]==0)

8、在棋盘中绘制已下的棋子

这里会按照下棋的顺序加上序号,在绘制时会区分黑棋和白棋:

    def printChessPiece(self,screen):
        player_one=(255,245,238)
        player_two=(41,36,33)
        player_color=[player_one,player_two]
        for i in range(len(self.steps)):
            x,y=self.steps[i]
            map_x,map_y,width,height=self.getLocate(x,y)
            pos,radius=(map_x+width//2,map_y+height//2),chess_size
            turn=self.map[y][x]
            pygame.draw.circle(screen,player_color[turn-1],pos,radius)

9、绘制棋盘

这里使用两个for循环分别绘制棋盘中的横线和竖线:

    def drawBoard(self,screen):
        color=(0,0,0)
        for y in range(self.height):
            start_pos,end_pos=(square_size//2,square_size//2+square_size*y),(map_w-square_size//2,square_size//2+square_size*y)
            pygame.draw.line(screen,color,start_pos,end_pos,1)
        for x in range(self.width):
            start_pos,end_pos=(square_size//2+square_size*x,square_size//2),(square_size//2+square_size*x,map_h-square_size//2)
            pygame.draw.line(screen,color,start_pos,end_pos,1)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值