以下是才开始编写的时的代码,代码的大体框架已将搭建出来,算法方面还是不够完善有许多不足资源链接
#属性类
class Setting():
def __init__(self):
self.screen_width = 1024
self.screen_height = 720
self.qipan_width = 720
self.qipan_height = 720
self.grid_width = self.qipan_width //20#距离边框的间距
self.rows = 18 #棋盘行数
self.cols = 18 #棋盘列数
self.left = 7
self.right = 7
self.top = 7
self.bottom = 7
self.game_name = '娇娇会下棋'
self.bg_color = (123,123,123)#线条颜色
self.bg_circle_color = (0,0,0)# 棋盘上小点的颜色
self.white_color = (255,255,255)
self.black_color = (0,0,0)
self.circle_r = 15#棋子大小
self.bg_image = 'image/bg3.jpg'
self.bgm = 'bgm/bgm.mp3'
self.qipan_image = 'image/qipan_gai.png'
self.fps = 30
#主框架及算法代码
import pygame
from setting import Setting
import os
import sys
import pygame.sysfont
from pygame.locals import *
import time
from AI import Evaluate
pygame.init()
clock = pygame.time.Clock()
setting = Setting()
#ev = Evaluate(setting)
screen = pygame.display.set_mode((setting.screen_width,setting.screen_height))
pygame.display.set_caption(setting.game_name)
FPS = setting.fps
COUNT = 0
#棋盘图片
bg_image = pygame.image.load(setting.qipan_image)
bg_image_rect = bg_image.get_rect()
#背景音乐
pygame.mixer.music.load(setting.bgm)
pygame.mixer.music.play(-1)
#计时器
dcounts = 200
#自定义计时时间
DTIME_COUNT = pygame.USEREVENT + 1
pygame.time.set_timer(DTIME_COUNT,1000)
#倒计时超时标识符
dtime_flag = False
#判断先手 等于 True 电脑先手 等于 False 对手先手
judeg_xianshou = False
#判断棋手的棋子颜色 等于 1 白子 等于 0 黑子
com_color = -1
emy_color = -1
#游戏开始选项 判断完先后手才允许选择此项
start_game = False
#游戏结束标识符 1 电脑获胜 2 对手获胜
game_win = 0
##存放棋子的坐标
circles = []
#棋子的总个数
emy_counts = 0#对手
com_counts = 0#电脑
#棋子的颜色
color = [setting.black_color,setting.white_color]
color2 = [setting.white_color,setting.black_color]
#下子的个数 主要用于判别第一个下子
com_now = 0 #当前黑棋下子的个数 最多等于2
emy_now = 0 #当前白子下棋的个数 最多等于2
#撤回操作
flag_z_start = 0
flag = 0#判断当前为谁在下子,将下子的对象返回到flag_kind中 0黑棋方落子,1 白棋方落子
flag_kind = []#保存棋盘每个位置的状态 -1 空 0 黑棋 1 白棋
for i in range(19):
flag_temp = [-1 for x in range(19)]
flag_kind.append(flag_temp)
SIX = 500000
HUO_FIVE = 50000
MIAN_FIVE = 10000
HUO_FOUR = 5000
MIAN_FOUR = 1000
HUO_THREE = 500
MENGLONG_THREE = 300
MIAN_THREE = 100
HUO_TWO = 50
MIAN_TWO = 10
LARGE_NUMBER = 1000000
SEARCH_DEPTH = 2
SAMPLE_NUMBER = 10
blackValue = [] # 保存每一个空位黑子的价值度
whiteValue = [] # 保存每一个空位白子的价值度
staticValue = [] # 保存每一点的位置价值,越靠中心,价值越大
for i in range(setting.cols + 1):
blackValue.append([0 for x in range(setting.rows + 1)])
whiteValue.append([0 for x in range(setting.rows + 1)])
staticValue.append([0 for x in range(setting.rows + 1)])
for i in range(setting.cols // 2 + 1):
for j in range(setting.rows // 2 + 1):
staticValue[i][j] = i if i < j else j
staticValue[setting.cols - i][j] = staticValue[i][j]
staticValue[i][setting.rows - j] = staticValue[i][j]
staticValue[setting.cols - i][setting.rows - j] = staticValue[i][j]
#绘制棋盘
def draw_qipan(screen):
rect_lines = [
((setting.grid_width,setting.grid_width),
(setting.grid_width,setting.qipan_height-setting.grid_width)),
((setting.grid_width,setting.grid_width),
(setting.qipan_width-setting.grid_width,setting.grid_width)),
((setting.grid_width,setting.screen_height-setting.grid_width),
(setting.qipan_width-setting.grid_width,setting.qipan_height-setting.grid_width)),
((setting.qipan_width-setting.grid_width,setting.grid_width),
(setting.qipan_width-setting.grid_width,setting.qipan_height-setting.grid_width))
]
for line in rect_lines:
pygame.draw.line(screen,setting.bg_color,line[0],line[1],2)
for i in range(17):
pygame.draw.line(screen,setting.bg_color,
(setting.grid_width*(2+i),setting.grid_width),
(setting.grid_width*(2+i),setting.qipan_height-setting.grid_width)
)
pygame.draw.line(screen,setting.bg_color,
(setting.grid_width,setting.grid_width*(2+i)),
(setting.qipan_height-setting.grid_width,setting.grid_width*(2+i))
)
circle_center = [
(setting.grid_width*4,setting.grid_width*4),
(setting.qipan_width-setting.grid_width*4,setting.grid_width*4),
(setting.grid_width*4,setting.qipan_height-setting.grid_width*4),
(setting.qipan_width-setting.grid_width*4,setting.qipan_height-setting.grid_width*4),
(setting.grid_width*10,setting.grid_width*10)
]
for circle in circle_center:
pygame.draw.circle(screen,setting.bg_circle_color,circle,5)
#绘制棋子
def draw_qizi():
counts = len(circles)
count = 0
for circle in circles:
pygame.draw.circle(screen, color[circle[2]], [circle[0]*setting.grid_width,circle[1]*setting.grid_width], setting.circle_r)
if counts % 2 == 1:
count = count + 1
if count >= counts - 1:
pygame.draw.circle(screen, color2[circle[2]], [circle[0]*setting.grid_width,circle[1]*setting.grid_width], setting.circle_r-10)
#判断输赢
def judge_win(pos,flag):
hang_counts = 1#所在行的相同子数目
lie_counts = 1#所在列相同子数目
zxie_counts = 1#左斜方向相同子数目
yxie_counts = 1#右斜方向相同子数目
#判断行
left = pos[0] - 1
while left >= 0 and flag_kind[left][pos[1]] == flag:
hang_counts += 1
left -= 1
right = pos[0] + 1
while right < 19 and flag_kind[right][pos[1]] == flag:
hang_counts += 1
right += 1
#判断列
up = pos[1] - 1
while up >= 0 and flag_kind[pos[0]][up] == flag:
lie_counts += 1
up -= 1
down = pos[1] + 1
while down < 19 and flag_kind[pos[0]][down] == flag:
lie_counts += 1
down += 1
#判断左斜
left = pos[0] - 1
up = pos[1] - 1
while left >= 0 and up >= 0 and flag_kind[left][up] == flag:
zxie_counts += 1
left -= 1
up -= 1
left = pos[0] + 1
down = pos[1] + 1
while left < 19 and down < 19 and flag_kind[left][down] == flag:
zxie_counts += 1
left += 1
down += 1
#判断右斜
right = pos[0] + 1
up = pos[1] - 1
while right < 19 and up >=0 and flag_kind[right][up] == flag:
yxie_counts += 1
right += 1
up -= 1
right = pos[0] - 1
down = pos[1] + 1
while right >= 0 and down < 19 and flag_kind[right][down] == flag:
yxie_counts += 1
right -= 1
down += 1
if max(hang_counts,lie_counts,zxie_counts,yxie_counts) >= 6:
return True
#悔棋操作
def back_last():
circle = circles.pop()
flag_kind[circle[0]-1][circle[1]-1] = -1
global emy_now
global com_now
global emy_counts
global com_counts
if circle[2] == emy_color:
emy_now = emy_now - 1
emy_counts = emy_counts - 1
if emy_counts < 0:
emy_counts = 0
if emy_now == 0:
com_now = 2
if circle[2] == com_color:
com_now = com_now - 1
com_counts = com_counts - 1
if com_counts < 0:
com_counts = 0
if com_now == 0:
emy_now =2
#print(circle)
#添加文本
def write_text(text_data,centerx,centery,text_color=(255,255,0)):
font = pygame.sysfont.SysFont(None,48)
text = font.render(text_data,True,text_color)
text_rect = text.get_rect()
text_rect.center = (centerx,centery)
screen.blit(text,text_rect)
#显示各类信息
def show_info():
now = time.ctime()#获取系统本地的时间
now_clock = now[11:19]#时间格式
if start_game == True:
write_text('emy-er ' + str(emy_counts), 800, 250, (0, 0, 0))
write_text('com-er ' + str(com_counts), 800, 300, (255, 255, 255))
write_text('Time '+now_clock,810,150,(123,1,222))
write_text('time remaining: '+str(dcounts),840,350,setting.black_color)
elif start_game == False and game_win == 0 and dtime_flag == False:
write_text("Select sequence:",450,250)
write_text('b - com first ',380,300,(0,0,0))
write_text('w - com second',400,350,(255,255,255))
elif start_game == False and game_win == 1 and dtime_flag == False:#电脑获胜提示
write_text('Game Over',800,200)
write_text('Winer is Com',800,250,(125,125,125))
elif start_game == False and game_win == 2 and dtime_flag == False:#对手获胜提示
write_text('Game Over',800,200)
write_text("Winer is Emy",800,250,(125,125,125))
elif start_game == False and dtime_flag == True and game_win == 0:#时间超限
write_text("You Have A Timeout",800,200,(255,0,0))
elif start_game == False and game_win == 3:
write_text('The world is peace',800,200)
#核心代码
def get_space_values():
#print(setting.left,setting.top,setting.right,setting.bottom)
l = setting.left - 2 if setting.left > 2 else 0
t = setting.top - 2 if setting.top > 2 else 0
r = setting.right + 2 if setting.right < setting.cols - 1 else setting.cols
b = setting.bottom + 2 if setting.bottom < setting.rows - 1 else setting.rows
#print(l , t ,r ,b)
#print('this is get spaces')
for i in range(l, r + 1):
for j in range(t, b + 1):
blackValue[i][j] = 0
whiteValue[i][j] = 0
if flag_kind[i][j] == -1:#如果是空位,进行估值
for m in range(1,4+1):#每个点的分值为四个方向分值之和
blackValue[i][j] += evaluate_value(0,i,j,m)
whiteValue[i][j] += evaluate_value(1,i,j,m)
def get_best_position2():
get_space_values()
max_value = -LARGE_NUMBER
value = 0
position = [0,0]
global COUNT
COUNT = 0
valuable_positions = get_most_valuable_positions()
# print(len(valuable_positions))
for i in range(0,len(valuable_positions)):
if valuable_positions[i][2] >= SIX:#已六连
position[0] = valuable_positions[i][0]
position[1] = valuable_positions[i][1]
break
flag_kind[valuable_positions[i][0]][valuable_positions[i][1]] = com_color
# print('this is best')
old_left = setting.left
old_top = setting.top
old_right = setting.right
old_bottom = setting.bottom
if old_left > valuable_positions[i][0]:
setting.left = valuable_positions[i][0]
if old_top > valuable_positions[i][1]:
setting.top = valuable_positions[i][1]
if old_right < valuable_positions[i][0]:
setting.right = valuable_positions[i][0]
if old_bottom < valuable_positions[i][1]:
setting.bottom = valuable_positions[i][1]
#value = Min(SEARCH_DEPTH, -LARGE_NUMBER, LARGE_NUMBER)
flag_kind[valuable_positions[i][0]][valuable_positions[i][1]] = -1
setting.left = old_left
setting.right = old_right
setting.top = old_top
setting.bottom = old_bottom
if value > max_value:
max_value = value
position[0] = valuable_positions[i][0]
position[1] = valuable_positions[i][1]
return position
def Min(depth,alpha,beta):
global COUNT
COUNT+=1
print(COUNT)
if(depth == 0):
return evaluate_game()
get_space_values()
value = 0
valuable_positions = get_most_valuable_positions()
# print(len(valuable_positions))
for i in range(0,len(valuable_positions)):
flag_kind[valuable_positions[i][0]][valuable_positions[i][1]] = emy_color
# print('this is min')
old_left = setting.left
old_top = setting.top
old_right = setting.right
old_bottom = setting.bottom
if old_left > valuable_positions[i][0] :
setting.left = valuable_positions[i][0]
if old_top > valuable_positions[i][1] :
setting.top = valuable_positions[i][1]
if old_right < valuable_positions[i][0] :
setting.right = valuable_positions[i][0]
if old_bottom < valuable_positions[i][1] :
setting.bottom = valuable_positions[i][1]
# print("min-left:",old_left)
# print("min-right:", old_right)
# print("min-top:", old_top)
# print("min-bottom:", old_bottom)
value = Max(depth-1,alpha,beta)
flag_kind[valuable_positions[i][0]][valuable_positions[i][1]] = -1
setting.left = old_left
setting.right = old_right
setting.top = old_top
setting.bottom = old_bottom
if value < beta:
beta = value
if alpha >= beta:
return alpha
return beta
def Max(depth,alpha,beta):
global COUNT
COUNT+=1
print(COUNT)
if depth == 0:
return evaluate_game()
get_space_values()
value = 0
valuable_positions = get_most_valuable_positions()
# print(len(valuable_positions))
for i in range(0,len(valuable_positions)):
flag_kind[valuable_positions[i][0]][valuable_positions[i][1]] = com_color
# print('this is max')
old_left = setting.left
old_top = setting.top
old_right = setting.right
old_bottom = setting.bottom
if old_left > valuable_positions[i][0] :
setting.left = valuable_positions[i][0]
if old_top > valuable_positions[i][1]:
setting.top = valuable_positions[i][1]
if old_right < valuable_positions[i][0]:
setting.right = valuable_positions[i][0]
if old_bottom < valuable_positions[i][1]:
setting.bottom = valuable_positions[i][1]
# print("max-left:",old_left)
# print("max-right:", old_right)
# print("max-top:", old_top)
# print("max-bottom:", old_bottom)
value = Min(depth-1,alpha,beta)
flag_kind[valuable_positions[i][0]][valuable_positions[i][1]] = -1
setting.left = old_left
setting.right = old_right
setting.top = old_top
setting.bottom = old_bottom
if value> alpha:
alpha = value
if alpha >= beta:
return beta
return alpha
def sort(all_value):
for i in range(len(all_value)):
for j in range(len(all_value)-1):
if all_value[j][2] < all_value[j+1][2]:
tvalue = all_value[j][2]
all_value[j][2] = all_value[j+1][2]
all_value[j+1][2] = tvalue
ti = all_value[j][0]
all_value[j][0] = all_value[j+1][0]
all_value[j+1][0] = ti
tj = all_value[j][1]
all_value[j][1] = all_value[j+1][1]
all_value[j+1][1] = tj
return all_value
def evaluate_value(color, col, row, dir):
# 黑棋 0 白棋 1
# col row 计算列、行坐标
# dir 计算方向 1 水平 2 垂直 3 左上到右下 4 右上到左下
# 返回此点的价值
k = 0
m = 0
value = 0
chess_count1 = 1
chess_count2 = 0
chess_count3 = 0
space_counts1 = 0
space_counts2 = 0
space_counts3 = 0
space_counts4 = 0
if dir == 1:#水平方向
for k in range(col+1,setting.cols+1):
if flag_kind[k][row] == color:
chess_count1 += 1#向右方向上连续相同颜色的棋子
else:
break
while k <= setting.cols and flag_kind[k][row] == -1:
space_counts1 += 1#右方向上连续颜色的棋子后的空位统计
k += 1
if space_counts1 == 1:#右方向上连续颜色的棋子后的空位如果只有一个,继续统计之后相同颜色的棋子
while k <= setting.cols and flag_kind[k][row] == color:
chess_count2 += 1
k += 1
while k <= setting.cols and flag_kind[k][row] == -1:
space_counts2 += 1#右方向上统计之后相同颜色的棋子之后的空位统计
k += 1
for k in range(col-1,-1,-1):#反向
if flag_kind[k][row] == color:
chess_count1 += 1
else:
break
while k >= 0 and k <= setting.cols and flag_kind[k][row] == -1:
space_counts3 += 1
k -= 1
if space_counts3 == 1:
while k >= 0 and k <= setting.cols and flag_kind[k][row] == color:
chess_count3 += 1
k -= 1
while k >= 0 and k <= setting.cols and flag_kind[k][row] == -1:
space_counts4 += 1
k -= 1
elif dir == 2:#垂直方向
for k in range(row+1,setting.rows+1):
if flag_kind[col][k] == color:
chess_count1 += 1
else:
break
while k <= setting.rows and flag_kind[col][k] == -1:
space_counts1 += 1
k += 1
if space_counts1 == 1:
while k <= setting.rows and flag_kind[col][k] == color:
chess_count2 += 1
k += 1
while k <= setting.rows and flag_kind[col][k] == -1:
space_counts2 += 1
k += 1
for k in range(row-1,-1,-1):#反向
if flag_kind[col][k] == color:
chess_count1 += 1
else:
break
while k >= 0 and k <= setting.rows and flag_kind[col][k] == -1:
space_counts3 += 1
k -= 1
if space_counts3 == 1:
while k >= 0 and k <= setting.rows and flag_kind[col][k] == color:
chess_count3 += 1
k -= 1
while k >= 0 and k <= setting.rows and flag_kind[col][k] == -1 :
space_counts4 += 1
k -= 1
elif dir == 3:#左上到右下
for k,m in zip(range(col+1,setting.cols+1),range(row+1,setting.rows+1)):
if flag_kind[k][m] == color:
chess_count1 += 1
else:
break
while k <= setting.cols and m <= setting.rows and flag_kind[k][m] == -1:
space_counts1 += 1
k += 1
m += 1
if space_counts1 == 1:
while k <= setting.cols and m <= setting.rows and flag_kind[k][m] == color:
chess_count2 += 1
k += 1
m += 1
while k <= setting.cols and m <= setting.rows and flag_kind[k][m] == -1:
space_counts2 += 1
k += 1
m += 1
for k ,m in zip(range(col-1,-1,-1),range(row-1,-1,-1)):
if flag_kind[k][m] == color:
chess_count1 += 1
else:
break
while k >= 0 and m >= 0 and k <= setting.cols and m <= setting.rows and flag_kind[k][m] == -1:
space_counts3 += 1
k -= 1
m -= 1
if space_counts3 == 1:
while k >= 0 and m >= 0 and k <= setting.cols and m <= setting.rows and flag_kind[k][m] == color:
chess_count3 += 1
k -= 1
m -= 1
while k >= 0 and m >= 0 and k <= setting.cols and m <= setting.rows and flag_kind[k][m] == -1:
space_counts4 += 1
k -= 1
m -= 1
elif dir == 4:#4 右上到左下
for k,m in zip(range(col+1,setting.cols+1),range(row-1,-1,-1)):
if flag_kind[k][m] == color:
chess_count1 += 1
else:
break
while k <= setting.cols and m >= 0 and m <= setting.rows and flag_kind[k][m] == -1:
space_counts1 += 1
k += 1
m -= 1
if space_counts1 == 1:
while k <= setting.cols and m >= 0 and m <= setting.rows and flag_kind[k][m] == color:
chess_count2 += 1
k += 1
m -= 1
while k<= setting.cols and m >= 0 and m <= setting.rows and flag_kind[k][m] == -1:
space_counts2 += 1
k += 1
m -= 1
for k,m in zip(range(col-1,-1,-1),range(row+1,setting.rows+1)):
if flag_kind[k][m] == color:
chess_count1 += 1
else:
break
#print(k,m)
while k >= 0 and m <= setting.rows and k <= setting.cols and flag_kind[k][m] == -1:
space_counts3 += 1
k -= 1
m += 1
if space_counts3 == 1:
while k >= 0 and m <= setting.rows and k <= setting.cols and flag_kind[k][m] == color:
chess_count3 += 1
k -= 1
m += 1
while k >= 0 and m <= setting.rows and k <= setting.cols and flag_kind[k][m] == -1:
space_counts4 += 1
k -= 1
m += 1
if chess_count1 + chess_count2 + chess_count3 + space_counts1 + space_counts2 + space_counts3 +space_counts4 >= 6:
value = get_value(chess_count1,chess_count2,chess_count3,space_counts1,space_counts2,space_counts3,space_counts4,color)
return value
def get_value( chessCount1, chessCount2, chessCount3, spaceCount1, spaceCount2, spaceCount3, spaceCount4, color):
value = 0
# 将六子棋棋型分为连六、活五、眠五、活四、眠四、活三、朦胧三、眠三、活二、眠二
if chessCount1 == 6:
value =SIX
elif chessCount1 == 5:
if spaceCount1 > 0 and spaceCount3 > 0:
value = HUO_FIVE
elif (spaceCount1 == 0 and spaceCount3 > 0) or (spaceCount1 > 0 and spaceCount3 == 0):
value = MIAN_FIVE
elif chessCount1 == 4:
if spaceCount1 > 1 and spaceCount3 > 1:
value = HUO_FOUR
elif (spaceCount1 > 1 and spaceCount3 == 0) or (spaceCount1 == 0 and spaceCount3 > 1):
value = MIAN_FOUR
elif chessCount1 == 3:
if spaceCount1 > 2 and spaceCount3 > 2:
value = HUO_THREE
elif (spaceCount1 == 0 and spaceCount3 > 3) or (spaceCount3 > 3 and chessCount3 == 0):
value = MIAN_THREE
elif chessCount1 == 2:
if spaceCount1 > 3 and spaceCount3 > 3:
value = HUO_TWO
elif (spaceCount1 > 3 and spaceCount3 == 0) or (spaceCount1 == 0 and spaceCount3 > 3):
value = MIAN_TWO
elif (spaceCount1 == 1 and chessCount2 == 1 and spaceCount2 == 2 and spaceCount3 == 1) or \
(spaceCount1 == 1 and chessCount3 == 1 and spaceCount3 == 1 and spaceCount4 == 2):
value = MENGLONG_THREE
elif chessCount1 == 1:
if (spaceCount1 == 2 and spaceCount3 == 1 and chessCount3 == 2 and spaceCount4 == 1) or\
(spaceCount1 == 1 and spaceCount2 == 1 and chessCount2 == 2 and spaceCount3 == 2):
value = MENGLONG_THREE
else:
value = 0
return value
def get_most_valuable_positions():
i,j,k = 0,0,0
all_value = []
for i in range(setting.cols+1):
for j in range(setting.rows+1):
if flag_kind[i][j] == -1:
all_value.append([i,j,blackValue[i][j]+whiteValue[i][j]+staticValue[i][j]])
k += 1
all_value = sort(all_value)
size = k if k < SAMPLE_NUMBER else SAMPLE_NUMBER
valuable_positions =[]
for i in range(size):
valuable_positions.append([all_value[i][0],all_value[i][1],all_value[i][2]])
return valuable_positions
def evaluate_game():
value = 0
i,j,k = 0,0,0
line = [-1 for x in range(setting.cols+1)]
#print(line)
for j in range(setting.rows+1):#行估值
for i in range(setting.cols+1):
line[i] = flag_kind[i][j]#第一个是列下表
#print(j,' ',line)
# print(setting.cols)
value += evaluate_line(line,setting.cols+1,0)
value -= evaluate_line(line,setting.cols+1,1)
for i in range(setting.cols+1):#列估值
for j in range(setting.rows+1):
line[j] = flag_kind[i][j]
value += evaluate_line(line,setting.rows+1,0)
value -= evaluate_line(line,setting.rows+1,1)
#左下到右上斜线估值
for j in range(4,setting.rows+1,1):
for k in range(0,j+1,1):
line[k] = flag_kind[k][j-k]
value += evaluate_line(line, j + 1, 0)
value -= evaluate_line(line, j + 1, 1)
for j in range(1,setting.rows - 4 + 1,1):
for k in range(0,setting.cols-j+1,1):
line[k] = flag_kind[k+j][setting.rows-k]
value += evaluate_line(line, setting.rows + 1 - j, 0)
value -= evaluate_line(line, setting.rows + 1 - j, 1)
# 左上到右下斜线估值
for j in range(0,setting.rows-4+1,1):
for k in range(0,setting.rows-j+1,1):
line[k] = flag_kind[k][k+j]
value += evaluate_line(line, setting.rows + 1 - j, 0)
value -= evaluate_line(line, setting.rows + 1 - j, 1)
for i in range(1,setting.cols-4+1,1):
for k in range(0,setting.rows-i+1,1):
line[k] = flag_kind[k+i][k]
value += evaluate_line(line,setting.rows+1-i,0)
value -= evaluate_line(line,setting.rows+1-i,1)
if com_color == 0:
return value
else:
return -value
def evaluate_line(line_state,num,color):
chess,space1,space2 = 0,0,0
i,j,k = 0,0,0
value = 0
begin,end = 0,0
# print("--num-",num)
# print("--len-",len(line_state))
for i in range(num):
if line_state[i] == color:
chess = 1
begin = i
j = begin + 1
while j < num and line_state[j] == color :
j += 1
chess += 1
if chess < 2:
continue
end = j - 1
space1 = 0
space2 = 0
j = begin - 1
while (j >= 0 and line_state[j] == -1) or ((j >=0 ) and line_state[j] == color):
j -= 1
space1 += 1
j = end + 1
# print(j)
while(j < num and line_state[j] == -1) or (j < num and line_state[j] == color):
j += 1
space2 += 1
if (chess+ space1 + space2) >= 6:
value += get_value2(chess,space1,space2)
i = end + 1
return value
def get_value2(chessCount1,spaceCount1,spaceCount2):
value = 0
if chessCount1 == 6:
value = SIX
elif chessCount1 == 5:
if spaceCount1 > 0 and spaceCount2 > 0:
value = HUO_FIVE
elif chessCount1 == 4:
if spaceCount1 > 0 and spaceCount2 > 0:
value = HUO_FOUR
elif chessCount1 == 3:
if spaceCount1 > 0 and spaceCount2 > 0:
value = HUO_THREE
elif chessCount1 == 2:
if spaceCount1 > 0 and spaceCount2 > 0:
value =HUO_TWO
else:
value = 0
return value
#主体执行
while True:
if start_game == False and game_win == 0:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_b:
judeg_xianshou = True
com_now = 1 # 电脑先行
emy_now = 2
com_color = 0 # 电脑黑子
emy_color = 1
start_game = True
if event.key == pygame.K_w:
judeg_xianshou = False
emy_now = 1 # 对手先行
com_now = 2
com_color = 1
emy_color = 0 # 对手黑子
start_game = True
if start_game == True and game_win == 0:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_z:#撤回开始
flag_z_start = 1
dcounts = 200
back_last()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:##撤回结束
flag_z_start = 0
dcounts = 200
#机机对打
if emy_now < 2 and flag_z_start == 0:
position = get_best_position2()
circles.append([position[0]+1, position[1]+1, emy_color])
flag_kind[position[0]][position[1]] = emy_color
emy_counts += 1
emy_now += 1
print('emy: ', chr(position[0]+64),20 - position[1])
if emy_now == 2:
com_now = 0 # 将对手的可以进行的棋子步数清零
if setting.left > position[0]:
setting.left = position[0]
if setting.top > position[1]:
setting.top = position[1]
if setting.right < position[0]:
setting.right = position[0]
if setting.bottom < position[1]:
setting.bottom = position[1]
if judge_win([position[0], position[1]], emy_color) == True:
print(" Win")
game_win = 1
start_game = False
dcounts = 200
screen.blit(bg_image,bg_image_rect)
#draw_qipan(screen)
show_info()
draw_qizi()
pygame.display.update()
clock.tick(FPS)
if com_now < 2 and flag_z_start == 0:
position = get_best_position()
print(position)
circles.append([position[0]+1, position[1]+1, com_color])
flag_kind[position[0]][position[1]] = com_color
com_counts += 1
com_now += 1
#print('com: ', chr(position[0]+64),20 - position[1])
if com_now == 2:
emy_now = 0 # 将对手的可以进行的棋子步数清零
if setting.left > position[0]:
setting.left = position[0]
if setting.top > position[1]:
setting.top = position[1]
if setting.right < position[0]:
setting.right = position[0]
if setting.bottom < position[1]:
setting.bottom = position[1]
if judge_win([position[0], position[1]], com_color) == True:
print(" Win")
game_win = 1
start_game = False
dcounts = 200
#for i in flag_kind:
# print(i)
if game_win!=0:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
break
#和局
if emy_counts+com_counts>19*19:
game_win = 3
start_game = False
print("peace")
screen.blit(bg_image,bg_image_rect)
#draw_qipan(screen)
show_info()
draw_qizi()
pygame.display.update()
clock.tick(FPS)